-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
186 lines (156 loc) · 5.42 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
'use strict'
const { ECS } = require("@aws-sdk/client-ecs");
const _ = require('lodash');
// Set the default region to 'us-east-1' if not already set
const region = process.env.AWS_DEFAULT_REGION || 'us-east-1';
/**
* updater
*
* Run the end-to-end image updating process on a service or task definition.
* @param {object} options A hash of options used when initiating this deployment
* @return {Promise}
*/
const updater = function (options) {
const ecs = new ECS({ region: region });
let taskDefinitionArn; // preserve this in the outer scope
return updater.currentTaskDefinition(options)
.then((currentTaskDefinition) => {
const newTaskDefinition = updater.updateTaskDefinitionImage(
currentTaskDefinition,
options.containerNames,
options.image
);
return ecs.registerTaskDefinition(newTaskDefinition);
})
.then((data) => {
taskDefinitionArn = data.taskDefinition.taskDefinitionArn;
if (!options.serviceName) return Promise.resolve(taskDefinitionArn);
return updater.updateService(options, taskDefinitionArn)
})
.then(_service => taskDefinitionArn);
};
Object.assign(updater, {
/**
* currentTaskDefinition
*
* Retrieve the currently deployed Task Definintion
* @param {object} options A hash of options used when initiating this deployment
* @return {Promise}
*/
currentTaskDefinition(options) {
if (!options.serviceName && !options.taskDefinitionFamily) {
throw new Error('Ensure either the serviceName or taskDefinitionFamily option are specified');
}
return Promise.all([
updater.getLatestActiveTaskDefinition(options),
updater.getServiceTaskDefinition(options)
])
.then((results) => {
const taskDefinitionArn = _.filter(results, (result) => result)[0];
if (!taskDefinitionArn) throw new Error('Error could not find task definition');
return updater.getTaskDefinition(taskDefinitionArn);
});
},
/**
* getServiceTaskDefinition
*
* Retrieve the active Task Definition Arn on a service
* @param {object} options A hash of options used when initiating this deployment
* @return {Promise}
*/
getServiceTaskDefinition(options) {
if (!options.serviceName) return Promise.resolve();
const ecs = new ECS({ region: region });
const params = {
cluster: options.clusterArn,
services: [options.serviceName]
};
return ecs.describeServices(params)
.then((data) => {
const service = _.find(data.services, (s) => s.serviceName === options.serviceName);
if (!service) throw new Error(`Could not find service "${options.serviceName}"`);
return service.taskDefinition;
});
},
/**
* getLatestActiveTaskDefinition
*
* Retrieve the newest Task Definition Arn in a Task Definition Family
* @param {object} options A hash of options used when initiating this deployment
* @return {Promise}
*/
getLatestActiveTaskDefinition(options) {
if (!options.taskDefinitionFamily) return Promise.resolve();
const ecs = new ECS({ region: region });
const params = {
familyPrefix: options.taskDefinitionFamily,
sort: 'DESC',
status: 'ACTIVE'
};
return ecs.listTaskDefinitions(params)
.then((data) => {
if (data.taskDefinitionArns.length === 0) {
throw new Error(`No Task Definitions found in family "${family}"`);
}
return data.taskDefinitionArns[0];
});
},
/**
* getTaskDefinition
*
* Retrieve a task definition
* @param {object} options A hash of options used when initiating this deployment
* @return {Promise}
*/
getTaskDefinition(taskDefinitionArn) {
const ecs = new ECS({ region: region });
const params = { taskDefinition: taskDefinitionArn };
return ecs.describeTaskDefinition(params)
.then(data => data.taskDefinition);
},
updateTaskDefinitionImage(taskDefinition, containerNames, image) {
if (!_.isArray(containerNames)) containerNames = [containerNames];
const newTaskDefinition = _.clone(taskDefinition);
containerNames.forEach((containerName) => {
const containerIndex = _.findIndex(newTaskDefinition.containerDefinitions, (containerDefinition) => {
return containerDefinition.name === containerName;
});
// Container was not found in the existing task definition
if (containerIndex === -1) {
throw new Error(`Could not find container name "${containerName}" in existing task definition`);
}
newTaskDefinition.containerDefinitions[containerIndex].image = image;
});
return _.pick(newTaskDefinition, [
'containerDefinitions',
'executionRoleArn',
'family',
'networkMode',
'placementConstraints',
'taskRoleArn',
'volumes',
'requiresCompatibilities',
'cpu',
'memory'
]);
},
/**
* updateService
*
* Update the service to use a new Task Definition
* @param {object} options A hash of options used when initiating this deployment
* @param {sting} taskDefinitionArn The task definition to deploy
* @return {Promise}
*/
updateService(options, taskDefinitionArn) {
const ecs = new ECS({ region: region });
const params = {
cluster: options.clusterArn,
service: options.serviceName,
taskDefinition: taskDefinitionArn
};
return ecs.updateService(params)
.then(data => data.service);
},
});
module.exports = updater;