-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
248 lines (202 loc) · 7.16 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* eslint-env node */
'use strict';
const BasePlugin = require('ember-cli-deploy-plugin');
const path = require('path');
const os = require('os');
const username = require('username');
const RSVP = require('rsvp');
const Rsync = require('rsync');
const SSHClient = require('./lib/ssh-client');
const defaultConfig = {
distDir: (context) => context.distDir,
host: '',
username: '',
password: null,
privateKeyPath: '~/.ssh/id_rsa',
rsyncFlags: 'rtvu',
agent: null,
port: 22,
directory: 'tmp/deploy-dist/.',
root(context) {
return path.posix.join('/usr/local/www', context.project.name());
},
activationDestination(context, pluginHelper) {
let root = pluginHelper.readConfig('root');
return path.posix.join(root, 'active');
},
activationStrategy: 'symlink',
uploadDestination(context, pluginHelper) {
let root = pluginHelper.readConfig('root');
return path.posix.join(root, 'revisions');
},
revisionManifest(context, pluginHelper) {
let root = pluginHelper.readConfig('root');
return path.posix.join(root, 'revisions.json');
},
revisionKey(context) {
return (context.commandOptions && context.commandOptions.revision) || (context.revisionData && context.revisionData.revisionKey);
},
revisionMeta(context, pluginHelper) {
let revisionKey = pluginHelper.readConfig('revisionKey');
let who = username.sync() + '@' + os.hostname();
return {
revision: revisionKey,
deployer: who,
timestamp: new Date().getTime(),
};
},
};
class DeployPlugin extends BasePlugin {
constructor(options) {
super();
this.name = options.name;
this.defaultConfig = defaultConfig;
this._rsyncClientClass = Rsync;
this._sshClient = SSHClient;
}
configure(context) {
super.configure(this, context);
let options = {
host: this.readConfig('host'),
username: this.readConfig('username'),
password: this.readConfig('password'),
port: this.readConfig('port'),
privateKeyPath: this.readConfig('privateKeyPath'),
passphrase: this.readConfig('passphrase'),
agent: this.readConfig('agent') || process.env['SSH_AUTH_SOCK']
};
this._client = new this._sshClient(options);
this._rsyncClient = new this._rsyncClientClass();
return this._client.connect(this);
}
activate(/*context*/) {
let client = this._client;
let revisionKey = this.readConfig('revisionKey');
let activationDestination = this.readConfig('activationDestination');
let uploadDestination = path.posix.join(this.readConfig('uploadDestination'), '/');
let activeRevisionPath = path.posix.join(uploadDestination, revisionKey, '/');
let activationStrategy = this.readConfig('activationStrategy');
let revisionData = {
revisionData: {
activatedRevisionKey: revisionKey
}
};
let linkCmd;
this.log('Activating revision ' + revisionKey);
if (activationStrategy === 'copy') {
linkCmd = 'cp -TR ' + activeRevisionPath + ' ' + activationDestination;
} else {
linkCmd = 'ln -fsn ' + activeRevisionPath + ' ' + activationDestination;
}
return client.exec(linkCmd)
.then(() => this._activateRevisionManifest())
.then(() => revisionData);
}
fetchRevisions(context) {
this.log('Fetching Revisions');
return this._fetchRevisionManifest()
.then((manifest) => context.revisions = manifest);
}
upload(/*context*/) {
return this._updateRevisionManifest()
.then(() => {
this.log('Successfully uploaded updated manifest.', { verbose: true });
return this._uploadApplicationFiles();
});
}
teardown(/*context*/) {
return this._client.disconnect();
}
_uploadApplicationFiles(/*context*/) {
let client = this._client;
let revisionKey = this.readConfig('revisionKey');
let uploadDestination = this.readConfig('uploadDestination');
let destination = path.posix.join(uploadDestination, revisionKey);
let generatedPath = this.readConfig('username') + '@' + this.readConfig('host') + ':' + destination;
this.log('Uploading `applicationFiles` to ' + destination);
return client.exec('mkdir -p ' + destination)
.then(() => this._rsync(generatedPath))
.then(() => this.log('Finished uploading application files!'));
}
_activateRevisionManifest(/*context*/) {
let client = this._client;
let revisionKey = this.readConfig('revisionKey');
let manifestPath = this.readConfig('revisionManifest');
return this._fetchRevisionManifest()
.then((manifest) => {
manifest.forEach((rev) => {
if (rev.revision == revisionKey) {
rev.active = true;
} else {
delete rev['active'];
}
return rev;
});
let data = Buffer.from(JSON.stringify(manifest), 'utf-8');
return client.upload(manifestPath, data, this);
});
}
_updateRevisionManifest() {
let revisionKey = this.readConfig('revisionKey');
let revisionMeta = this.readConfig('revisionMeta');
let manifestPath = this.readConfig('revisionManifest');
let client = this._client;
this.log('Updating `revisionManifest` ' + manifestPath, { verbose: true });
return this._fetchRevisionManifest()
.then((manifest) => {
let existing = manifest.some((rev) => rev.revision === revisionKey);
if (existing) {
this.log('Revision ' + revisionKey + ' already added to `revisionManifest` moving on.', { verbose: true });
return;
}
this.log('Adding ' + JSON.stringify(revisionMeta), { verbose: true });
manifest.unshift(revisionMeta);
let data = Buffer.from(JSON.stringify(manifest), 'utf-8');
return client.upload(manifestPath, data);
});
}
_fetchRevisionManifest() {
let manifestPath = this.readConfig('revisionManifest');
let client = this._client;
return client.readFile(manifestPath)
.then((manifest) => {
this.log('fetched manifest ' + manifestPath, { verbose: true });
return JSON.parse(manifest);
})
.catch((error) => {
if (error.message === 'No such file') {
this.log('Revision manifest not present building new one.', { verbose: true });
return RSVP.resolve([ ]);
} else {
return RSVP.reject(error);
}
});
}
_rsync(destination) {
let rsync = this._rsyncClient
.shell('ssh -p ' + this.readConfig('port'))
.flags(this.readConfig('rsyncFlags'))
.source(this.readConfig('directory'))
.destination(destination);
if (this.readConfig('exclude')) {
rsync.set('exclude', this.readConfig('exclude'));
}
if (this.readConfig('displayCommands')) {
this.log(rsync.command());
}
return new RSVP.Promise((resolve, reject) => {
rsync.execute((error/*, code, cmd*/) => {
if (error) {
reject(error);
}
return resolve();
}, (stdout) => this.log(stdout, { verbose: true }), (stderr) => this.log(stderr, { color: 'red', verbose: true }));
});
}
}
module.exports = {
name: 'ember-cli-deploy-with-rsync',
createDeployPlugin(options) {
return new DeployPlugin(options);
}
};