-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreate-vm.js
More file actions
82 lines (66 loc) · 2.11 KB
/
create-vm.js
File metadata and controls
82 lines (66 loc) · 2.11 KB
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
const readline = require('readline')
const { ArmApi, ApiClient } = require('@arm-avh/avh-api');
const apiClient = new ApiClient('https://app.corellium.io/api')
const BearerAuth = apiClient.authentications['BearerAuth']
const api = new ArmApi(apiClient)
function prompt (prompt) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(prompt, resolve)
}).finally(() => {
rl.close()
})
}
function sleep (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function waitForState (instance, callback) {
instance = await api.v1GetInstance(instance.id)
while (!callback(instance.state)) {
if (instance.state === 'error') {
throw new Error('Instance is in error state')
}
await sleep(1000);
instance = await api.v1GetInstance(instance.id)
}
return instance
}
async function main() {
const apiToken = process.env.API_TOKEN || await prompt('Please enter AVH API Token: ')
console.log('Logging in...');
const authInfo = await api.v1AuthLogin({ apiToken });
BearerAuth.accessToken = authInfo.token
console.log('Listing projects...');
let projects = await api.v1GetProjects();
let project = projects[0];
console.log('Finding software...');
const softwares = (await api.v1GetModelSoftware('stm32u5-b-u585i-iot02a')).filter(software => software.buildid === 'WB')
if (!softwares.length > 0) {
throw new Error('Unable to find software for STM32');
}
const software = softwares[0];
console.log('Selected software', software);
console.log('Creating VM...')
let instance = await api.v1CreateInstance({
project: project.id,
name: "STM32U5",
flavor: 'stm32u5-b-u585i-iot02a',
os: software.version,
osbuild: software.buildid
})
console.log('Waiting for VM to finish creating...')
instance = await waitForState(instance, state => state !== 'creating')
console.log('VM Created successfully')
}
if (require.main === module) {
main().catch((error) => {
console.error('Error: ', error)
process.exit(1)
})
}
module.exports = main