Lxc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-05)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\models;
  20. use \MGProvision\Proxmox\v2\repository\MountPointRepostiory;
  21. /**
  22. * Description of Lxc
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class Lxc extends AbstractVm implements \MGProvision\Proxmox\v2\interfaces\LxcInterface
  28. {
  29. public function getVirtualization()
  30. {
  31. return 'lxc';
  32. }
  33. public function protectionOff()
  34. {
  35. return $this->updateConfig(array("protection" => "0"));
  36. }
  37. public function getCreateTask()
  38. {
  39. $tasks = $this->api()->get("/cluster/tasks");
  40. foreach ($tasks as $task)
  41. {
  42. if ($task['saved'] != "0")
  43. continue;
  44. if ($task['node'] != $this->getNode())
  45. continue;
  46. if ($task['id'] != $this->getVmid())
  47. continue;
  48. if ($task['type'] != "vzcreate")
  49. continue;
  50. return $task['upid'];
  51. }
  52. }
  53. public function getReinstallConfig()
  54. {
  55. $setting = array();
  56. foreach ($this->config(true) as $k => $v)
  57. {
  58. $setting[$k] = $v;
  59. }
  60. if (isset($setting['rootfs']))
  61. {
  62. $storage = preg_replace('/:(.*)/', '', $setting['rootfs']);
  63. $size = preg_replace('/(.*)=/', '', $setting['rootfs']);
  64. $unit = preg_replace('/[0-9]/', '', $size);
  65. $size = preg_replace('/\D/', '', $size);
  66. if ($unit == "T")
  67. {
  68. $size = $size * 1024;
  69. }
  70. $setting['rootfs'] = "{$storage}:{$size}";
  71. }
  72. unset($setting['digest'], $setting['ostype'], $setting['arch'], $setting['lxc']);
  73. return $setting;
  74. }
  75. public function findFreeNetworDeviceId()
  76. {
  77. $config = $this->config(true);
  78. for($i=0; $i<=50; $i++){
  79. if($config['net'.$i]){
  80. continue;
  81. }
  82. return $i;
  83. }
  84. }
  85. /**
  86. *
  87. * @param \MGProvision\Proxmox\v2\models\NetworkDeviceLxc[] $networkDevices
  88. */
  89. public function updateNetworkDevices($networkDevices)
  90. {
  91. $container = array();
  92. foreach ($networkDevices as $networkDevice)
  93. {
  94. $container[$networkDevice->getId()] = $networkDevice->asConfig();
  95. }
  96. return $this->updateConfig($container);
  97. }
  98. public function getMasterHddSize()
  99. {
  100. $config = $this->config(true);
  101. if ($config['rootfs'])
  102. {
  103. $hdd = self::asArray($config['rootfs']);
  104. return (int) $hdd['size'];
  105. }
  106. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Master Disk no found!");
  107. }
  108. public function resize($config)
  109. {
  110. return $this->api()->put("/nodes/{$this->node}/lxc/{$this->vmid}/resize", $config);
  111. }
  112. /**
  113. *
  114. * @param type $findBridge
  115. * @param type $force
  116. * @return NetworkDeviceLxc[]
  117. */
  118. public function getNetworkDevices($findBridge = null, $force = true)
  119. {
  120. $networks = array();
  121. foreach ($this->config($force) as $k => $v)
  122. {
  123. if (!preg_match('/net/', $k))
  124. continue;
  125. $network = new NetworkDeviceLxc($k, $v);
  126. if ($findBridge && $network->getBridge() !== $findBridge)
  127. continue;
  128. $networks[] = $network;
  129. }
  130. return $networks;
  131. }
  132. public function findNetworkDevice($ipAddress)
  133. {
  134. foreach ($this->getNetworkDevices() as $nd)
  135. {
  136. if ($nd->getIp() == $ipAddress || $nd->getIp6() == $ipAddress)
  137. {
  138. return $nd;
  139. }
  140. }
  141. return null;
  142. }
  143. public function deleteNetworkDevice(NetworkDeviceLxc $networkDevice)
  144. {
  145. $this->deleteConfig($networkDevice->getId());
  146. }
  147. public function restore($ostemplate)
  148. {
  149. try
  150. {
  151. if ($this->isRunning())
  152. {
  153. $this->stop();
  154. sleep(5);
  155. }
  156. $container = $this->getReinstallConfig();
  157. }
  158. catch (\Exception $ex)
  159. {//error i.e.: Configuration file 'nodes/proxmox/lxc/117.conf' does not exist
  160. if ($ex->getCode() != 500)
  161. {
  162. throw $ex;
  163. }
  164. }
  165. $data = array(
  166. 'ostemplate' => $ostemplate,
  167. 'vmid' => $this->vmid,
  168. 'restore' => 1,
  169. 'force' => 1
  170. );
  171. $container = array_merge($data, (array) $container);
  172. return $this->api()->post("/nodes/{$this->node}/lxc", $container);
  173. }
  174. /**
  175. *
  176. * @return MountPointRepostiory
  177. */
  178. public function getMounPoints()
  179. {
  180. $repostiory = new MountPointRepostiory;
  181. $repostiory->setApi($this->api);
  182. $repostiory->findByPath("/nodes/{$this->node}/lxc/{$this->vmid}/config");
  183. return $repostiory;
  184. }
  185. /**
  186. * @return MountPointRepostiory
  187. */
  188. public function getHardDiskRepostiory(){
  189. return $this->getMounPoints();
  190. }
  191. }