Skip to content

Commit

Permalink
Add new s3-copy release method
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Varache committed Aug 10, 2018
1 parent bbaa7de commit 4d3d739
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/deploy-methods/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
s3: require('./s3')
};
s3: require('./s3'),
's3-copy': require('./s3-copy'),
};
127 changes: 127 additions & 0 deletions lib/deploy-methods/s3-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const config = require('../config'),
{ Build, Release } = require('../data'),
mime = require('mime-types'),
s3 = require('../s3-helpers');

function _updateProgress(reporter, message) {
if (reporter) {
reporter.emit('update', {message});
}
}

function _doRelease(build, channel, reporter) {
// Copy the original build and change the channel to the target one
const targetBuild = Build.fromListEntry({ Key: build.path, LastModified: build.buildDate });
targetBuild.channel = channel;
// Copt the s3 object to the new location
return (new Promise((resolve, reject) => {
s3.getInstance().copyObject({
Bucket: config.local.rootBucket.name,
CopySource: `${config.local.rootBucket.name}/${build.path}`,
Key: targetBuild.path,
}, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
})
}))
.then(() => {
_updateProgress(reporter, 'File moved to release channel')
const release = new Release(build);
release.updateReleaseDate();
return release;
});
}

function _uploadKartFile(release, bucket, path) {
return new Promise((resolve, reject) => {
s3.getInstance().upload(
{
Bucket: bucket,
Key: `${path}/kart.json`,
Body: release.toJSON(),
},
(err, data) => {
if (err) {
return reject('Failed uploading the kart file:' + err);
}

resolve();
}
);
});
}

function _downloadKartFile(project, channel) {
return new Promise((resolve, reject) => {
const build = new Build({
project,
channel,
version: 0,
number: 0,
arch: 'all',
metadata: {},
ext: 'tar.gz',
});
const p = build.path;
const pieces = p.split('/');
pieces.pop();
const prefix = pieces.join('/');
s3.getInstance().getObject(
{
Bucket: config.local.rootBucket.name,
Key: `${prefix}/kart.json`
},
(err, data) => {
if (err) {
if (err.code === 'NoSuchKey') {
return resolve(null);
}
return reject('Failed downloading kart.json: ' + err);
}

resolve(new Release(JSON.parse(data.Body.toString())));
}
);
});
}

/**
* Release a build.
*
* Uploading algorithms:
* * clear: Clears the target bucket before writing the build into it.
* * overwrite: Writes all the files into the bucked without clearing it out first.
* * sync: Uses the `aws sync` command to minimise bandwith usage.
*
* @param {Object} build The build object to be released.
* @param {Object} opts Options.
* @param {String} opts.channel Target channel.
* @param {String} opts.reporter An optional event listener to report events.
*/
function release(build, opts) {
if (!opts.channel) {
throw new Error('No target channel specified');
}
const { channel, reporter } = opts;

return _doRelease(build, channel, reporter)
.then((r) => {
const p = r.path;
const pieces = p.split('/');
pieces.pop();
const kartJsonPath = pieces.join('/');
return _uploadKartFile(r, config.local.rootBucket.name, kartJsonPath)
.then(() => r);
});
}

function status(project, channel) {
return _downloadKartFile(project, channel);
}

module.exports = {
release,
status,
};
17 changes: 17 additions & 0 deletions test/release-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,21 @@ describe('kart', function () {
});
});
});

describe('Deploy method: s3-copy', () => {
describe('to channel', () => {
it('deploy archive', () => {
let build;

return testUtil.generateAndArchiveBuilds([
{ project: 'testing', channel: 'copy', version: '1.2.3', metadata: { revision: '1234567' }, options: { fileCount: 1, subdirs: 0 } },
]).then((res) => {
build = res[0];
return kart.release(build.archive);
}).then((release) => {
return testUtil.assertFileExists(testUtil.ROOT_BUCKET, release.path);
});
});
});
});
});
18 changes: 17 additions & 1 deletion test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class TestUtil {
bucket: "testing-clear",
algorithm: "clear"
}
},
copy: {
deploy: {
method: "s3-copy",
channel: "testing-public",
}
}
}
}
Expand Down Expand Up @@ -322,7 +328,17 @@ class TestUtil {
});
});
}

assertFileExists(bucket, path) {
return new Promise((resolve, reject) => {
this.s3.getObject({
Bucket: bucket,
Key: path
}, (err, data) => {
assert(data, `File ${path} not found in ${bucket}`);
resolve();
});
});
}
assertRelease (archive) {
return new Promise((resolve, reject) => {
let downloadStream = this.s3.getObject({
Expand Down

0 comments on commit 4d3d739

Please sign in to comment.