-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (174 loc) · 5.32 KB
/
index.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
/** global process */
const fs = require('fs');
const path = require('path');
const { NodeSSH } = require('node-ssh');
const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/utc'));
const logger = require('./src/logger');
const { resolveHomePath, isFn } = require('./src/helper');
const AsyncSpawner = require('./src/async-spawner');
const keyFileNames = ['id_rsa', 'id_ed25519'];
class Slingshot {
constructor(config = {}, cli = null) {
this.config = { ...config };
this.cli = cli;
this.logger = logger;
this.ssh = new NodeSSH();
this.releasePath = this._releasePath();
return this;
}
set(key, val = null) {
switch (key) {
case 'cli':
this.cli = val;
break;
case 'config':
this.config = { ...this.config, ...val };
break;
case null:
break;
default:
this.config[key] = val;
break;
}
return this;
}
async _checkConfig() {
if (!this.config.username || !this.config.host) {
throw new Error('Credentials are missing!');
}
// set ssh config
this.sshconfig = {
host: this.config.host,
port: this.config.port ?? 22,
username: this.config.username,
};
if (this.config.password) {
this.sshconfig.password = this.config.password;
}
if (this.config.privateKeyPath) {
this.sshconfig.privateKeyPath = this._resolveSSHKey(this.config.privateKeyPath);
}
// try to extract from local key files if nothing has been passed as option
if (this._noSSHCredentialsSet()) {
for (var i = 0; i < keyFileNames.length; i++) {
const privateKeyPath = path.join(process.env.HOME, '.ssh/', keyFileNames[i]);
if (fs.existsSync(privateKeyPath)) {
this.sshconfig.privateKeyPath = this._resolveSSHKey(privateKeyPath);
break;
}
}
}
// transform privateKeyPath to Buffer output
if (this.sshconfig.privateKeyPath) {
this.sshconfig.privateKey = fs.readFileSync(this.sshconfig.privateKeyPath, 'utf8');
delete this.sshconfig.privateKeyPath;
}
// throw error if still no ssh credentials are set
if (this._noSSHCredentialsSet()) {
throw `SSH Credentials are missing!`;
}
logger.stepsuccess(`valid config`);
return true;
}
_noSSHCredentialsSet() {
return !this.sshconfig.password && !this.sshconfig.privateKey;
}
_resolveSSHKey(filepath) {
const p = resolveHomePath(filepath);
return fs.existsSync(p) ? p : null;
}
async _checkConnection() {
try {
await this.ssh.connect(this.sshconfig);
logger.stepsuccess('ssh connection successful');
} catch (error) {
logger.steperror('shh connection not successful');
throw error;
}
}
async _checkRemoteStructure() {
const dir = this.config.deployTo;
// do nothing if deployTo is not set and
// assume the directory of ssh connection
// must be the target.
if (!dir) {
return;
}
const cmd = `if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi;`;
const res = await this.ssh.execCommand(cmd, {});
if (res.stderr) {
logger.steperror('remote structure invalid');
throw new Error(res.stderr);
}
logger.stepsuccess('remote structure valid');
return true;
}
_releasePath() {
const dirname = dayjs().utc().format('YYYYMMDDHHmmss');
return path.join(this.config.deployTo, 'releases/', dirname);
}
async _createRelease() {
logger.step('create release directory');
await this.ssh.execCommand(`mkdir -p ${this.releasePath}`);
}
async _copySharedPaths() {}
async _runChecks() {
logger.task(`checks`);
await this._checkConfig();
await this._checkConnection();
await this._checkRemoteStructure();
logger.success();
}
async _runHook(hook) {
if (isFn(this.config[hook])) {
logger.task(hook + '()');
await this.config[hook](this);
logger.success();
}
}
async exec(...args) {
const spawner = new AsyncSpawner();
return spawner.spawn(...args);
}
async deploy(rev = null) {
try {
// assume argument is a git revision
// and update config field related to it.
this.config.revision = rev ?? this.config.revision;
// checkout revision
if (this.config.revision) {
await this.exec('git', ['checkout', this.config.revision]);
}
// start pipeline
await this._runHook('init');
await this._runHook('preChecks');
await this._runChecks();
await this._runHook('postChecks');
await this._runHook('preDeploy');
await this._runHook('preCreate');
await this._createRelease();
await this._runHook('postCreate');
await this._runHook('preCopyShared');
await this._copySharedPaths();
await this._runHook('postCopyShared');
await this._runHook('preUpload');
await this._upload();
await this._runHook('postUpload');
await this._runHook('prePublish');
await this._publish();
await this._runHook('postPublish');
await this._runHook('preCleanup');
await this._cleanup();
await this._runHook('postCleanup');
await this._runHook('postDeploy');
Promise.resolve();
process.exit(0);
} catch (error) {
console.error(error.stack);
Promise.reject();
process.exit(1);
}
}
}
module.exports = Slingshot;