-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
92 lines (71 loc) · 2.77 KB
/
app.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
const helpers = require('./helpers');
const { listRegions } = require('./autocomplete');
var URL = require('url').URL;
async function getEcrLogin(action, settings){
const ecr = helpers.getEcr(action, settings);
let registryIds;
if(typeof action.params.registryIds == "string"){
registryIds = [action.params.registryIds];
} else {
registryIds = action.params.registryIds;
}
if(!registryIds){
throw "You must specify registry IDs";
}
const authToken = await new Promise((resolve,reject)=>{
ecr.getAuthorizationToken({registryIds},helpers.operationCallback(resolve,reject));
});
authToken.authorizationData.forEach(authData=>{
const [user, password] = Buffer.from(authData.authorizationToken, 'base64').toString('utf-8').split(':');
const host = new URL(authData.proxyEndpoint).host;
authData.decodedToken = {host, user, password};
authData.baseDockerLoginCommand = `docker login --username ${user} --password "${password}" ${host}`
})
return authToken;
}
async function runDockerFuncOnECR(action, settings, dockerCmd){
const {repoName, registryId} = action.params;
action.params.registryIds = [registryId];
const loginData = await getEcrLogin(action, settings);
const authData = loginData.authorizationData[0];
const loginCommand = `${authData.baseDockerLoginCommand}/${repoName}`
const fullCommand = [
loginCommand,
dockerCmd,
].join(' && ');
let hasErr;
let pushResult;
try{
pushResult = await helpers.executeCmd(fullCommand);
} catch (err){
hasErr = err;
}
const logoutRes = await helpers.executeCmd(`docker logout ${authData.decodedToken.host}`);
if(hasErr){
const errMsg = hasErr.message.replace(authData.decodedToken.password, '<DOCKER_PASS>')
throw [errMsg, logoutRes].join('\n');
}
return [fullCommand.replace(authData.decodedToken.password, '<DOCKER_PASS>'), pushResult, logoutRes].join('\n');
}
async function pullImage(action, settings){
return runDockerFuncOnECR(action, settings, `docker pull ${action.params.imageName}`);
}
async function pushImgaeToRepo(action, settings){
return runDockerFuncOnECR(action, settings, `docker push ${action.params.imageName}`);
}
async function describeRepositories(action, settings){
const registryId = action.params.registryId;
const ecr = helpers.getEcr(action, settings);
const repositories = await new Promise((resolve,reject)=>{
ecr.describeRepositories({registryId}, helpers.operationCallback(resolve,reject))
});
return repositories;
}
module.exports = {
describeRepositories,
pushImgaeToRepo,
getEcrLogin,
pullImage,
// autocomplete
listRegions
};