-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-localhost.js
222 lines (189 loc) · 5.8 KB
/
deploy-localhost.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const { program, CommanderError } = require('commander');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const chalk = require('chalk');
const clear = require('clear');
async function checkRequirements(debugEnabled) {
try {
await exec('minikube');
} catch {
throw new Error('Minikube missing');
}
try {
await exec('eval $(minikube -u minikube docker-env)');
await exec('docker info');
} catch {
throw new Error('Docker missing or not running');
}
try {
await exec('helm');
} catch {
throw new Error('Helm missing');
}
try {
await exec('kubectl');
} catch {
throw new Error('Kubectl missing');
}
}
async function setupCluster(debugEnabled) {
try {
console.log('Deleting old cluster...');
await exec('minikube delete');
console.log('Cluster deleted ✅');
console.log('Setting up fresh cluster...');
await exec('minikube start');
console.log('Cluster created ✅');
} catch (error) {
if (debugEnabled) {
console.error(error);
}
throw new Error('Issue while setting up cluster, is your docker running?');
}
}
async function cleanup(debugEnabled) {
try {
console.log('Cleaning up...');
await exec('eval $(minikube -u minikube docker-env)');
console.log('Cleaned up ✅');
} catch (error) {
if (debugEnabled) {
console.error(error);
}
throw new Error('Issue while setting up docker, is your docker running?');
}
}
async function getMicroservices(debugEnabled) {
try {
const { stdout } = await exec('ls apps/api');
const microservices = stdout
.split(/\r?\n/)
.filter((service) => service.length > 0);
if (debugEnabled) {
console.log('Microservices ', microservices);
}
return microservices;
} catch (error) {
if (debugEnabled) {
console.error(error);
}
throw new Error('Error while listing microservices');
}
}
async function buildMicroservices(microservices, debugEnabled) {
try {
const buildCommands = [];
for (let i = 0; i < microservices.length; i++) {
const microservice = microservices[i];
buildCommands.push(
`docker build -f ./apps/api/${microservice}/Dockerfile.local -t ${microservice} --build-arg LOCAL=true .`,
);
}
console.log(
`Building ${
microservices.length === 1 ? 'microservice' : 'microservices'
}...(this might take a while)`,
);
if (debugEnabled) {
console.log(
`eval $(minikube docker-env) ${buildCommands
.map((command) => `&& ${command}`)
.join(' ')}`,
);
}
await exec(
`eval $(minikube docker-env) ${buildCommands
.map((command) => `&& ${command}`)
.join(' ')}`,
);
console.log('Build successful ✅');
} catch (error) {
if (debugEnabled) {
console.error(JSON.stringify(error, null, 2));
}
throw new Error('Error while building microservices');
}
}
async function deployMicroservices(microservices, debugEnabled) {
try {
for (let i = 0; i < microservices.length; i++) {
const microservice = microservices[i];
console.log(`Deploying ${microservice}...`);
await exec(
`helm upgrade --install ${microservice} charts/node-service --values charts/node-service/values.yaml --values charts/node-service/development.values.yaml --values apps/api/${microservice}/deployment/values.yaml --values apps/api/${microservice}/deployment/development.values.yaml --set tag=latest --set image=${microservice} --set local=true --set environment=local --set autoscaling.enabled=false --force`,
);
console.log(`${microservice} deployed ✅`);
}
} catch (error) {
if (debugEnabled) {
console.error(error);
}
throw new Error('Error while deploying microservices');
}
}
async function deployIngressController(install = true, debugEnabled) {
try {
console.log('Enabling ingress controller...');
if (install) {
await exec('minikube addons enable ingress');
await sleep(30000);
console.log('Ingress controller enabled ✅');
}
console.log('Installing ingress controller');
await exec(
'helm upgrade --install ingress charts/ingress-controller --set local=true',
);
} catch (error) {
if (debugEnabled) {
console.error(error);
}
throw new Error('Error while deploying ingress controller');
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
program
.command('install')
.alias('i')
.option('-d, --debug')
.action(async ({ debug }) => {
clear();
console.log(
chalk.inverse.bold('Starting local microservice deployment...'),
);
try {
await checkRequirements(debug);
await setupCluster(debug);
const microservices = await getMicroservices(debug);
await buildMicroservices(microservices, debug);
await deployMicroservices(microservices, debug);
await deployIngressController(debug);
} catch (error) {
console.log(chalk.red.bold(error.message));
}
await cleanup();
});
program
.command('upgrade')
.alias('u')
.requiredOption('-s, --service <service>', 'service that should be upgraded')
.option('-d, --debug')
.action(async ({ service, debug }) => {
console.log(chalk.inverse.bold(`Starting ${service} upgrade...`));
if (service === 'ingress') {
await deployIngressController(false, debug);
} else {
const microservices = await getMicroservices();
if (!microservices.includes(service)) {
console.log(chalk.red.bold('Service does not exist. Aborting ❌'));
return;
}
await buildMicroservices([service], debug);
await deployMicroservices([service], debug);
}
console.log(chalk.inverse.bold('Upgrade completed ✅'));
});
program.parse();