forked from apim-haufe-io/wicked.env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-tools.js
58 lines (53 loc) · 1.98 KB
/
docker-tools.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
const request = require('request');
const debug = require('debug')('portal-env:docker-tools');
const dockerTools = function () { };
function getJson(ob) {
if (ob instanceof String || typeof ob === "string")
return JSON.parse(ob);
return ob;
}
dockerTools.getMatchingTag = function (namespace, imageName, tagName, callback) {
debug(`getMatchingTag(${namespace}, ${imageName}, ${imageName}`);
const tagUrl = `https://index.docker.io/v1/repositories/${namespace}/${imageName}/tags`;
request.get({
url: tagUrl
}, function (err, res, body) {
if (err)
return callback(err);
if (res.statusCode !== 200) {
return callback(new Error('getMatchingTag: Unexpected status code ' + res.statusCode));
}
const tagList = getJson(body);
// This should be an array of tag names
debug(tagList);
const alpineIndex = tagName.indexOf('alpine');
let exactTag = null;
let fallbackTag = null;
if (alpineIndex > 0) {
const baseName = tagName.substring(0, alpineIndex - 1);
if (imageName.indexOf('portal-env') > 0) {
exactTag = baseName + '-onbuild-alpine';
fallbackTag = 'next-onbuild-alpine';
} else {
exactTag = tagName;
fallbackTag = 'next-alpine';
}
} else {
const baseName = tagName;
if (imageName.indexOf('portal-env') > 0) {
exactTag = baseName + '-onbuild';
fallbackTag = 'next-onbuild';
} else {
exactTag = baseName;
fallbackTag = 'next';
}
}
debug('exactTag: ' + exactTag);
debug('fallbackTag: ' + fallbackTag);
const exactMatch = tagList.find(tag => tag.name == exactTag);
if (exactMatch)
return callback(null, exactTag);
return callback(null, fallbackTag);
});
};
module.exports = dockerTools;