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\repository\MountPointRepostiory; /** * Description of Lxc * * @author Pawel Kopec * @version 1.0.0 */ class Lxc extends AbstractVm implements \MGProvision\Proxmox\v2\interfaces\LxcInterface { public function getVirtualization() { return 'lxc'; } public function protectionOff() { return $this->updateConfig(array("protection" => "0")); } public function getCreateTask() { $tasks = $this->api()->get("/cluster/tasks"); foreach ($tasks as $task) { if ($task['saved'] != "0") continue; if ($task['node'] != $this->getNode()) continue; if ($task['id'] != $this->getVmid()) continue; if ($task['type'] != "vzcreate") continue; return $task['upid']; } } public function getReinstallConfig() { $setting = array(); foreach ($this->config(true) as $k => $v) { $setting[$k] = $v; } if (isset($setting['rootfs'])) { $storage = preg_replace('/:(.*)/', '', $setting['rootfs']); $size = preg_replace('/(.*)=/', '', $setting['rootfs']); $unit = preg_replace('/[0-9]/', '', $size); $size = preg_replace('/\D/', '', $size); if ($unit == "T") { $size = $size * 1024; } $setting['rootfs'] = "{$storage}:{$size}"; } unset($setting['digest'], $setting['ostype'], $setting['arch'], $setting['lxc']); return $setting; } public function findFreeNetworDeviceId() { $config = $this->config(true); for($i=0; $i<=50; $i++){ if($config['net'.$i]){ continue; } return $i; } } /** * * @param \MGProvision\Proxmox\v2\models\NetworkDeviceLxc[] $networkDevices */ public function updateNetworkDevices($networkDevices) { $container = array(); foreach ($networkDevices as $networkDevice) { $container[$networkDevice->getId()] = $networkDevice->asConfig(); } return $this->updateConfig($container); } public function getMasterHddSize() { $config = $this->config(true); if ($config['rootfs']) { $hdd = self::asArray($config['rootfs']); return (int) $hdd['size']; } throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Master Disk no found!"); } public function resize($config) { return $this->api()->put("/nodes/{$this->node}/lxc/{$this->vmid}/resize", $config); } /** * * @param type $findBridge * @param type $force * @return NetworkDeviceLxc[] */ public function getNetworkDevices($findBridge = null, $force = true) { $networks = array(); foreach ($this->config($force) as $k => $v) { if (!preg_match('/net/', $k)) continue; $network = new NetworkDeviceLxc($k, $v); if ($findBridge && $network->getBridge() !== $findBridge) continue; $networks[] = $network; } return $networks; } public function findNetworkDevice($ipAddress) { foreach ($this->getNetworkDevices() as $nd) { if ($nd->getIp() == $ipAddress || $nd->getIp6() == $ipAddress) { return $nd; } } return null; } public function deleteNetworkDevice(NetworkDeviceLxc $networkDevice) { $this->deleteConfig($networkDevice->getId()); } public function restore($ostemplate) { try { if ($this->isRunning()) { $this->stop(); sleep(5); } $container = $this->getReinstallConfig(); } catch (\Exception $ex) {//error i.e.: Configuration file 'nodes/proxmox/lxc/117.conf' does not exist if ($ex->getCode() != 500) { throw $ex; } } $data = array( 'ostemplate' => $ostemplate, 'vmid' => $this->vmid, 'restore' => 1, 'force' => 1 ); $container = array_merge($data, (array) $container); return $this->api()->post("/nodes/{$this->node}/lxc", $container); } /** * * @return MountPointRepostiory */ public function getMounPoints() { $repostiory = new MountPointRepostiory; $repostiory->setApi($this->api); $repostiory->findByPath("/nodes/{$this->node}/lxc/{$this->vmid}/config"); return $repostiory; } /** * @return MountPointRepostiory */ public function getHardDiskRepostiory(){ return $this->getMounPoints(); } public function getMasterHardDisk(){ foreach ($this->getMounPoints()->fetch() as $mounPoint){ if($mounPoint->isMaster()){ return $mounPoint; } } throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Master Hard Disk not found"); } }