This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
az-firmware-updater.js
130 lines (107 loc) · 3.82 KB
/
az-firmware-updater.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
const url = require('url');
const downloader = require('./lib/downloader');
const HubReporter = require('./lib/hub-reporter');
const rimraf = require('rimraf');
const os = require('os');
const path = require('path');
module.exports = class FirmwareUpdater {
constructor(client, options) {
this.hubReporter = new HubReporter(client);
const defaultOptions = {
downloadOpts: {
location: path.join(os.homedir(), ".azfirmware"),
decompress: {
location: path.join(os.homedir(), ".azfirmware\\uncompressed")
}
},
};
this.options = Object.assign({}, defaultOptions, options);;
}
isConnectionValid(fwPackageUri = "") {
const fwPackageUriObj = url.parse(fwPackageUri);
// Ensure that the url is to a secure url
if (fwPackageUriObj.protocol !== 'https:') {
return false;
}
return true;
}
initiateFirmwareUpdateFlow(fwPackageUri, callback) {
console.log("firmware upgrade started")
let currentLocation;
this.hubReporter.resetFirmwareReport()
.then(_ => this.download(fwPackageUri))
.then(fileLocation => {
currentLocation = fileLocation;
return this.apply(fileLocation);
})
.then(_ => this.restart())
.then(_ => this.cleanup(currentLocation))
.then(_ => {
// complete
callback();
})
.catch(err => {
console.log(err);
this.hubReporter.sendErrorReport(err).then(_ => {
console.log("error reported.")
}).catch(err => {
// should retry?
console.error("Could not send error report.")
});
callback(err);
});
}
restart() {
if (!this.options.restart) {
console.log('restart skipped');
return Promise.resolve();
}
if (typeof this.options.restart !== 'function') {
return Promise.reject('restart option must be function');
}
console.log("restarting...");
const self = this;
return this.hubReporter.sendRestartReport()
.then(_ => self.options.restart());
}
cleanup(fileLocation) {
if (!this.options.cleanup) {
console.log("cleanup skipped. you are responsible for cleaning up firmware files.")
return Promise.resolve();
}
return new Promise(function (fulfill, reject) {
rimraf(fileLocation, function (err) {
if (err) {
return reject(err);
}
console.log("all files removed");
fulfill();
});
});
}
download(fwPackageUri) {
return this.hubReporter.sendDownloadingReport()
.then(_ => downloader(fwPackageUri, this.options.downloadOpts))
.then(fileLocation => {
return this.hubReporter.sendDownloadedReport().then(_ => {
return Promise.resolve(fileLocation);
});
});
}
apply(fileLocation) {
return this.hubReporter.sendApplyingReport()
.then(_ => this.applyImage(fileLocation))
.then(_ => this.hubReporter.sendAppliedReport())
}
applyImage(fileLocation) {
if (!this.options.applyImage || typeof this.options.applyImage !== 'function') {
return Promise.reject('applyImage option must be function');
}
const result = this.options.applyImage(fileLocation);
if (Promise.resolve(result) == result) {
return result;
} else {
return Promise.resolve(result);
}
}
}