-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-deployment-readiness
executable file
·130 lines (108 loc) · 3.34 KB
/
validate-deployment-readiness
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
'use strict';
const process = require('process');
const https = require('https');
const fs = require('fs');
const crypto = require('crypto');
const green = '\u001B[0;32m';
const red = '\u001B[0;31m';
const yellow = '\u001B[1;33m';
const reset = '\u001B[0m';
const hash = crypto.createHash('sha256');
class HttpError extends Error {
constructor(message = '', ...args) {
super(message, ...args);
}
}
class ServerNotOkError extends Error {
constructor(message = '', ...args) {
super(message, ...args);
}
}
function getLocalList(migrationPath) {
return new Promise((resolve, reject) => {
fs.readdir(migrationPath, (err, files) => {
if (err) {
return reject(err);
}
const migrationFiles = files
.sort()
.reverse()
.filter(file => file.substring(file.length - 3) === '.js');
return resolve(migrationFiles);
});
});
}
function getDeployedHash(target) {
return new Promise((resolve, reject) => {
const hostname = target === 'staging' ? 'api-stg.ca.la' : 'api.ca.la';
let response = [];
const req = https.request(
{
hostname,
port: 443,
path: '',
method: 'GET'
},
(res) => {
const isSuccessCode = res.statusCode === 200;
if (!isSuccessCode) {
reject(new HttpError(`The server returned a status other than 200. Status: ${res.statusCode}`));
}
res.on('data', (d) => { response = response.concat(d); });
res.on('end', () => {
const responseString = response.join('');
let responseJson;
try {
responseJson = JSON.parse(responseString);
} catch (e) {
return reject(new HttpError(`Server returned invalid JSON. Response: \n${responseString}`));
}
if (responseJson.status === 'ok') {
return resolve(responseJson.migrationListHash);
}
return reject(new ServerNotOkError(`The server indicated that there was something wrong:
${JSON.stringify(responseJson, null, 2)}`));
});
}
);
req.on('error', reject);
req.end();
});
}
async function run() {
const [_executor, _script, target, migrationPath] = process.argv;
if (!target || !migrationPath) {
console.log('Usage: validate-deployment-readiness [staging|production] [path_to_migrations]');
process.exit(1);
}
let deployedHash;
try {
deployedHash = await getDeployedHash(target);
} catch (error) {
if (error instanceof HttpError) {
console.log(`${yellow}Warning: Bad response from server. Skipping.${reset}`);
console.error(error);
process.exit(0);
} else {
throw error;
}
}
console.log(`${yellow}Deployed hash: ${reset}${deployedHash}`);
const localList = await getLocalList(migrationPath);
const localHash = hash.update(localList.join('\n')).digest('hex');
console.log(`${yellow}Local hash: ${reset}${localHash} (latest file: ${localList[0]})`);
if (deployedHash === localHash) {
console.log(`${green}Migration hashes match. Ready to roll.${reset}`);
process.exit(0);
} else {
console.error(`${red}Hash mismatch! Make sure to run migrations _before_ deploying. Aborting.${reset}`);
process.exit(1);
}
}
run()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});