forked from oxheadalpha/teztnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTezosImageResolver.ts
104 lines (93 loc) · 3.71 KB
/
TezosImageResolver.ts
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
import * as http from 'https';
export class TezosImageResolver {
image = 'tezos/tezos';
getAuthToken(): Promise<string> {
return new Promise((resolve) => {
http.get({
hostname: 'auth.docker.io',
path: `/token?service=registry.docker.io&scope=repository:${this.image}:pull`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
}, (res) => {
var str = ''
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
resolve(JSON.parse(str).token);
});
})
})
};
getTags(authToken: string): Promise<string[]> {
return new Promise((resolve, reject) => {
http.get({
hostname: 'registry-1.docker.io',
path: `/v2/${this.image}/tags/list`,
headers: {
'Authorization': `Bearer ${authToken}`,
'Accept': 'application/json'
}
}, (res) => {
const { statusCode } = res;
const contentType: string = res.headers['content-type'] || "";
var error = null;
if (statusCode !== 200) {
error = new Error('Tag list request failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Tag list response invalid.\n' +
`Expected content type application/json but received ${contentType}`);
}
if (error) {
res.resume();
reject(error);
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
resolve(JSON.parse(rawData).tags);
} catch (e) {
reject(e);
}
});
}).on('error', (e) => {
reject(e);
});
});
}
getLatestTag(tags: string[], cutoff: Date): string {
var latestTag: { tag:string, sha: string, date: Date } = { tag: '', sha: '', date: new Date(0) };
tags.forEach((tag: string) => {
const splits: string[] = tag.split('_');
if ((splits.length == 3) && (splits[0] === "master") && (splits[1].length == 8) && (splits[2].length == 14)) {
var date = new Date(Date.UTC(
parseInt(splits[2].substr(0, 4)),
parseInt(splits[2].substr(4, 2)) - 1,
parseInt(splits[2].substr(6, 2)),
parseInt(splits[2].substr(8, 2)),
parseInt(splits[2].substr(10, 2)),
parseInt(splits[2].substr(12, 2)),
));
if ((date.valueOf() > latestTag.date.valueOf()) &&
(date.valueOf() < cutoff.valueOf())) {
latestTag = {
tag: tag,
sha: splits[1],
date: date
}
};
}
})
return latestTag.tag;
}
async getLatestTagAsync(cutoff: Date): Promise<string> {
const authToken = await this.getAuthToken();
const tags = await this.getTags(authToken);
return this.getLatestTag(tags, cutoff);
}
}