AbstractVm.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. if((int) $maxFiles > 0){
  233. $data['maxfiles'] = (int) $maxFiles;
  234. }
  235. }
  236. if ($compress)
  237. $data['compress'] = $compress;
  238. if ($mode)
  239. $data['mode'] = $mode;
  240. return $this->api()->post("/nodes/{$this->node}/vzdump", $data);
  241. }
  242. /**
  243. *
  244. * @return \MGProvision\Proxmox\v2\models\FirewallOptions
  245. */
  246. public function firewallOptions()
  247. {
  248. $obj = new FirewallOptions();
  249. return $obj->setPath("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/firewall/options");
  250. }
  251. public function findFreeDeviceId($busType)
  252. {
  253. $used = array();
  254. $busType = strtolower($busType);
  255. foreach ($this->config(true) as $k => $v)
  256. {
  257. if (strpos($k, $busType) !== false)
  258. {
  259. $used[] = preg_replace("/[a-z]+/", "", $k);
  260. }
  261. }
  262. /**
  263. * ide 0-3
  264. * sata 0-5
  265. * virtio 0-15
  266. * scsi 0-13
  267. */
  268. switch ($busType)
  269. {
  270. case 'ide':
  271. $to = 3;
  272. break;
  273. case 'sata':
  274. $to = 5;
  275. break;
  276. case 'virtio':
  277. $to = 15;
  278. break;
  279. case 'scsi':
  280. $to = 13;
  281. break;
  282. default:
  283. $to = 3;
  284. }
  285. for ($n = 0; $n <= $to; $n++)
  286. {
  287. if (!in_array($n, $used))
  288. {
  289. return $n;
  290. }
  291. }
  292. return null;
  293. }
  294. public function addIpSet(IpSet $ipSet)
  295. {
  296. $ipSet->setPath($this->getPath() . "/firewall/ipset")->create();
  297. }
  298. /**
  299. * Get IpSet
  300. * @return IpSet[]
  301. */
  302. public function getIpSet()
  303. {
  304. if ($this->ipSet)
  305. {
  306. return $this->ipSet;
  307. }
  308. $repostitory = new IpSetRepository();
  309. $repostitory->findByVm($this);
  310. $this->ipSet = $repostitory->fetch();
  311. unset($repostitory);
  312. return $this->ipSet;
  313. }
  314. public function migrate(array $setting)
  315. {
  316. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/migrate", $setting);
  317. }
  318. public function sshkeysName()
  319. {
  320. $sshkeys = rawurldecode($this->config()['sshkeys']);
  321. if (!$sshkeys)
  322. {
  323. return null;
  324. }
  325. $sshkeys = preg_replace('/\s+/', ' ', $sshkeys);
  326. $ex = explode(" ", $sshkeys);
  327. return $ex[2];
  328. }
  329. public function isHaManaged(){
  330. return $this->status()['ha']['managed']==1;
  331. }
  332. }