AbstractVm.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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\ProxmoxApiException;
  21. use \MGProvision\Proxmox\v2\repository\IpSetRepository;
  22. /**
  23. * Description of AbstractVm
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgarden.com>
  26. * @version 1.0.0
  27. */
  28. abstract class AbstractVm extends AbstractObject implements \MGProvision\Proxmox\v2\interfaces\VmInterface
  29. {
  30. protected $vmid;
  31. protected $node;
  32. protected $status;
  33. private $name;
  34. protected $config;
  35. public $ipSet;
  36. function __construct($node, $vmid = null)
  37. {
  38. $this->node = $node;
  39. if ($node !== null && $vmid !== null)
  40. {
  41. $this->vmid = $vmid;
  42. $this->validate();
  43. }
  44. }
  45. /**
  46. * @throws ProxmoxApiException
  47. * @todo remove debug() function
  48. */
  49. public function validate()
  50. {
  51. if (empty($this->vmid)){
  52. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Field 'VMID' is empty");
  53. }
  54. if (!is_numeric($this->vmid))
  55. throw new \MGProvision\Proxmox\v2\ProxmoxApiException(sprintf("Field VMID '%s' is invalid", $this->vmid));
  56. if (empty($this->node))
  57. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Field 'Node' is empty");
  58. }
  59. abstract function getVirtualization();
  60. public function status($force = false)
  61. {
  62. if (!empty($this->status) && $force === false)
  63. return $this->status;
  64. return $this->status = $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/current");
  65. }
  66. public function config($force = false)
  67. {
  68. if (!empty($this->config) && $force === false)
  69. return $this->config;
  70. $this->config = $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/config");
  71. ksort($this->config);
  72. return $this->config;
  73. }
  74. public function updateConfig($config)
  75. {
  76. $res = $this->api()->put("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/config", (array) $config);
  77. $this->config = null;
  78. return $res;
  79. }
  80. public function resume()
  81. {
  82. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/resume");
  83. }
  84. public function shutdown()
  85. {
  86. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/shutdown");
  87. }
  88. public function start()
  89. {
  90. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/start");
  91. }
  92. public function stop()
  93. {
  94. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/status/stop");
  95. }
  96. public function delete()
  97. {
  98. $status = $this->status(true);
  99. if ($status['status'] == "running")
  100. {
  101. $this->stop();
  102. sleep(5);
  103. }
  104. return $this->api()->delete("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}");
  105. }
  106. public function restart()
  107. {
  108. $status = $this->status(true);
  109. if ($status['status'] == "running")
  110. {
  111. $this->stop();
  112. sleep(15);
  113. }
  114. $end = time() + 250;
  115. while (time() < $end)
  116. {
  117. if ($this->isRunning())
  118. {
  119. sleep(5);
  120. continue;
  121. }
  122. return $this->start();
  123. }
  124. return $this->start();
  125. }
  126. public function getVmid()
  127. {
  128. return $this->vmid;
  129. }
  130. public function getNode()
  131. {
  132. return $this->node;
  133. }
  134. public function setNode($node){
  135. $this->node = $node;
  136. return $this;
  137. }
  138. /**
  139. *
  140. * @return \MGProvision\Proxmox\v2\models\Node
  141. */
  142. public function node()
  143. {
  144. return new Node($this->node);
  145. }
  146. public function getName()
  147. {
  148. return $this->name;
  149. }
  150. public function setName($name)
  151. {
  152. $this->name = $name;
  153. }
  154. public function parseResponse(&$response)
  155. {
  156. //set your own lang here
  157. }
  158. public function parseError(&$error, &$httpCode)
  159. {
  160. //parse error here
  161. $search = array("CT {$this->getVmid()}");
  162. $replace = array($this->getName());
  163. $error = str_replace($search, $replace, $error);
  164. }
  165. public function setApiParser()
  166. {
  167. $this->api()->setResponsePaser($this);
  168. }
  169. public function rrd($parameter)
  170. {
  171. return $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/rrd", $parameter);
  172. }
  173. public function rrdData($parameter)
  174. {
  175. return $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/rrddata", $parameter);
  176. }
  177. public function create($container)
  178. {
  179. $result = $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/", $container);
  180. $this->vmid = $container['vmid'];
  181. return $result;
  182. }
  183. public function getPath()
  184. {
  185. return "/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}";
  186. }
  187. public function isRunning()
  188. {
  189. $status = $this->status(true);
  190. return $status['status'] == "running";
  191. }
  192. public function spiceproxy()
  193. {
  194. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/spiceproxy");
  195. }
  196. public function hasSpiceproxy(){
  197. if(!$this->isRunning()){
  198. return false;
  199. }
  200. try{
  201. return $this->spiceproxy();
  202. }catch (ProxmoxApiException $ex){
  203. if(preg_match("/Timeout while waiting for port/", $ex->getMessage())){
  204. return true;
  205. }
  206. else if(!preg_match("/No spice port/", $ex->getMessage())){
  207. return false;
  208. }
  209. return false;
  210. }
  211. }
  212. public function vncproxy($websocket = 0)
  213. {
  214. $setting = array();
  215. if ($websocket)
  216. $setting['websocket'] = 1;
  217. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/vncproxy", $setting);
  218. }
  219. public function deleteConfig($id)
  220. {
  221. $this->config = null;
  222. return $this->updateConfig(array("delete" => $id));
  223. }
  224. public function backup($storage = 'local', $routing = "1", $maxFiles = null, $compress = null, $mode = null)
  225. {
  226. $data = array();
  227. $data['all'] = "0";
  228. $data['vmid'] = $this->vmid;
  229. $data['storage'] = $storage;
  230. $data['remove'] = $routing;
  231. if(is_numeric($maxFiles)){
  232. $data['maxfiles'] = (int) $maxFiles;
  233. }
  234. if ($compress)
  235. $data['compress'] = $compress;
  236. if ($mode)
  237. $data['mode'] = $mode;
  238. return $this->api()->post("/nodes/{$this->node}/vzdump", $data);
  239. }
  240. /**
  241. *
  242. * @return \MGProvision\Proxmox\v2\models\FirewallOptions
  243. */
  244. public function firewallOptions()
  245. {
  246. $obj = new FirewallOptions();
  247. return $obj->setPath("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/firewall/options");
  248. }
  249. public function findFreeDeviceId($busType)
  250. {
  251. $used = array();
  252. $busType = strtolower($busType);
  253. foreach ($this->config(true) as $k => $v)
  254. {
  255. if (strpos($k, $busType) !== false)
  256. {
  257. $used[] = preg_replace("/[a-z]+/", "", $k);
  258. }
  259. }
  260. /**
  261. * ide 0-3
  262. * sata 0-5
  263. * virtio 0-15
  264. * scsi 0-13
  265. */
  266. switch ($busType)
  267. {
  268. case 'ide':
  269. $to = 3;
  270. break;
  271. case 'sata':
  272. $to = 5;
  273. break;
  274. case 'virtio':
  275. $to = 15;
  276. break;
  277. case 'scsi':
  278. $to = 13;
  279. break;
  280. default:
  281. $to = 3;
  282. }
  283. for ($n = 0; $n <= $to; $n++)
  284. {
  285. if (!in_array($n, $used))
  286. {
  287. return $n;
  288. }
  289. }
  290. return null;
  291. }
  292. public function addIpSet(IpSet $ipSet)
  293. {
  294. $ipSet->setPath($this->getPath() . "/firewall/ipset")->create();
  295. }
  296. /**
  297. * Get IpSet
  298. * @return IpSet[]
  299. */
  300. public function getIpSet()
  301. {
  302. if ($this->ipSet)
  303. {
  304. return $this->ipSet;
  305. }
  306. $repostitory = new IpSetRepository();
  307. $repostitory->findByVm($this);
  308. $this->ipSet = $repostitory->fetch();
  309. unset($repostitory);
  310. return $this->ipSet;
  311. }
  312. public function migrate(array $setting)
  313. {
  314. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/migrate", $setting);
  315. }
  316. public function sshkeysName()
  317. {
  318. $sshkeys = rawurldecode($this->config()['sshkeys']);
  319. if (!$sshkeys)
  320. {
  321. return null;
  322. }
  323. $sshkeys = preg_replace('/\s+/', ' ', $sshkeys);
  324. $ex = explode(" ", $sshkeys);
  325. return $ex[2];
  326. }
  327. public function isHaManaged(){
  328. return $this->status()['ha']['managed']==1;
  329. }
  330. }