|
| 1 | +const _ = require('lodash'); |
| 2 | +const { sendHttpRequest } = require('./helper'); |
| 3 | +const Cluster = require('../entities/Cluster'); |
| 4 | +const rp = require('request-promise'); |
| 5 | +const fs = require('fs'); |
| 6 | +const { spawn } = require('child_process'); |
| 7 | +const { homedir, arch } = require('os'); |
| 8 | +const request = require('request'); |
| 9 | +const decompress = require('decompress'); |
| 10 | +const decompressTargz = require('decompress-targz'); |
| 11 | +const compareVersions = require('compare-versions'); |
| 12 | + |
| 13 | +const _createClusterScript = (info, filePath) => { |
| 14 | + const { name, context } = info; |
| 15 | + fs.chmodSync(filePath, '755'); |
| 16 | + const clusterScript = spawn(filePath, ['create', '--c', name, '--token', context.token, '--api-host', `${context.url}/`] ); |
| 17 | + clusterScript.stdout.pipe(process.stdout); |
| 18 | + clusterScript.stderr.pipe(process.stderr); |
| 19 | + process.stdin.pipe(clusterScript.stdin); |
| 20 | + clusterScript.on('exit', (code) => { |
| 21 | + process.exit(code); |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +const _extractFieldsForTeamEntity = cluster => ({ |
| 26 | + id: cluster._id, |
| 27 | + name: cluster.selector, |
| 28 | + provider: cluster.provider, |
| 29 | + providerAgent: cluster.providerAgent, |
| 30 | +}); |
| 31 | + |
| 32 | +const getAllClusters = async () => { |
| 33 | + const userOptions = { |
| 34 | + url: '/api/clusters', |
| 35 | + method: 'GET', |
| 36 | + }; |
| 37 | + |
| 38 | + const result = await sendHttpRequest(userOptions); |
| 39 | + const clusters = []; |
| 40 | + let data = {}; |
| 41 | + _.forEach(result, (cluster) => { |
| 42 | + data = _extractFieldsForTeamEntity(cluster); |
| 43 | + clusters.push(new Cluster(data)); |
| 44 | + }); |
| 45 | + return clusters; |
| 46 | +}; |
| 47 | + |
| 48 | +const createCluster = async (info) => { |
| 49 | + const dirPath = `${homedir()}/.Codefresh/cluster`; |
| 50 | + const filePath = `${homedir()}/.Codefresh/cluster/stevedore`; |
| 51 | + const versionPath = `${homedir()}/.Codefresh/cluster/version.txt`; |
| 52 | + const versionUrl = 'https://raw.githubusercontent.com/codefresh-io/Stevedore/master/VERSION'; |
| 53 | + let zipPath = `${homedir()}/.Codefresh/cluster/data`; |
| 54 | + let shouldUpdate = true; |
| 55 | + const options = { |
| 56 | + url: versionUrl, |
| 57 | + method: 'GET', |
| 58 | + headers: { |
| 59 | + 'User-Agent': 'codefresh', |
| 60 | + }, |
| 61 | + }; |
| 62 | + const version = await rp(options); |
| 63 | + if (!fs.existsSync(dirPath)) { |
| 64 | + fs.mkdirSync(dirPath); |
| 65 | + } else if (fs.existsSync(versionPath)) { |
| 66 | + const currVersion = fs.readFileSync(versionPath, { encoding: 'UTF8' }).trim(); |
| 67 | + if (compareVersions(currVersion, version) >= 0) { |
| 68 | + shouldUpdate = false; |
| 69 | + } |
| 70 | + } |
| 71 | + if (shouldUpdate) { |
| 72 | + let osType; |
| 73 | + const { platform } = process; |
| 74 | + if (_.isEqual(platform, 'darwin')) { |
| 75 | + osType = _.isEqual(arch(), 'x32') ? 'Darwin_i386.tar.gz' : 'Darwin_x86_64.tar.gz'; |
| 76 | + zipPath = `${zipPath}.tar.gz`; |
| 77 | + } else if (_.isEqual(platform, 'linux')) { |
| 78 | + osType = _.isEqual(arch(), 'x32') ? 'Linux_i386.tar.gz' : 'Linux_x86_64.tar.gz'; |
| 79 | + zipPath = `${zipPath}.tar.gz`; |
| 80 | + } else if (_.isEqual(platform, 'ein32')) { |
| 81 | + osType = _.isEqual(arch(), 'x32') ? 'Windows_i386.zip' : 'Windows_x86_64.zip'; |
| 82 | + zipPath = `${zipPath}.zip`; |
| 83 | + } |
| 84 | + const assetUrl = `https://github.com/codefresh-io/Stevedore/releases/download/v${version}/stevedore_${version}_${osType}`; |
| 85 | + const req = request(assetUrl); |
| 86 | + req.pipe(fs.createWriteStream(zipPath)); |
| 87 | + req.on('end', () => { |
| 88 | + console.log('File written!'); |
| 89 | + decompress(zipPath, `${homedir()}/.Codefresh/cluster`, { |
| 90 | + plugins: [ |
| 91 | + decompressTargz(), |
| 92 | + ], |
| 93 | + }).then(() => { |
| 94 | + console.log('Files decompressed'); |
| 95 | + fs.writeFile(versionPath, version, (err) => { |
| 96 | + if (err) { |
| 97 | + throw err; |
| 98 | + } |
| 99 | + }); |
| 100 | + _createClusterScript(info, filePath); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } else { |
| 104 | + _createClusterScript(info, filePath); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +const deleteCluster = async (clusterName) => { |
| 109 | + const clusters = await getAllClusters(); |
| 110 | + const cluster = _.find(clusters, (curr) => { |
| 111 | + return _.isEqual(curr.info.name, clusterName); |
| 112 | + }); |
| 113 | + const options = { |
| 114 | + url: `/api/clusters/${cluster.info.provider}/cluster/${cluster.info.id}`, |
| 115 | + method: 'DELETE', |
| 116 | + }; |
| 117 | + |
| 118 | + return sendHttpRequest(options); |
| 119 | +}; |
| 120 | + |
| 121 | +module.exports = { |
| 122 | + createCluster, |
| 123 | + getAllClusters, |
| 124 | + deleteCluster, |
| 125 | +}; |
0 commit comments