|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const qiniu = require('../index'); |
| 5 | +const { shellQuote } = require('../qiniu/sandbox/util'); |
| 6 | + |
| 7 | +function loadDotEnvIfPresent () { |
| 8 | + const files = [ |
| 9 | + path.join(process.cwd(), '.env') |
| 10 | + ]; |
| 11 | + |
| 12 | + files.forEach(filepath => { |
| 13 | + if (!fs.existsSync(filepath)) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + fs.readFileSync(filepath, 'utf8') |
| 18 | + .split(/\r?\n/) |
| 19 | + .forEach(line => { |
| 20 | + line = line.trim(); |
| 21 | + if (!line || line[0] === '#') { |
| 22 | + return; |
| 23 | + } |
| 24 | + const index = line.indexOf('='); |
| 25 | + if (index < 0) { |
| 26 | + return; |
| 27 | + } |
| 28 | + const key = line.slice(0, index).trim(); |
| 29 | + let value = line.slice(index + 1).trim(); |
| 30 | + if ( |
| 31 | + (value[0] === '"' && value[value.length - 1] === '"') || |
| 32 | + (value[0] === '\'' && value[value.length - 1] === '\'') |
| 33 | + ) { |
| 34 | + value = value.slice(1, -1); |
| 35 | + } |
| 36 | + if (process.env[key] === undefined) { |
| 37 | + process.env[key] = value; |
| 38 | + } |
| 39 | + }); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function env (key, fallback) { |
| 44 | + return process.env[key] || fallback; |
| 45 | +} |
| 46 | + |
| 47 | +function requiredEnv (key) { |
| 48 | + if (process.env[key]) { |
| 49 | + return process.env[key]; |
| 50 | + } |
| 51 | + throw new Error(`Please set ${key}`); |
| 52 | +} |
| 53 | + |
| 54 | +function sandboxEndpoint () { |
| 55 | + return env('QINIU_SANDBOX_ENDPOINT'); |
| 56 | +} |
| 57 | + |
| 58 | +function sandboxApiKey () { |
| 59 | + return requiredEnv('QINIU_SANDBOX_API_KEY'); |
| 60 | +} |
| 61 | + |
| 62 | +function sandboxTemplate () { |
| 63 | + return env('QINIU_SANDBOX_TEMPLATE', 'base'); |
| 64 | +} |
| 65 | + |
| 66 | +function sandboxClient (options) { |
| 67 | + options = Object.assign({ |
| 68 | + endpoint: sandboxEndpoint(), |
| 69 | + apiKey: sandboxApiKey() |
| 70 | + }, options || {}); |
| 71 | + return new qiniu.sandbox.SandboxClient(options); |
| 72 | +} |
| 73 | + |
| 74 | +function sandboxMac () { |
| 75 | + const accessKey = requiredEnv('QINIU_SANDBOX_ACCESS_KEY'); |
| 76 | + const secretKey = requiredEnv('QINIU_SANDBOX_SECRET_KEY'); |
| 77 | + return new qiniu.auth.digest.Mac(accessKey, secretKey); |
| 78 | +} |
| 79 | + |
| 80 | +function createSandboxAndWait (options, pollOptions) { |
| 81 | + const client = options && options.client ? options.client : sandboxClient(); |
| 82 | + const params = Object.assign({ |
| 83 | + client, |
| 84 | + template: sandboxTemplate(), |
| 85 | + timeout: 300 |
| 86 | + }, options || {}); |
| 87 | + |
| 88 | + return qiniu.sandbox.Sandbox.create(params).then(sandbox => { |
| 89 | + console.log('Sandbox created:', sandbox.sandboxId); |
| 90 | + return sandbox.waitForReady(Object.assign({ |
| 91 | + interval: 3000, |
| 92 | + timeout: 180000 |
| 93 | + }, pollOptions || {})).then(info => { |
| 94 | + console.log('Sandbox ready:', sandbox.sandboxId, info.state || ''); |
| 95 | + return sandbox; |
| 96 | + }); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function cleanupSandbox (sandbox) { |
| 101 | + if (!sandbox) { |
| 102 | + return Promise.resolve(); |
| 103 | + } |
| 104 | + return sandbox.kill() |
| 105 | + .then(() => { |
| 106 | + console.log('Sandbox killed:', sandbox.sandboxId); |
| 107 | + }, err => { |
| 108 | + console.log('Failed to kill sandbox:', sandbox.sandboxId, err.message); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +function runExample (fn) { |
| 113 | + loadDotEnvIfPresent(); |
| 114 | + Promise.resolve() |
| 115 | + .then(fn) |
| 116 | + .catch(err => { |
| 117 | + console.error(err && err.stack ? err.stack : err); |
| 118 | + process.exitCode = 1; |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + qiniu, |
| 124 | + shellQuote, |
| 125 | + env, |
| 126 | + requiredEnv, |
| 127 | + sandboxEndpoint, |
| 128 | + sandboxApiKey, |
| 129 | + sandboxTemplate, |
| 130 | + sandboxClient, |
| 131 | + sandboxMac, |
| 132 | + createSandboxAndWait, |
| 133 | + cleanupSandbox, |
| 134 | + runExample |
| 135 | +}; |
0 commit comments