-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_vms.js
78 lines (70 loc) · 1.91 KB
/
clone_vms.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
const axios = require('axios');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv))
.option('url', {
alias: 'u',
description: 'TrueNAS URL',
type: 'string',
})
.option('username', {
alias: 'n',
description: 'TrueNAS Username',
type: 'string',
})
.option('password', {
alias: 'p',
description: 'TrueNAS Password',
type: 'string',
})
.option('template_vm_id', {
alias: 't',
description: 'Template VM ID',
type: 'string',
})
.option('num_vms', {
alias: 'v',
description: 'Number of VMs to create',
type: 'number',
})
.help()
.alias('help', 'h')
.argv;
const createVms = async (url, username, password, template_vm_id, num_vms) => {
try {
const loginResponse = await axios.post(`${url}/api/v2.0/auth/login`, {
username: username,
password: password,
});
const token = loginResponse.headers['x-auth-token'];
for (let i = 1; i <= num_vms; i++) {
const payload = {
name: `New_Clone_VM_${i}`,
description: 'Cloned from template',
};
const response = await axios.post(
`${url}/api/v2.0/vm/${template_vm_id}/clone/`,
payload,
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
}
);
if (response.status === 200) {
console.log(`VM ${i} cloned successfully!`);
} else {
console.log(`Failed to clone VM ${i}. Response: ${response.statusText}`);
}
}
await axios.post(`${url}/api/v2.0/auth/logout`, null, {
headers: {
'Authorization': `Bearer ${token}`,
},
});
} catch (error) {
console.error(`An error occurred: ${error}`);
}
};
createVms(argv.url, argv.username, argv.password, argv.template_vm_id, argv.num_vms);