Skip to content

Commit 5b300d0

Browse files
authored
Merge pull request #40 from lucapiccio/master
Added usefull functions (getallvm,start,stop,...)
2 parents 51a8048 + e51f8ba commit 5b300d0

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

pve2_api.class.php

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,229 @@ public function get_next_vmid () {
320320
}
321321
}
322322

323+
/*
324+
* array get_vms ()
325+
* Get List of all vms
326+
*/
327+
public function get_vms () {
328+
$node_list = $this->get_node_list();
329+
$result=[];
330+
if (count($node_list) > 0) {
331+
foreach ($node_list as $node_name) {
332+
$vms_list = $this->get("nodes/" . $node_name . "/qemu/");
333+
if (count($vms_list) > 0) {
334+
$key_values = array_column($vms_list, 'vmid');
335+
array_multisort($key_values, SORT_ASC, $vms_list);
336+
foreach($vms_list as &$row) {
337+
$row[node] = $node_name;
338+
}
339+
$result = array_merge($result, $vms_list);
340+
}
341+
if (count($result) > 0) {
342+
$this->$cluster_vms_list = $result;
343+
return $this->$cluster_vms_list;
344+
} else {
345+
error_log(" Empty list of vms returned in this cluster.");
346+
return false;
347+
}
348+
}
349+
} else {
350+
error_log(" Empty list of nodes returned in this cluster.");
351+
return false;
352+
}
353+
}
354+
355+
/*
356+
* bool|int start_vm ($node,$vmid)
357+
* Start specific vm
358+
*/
359+
public function start_vm ($node,$vmid) {
360+
if(isset($vmid) && isset($node)){
361+
$parameters = array(
362+
"vmid" => $vmid,
363+
"node" => $node,
364+
);
365+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/status/start";
366+
$post = $this->post($url,$parameters);
367+
if ($post) {
368+
error_log("Started vm " . $vmid . "");
369+
return true;
370+
} else {
371+
error_log("Error starting vm " . $vmid . "");
372+
return false;
373+
}
374+
} else {
375+
error_log("no vm or node valid");
376+
return false;
377+
}
378+
}
379+
380+
/*
381+
* bool|int shutdown_vm ($node,$vmid)
382+
* Gracefully shutdown specific vm
383+
*/
384+
public function shutdown_vm ($node,$vmid) {
385+
if(isset($vmid) && isset($node)){
386+
$parameters = array(
387+
"vmid" => $vmid,
388+
"node" => $node,
389+
"timeout" => 60,
390+
);
391+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/status/shutdown";
392+
$post = $this->post($url,$parameters);
393+
if ($post) {
394+
error_log("Shutdown vm " . $vmid . "");
395+
return true;
396+
} else {
397+
error_log("Error shutting down vm " . $vmid . "");
398+
return false;
399+
}
400+
} else {
401+
error_log("no vm or node valid");
402+
return false;
403+
}
404+
}
405+
406+
/*
407+
* bool|int stop_vm ($node,$vmid)
408+
* Force stop specific vm
409+
*/
410+
public function stop_vm ($node,$vmid) {
411+
if(isset($vmid) && isset($node)){
412+
$parameters = array(
413+
"vmid" => $vmid,
414+
"node" => $node,
415+
"timeout" => 60,
416+
);
417+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/status/stop";
418+
$post = $this->post($url,$parameters);
419+
if ($post) {
420+
error_log("Stopped vm " . $vmid . "");
421+
return true;
422+
} else {
423+
error_log("Error stopping vm " . $vmid . "");
424+
return false;
425+
}
426+
} else {
427+
error_log("no vm or node valid");
428+
return false;
429+
}
430+
}
431+
432+
/*
433+
* bool|int resume_vm ($node,$vmid)
434+
* Resume from suspend specific vm
435+
*/
436+
public function resume_vm ($node,$vmid) {
437+
if(isset($vmid) && isset($node)){
438+
$parameters = array(
439+
"vmid" => $vmid,
440+
"node" => $node,
441+
);
442+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/status/resume";
443+
$post = $this->post($url,$parameters);
444+
if ($post) {
445+
error_log("Resumed vm " . $vmid . "");
446+
return true;
447+
} else {
448+
error_log("Error resuming vm " . $vmid . "");
449+
return false;
450+
}
451+
} else {
452+
error_log("no vm or node valid");
453+
return false;
454+
}
455+
}
456+
457+
/*
458+
* bool|int suspend_vm ($node,$vmid)
459+
* Suspend specific vm
460+
*/
461+
public function suspend_vm ($node,$vmid) {
462+
if(isset($vmid) && isset($node)){
463+
$parameters = array(
464+
"vmid" => $vmid,
465+
"node" => $node,
466+
);
467+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/status/suspend";
468+
$post = $this->post($url,$parameters);
469+
if ($post) {
470+
error_log("Suspended vm " . $vmid . "");
471+
return true;
472+
} else {
473+
error_log("Error suspending vm " . $vmid . "");
474+
return false;
475+
}
476+
} else {
477+
error_log("no vm or node valid");
478+
return false;
479+
}
480+
}
481+
482+
/*
483+
* bool|int clone_vm ($node,$vmid)
484+
* Create fullclone of vm
485+
*/
486+
public function clone_vm ($node,$vmid) {
487+
if(isset($vmid) && isset($node)){
488+
$lastid = $this->get_next_vmid();
489+
$parameters = array(
490+
"vmid" => $vmid,
491+
"node" => $node,
492+
"newid" => $lastid,
493+
"full" => true,
494+
);
495+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/clone";
496+
$post = $this->post($url,$parameters);
497+
if ($post) {
498+
error_log("Cloned vm " . $vmid . " to " . $lastid . "");
499+
return true;
500+
} else {
501+
error_log("Error cloning vm " . $vmid . " to " . $lastid . "");
502+
return false;
503+
}
504+
} else {
505+
error_log("no vm or node valid");
506+
return false;
507+
}
508+
}
509+
510+
/*
511+
* bool|int snapshot_vm ($node,$vmid,$snapname = NULL)
512+
* Create snapshot of vm
513+
*/
514+
public function snapshot_vm ($node,$vmid,$snapname = NULL) {
515+
if(isset($vmid) && isset($node)){
516+
$lastid = $this->get_next_vmid();
517+
if (is_null($snapname)){
518+
$parameters = array(
519+
"vmid" => $vmid,
520+
"node" => $node,
521+
"vmstate" => true,
522+
);
523+
} else {
524+
$parameters = array(
525+
"vmid" => $vmid,
526+
"node" => $node,
527+
"vmstate" => true,
528+
"snapname" => $snapname,
529+
);
530+
}
531+
$url = "/nodes/" . $node . "/qemu/" . $vmid . "/snapshot";
532+
$post = $this->post($url,$parameters);
533+
if ($post) {
534+
error_log("Cloned vm " . $vmid . " to " . $lastid . "");
535+
return true;
536+
} else {
537+
error_log("Error cloning vm " . $vmid . " to " . $lastid . "");
538+
return false;
539+
}
540+
} else {
541+
error_log("no vm or node valid");
542+
return false;
543+
}
544+
}
545+
323546
/*
324547
* bool|string get_version ()
325548
* Return the version and minor revision of Proxmox Server

0 commit comments

Comments
 (0)