-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
112 lines (88 loc) · 2.78 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
var AWS = require('aws-sdk'),
Promise = require('promise'),
Service = require('./lib/service'),
EventEmitter = require('events').EventEmitter,
util = require('util')
if (!AWS.config.region) {
AWS.config.update({region: process.env.AWS_REGION});
}
// imagePath can be optionally defined in the manifest to control a deploy
var Deployer = function(app) {
EventEmitter.call(this);
this.app = app;
this.services = [];
var service;
for (var i = 0; i < app.services.length; i++) {
this.services.push(new Service(app.services[i], this));
}
}
util.inherits(Deployer, EventEmitter);
/**
* Determine if the app deployment is valid.
*/
Deployer.prototype._isValid = function() {
var d = this;
return new Promise(function(resolve, reject) {
if (!d.app) {
reject(new Error('No application object defined for deployment'));
return;
}
if (!d.services) {
reject(new Error('No services defined for deployment'));
return;
}
if (!d.app.docker) {
reject(new Error('No docker repo defined for deployment'));
return;
}
resolve();
})
};
Deployer.prototype.isReady = function(version) {
var deployer = this;
var repoType = this.app.docker.type || 'quay';
var Repo = require(`./lib/repos/${repoType.toLowerCase()}`);
var repo = new Repo(this.app.docker);
var tagCheckDeferred = repo.isExists(version);
tagCheckDeferred.then(() => {
this.emit('progress', { msg: 'Found tagged docker image', service: { name: repoType }});
})
// TODO: Optionally check git too
var deferreds = [ tagCheckDeferred ];
for (var i = 0; i < this.services.length; i++) {
deferreds.push(this.services[i].isReady(version));
}
return Promise.all(deferreds);
};
// imagePath can be carried through the function calls as 'undefined'
Deployer.prototype.deploy = function(version) {
var d = this;
return this._isValid().then(function() {
return d.isReady(version).then(function() {
d.emit('ready')
var deferreds = [];
for (var i = 0; i < d.services.length; i++) {
deferreds.push(d.services[i].deploy(version, d.services[i].service.imagePath));
}
// Number of services that have completed their deploys
var completed = 0;
return Promise.all(deferreds).then(function(completedServices) {
for (var i = 0; i < completedServices.length; i++) {
d.emit('deployed', completedServices[i]);
}
completed += completedServices.length;
if (completed >= d.app.services.length) {
d.emit('end');
}
}, function(err) {
d.emit('failure', err);
completed += err.length;
if (completed >= d.app.services.length) {
d.emit('end');
}
})
});
});
}
Deployer.Service = Service;
module.exports = Deployer