-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnames.js
75 lines (64 loc) · 1.82 KB
/
names.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
let balena = require('balena-sdk')();
let request = require('request-promise');
let log = console.log;
module.exports = { update };
async function update(hostnames) {
let ready = await login();
let token = await balena.auth.getToken();
if (ready) {
for (uuid in hostnames) {
// update hostname
const name = hostnames[uuid];
log(`Updating hostname for ${uuid} ...`);
const oldname = await getHostname(uuid);
log(`Current hostname ${oldname}`);
const hostresult = await setHostname(uuid, name);
log(hostresult);
log(`New hostname: ${name}`);
// update balena name
const getnameresult = await balena.models.device.getName(uuid);
log(`Updating balena name for ${uuid} ...`);
const setnameresult = await balena.models.device.rename(uuid, name);
log(setnameresult);
}
} else {
log('Sorry you need to log in ...');
}
async function login() {
return balena.auth.isLoggedIn();
}
async function getHostname(uuid) {
const response = await request({
method: 'POST',
url: 'https://api.balena-cloud.com/supervisor/v1/device/host-config',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: { uuid: uuid, method: 'GET' },
json: true
});
return response.network.hostname;
}
async function setHostname(uuid, hostname) {
const response = await request({
method: 'POST',
url: 'https://api.balena-cloud.com/supervisor/v1/device/host-config',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: {
uuid: uuid,
method: 'PATCH',
data: {
network: {
hostname: hostname
}
}
},
json: true
});
return response;
}
}