-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_machine_status.js
273 lines (248 loc) · 7.25 KB
/
update_machine_status.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const fs = require('fs');
const co = require('co');
const AppPot = require('./apppot-sdk-lite');
const config = require('./config');
const account = require('./account');
const indexFile = process.argv[2];
const macAddr = process.argv[3];
const siteIdFile = process.argv[4];
const ssidFile = process.argv[5];
const pswdFile = process.argv[6];
const gittagFile = process.argv[7];
const machineTypeFile = process.argv[8];
const methodFile = process.argv[9];
/** exit code
* exit 0 => normal
* exit 1 => reboot
* exit 2 => waiting
* exit 3 => halt
* exit 4 => update wlan settings
* exit 5 => update all codes using git
* exit 6 => skip existing files in flashari
* exit 7 => reconnect 3G network
* exit 8 => change sim settings to soracom
* exit 9 => change sim settings to marubeni
* exit 10 => remove & clone repository
* exit 255 => unknown error
**/
function getMachine(ajax){
const query = {
'from': {
'phyName': 'Machine',
'alias': 'Machine'
},
'where': {
'expression': {
'source': '#Machine.macAddress = ?',
'params': [macAddr]
}
}
};
return new Promise((resolve, reject) => {
ajax.post('data/Machine')
.send(query)
.end(AppPot.Ajax.end((obj) => {
resolve(obj.Machine[0]);
}, (err) => {
reject(err);
}));
});
}
function updateMachineStatus(ajax, machine) {
return new Promise((resolve, reject) => {
ajax.post('data/batch/updateData')
.send({
objectName: 'Machine',
data: [{
objectId: machine.objectId,
serverUpdateTime: machine.serverUpdateTime,
status: machine.status,
overrideIndex: machine.overrideIndex,
updateTime: Math.floor(Date.now() / 1000),
pingDate: Date.now(),
currentVersion: machine.currentVersion
}]
})
.end(AppPot.Ajax.end(resolve, reject));
});
}
function getTypeAndMethod(ajax, siteId) {
const searchMachineQuery = {
'from': {
'phyName' :'SiteMethod',
'alias' :'SiteMethod'
},
'join': [{
'entity': 'Method',
'entityAlias': 'Method',
'expression': {
'source': '#SiteMethod.methodId = #Method.objectId'
},
'type': 'LEFT OUTER JOIN'
}],
'where': {
'expression': {
'source': '#SiteMethod.siteId = ?',
'params': [siteId]
}
}
};
return new Promise((resolve, reject)=>{
ajax.post('data/SiteMethod')
.send(searchMachineQuery)
.end(AppPot.Ajax.end((obj)=>{
resolve({
machineType: obj.SiteMethod[0].kuiHitMachineManagerId,
method: obj.Method[0].objectId
});
}, (err)=>{
reject(err);
})
);
});
}
co(function*(){
// AppPot API呼び出し準備
const authInfo = new AppPot.AuthInfo();
const conf = new AppPot.Config(config, macAddr);
const ajax = new AppPot.Ajax(authInfo, conf);
const authenticator = new AppPot.LocalAuthenticator(authInfo, conf, ajax);
const log = (msg, level) => {
return new Promise( (resolve, reject)=>{
ajax.post('logs')
.send({
message: '[tpredge]['+macAddr+'] ' + msg,
logLevel: level || 'MONITOR'
})
.end(AppPot.Ajax.end(resolve, reject));
});
};
yield authenticator.login(account.username, account.password);
const siteId = fs.readFileSync(siteIdFile, {
encoding: 'utf8'
});
const machineType = fs.readFileSync(machineTypeFile, {
encoding: 'utf8'
});
const method = fs.readFileSync(methodFile, {
encoding: 'utf8'
});
const ssid = fs.readFileSync(ssidFile, {
encoding: 'utf8'
});
const pswd = fs.readFileSync(pswdFile, {
encoding: 'utf8'
});
const gittag = fs.readFileSync(gittagFile, {
encoding: 'utf8'
});
yield log(`current version: ${gittag}`);
let machine = yield getMachine(ajax);
machine.currentVersion = gittag;
let doUpdate = false;
let updatedWlan = false;
// ハートビート代わりに、pingDateを更新する
yield updateMachineStatus(ajax, machine);
yield log('ping');
machine = yield getMachine(ajax);
if(machine.status == 'reboot'){
yield log('rebooting...');
machine.status = 'rebooting';
yield updateMachineStatus(ajax, machine);
process.exit(1);
}else if(machine.status == 'waiting' || machine.status == 'halted'){
yield log('waiting...');
if(machine.status == 'halted'){
machine.status = 'waiting';
yield updateMachineStatus(ajax, machine);
}
process.exit(2);
}else if(machine.status == 'halt'){
yield log('halting...');
machine.status = 'halted';
yield updateMachineStatus(ajax, machine);
process.exit(3);
}else if(machine.status == 'sim-to-soracom'){
machine.status = 'halted';
yield updateMachineStatus(ajax, machine);
process.exit(8);
}else if(machine.status == 'sim-to-marubeni'){
machine.status = 'halted';
yield updateMachineStatus(ajax, machine);
process.exit(9);
}else if(machine.status == 'reset-repository'){
machine.status = 'waiting';
yield updateMachineStatus(ajax, machine);
process.exit(10);
}else if(machine.status != 'normal'){
machine.status = 'normal';
doUpdate = true;
}
if(machine.version != gittag){
fs.writeFileSync(gittagFile, machine.version);
console.log('dummy message 6');
yield log('update version to: ' + machine.version);
process.exit(5);
}
if(siteId != machine.siteId){
fs.writeFileSync(siteIdFile, machine.siteId);
yield log('overrided siteId to: ' + machine.siteId);
}
let {
machineType: newMachineType,
method: newMethod
} = yield getTypeAndMethod(ajax, machine.siteId);
if (!newMachineType) {
yield log('machineType not registered');
newMachineType = 'kuiHitMachineManager-0001';
}
if(machineType != newMachineType) {
fs.writeFileSync(machineTypeFile, newMachineType);
yield log('overrided machineType to: ' + newMachineType);
}
if(method != newMethod) {
fs.writeFileSync(methodFile, newMethod);
yield log('overrided method to: ' + newMethod);
}
if(machine.wlanSsid != ssid){
fs.writeFileSync(ssidFile, machine.wlanSsid);
yield log('overrided wlanSsid to: ' + machine.wlanSsid);
updatedWlan = true;
}
if(machine.wlanPassword != pswd){
fs.writeFileSync(pswdFile, machine.wlanPassword);
yield log('overrided wlanPW to: ' + machine.wlanPassword);
updatedWlan = true;
}
if(updatedWlan){
process.exit(4);
}
if(machine.overrideIndex == '0') {
const currentIndex = fs.readFileSync(indexFile, {
encoding: 'utf8'
});
machine.index = currentIndex;
doUpdate = true;
}else if(machine.overrideIndex == '1') {
fs.writeFileSync(indexFile, machine.index);
yield log('overrided index to: ' + machine.index);
machine.overrideIndex = '0';
doUpdate = true;
}else if(machine.overrideIndex == '2') {
fs.writeFileSync(indexFile, -1);
yield log('overrided index and skip existing files');
yield updateMachineStatus(ajax, machine);
process.exit(6);
}
if(doUpdate){
yield updateMachineStatus(ajax, machine);
}
})
.catch(error=>{
console.log(error);
if(error.results.code == 'ENOTFOUND' && error.results.syscall == 'getaddrinfo'){
process.exit(7);
}
console.log('-----finish unknown error');
process.exit(255);
});