-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
177 lines (146 loc) · 4.43 KB
/
utils.js
File metadata and controls
177 lines (146 loc) · 4.43 KB
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
const path = require("path");
const assert = require("assert");
const fs = require("fs-extra");
const util = require("util");
const child_process = require("child_process");
const os = require("os");
const fsp = require("fs").promises;
const constants = require("./constants.js");
const {globalDB} = require("./db.js");
const {ServerNotDefinedError} = require("./errors.js");
const exec = util.promisify(child_process.exec);
// Read json sync, returning empty object {} if no file existed
// Also return {} if an empty file
function readJson(filePath, suppressSyntaxError = false) {
let json = {};
try {
if (fs.statSync(filePath).size == 0) {
return {};
}
json = fs.readJsonSync(filePath);
} catch (error) {
let ignoreError = error.code == "ENOENT"
|| (suppressSyntaxError && error instanceof SyntaxError);
if (!ignoreError) {
throw error;
}
}
return json;
}
// lower case and replace any spaces with a hyphen
// used for both app names and also some other things (target name, project name...)
function validateAppName(name) {
name = name.trim().toLowerCase().replace(/\s+/g, "_");
assert(/^[a-z][-_a-z0-9]+$/.test(name), `Invalid app name or other name: ${name}`);
assert(!name.includes("---"), `App name or other name must not contain three hyphens: ${name}`);
return name;
}
function appNameFromDir(dirPath) {
return validateAppName(path.basename(dirPath));
}
function getNumberedReleaseDir(releaseDir, releaseNumber) {
return releaseDir + `/release-${releaseNumber}`;
}
// a key used by control server to access other servers
// unique for each deploy target
function getControlKeyPath(target) {
return constants.CONTROL_KEY_DIR + "/" + target;
}
// Utility for informational output only
function serverToStr(server) {
if (server.hostname) {
return server.hostname;
}
return hostFromServer(server);
}
function hostFromServer(server) {
assert(server.ipv6 || server.ipv4);
if (process.env.APPCONTROL_FORCE_IPV4) {
if (server.ipv4) {
return server.ipv4;
} else {
return server.ipv6;
}
} else {
if (server.ipv6) {
return server.ipv6;
} else {
return server.ipv4;
}
}
}
function getGlobalServerDefinitions() {
const serverGroups = globalDB.get("servers").value();
// Combine all servers from different groups into one list
if (serverGroups) {
return Object.values(serverGroups).reduce((accum, value) => accum.concat(value), []);
}
return [];
}
async function updateKnownHosts(hostIP, fingerprint) {
await exec(`ssh-keygen -R "${hostIP}"`);
await fsp.appendFile(constants.KNOWN_HOSTS_PATH, `${hostIP} ssh-ed25519 ${fingerprint}\n`);
}
// If fingerprint is not given, will use the current one from server
// Otherwise, use the specified fingerprint
async function updateKnownHostsForServer(server, fingerprint = null) {
if (fingerprint == null) {
fingerprint = server.fingerprint;
}
if (server.ipv4) {
await updateKnownHosts(server.ipv4, fingerprint);
}
if (server.ipv6) {
await updateKnownHosts(server.ipv6, fingerprint);
}
}
// Check there are no duplicates in array
function allDifferent(array) {
return array.length === new Set(array).size;
}
function compareServerToKey(server, serverKey) {
// Ensure there is nothing stupid like a hostname that contains a wrong IP address, which would match
assert(allDifferent(constants.VALID_SERVER_KEYS.map(key => server[key])));
return constants.VALID_SERVER_KEYS.map(key => server[key]).includes(serverKey);
}
function getServerDefinition(serverKey) {
const serverInfos = getGlobalServerDefinitions();
if (serverInfos.length > 0) {
for (let serverInfo of serverInfos) {
if (compareServerToKey(serverInfo, serverKey)) {
return serverInfo;
}
}
}
throw new ServerNotDefinedError(serverKey);
}
function getServerGroup(serverKey) {
const serverGroups = globalDB.get("servers").value();
if (serverGroups) {
for (const [group, serverInfos] of Object.entries(serverGroups)) {
for (let serverInfo of serverInfos) {
if (compareServerToKey(serverInfo, serverKey)) {
return group;
}
}
}
}
throw new ServerNotDefinedError(serverKey);
}
function createKeyPair(keyPath) {
console.log(child_process.execSync(`ssh-keygen -t ed25519 -N "" -f "${keyPath}"`).toString());
}
module.exports = {
readJson,
validateAppName,
appNameFromDir,
getNumberedReleaseDir,
getControlKeyPath,
serverToStr,
hostFromServer,
updateKnownHostsForServer,
getGlobalServerDefinitions,
getServerDefinition,
getServerGroup,
createKeyPair
};