forked from Aelloy/ember-cli-deploy-simply-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (178 loc) · 6.87 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
/* jshint node: true */
'use strict';
const RSVP = require('rsvp');
const BasePlugin = require('ember-cli-deploy-plugin');
const {NodeSSH} = require('node-ssh');
const path = require('path');
const minimatch = require('minimatch');
const node_ssh = new NodeSSH();
module.exports = {
name: 'ember-cli-deploy-simply-ssh',
createDeployPlugin(options) {
const DeployPlugin = BasePlugin.extend({
name: options.name,
defaultConfig: {
dir: '/var/www',
releasesDir: 'releases',
targetLink: 'current',
ignorePattern: null,
keep: 2
},
setup() {
const ssh = this.readConfig('sshClient') || node_ssh;
return ssh.connect(this.readConfig('connection')).then((conn) => {
this.log("SSH connection established.", {color: "green"});
return {ssh: conn};
});
},
fetchRevisions(context) {
return this._fetchRevisionsJson(context).then((revisions) => {
return {revisions: revisions};
});
},
willUpload(context) {
let dir = this.readConfig('dir');
let releasesDir = this.readConfig('releasesDir');
if (context.revisionData && context.revisionData.revisionKey) {
dir = path.posix.join(dir, releasesDir, context.revisionData.revisionKey);
};
this.log("Creating directory " + dir, {color: "green"});
return this._execCommand(context, 'mkdir -p ' + dir).then(() => {
return {releaseDir: dir};
});
},
upload(context) {
this.log("Uploading files:", {color: "green"});
let files = context.distFiles;
const ignorePattern = this.readConfig('ignorePattern');
if (ignorePattern != null) {
this.log('ignoring `' + ignorePattern + '`', { verbose: true });
files = files.filter(function(path){
return !minimatch(path, ignorePattern, { matchBase: true });
});
}
const distDir = path.join(process.cwd(), context.distDir);
return files.reduce((promise, file) => {
return promise.then(() => {
const local = path.join(distDir, file);
const remote = path.posix.join(context.releaseDir, file);
return context.ssh.putFile(local, remote).then(() => {
this.log(file);
});
});
}, RSVP.Promise.resolve()).then(() => {
if (context.revisionData && context.revisionData.revisionKey) {
return {
uploadedRevision: {
revision: context.revisionData.revisionKey,
timestamp: Date.now(),
active: false
}
}
};
});
},
didUpload(context) {
if (!context.uploadedRevision) { return RSVP.resolve() };
return this._fetchRevisionsJson(context).then((revisions) => {
revisions = this._mergeRevision(revisions, context.uploadedRevision);
const split = this._splitRevisions(revisions, this.readConfig('keep'));
const del = split[0];
const keep = split[1];
let promises = [];
if (del.length > 0) { promises.push(this._deleteRevisions(context, del)) };
if (keep.length > 0) { promises.push(this._saveRevisions(context, keep)) };
return RSVP.all(promises);
});
},
activate(context) {
if (!(context.commandOptions.revision || context.uploadedRevision)) {
return RSVP.resolve();
};
const revision = context.commandOptions.revision || context.revisionData.revisionKey;
const source = path.posix.join(this.readConfig('dir'), this.readConfig('releasesDir'), revision);
const target = path.posix.join(this.readConfig('dir'), this.readConfig('targetLink'));
const cmd = "test -e " + source
+ " && ln -sfn " + source + " " + target
+ " || >&2 echo \"Revision is missing!\"";
return this._execCommand(context, cmd).then(() => {
this.log("Revision " + revision + " is now active!", {color: 'green'});
return {activatedRevision: revision};
});
},
didActivate(context) {
if (!context.activatedRevision) {
return RSVP.resolve();
};
const revisions = context.revisions.map((r) => {
r.active = r.revision == context.activatedRevision;
return r;
});
return this._saveRevisions(context, revisions);
},
teardown(context) {
context.ssh.dispose();
this.log("SSH connection closed", {color: 'green'});
},
_execCommand(context, command){
return new RSVP.Promise((resolve, reject) => {
context.ssh.execCommand(command, {stream: 'both'}).then((result) => {
if (result.stderr) {
reject(result.stderr);
};
resolve(result.stdout);
});
});
},
_fetchRevisionsJson(context) {
const revPath = path.posix.join(this.readConfig('dir'), this.readConfig('releasesDir'), 'revisions.json');
const cmd = `(test -e ${revPath} && cat ${revPath}) || echo "[]"`;
return this._execCommand(context, cmd).then((revisions) => {
try {
return this._normalizeRevisions(JSON.parse(revisions));
} catch (e) {
this.log(e, {color: 'red'});
return [];
};
});
},
_normalizeRevisions(revisions) {
return revisions.filter((r) => !!r.revision).sort((a,b) => a.timestamp - b.timestamp)
},
_mergeRevision(revisions, revision) {
revisions = revisions.filter((r) => r.revision != revision.revision);
revisions.push(revision);
return revisions;
},
_splitRevisions(revisions, splitPoint) {
const offset = revisions.length - splitPoint;
let keep = [], del = [];
revisions.forEach((r, i) => {
if (i >= offset || r.active) {
keep.push(r);
} else {
del.push(r);
}
});
return [del, keep];
},
_deleteRevisions(context, revisions) {
const deleting = revisions.map((r) => {
return path.posix.join(this.readConfig('dir'), this.readConfig('releasesDir'), r.revision);
});
return this._execCommand(context, "rm -rf " + deleting.join(" ")).then(() => {
this.log("Purging revisions:", {color: 'green'});
deleting.forEach((rev) => this.log(rev));
});
},
_saveRevisions(context, revisions) {
const revisionPath = path.posix.join(this.readConfig('dir'), this.readConfig('releasesDir'), 'revisions.json');
const cmd = "echo '" + JSON.stringify(revisions) + "' > " + revisionPath;
return this._execCommand(context, cmd).then(() => {
this.log("revisions.json updated", {color: 'green'});
});
}
});
return new DeployPlugin();
}
};