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. $status = $this->status(true);
  108. if ($status['status'] == "running")
  109. {
  110. $this->stop();
  111. sleep(15);
  112. }
  113. $end = time() + 250;
  114. while (time() < $end)
  115. {
  116. if ($this->isRunning())
  117. {
  118. sleep(5);
  119. continue;
  120. }
  121. return $this->start();
  122. }
  123. return $this->start();
  124. }
  125. public function getVmid() {
  126. return $this->vmid;
  127. }
  128. public function getNode()
  129. {
  130. return $this->node;
  131. }
  132. public function setNode($node){
  133. $this->node = $node;
  134. return $this;
  135. }
  136. /**
  137. *
  138. * @return \MGProvision\Proxmox\v2\models\Node
  139. */
  140. public function node()
  141. {
  142. return new Node($this->node);
  143. }
  144. public function getName()
  145. {
  146. return $this->name;
  147. }
  148. public function setName($name)
  149. {
  150. $this->name = $name;
  151. }
  152. public function parseResponse(&$response)
  153. {
  154. //set your own lang here
  155. }
  156. public function parseError(&$error, &$httpCode)
  157. {
  158. //parse error here
  159. $search = array("CT {$this->getVmid()}");
  160. $replace = array($this->getName());
  161. $error = str_replace($search, $replace, $error);
  162. }
  163. public function setApiParser()
  164. {
  165. $this->api()->setResponsePaser($this);
  166. }
  167. public function rrd($parameter)
  168. {
  169. return $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/rrd", $parameter);
  170. }
  171. public function rrdData($parameter)
  172. {
  173. return $this->api()->get("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/rrddata", $parameter);
  174. }
  175. public function create($container)
  176. {
  177. $result = $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/", $container);
  178. $this->vmid = $container['vmid'];
  179. return $result;
  180. }
  181. public function getPath()
  182. {
  183. return "/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}";
  184. }
  185. public function isRunning()
  186. {
  187. $status = $this->status(true);
  188. return $status['status'] == "running";
  189. }
  190. public function spiceproxy()
  191. {
  192. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/spiceproxy");
  193. }
  194. public function hasSpiceproxy(){
  195. if(!$this->isRunning()){
  196. return false;
  197. }
  198. try{
  199. return $this->spiceproxy();
  200. }catch (ProxmoxApiException $ex){
  201. if(preg_match("/Timeout while waiting for port/", $ex->getMessage())){
  202. return true;
  203. }
  204. else if(!preg_match("/No spice port/", $ex->getMessage())){
  205. return false;
  206. }
  207. return false;
  208. }
  209. }
  210. public function vncproxy($websocket = 0)
  211. {
  212. $setting = array();
  213. if ($websocket)
  214. $setting['websocket'] = 1;
  215. return $this->api()->post("/nodes/{$this->node}/{$this->getVirtualization()}/{$this->vmid}/vncproxy", $setting);
  216. }
  217. public function deleteConfig($id)
  218. {
  219. $this->config = null;
  220. return $this->updateConfig(array("delete" => $id));
  221. }
  222. public function backup($storage = 'local', $routing = "1", $maxFiles = null, $compress = null, $mode = null)
  223. {
  224. $data = array();
  225. $data['all'] = "0";
  226. $data['vmid'] = $this->vmid;
  227. $data['storage'] = $storage;
  228. $data['remove'] = $routing;
  229. if(is_numeric($maxFiles)){
  230. if((int) $maxFiles > 0){
  231. $data['maxfiles'] = (int) $maxFiles;
  232. }
  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. }