| 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
249
250
251
252
253
254
255
256
257 | 1×
1×
52×
52×
52×
11×
41×
1×
54×
54×
1×
53×
1×
52×
52×
52×
1×
52×
52×
52×
1×
51×
51×
51×
51×
51×
1×
5×
5×
5×
5×
5×
1×
65×
55×
55×
55×
38×
42×
15×
55×
1×
65×
27×
27×
22×
22×
10×
12×
22×
28×
28×
28×
28×
22×
27×
1×
67×
67×
1×
66×
1×
65×
65×
65×
65×
65×
55×
65×
1×
| const tarFs = require('tar-fs'),
fs = require('fs'),
zlib = require('zlib'),
semver = require('semver'),
config = require('./config'),
Build = require('./data').Build,
s3 = require('./s3-helpers');
function _getNextBuildNumber(projectName, channel, version) {
let opts = {
filter: {
version: version
},
sort: {
key: 'number',
order: -1
},
limit: 1
};
return list(projectName, channel, opts).then((res) => {
if (res.length > 0) {
return res[0].number + 1;
}
return 1;
});
}
/*
* Compress and upload a build to the apropriate place in the archive.
*
* @param {String} buildDir Path to a directory to be archived (top level dir won't be included).
* @param {String} projectName Name of the project being archived (e.g., kano-code).
* @param {String} channel Target channel (e.g., staging).
* @param {String} version Semver version (e.g., 1.2.3).
* @param {Number} number (optional) Build sequence number.
* Will be determined automatically when omitted.
* @param {String} arch (optional) Architecture for this build. Defaults to 'all'.
* @param {Object} metadata (optional) Additional info to store with the build.
* @param {String} .revision Git SHA to tie the build with a particular commit.
*/
function storeBuild(buildDir, projectName, channel, version, number, arch, metadata) {
arch = arch || 'all';
if (Object.keys(config.remote.projects).indexOf(projectName) < 0) {
return Promise.reject(`Project '${projectName}' not found`);
}
if (Object.keys(config.remote.projects[projectName].channels).indexOf(channel) < 0) {
return Promise.reject(`Channel '${channel}' not found in project '${projectName}'`);
}
Iif (number) {
return _doStoreBuild(buildDir, projectName, channel, version, number, arch, metadata);
} else {
return _getNextBuildNumber(projectName, channel, version)
.then((n) => {
return _doStoreBuild(buildDir, projectName, channel, version, n, arch, metadata);
});
}
}
function _doStoreBuild(buildDir, projectName, channel, version, number, arch, metadata) {
return new Promise((resolve, reject) => {
fs.lstat(buildDir, (err, stat) => {
if (err) {
return reject(new Error(`Could not find or access build directory: ${buildDir}`));
}
Iif (!stat.isDirectory()) {
return reject(new Error(`Path provided is not a directory: ${buildDir}`));
}
let gzip = zlib.createGzip(),
stream = tarFs.pack(buildDir),
build = new Build({
project: projectName,
channel: channel,
version: version,
metadata: metadata,
number: number,
arch: arch,
buildDate: new Date()
});
s3.getInstance().upload(
{
Bucket: config.local.rootBucket.name,
Key: build.path,
Body: stream.pipe(gzip),
ACL: 'public-read',
Metadata: metadata
},
(err, data) => {
Iif (err) {
return reject(new Error('Failed to upload archive: ' + err));
}
resolve(build);
}
);
});
});
}
/*
* Remove a build from the archive by path or build object.
*
* @param {String, Object} build Either a direct path to build or a build object.
*
*/
function removeBuild(build) {
Iif (typeof build === 'string') {
build = {path: build};
}
return new Promise((resolve, reject) => {
s3.getInstance().deleteObject(
{
Bucket: config.local.rootBucket.name,
Key: build.path
},
(err, data) => {
Iif (err) {
return reject('Failed to remove ' + path + ':' + err);
}
resolve();
}
);
});
}
function _getFilter(opts) {
return (build) => {
let match = true;
Iif (!build) {
return false;
}
if (opts.filter) {
Object.keys(opts.filter).forEach((key) => {
if (build[key] !== opts.filter[key]) {
match = false;
}
});
}
return match;
};
}
function _getComparator(opts) {
return (a, b) => {
let res = 0;
if (opts.sort) {
let keys,
order = opts.sort.order || 1;
if (!Array.isArray(opts.sort.key)) {
keys = [opts.sort.key];
} else {
keys = opts.sort.key.slice(0);
}
while (res === 0 && keys.length) {
let key = keys.shift();
/* Exception: use semver to compare versions */
Iif (key === 'version') {
let ca = semver.clean(a[key]),
cb = semver.clean(b[key]);
if (semver.gt(ca, cb)) {
res = order > 0 ? 1 : -1;
} else if (semver.lt(ca, cb)) {
res = order > 0 ? -1 : 1;
}
} else {
Iif (a[key] > b[key]) {
res = order > 0 ? 1 : -1;
} else if (a[key] < b[key]) {
res = order > 0 ? -1 : 1;
}
}
}
}
return res;
};
}
/*
* List builds of a project in a channel.
*
* @param {String} project The name of the project (e.g., kano-code).
* @param {String} channel The name of the channel (e.g., staging).
*
* @param {Object} opts (optional) Modify the listing results.
* @param {Object} opts.filter Filtering options (key value pairs).
* Filtering based on metadata not supported at the moment.
*
* @param {Object} opts.sort Sorting options.
* @param {Sring, Array} opts.sort.key One or more keys to sort by in order of priority
* @param {Number} opts.sort.order 1 for ascending, -1 for descending.
*
* @param {Number} opts.limit Limit the list to N entries.
*
*/
function list(project, channel, opts) {
opts = Object.assign({}, opts);
if (!config.remote.projects[project]) {
return Promise.reject(`Project '${project}' not found`);
}
if (!config.remote.projects[project].channels[channel]) {
return Promise.reject(`Channel '${channel}' not found in project '${project}'`);
}
return new Promise((resolve, reject) => {
let folder = `${project}/${channel}/`,
results;
s3.getInstance().listObjects(
{
Bucket: config.local.rootBucket.name,
Delimiter: '/',
Prefix: folder
},
(err, data) => {
Iif (err) {
return reject('Unable to list files in the root bucket: ' + err);
}
results = data.Contents
.filter((entry) => {
// Remove the directory itself from the results
return entry.Key != folder;
})
.map(Build.fromListEntry)
.filter(_getFilter(opts))
.sort(_getComparator(opts));
resolve(results.slice(0, opts.limit || results.length));
}
);
});
}
module.exports = {
list,
store: storeBuild,
remove: removeBuild
} |