| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2016-10-05)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace MGProvision\Proxmox\v2\models;
- use MGProvision\Proxmox\v2\models\HardDisk;
- use MGProvision\Proxmox\v2\repository\CdRomRepository;
- use MGProvision\Proxmox\v2\repository\HardDiskRepostiory;
- use \MGProvision\Proxmox\v2\repository\IpConfigRepository;
- /**
- * Description of Kvm
- *
- * @author Pawel Kopec <pawelk@modulesgarden.com>
- * @version 1.0.0
- */
- class Kvm extends AbstractVm implements \MGProvision\Proxmox\v2\interfaces\KvmInterface
- {
- public function getVirtualization()
- {
- return 'qemu';
- }
- public function suspend()
- {
- return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/suspend");
- }
- public function cdrom()
- {
- foreach ($this->config() as $k => $c)
- {
- if (strpos($c, "media=cdrom") !== false && !preg_match('/cloudinit/', $c))
- {
- $c = explode(",", $c);
- $isoRaw = $c[0];
- $c = explode(":iso/", $isoRaw);
- $iso = end($c);
- $iso = ucwords(str_replace(array("-", "_", ".iso"), array(" ", " ", ""), $iso));
- return array(
- "iso" => $iso,
- "isoRaw" => $isoRaw,
- "bus" => $k
- );
- }
- }
- }
- public function getBootOrder()
- {
- $config = $this->config();
- //order=scsi0;ide1;scsi2
- if($config['boot'] && preg_match("/order/",$config['boot'])){
- list($key, $order) = explode("=", $config['boot']);
- return explode(";",$order);
- }
- $boot = array("c", "d", "n");
- if (isset($config['boot']))
- {
- $boot = str_split($config['boot']);
- }
- return $boot;
- }
- public function changeBootOrder($boot)
- {
- $data = array();
- $data['boot'] = $boot;
- $this->config = null;
- return $this->api()->put("/nodes/{$this->node}/qemu/{$this->vmid}/config", $data);
- }
- public function changeIso($iso)
- {
- $cdrom = $this->cdrom();
- if (!$cdrom['bus'])
- throw new \MGProvision\Proxmox\v2\ProxmoxApiException("CD-ROM volume not found");
- $data = array();
- $data[$cdrom['bus']] = "{$iso},media=cdrom";
- $this->config = null;
- return $this->api()->put("/nodes/{$this->node}/qemu/{$this->vmid}/config", $data);
- }
- public function cloneVm($container)
- {
- return $this->api()->post("/nodes/{$this->node}/qemu/{$this->vmid}/clone", $container);
- }
- public function getIso()
- {
- $result = array();
- foreach ($this->config() as $k => $c)
- {
- if (strpos($c, "media=cdrom") !== false && !preg_match('/cloudinit/', $c ))
- {
- $c = explode(",", $c);
- $isoRaw = $c[0];
- $c = explode(":iso/", $isoRaw);
- $iso = end($c);
- $iso = ucwords(str_replace(array("-", "_", ".iso"), array(" ", " ", ""), $iso));
- return array(
- "iso" => $iso,
- "isoRaw" => $isoRaw,
- "bus" => $k
- );
- }
- }
- }
- public function updateCdrom($iso)
- {
- $cdrom = $this->cdrom();
- if (!$cdrom['bus'])
- throw new \MGProvision\Proxmox\v2\ProxmoxApiException("CD-ROM volume not found");
- $data = array();
- $data[$cdrom['bus']] = "{$iso},media=cdrom";
- $this->config = null;
- return $this->api()->put("/nodes/{$this->node}/qemu/{$this->vmid}/config", $data);
- }
- public function findFreeNetworDeviceId()
- {
- $config = $this->config(true);
- for($i=0; $i<=50; $i++){
- if($config['net'.$i]){
- continue;
- }
- return $i;
- }
- }
- public function findFreeIde($type)
- {
- $i = 0;
- $type = strtolower($type);
- foreach ($this->config(true) as $k => $v)
- {
- if (preg_match("/{$type}/", $k))
- $i++;
- }
- return $i;
- }
- /**
- * @deprecated since version 2.4.0
- * @return array
- */
- public function getHdds()
- {
- $disks = array();
- $isPrimary = false;
- $config = $this->config();
- foreach ($config as $key => $val)
- {
- if (strpos($val, 'disk') !== false)
- {
- if (in_array($key, ['hotplug']))
- {
- continue;
- }
- $sets = explode(",", $val);
- $disks[$key] = array("bus" => $key, "location" => current($sets), "format" => "", "backup" => "", "size" => "", "isPrimary" => false);
- foreach ($sets as $set)
- {
- list($k, $v) = explode("=", $set);
- $disks[$key][$k] = $v;
- if ($k == "size")
- {
- if (preg_match('/M/', $v))
- {//MB => GB
- $disks[$key]['sizeRaw'] = (int) $v / 1024;
- }
- else if (preg_match('/T/', $v))
- {//T => GB
- $disks[$key]['sizeRaw'] = (int) $v * 1024;
- }
- else
- {//GB
- $disks[$key]['sizeRaw'] = (int) $v;
- }
- }
- }
- if (empty($disks[$key]['format']) && preg_match('/disk-[0-9]\./', $sets['0']))
- {
- $disks[$key]['format'] = preg_replace('/(.*)\./', '', $sets['0']);
- }
- if (isset($config['bootdisk']) && $key == $config['bootdisk'])
- {
- $disks[$key] ['isPrimary'] = true;
- $isPrimary = true;
- }
- else if (!isset($config['bootdisk']) && !$isPrimary && strpos($disks[$key] ['location'], 'disk-0') !== false)
- {
- $disks[$key] ['isPrimary'] = true;
- $isPrimary = true;
- }
- $matches = array();
- preg_match('/disk-[0-9]/', $disks[$key] ['location'], $matches);
- if ($matches[0])
- $disks[$key] ['name'] = $matches[0];
- }
- }
- ksort($disks);
- return $disks;
- }
- /**
- * @return HardDisk[]
- */
- public function getHardDisks()
- {
- $disks = array();
- $config = $this->config(true);
- $boot = (array) $this->getBootOrder();
- $bootDisk = reset($boot );
- foreach ($config as $id => $value)
- {
- if (in_array($id, ['hotplug','agent']))
- {
- continue;
- }
- if(preg_match("/unused/", $id)){
- continue;
- }
- if (!preg_match('/disk/', $value))
- {
- continue;
- }
- $hdd = (new HardDisk($id, $value))->setPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- if (isset($config['bootdisk']))
- {
- $hdd->setMaster($config['bootdisk'] == $id);
- }elseif ($bootDisk ){
- $hdd->setMaster($bootDisk == $id);
- }
- $disks[] = $hdd;
- }
- return $disks;
- }
- /**
- * @return HardDisk[]
- */
- public function getUnusedHardDisks()
- {
- $disks = array();
- $config = $this->config(true);
- foreach ($config as $id => $value)
- {
- if (in_array($id, ['hotplug','agent']))
- {
- continue;
- }
- if(!preg_match("/unused/", $id)){
- continue;
- }
- if (!preg_match('/disk/', $value))
- {
- continue;
- }
- $hdd = (new HardDisk($id, $value))->setPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- $disks[] = $hdd;
- }
- return $disks;
- }
- /**
- *
- * @return HardDisk
- * @throws \MGProvision\Proxmox
- */
- public function getMasterHardDisk()
- {
- $hdds = [];
- $configs = $this->config();
- $boot = (array) $this->getBootOrder();
- $bootDisk = reset($boot );
- foreach ($configs as $id => $config)
- {
- if (!HardDisk::isConfigValid($id, $config))
- {
- continue;
- }
- $hdd = new HardDisk($id, $config);
- $hdd->setPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- if (isset($configs['bootdisk']))
- {
- $hdd->setMaster($configs['bootdisk'] == $id);
- }elseif ($bootDisk ){
- $hdd->setMaster($bootDisk == $id);
- }
- if ($hdd->isMaster())
- {
- return $hdd;
- }
- $hdds[] = $hdd;
- }
- foreach ($hdds as $hdd)
- {
- if(preg_match('/unused/', $hdd->getId())){
- continue;
- }
- return $hdd;
- }
- throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Master Hard Disk not found");
- }
- public function resizeHdd(array $setting)
- {
- return $this->api()->put("/nodes/{$this->node}/qemu/{$this->vmid}/resize", $setting);
- }
- public function getMasterHddSize()
- {
- $hdds = $this->getHdds();
- foreach ($hdds as $hdd)
- {
- if ($hdd['isPrimary'])
- {
- return $hdd['sizeRaw'];
- }
- }
- throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Master Disk no found!");
- }
- /**
- * @param $id
- * @return \MGProvision\Proxmox\v2\models\HardDisk
- * @throws \Exception
- */
- public function findHardDiskById($id)
- {
- foreach($this->getHardDisks() as $disk)
- {
- if($disk->getId() == $id)
- {
- return $disk;
- }
- }
- throw new \Exception('Cannot find disk');
- }
- public function getSlaveHddsSize($skip = null)
- {
- $size = 0;
- foreach ($this->getHdds() as $hdd)
- {
- if ($hdd['isPrimary'] || $skip == $hdd['bus'])
- continue;
- $size += $hdd['sizeRaw'];
- }
- return $size;
- }
- /**
- *
- * @param type $findBridge
- * @param type $force
- * @return \MGProvision\Proxmox\v2\models\NetworkDeviceKvm[]
- */
- public function getNetworkDevices($findBridge = null, $force = true)
- {
- $networks = array();
- foreach ($this->config($force) as $k => $v)
- {
- if (!preg_match('/net/', $k))
- continue;
- $network = new NetworkDeviceKvm($k, $v);
- if ($findBridge && $network->getBridge() !== $findBridge)
- continue;
- $networks[] = $network;
- }
- return $networks;
- }
- public function getMacAddresses($findBridge = null, $force = false)
- {
- $macs = array();
- foreach ($this->getNetworkDevices($findBridge, $force) as $network)
- {
- $macs[] = $network->getMacAddress();
- }
- return $macs;
- }
- /**
- *
- * @param \MGProvision\Proxmox\v2\models\NetworkDeviceKvm[] $networkDevices
- */
- public function updateNetworkDevices($networkDevices)
- {
- $container = array();
- foreach ($networkDevices as $networkDevice)
- {
- $container[$networkDevice->getId()] = $networkDevice->asConfig();
- }
- return $this->updateConfig($container);
- }
- public function getReinstallConfig()
- {
- $setting = array();
- foreach ($this->config(true) as $k => $v)
- {
- $setting[$k] = $v;
- }
- unset($setting['digest']);
- return $setting;
- }
- public function restore($archive)
- {
- try
- {
- if ($this->isRunning())
- {
- $this->stop();
- sleep(5);
- }
- }
- catch (\Exception $ex)
- {//error i.e.: Configuration file 'nodes/proxmox/lxc/117.conf' does not exist
- if ($ex->getCode() != 500)
- {
- throw $ex;
- }
- }
- $container = array(
- 'vmid' => $this->vmid,
- 'archive' => $archive,
- 'force' => 1,
- );
- return $this->api()->post("/nodes/{$this->node}/qemu", $container);
- }
- /**
- *
- * @return \MGProvision\Proxmox\v2\models\SnapshotKvm
- */
- public function snapshot($name = null)
- {
- $snap = new SnapshotKvm();
- $path = "/nodes/{$this->getNode()}/{$this->getVirtualization()}/{$this->getVmid()}/snapshot";
- if ($name !== null)
- {
- $path .= "/{$name}";
- }
- $snap->setPath($path);
- return $snap;
- }
- public function convertToTemplate()
- {
- return $this->api()->post("/nodes/{$this->node}/qemu/{$this->vmid}/template");
- }
- public function findNetworkDevice($macAddress)
- {
- foreach ($this->getNetworkDevices() as $nd)
- {
- if ($nd->getMacAddress() == $macAddress)
- {
- return $nd;
- }
- }
- return null;
- }
- public function deleteNetworkDevice(NetworkDeviceKvm $networkDevice)
- {
- $this->deleteConfig($networkDevice->getId());
- }
- /**
- *
- * @param HardDisk $hardDisks
- * @return array
- */
- public function updateHardDisks($hardDisks)
- {
- $container = array();
- foreach ($hardDisks as $hardDisk)
- {
- $container[$hardDisk->getId()] = $hardDisk->asConfig();
- }
- return $this->updateConfig($container);
- }
- /**
- *
- * @return IpConfigRepository
- */
- public function getIpConfig()
- {
- $repostiory = new IpConfigRepository;
- $repostiory->findByPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- return $repostiory;
- }
- public function getCdRom()
- {
- $repostiory = new CdRomRepository();
- $repostiory->findByPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- return $repostiory;
- }
- /**
- * @return Agent
- */
- public function agent()
- {
- return (new Agent())->setPath("/nodes/{$this->node}/qemu/{$this->vmid}/agent");
- }
- public function getCdRomRepository(){
- $repostiory = new CdRomRepository();
- $repostiory->findByPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- return $repostiory;
- }
- public function hasCloudInit(){
- $repostiory = new CdRomRepository();
- $repostiory->findByPath("/nodes/{$this->node}/qemu/{$this->vmid}/config");
- return $repostiory->hasCloudInit();
- }
- /**
- * @return HardDiskRepostiory
- * @throws \MGProvision\Proxmox\v2\ProxmoxApiException
- */
- public function getHardDiskRepostiory(){
- $repository = new HardDiskRepostiory();
- $repository->setApi($this->api());
- $repository->findByPath($this->getPath() . '/config');
- return $repository;
- }
- }
|