-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
105 lines (89 loc) · 3.04 KB
/
deploy.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
const { exec } = require("child_process");
const fs = require("fs").promises;
const hostname = require("os").hostname;
const path = require("path");
const { URL } = require("url");
const config = require(`./config-${process.env.NODE_ENV}`);
const secret = require(`./secret-${process.env.NODE_ENV}`);
config.environment = process.env.NODE_ENV;
const namespace = config.namespace;
const imageTag = {
production: "production",
staging: "staging",
development: "latest"
}[config.environment];
const actions = {
async execCommand(command) {
console.log(`$ ${command}`);
const out = await await new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if(err) reject(err);
else if(stderr) reject(stderr);
else if(stdout) resolve(stdout);
});
});
console.log(`${out}\n`);
return out;
},
async deleteNamespace() {
try {
await actions.execCommand(`kubectl delete namespaces ${namespace}`);
} catch(err) { } // eslint-disable-line no-empty
},
async createNamespace() {
await actions.execCommand(`kubectl create namespace ${namespace}`);
},
async applyConfig() {
await actions.execCommand(`kubectl create configmap config ${
Object.entries(config).map(([key, value]) => `--from-literal=${key}=${value}`).join(" ")
} --namespace=${namespace}`);
},
async applySecrets() {
for(const [name, values] of Object.entries(secret)) {
await actions.execCommand(`kubectl create secret generic ${name} ${
Object.entries(values).map(([key, value]) => `--from-literal=${key}=${value}`).join(" ")
} --namespace=${namespace}`);
}
},
async applyDirectory(directory) {
const items = await fs.readdir(directory);
for(const item of items) {
const resolvedPath = path.resolve(directory, item);
const stat = await fs.stat(resolvedPath);
if(stat.isDirectory()) await actions.applyDirectory(resolvedPath);
else if(stat.isFile()) await actions.applyFile(resolvedPath);
}
},
async applyFile(filePath) {
const file = await fs.readFile(filePath, "utf8");
const configured = file.replace(/\{\{namespace\}\}/g, namespace)
.replace(/\{\{tag\}\}/g, imageTag)
.replace(/\{\{hostname\}\}/g, hostname)
.replace(/\{\{dashboard_hostname\}\}/g, new URL(config.dashboardURL).hostname);
const name = `${(Date.now() + process.hrtime().reduce((a, b) => a + b)).toString(36)}.yml`;
const location = path.resolve(__dirname, name);
await fs.writeFile(location, configured);
try {
await actions.execCommand(`kubectl apply -f ${location}`);
} catch(err) {
await fs.unlink(location);
throw err;
}
await fs.unlink(location);
return location;
}
};
async function init() {
await actions.deleteNamespace();
await actions.createNamespace();
await actions.applyConfig();
await actions.applySecrets();
await actions.applyDirectory(path.resolve(__dirname, "services"));
await actions.applyDirectory(path.resolve(__dirname, "other"));
await actions.applyDirectory(path.resolve(__dirname, "deployments"));
}
init();
process.on("unhandledRejection", err => {
console.error(err.stack);
process.exit(1);
});