|
| 1 | +const URL = require('url').URL; |
| 2 | +const https = require('https'); |
| 3 | +const child_process = require('child_process'); |
| 4 | + |
| 5 | +function apiCall(url, method, headers, cb) { |
| 6 | + const u = new URL(url); |
| 7 | + const req = https.request({ hostname: u.hostname, path: u.pathname + u.search, method, headers }, (res) => { |
| 8 | + if (res.statusCode !== 200 && res.statusCode !== 204) { |
| 9 | + cb(new Error(res.statusMessage)); |
| 10 | + return; |
| 11 | + } |
| 12 | + const chunks = []; |
| 13 | + res.on('data', chunk => chunks.push(chunk)); |
| 14 | + res.on('end', () => { |
| 15 | + try { |
| 16 | + if (res.statusCode === 204) cb(null, null); |
| 17 | + else cb(null, JSON.parse(Buffer.concat(chunks).toString()), res); |
| 18 | + } catch (e) { |
| 19 | + cb(e); |
| 20 | + } |
| 21 | + }); |
| 22 | + }); |
| 23 | + req.on('error', cb); |
| 24 | + req.end(); |
| 25 | +} |
| 26 | + |
| 27 | +function getDeployedHashes(headers, environments, cb) { |
| 28 | + const hashes = []; |
| 29 | + const envFound = new Array(environments ? environments.length : 2).fill(false); |
| 30 | + let pending = 1; |
| 31 | + function fetch(url) { |
| 32 | + apiCall(url, 'GET', headers, (err, responseJSON, response) => { |
| 33 | + if (err) { |
| 34 | + cb && cb(err); |
| 35 | + cb = null; |
| 36 | + return; |
| 37 | + } |
| 38 | + for (let deployment of responseJSON) { |
| 39 | + const index = environments ? environments.indexOf(deployment.environment) : 0; |
| 40 | + if (index >= 0 && deployment.statuses_url.startsWith('https://api.github.com/')) { |
| 41 | + ++pending; |
| 42 | + apiCall(deployment.statuses_url, 'GET', headers, (err, statuses) => { |
| 43 | + if (err) { |
| 44 | + cb && cb(err); |
| 45 | + cb = null; |
| 46 | + return; |
| 47 | + } |
| 48 | + if (statuses[0] && statuses[0].state == 'success') { |
| 49 | + hashes.push(deployment.sha); |
| 50 | + envFound[index] = true; |
| 51 | + } |
| 52 | + if (!--pending && cb) { |
| 53 | + cb(null, hashes); |
| 54 | + cb = null; |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + const match = (response.headers.link || '').match(/<(.[^>]+)>;\s*rel="next"/); |
| 60 | + if (cb && match && match[1] && match[1].startsWith('https://api.github.com/')) { |
| 61 | + if (!envFound.every(_ => _)) { |
| 62 | + fetch(match[1]); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + if (!--pending && cb) { |
| 67 | + cb(null, hashes); |
| 68 | + cb = null; |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + fetch('https://api.github.com/repos/rapidlua/luajit.me/deployments'); |
| 73 | +} |
| 74 | + |
| 75 | +function listDigitalOceanPrivateImages(headers, cb) { |
| 76 | + const images = []; |
| 77 | + function fetch(url) { |
| 78 | + apiCall(url, 'GET', headers, (err, response) => { |
| 79 | + if (err) |
| 80 | + return cb(err); |
| 81 | + images.push(...response.images); |
| 82 | + try { |
| 83 | + if (response.links.pages.next.startsWith('https://api.digitalocean.com/')) |
| 84 | + return fetch(response.links.pages.next); |
| 85 | + } catch (e) {} |
| 86 | + cb(null, images); |
| 87 | + }); |
| 88 | + } |
| 89 | + fetch('https://api.digitalocean.com/v2/images?private=true&per_page=200'); |
| 90 | +} |
| 91 | + |
| 92 | +function isOrphanedImage(name, hashes) { |
| 93 | + if (!name.startsWith('image-')) |
| 94 | + return false; |
| 95 | + const res = child_process.spawnSync( |
| 96 | + 'git', [ 'rev-parse', '--verify', 'v' + name.substr(6) ], |
| 97 | + { encoding: 'utf8' } |
| 98 | + ); |
| 99 | + return res.status || !hashes.includes(res.stdout.trim()); |
| 100 | +} |
| 101 | + |
| 102 | +function fatal(...args) { |
| 103 | + console.error(...args); |
| 104 | + process.exit(1); |
| 105 | +} |
| 106 | + |
| 107 | +const githubHeaders = { |
| 108 | + 'User-Agent': 'rapidlua', |
| 109 | + Authorization: 'token ' + process.env.GITHUB_TOKEN, |
| 110 | + Accept: 'application/vnd.github.ant-man-preview+json' |
| 111 | +}; |
| 112 | + |
| 113 | +const digitalOceanHeaders = { |
| 114 | + Authorization: 'Bearer ' + process.env.DIGITALOCEAN_TOKEN |
| 115 | +} |
| 116 | + |
| 117 | +function hourToMSec(v) { return v * 1000 * 60 * 60; } |
| 118 | + |
| 119 | +getDeployedHashes(githubHeaders, [ 'production' ], (err, hashes) => { |
| 120 | + if (err) fatal('get deployed hashes:', err); |
| 121 | + listDigitalOceanPrivateImages(digitalOceanHeaders, (err, images) => { |
| 122 | + if (err) fatal('list DigitalOcean private images:', err); |
| 123 | + for (let image of images) { |
| 124 | + if (image.type === 'snapshot' && Date.now() - new Date(image.created_at) > hourToMSec(3) && isOrphanedImage(image.name, hashes)) { |
| 125 | + apiCall('https://api.digitalocean.com/v2/images/' + image.id, 'DELETE', digitalOceanHeaders, (err) => { |
| 126 | + if (err) fatal('removing ' + image.name + ':', err); |
| 127 | + console.log('removed', image.name); |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | +}); |
0 commit comments