-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpve-utils.php
232 lines (170 loc) · 5.43 KB
/
pve-utils.php
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
<?php
require_once 'Proxmox.php';
class PmUtils {
private $logintime;
private $storages;
private $proxmox;
private $nodes;
public function __construct($auth) {
$this->login($auth);
$this->nodes = $this->proxmox->request("/nodes");
$storages = $this->proxmox->request("/storage");
$this->storages = array_filter($storages, function($storage) {
return (strpos($storage->content, "images") !== false);
});
}
public function getNodes() {
return $this->nodes;
}
public function getStorages() {
return $this->storages;
}
public static function options($opts = "") {
return getOpt("h:u:p:r:P:n".$opts);
}
public static function getOpt($options) {
if (!isset($options['h']) || !isset($options['u']) || !isset($options['p']))
return false;
if (isset($options['P']) && !ctype_digit($options['P']))
return false;
return [
'hostname' => $options['h'],
'username' => $options['u'],
'password' => $options['p'],
'dryrun' => isset($options['n']) ? true : false,
'realm' => isset($options['r']) ? $options['r'] : "pam",
'port' => isset($options['P']) ? $options['P'] : 8006
];
}
public static function usage() {
return "-h Proxmox hostname (required)
-u Username (required)
-p Password (required)
-r Realm (Default: 'pam')
-P Port (Default: 8006)
-n Dry run";
}
private function login($auth) {
try {
$this->proxmox = new Proxmox($auth['hostname'], $auth['username'], $auth['password'], $auth['realm'], $auth['port']);
} catch (Exception $e) {
//var_dump($e);
echo $e->getMessage()."\n";
exit();
}
}
public function getConfig($vm) {
return $this->proxmox->request("/nodes/{$vm->node}/{$vm->id}/config");
}
public function getStatus($node) {
return $this->proxmox->request("/nodes/$node/status");
}
public function storageExists($name) {
foreach ($this->storages as $storage)
if ($name === $storage->storage) return true;
return false;
}
public function getNode($name) {
foreach ($this->nodes as $node)
if ($node->node === $name) return $node;
return false;
}
public function virtualMachines($running=false) {
$allVm = $this->proxmox->request("/cluster/resources", "GET", ["type" => "vm"]);
$result = array();
foreach ($allVm as $vm) {
if ($vm->type!=="qemu") continue;
if ($running && $vm->status!=="running") continue;
$result[$vm->vmid] = $vm;
}
ksort($result);
return $result;
}
public function nodeVirtalMachines($node, $running=false) {
return array_filter($this->virtualMachines($running), function($vm) use ($node) {
return ($vm->node === $node);
});
}
public function getAllDisks($storage) {
$vms = $this->virtualMachines();
$result = [
"cdroms" => [],
"unused" => [],
"disks" => []
];
foreach ($vms as $vm) {
$config = $this->getConfig($vm);
foreach($config as $key => $val) {
if (!preg_match("/^(efidisk|unused|ide|scsi|virtio|sata)\d{1,2}/", $key)) continue;
if ($storage && strpos($val, $storage) !== 0) continue;
$res = [
"nodename" => $vm->node,
"nodeid" => $vm->id,
"vmname" => $vm->name,
"vmid" => $vm->vmid,
"vm" => $vm,
"disk" => $key,
"content" => $val
];
if (strpos ($val, 'media=cdrom') !== false) {
$result['cdroms'][] = $res;
continue;
}
if (strpos ($key, "unused") === 0) {
$result['unused'][] = $res;
continue;
}
$result['disks'][] = $res;
}
}
return $result;
}
public function activeTasks() {
$active = 0;
$tasks = $this->proxmox->request("/cluster/tasks");
foreach ($tasks as $task)
if (!isset($task->endtime)) $active++;
return $active;
}
public function delayTasks($procs) {
while ($this->activeTasks() >= $procs) {
echo ".";
sleep(3);
// Proxmox times out after 2h - relogin every 1h
if (microtime(true) - $this->proxmox->logintime > 3600)
$this->proxmox->login();
}
}
public function migrateVm($vm, $targetNode) {
$args = [
"target" => $targetNode,
"migration_type" => "insecure",
"online" => true
];
$this->proxmox->request("/nodes/{$vm->node}/{$vm->id}/migrate", "POST", $args);
}
public function moveDisk($disk, $dstorage, $format) {
$args = [
"disk" => $disk['disk'],
"storage" => $dstorage
];
if ($format) $args["format"] = $format;
$this->proxmox->request("/nodes/{$disk['nodename']}/{$disk['nodeid']}/move_disk", "POST", $args);
}
public function deleteDisk($disk) {
$this->proxmox->request("/nodes/{$disk['nodename']}/{$disk['nodeid']}/config", "PUT", ["delete" => $disk['disk']]);
}
public function ejectCdrom($disk) {
$this->proxmox->request("/nodes/{$disk['nodename']}/{$disk['nodeid']}/config", "PUT", [$disk["disk"] => "none,media=cdrom"]);
}
public function setName($vm, $name) {
$this->proxmox->request("/nodes/{$vm->node}/qemu/{$vm->vmid}/config", "PUT", ["name" => $name]);
}
public function disableVm($vm) {
$this->proxmox->request("/nodes/{$vm->node}/qemu/{$vm->vmid}/config", "PUT", ["args" => "MIGRATED-AND-DISABLED"]);
}
public function startVm($vm) {
$this->proxmox->request("/nodes/{$vm->node}/qemu/{$vm->vmid}/status/start", "POST");
}
}
?>