-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcreate-container.js
108 lines (100 loc) · 3.22 KB
/
create-container.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
/* eslint no-process-exit: 0*/
'use strict';
var fs = require('fs');
var yaml = require('js-yaml');
var request = require('request');
var Container = require('../lib/Container');
var exports = function() {
this.configure = this.configure.bind(this);
this.run = this.run.bind(this);
};
exports.shortDescription = 'Simple comand line interface to perform a build on a container.';
exports.config = function() {
};
exports.options = function(yargs) {
return yargs
.describe('build-file', 'The .probo.yml file to build from.')
.alias('build-file', 'b')
.demand('build-file')
.describe('container-name', 'The name to give the docker container')
.alias('container-name', 'n')
.demand('container-name')
.describe('container-manager-url', 'If specified, the running container manager URL to start the container from.')
.alias('container-manager-url', 'u')
.describe('commit-ref', 'The commit to in for use in build steps.')
.alias('commit-ref', 'r')
.describe('provider-slug', 'The identifying string used in this provider. With Github this would be `organization/repository`.')
.alias('provider-slug', 'R')
.demand('provider-slug')
.describe('provider-type', 'The provider to fetch code from (defaults to `github`.')
.default('provider-type', 'github')
.describe('provider-api-token', 'The personal API token for the provider.')
.alias('provider-api-token', 'A')
.demand('provider-api-token')
;
};
exports.run = function(probo) {
var config = probo.config;
var jobConfig = yaml.safeLoad(fs.readFileSync(probo.config.buildFile));
// Defaults should move into the CM.
var image = jobConfig.image || config.image;
var imageConfig = config.images[image];
if (!imageConfig) return exitWithError('Invalid image ' + image + ' selected.');
var options = {
containerName: probo.config.containerName,
docker: config.docker,
imageConfig: imageConfig,
config: jobConfig,
// TODO: We seem to treat this differently in Container from ContainerManager.
// This structure needs to be audited/cleaned up.
jobConfig: jobConfig,
binds: config.binds,
attachLogs: true,
build: {
config: {
image: image,
},
},
project: {
// TODO: What should we do about the expectation that we pass in project ID?
id: 123,
slug: config.providerSlug,
provider: {
type: config.providerType,
},
service_auth: {
token: config.providerApiToken,
},
},
};
if (config.commitRef) {
options.build.ref = config.commitRef;
}
if (config.containerManagerUrl) {
var requestOptions = {
method: 'post',
uri: 'http://' + config.containerManagerUrl + '/startbuild',
body: options,
json: true,
};
console.log(requestOptions);
request(requestOptions, function(error, response, body) {
console.log(error, body);
});
}
else {
var container = new Container(options);
container.runBuild()
.then(function(data) {
console.log('Created', data.Id);
})
.catch(function(error) {
console.error('ERROR', error);
});
}
};
function exitWithError(message) {
console.error(message);
process.exit(1);
}
module.exports = exports;