-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateStableManifest.js
executable file
·150 lines (125 loc) · 4.31 KB
/
updateStableManifest.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
#!/usr/bin/nodejs
/* update a packer json manifest when a new stable release
* appears on cdimage.debian.org
*
* debian packages required for this script:
* nodejs node-semver node-xmlhttprequest
*/
const fs = require('fs');
const semver = require('semver');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const debug = true;
const currentURL = 'http://cdimage.debian.org/debian-cd/current/amd64/iso-cd/';
var main = function() {
var pathToManifest = getArgs();
var manifestVersion = getManifestVersion(pathToManifest);
var lastRelease = getlastRelease(currentURL);
console.log("manifest has", manifestVersion);
console.log("cdimage has ", lastRelease.version);
var outdated = isManifestOutdated(lastRelease.version, manifestVersion);
if (outdated) {
console.log("updating ", pathToManifest);
updateManifest(lastRelease, pathToManifest);
}
else {
console.log("no update needed");
}
}
var usage = function() {
console.error('usage: ' + process.argv[1] + ' manifest to update');
process.exit(1);
}
var getArgs = function getArgs(debug) {
debug && console.error(getArgs.name);
var targetFile;
if (process.argv[2]) {
targetFile = process.argv[2];
} else {
usage();
}
debug && console.error(targetFile);
return targetFile;
}
var getManifestVersion = function getManifestVersion(pathToManifest, debug) {
debug && console.error(getManifestVersion.name);
var jsonFile = fs.readFileSync(pathToManifest, 'utf-8');
if (!jsonFile) {
usage();
}
debug && console.error(jsonFile.toString());
try {
var manifest = JSON.parse(jsonFile);
}
catch(e) {
console.error('error parsing ' + pathToManifest);
usage();
}
var version = manifest.variables.box_version;
debug && console.error(version);
return version;
}
var getlastRelease = function getlastRelease(currentURL, debug) {
debug && console.error(getlastRelease.name);
var req = new XMLHttpRequest();
req.open('GET', currentURL + 'SHA256SUMS', false);
req.send(null);
var lines = req.responseText.split('\n');
var netinst;
//isoName: debian string and three digits separated dots
var isoRegex = /(debian-\d+\.\d+\.\d+-amd64-netinst.iso$)/;
for (var i = 0; i < lines.length; i++) {
if (isoRegex.test(lines[i])) {
netinst = lines[i];
break;
}
}
var lastRelease = {};
// sha256 sum: 64 characters at beginning of line, followed by two spaces
lastRelease.sha256sum = netinst.match(/^(\S{64})\s{2}/)[1];
// version: three digits separated by dots and ended by string amd64..
lastRelease.version = netinst.match(/(\d+\.\d+\.\d+)-amd64-netinst.iso$/)[1];
lastRelease.url = currentURL + netinst.match(isoRegex)[1];
if (debug) {
for (prop in lastRelease) {
if (lastRelease.hasOwnProperty(prop)) {
console.error(prop, lastRelease[prop]);
}
}
}
return lastRelease;
}
var isManifestOutdated = function isManifestOutdated(releasedVersion, manifestVersion, debug) {
debug && console.error(isManifestOutdated.name);
var isOutdated = semver.gt(releasedVersion, manifestVersion);
debug && console.error('isOutdated:', isOutdated);
return isOutdated;
}
var updateManifest = function updateManifest(lastRelease, pathToManifest, debug) {
debug && console.error(updateManifest.name);
var manifest = fs.readFileSync(pathToManifest, 'utf-8');
var lines = manifest.split('\n');
lines.forEach(function(elt, i) {
if (/"box_version"/.test(elt)) {
lines[i] = elt.replace(/\d+\.\d+\.\d+/, lastRelease.version);
debug && console.error(lines[i]);
}
else if (/"iso_checksum"/.test(lines[i])) {
lines[i] = elt.replace(/"iso_checksum": ".*",/, '"iso_checksum": "' + lastRelease.sha256sum + '",');
debug && console.error(lines[i]);
}
else if(/"iso_url"/.test(lines[i])) {
lines[i] = elt.replace(/"iso_url": ".*",/, '"iso_url": "' + lastRelease.url + '",');
debug && console.error(lines[i]);
}
});
var fileToWrite ='';
lines.forEach(function(elt, i) {
if (lines[i] !== lines[lines.length -1]) {
fileToWrite += elt + '\n';
}
});
fs.writeFileSync(pathToManifest, fileToWrite);
debug && console.error(fs.readFileSync(pathToManifest, 'utf-8'));
return 'TODO';
}
main();