CreateQemuJob.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Cloud;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use MGProvision\Proxmox\v2\Api;
  5. use MGProvision\Proxmox\v2\models\HardDisk;
  6. use MGProvision\Proxmox\v2\models\Node;
  7. use MGProvision\Proxmox\v2\VmFactory;
  8. use ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmCreatedEvent;
  9. use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
  10. use ModulesGarden\ProxmoxAddon\App\Models\NodeSetting;
  11. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AdditionalDiskService;
  12. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ContainerService;
  13. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
  14. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  15. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
  16. use ModulesGarden\ProxmoxAddon\App\Services\EmailService;
  17. use ModulesGarden\ProxmoxAddon\App\Traits\Cloud\VmNetwork;
  18. use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
  19. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  20. class CreateQemuJob extends BaseJob
  21. {
  22. use ProductService;
  23. use UserService;
  24. use VmNetwork;
  25. /**
  26. * @var NetworkService
  27. */
  28. protected $networkService;
  29. protected function initServices()
  30. {
  31. $this->emailService = new EmailService();
  32. $this->containerService = new ContainerService();
  33. $this->networkService = new NetworkService();
  34. $this->additionalDiskService = new AdditionalDiskService();
  35. }
  36. public function handle()
  37. {
  38. $this->initParams();
  39. $this->initServices();
  40. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  41. //create task validation
  42. if ($this->isDone())
  43. {
  44. $this->initVm();
  45. if($this->getModelData()['additionalDiskSize1'] && !$this->additionalDiskService->hasDisk()){
  46. $this->additionalDiskService->create($this->getModelData());
  47. }
  48. fire(new VmCreatedEvent($this->getVmModel()));
  49. return true;
  50. }
  51. elseif ($this->isTaskRunning())
  52. {
  53. //sleep
  54. $this->sleep(5);
  55. return false;
  56. }
  57. try
  58. {
  59. Api::beginTransaction();
  60. DB::beginTransaction();
  61. //vmid
  62. $vmid = $this->nextVmid();
  63. $vmModel = $this->getVmModel();
  64. $vmModel->vmid = $vmid;
  65. $vmModel->save();
  66. sl('Vm')->setVm(null);
  67. sl('Vm')->setVmModel( $vmModel);
  68. $container = [
  69. 'vmid' => $vmid,
  70. 'name' => $this->getVmModel()->name,
  71. "ostype" => $this->getModelData()['ostype'] ? $this->getModelData()['ostype'] : $this->configuration()->getOsType(),
  72. ];
  73. //vcpus
  74. if ($this->getVmModel()->vcpus)
  75. {
  76. $container['vcpus'] = $this->getVmModel()->vcpus ;
  77. }
  78. //cpulimit
  79. if ($this->getVmModel()->cpulimit)
  80. {
  81. $container['cpulimit'] = $this->getVmModel()->cpulimit;
  82. }
  83. // Boot order device
  84. if ($this->configuration()->getBootOrder())
  85. {
  86. $container['boot'] = $this->configuration()->getBootOrder();
  87. }
  88. //numa
  89. if ($this->configuration()->isNuma())
  90. {
  91. $container['numa'] = 1;
  92. }
  93. //Memory
  94. if ($this->getVmModel()->memory)
  95. {
  96. $container['memory'] = $this->getVmModel()->memory;
  97. }
  98. //cpuunits
  99. if ($this->getVmModel()->cpuunits)
  100. {
  101. $container['cpuunits'] = $this->getVmModel()->cpuunits;
  102. }
  103. //Name servers
  104. $ns = [];
  105. for ($i = 0; $i <= 1; $i++)
  106. {
  107. $n = trim($this->getModelData()['nameserver'][$i]);
  108. if (!empty($n) && !filter_var($n, FILTER_VALIDATE_IP))
  109. {
  110. $n = gethostbyname($n);
  111. }
  112. if (!empty($n) && filter_var($n, FILTER_VALIDATE_IP))
  113. {
  114. $ns[] = $n;
  115. }
  116. }
  117. if ($ns)
  118. {
  119. $container['nameserver'] = implode(" ", $ns);
  120. }
  121. //sockets
  122. if ($this->getVmModel()->sockets)
  123. {
  124. $container['sockets'] = $this->getVmModel()->sockets;
  125. }
  126. //cores
  127. if ($this->getVmModel()->cores)
  128. {
  129. $container['cores'] = $this->getVmModel()->cores;
  130. }
  131. //description
  132. if ($this->getModelData()['description'])
  133. {
  134. $container['description'] = $this->getModelData()['description'];
  135. }
  136. //onboot
  137. if ($this->configuration()->isOnboot())
  138. {
  139. $container['onboot'] = 1;
  140. }
  141. //pool
  142. if ($this->configuration()->getPool())
  143. {
  144. $container['pool'] = $this->configuration()->getPool();
  145. }
  146. //startup
  147. if ($this->configuration()->getStartup())
  148. {
  149. $container['startup'] = $this->configuration()->getStartup();
  150. }
  151. //busDevces
  152. $busDevices = [
  153. "ide" => 0,
  154. "sata" => 0,
  155. "virtio" => 0,
  156. "scsi" => 0
  157. ];
  158. if (!$this->configuration()->getCdromType())
  159. {
  160. throw new \InvalidArgumentException("CD-ROM Type is empty, product configuration is not valid");
  161. }
  162. //CD-ROM primary ISO
  163. $type = $this->configuration()->getCdromType();
  164. $bus = $busDevices[$type];
  165. $busDevices[$type]++;
  166. $isoImage = $this->getModelData()['isoImage'] ? $this->getModelData()['isoImage'] : "none";
  167. $container[$type . $bus] = "{$isoImage},media=cdrom";
  168. //secondaryIsoImage
  169. if($this->getModelData()['secondaryIsoImage']){
  170. $bus = $busDevices[$type];
  171. $busDevices[$type]++;
  172. $container[$type . $bus] = "{$this->getModelData()['secondaryIsoImage']},media=cdrom";
  173. }
  174. //Disk
  175. $diskSize = $this->getVmModel()->disk;
  176. $type = $this->configuration()->getDiskType();
  177. $bus = $busDevices[$type];
  178. $busDevices[$type]++;
  179. $harDisk = new HardDisk($type . $bus);
  180. $dafaultStorage = NodeSetting::ofServer($this->getWhmcsParamByKey('serverid'))
  181. ->ofNode($this->getNode()->getNode())
  182. ->ofSetting('defaultStorage')
  183. ->value("value");
  184. $harDisk->setSize($diskSize)
  185. ->setStorage($dafaultStorage ? $dafaultStorage : $this->configuration()->getDiskStorage())
  186. ->setCache($this->configuration()->getDiskCache())
  187. ->setFormat($this->configuration()->getDiskFormat())
  188. ->setMbps_rd($this->configuration()->getMbpsRd())
  189. ->setMbps_wr($this->configuration()->getMbpsWr())
  190. ->setDiscard($this->configuration()->isDiscard() ? "on" : null)
  191. ->setIops_rd($this->configuration()->getIopsRd())
  192. ->setIops_rd_max($this->configuration()->getIopsRdMax())
  193. ->setIops_wr($this->configuration()->getIopsWr())
  194. ->setIops_wr_max($this->configuration()->getIopsWrMax())
  195. ->setReplicate($this->configuration()->isReplicate() ? 0 : null)
  196. ->setSsd($this->configuration()->isSsd() ? 1 : null);
  197. if ($this->configuration()->isIoThread() && in_array($type, ['virtio', 'scsi']))
  198. {
  199. $harDisk->setIothread($this->configuration()->isIoThread());
  200. }
  201. $container[$harDisk->getId()] = $harDisk->asConfig();
  202. //Network
  203. $container += $this->createNetwork();
  204. //acpi
  205. if ($this->configuration()->isAcpi())
  206. {
  207. $container['acpi'] = 1;
  208. }
  209. //agent
  210. if ($this->configuration()->isAgent())
  211. {
  212. $container['agent'] = 1;
  213. }
  214. //autostart
  215. if ($this->configuration()->isAutostart())
  216. {
  217. $container['autostart'] = 1;
  218. }
  219. //balloon
  220. if ($this->configuration()->getBalloon())
  221. {
  222. $container['balloon'] = $this->configuration()->getBalloon();
  223. }
  224. //shares
  225. if ($this->configuration()->getShares())
  226. {
  227. $container['shares'] = $this->configuration()->getShares();
  228. }
  229. //cdrom
  230. if ($this->configuration()->getCdrom())
  231. {
  232. $container['cdrom'] = $this->configuration()->getCdrom();
  233. }
  234. //cpu
  235. if ($this->configuration()->getCpu())
  236. {
  237. $container['cpu'] = $this->configuration()->getCpu();
  238. }
  239. //freeze
  240. if ($this->configuration()->isFreeze())
  241. {
  242. $container['freeze'] = 1;
  243. }
  244. //hotplug
  245. if ($this->configuration()->getHotplug())
  246. {
  247. $container['hotplug'] = $this->configuration()->getHotplug();
  248. }
  249. //keyboard
  250. if ($this->configuration()->getKeyboard())
  251. {
  252. $container['keyboard'] = $this->configuration()->getKeyboard();
  253. }
  254. //kvm
  255. if ($this->configuration()->isKvm())
  256. {
  257. $container['kvm'] = 1;
  258. }
  259. //localtime
  260. if ($this->configuration()->isLocaltime())
  261. {
  262. $container['localtime'] = 1;
  263. }
  264. //migrate_downtime
  265. if ($this->configuration()->getMigrateDowntime())
  266. {
  267. $container['migrate_downtime'] = $this->configuration()->getMigrateDowntime();
  268. }
  269. //migrate_speed
  270. if ($this->configuration()->getMigrateSpeed())
  271. {
  272. $container['migrate_speed'] = $this->configuration()->getMigrateSpeed();
  273. }
  274. //reboot
  275. if ($this->configuration()->isReboot())
  276. {
  277. $container['reboot'] = 1;
  278. }
  279. //startdate
  280. if ($this->configuration()->getStartdate())
  281. {
  282. $container['startdate'] = $this->configuration()->getStartdate();
  283. }
  284. //startup
  285. if ($this->configuration()->getStartup())
  286. {
  287. $container['startup'] = $this->configuration()->getStartup();
  288. }
  289. //tablet
  290. if ($this->configuration()->isTablet())
  291. {
  292. $container['tablet'] = 1;
  293. }
  294. //tdf
  295. if ($this->configuration()->isTdf())
  296. {
  297. $container['tdf'] = 1;
  298. }
  299. //watchdog
  300. if ($this->configuration()->getWatchdog())
  301. {
  302. $container['watchdog'] = $this->configuration()->getWatchdog();
  303. }
  304. //bootdisk
  305. if ($this->configuration()->getBootdisk())
  306. {
  307. $container['bootdisk'] = $this->configuration()->getBootdisk();
  308. }
  309. //scsihw
  310. if ($this->configuration()->getScsihw())
  311. {
  312. $container['scsihw'] = $this->configuration()->getScsihw();
  313. }
  314. //args
  315. if ($this->configuration()->getArgs())
  316. {
  317. $container['args'] = $this->configuration()->getArgs();
  318. }
  319. //vga
  320. $vga=[];
  321. if ($this->configuration()->getVga())
  322. {
  323. $vga[] = $this->configuration()->getVga();
  324. }
  325. if($container['vga']!="none" && $this->configuration()->getVgaMemory()){
  326. $vga[]="memory=".$this->configuration()->getVgaMemory();
  327. }
  328. if(!empty($vga)){
  329. $container['vga'] = implode(",",$vga);
  330. }
  331. //xterm.js Console
  332. if ($this->configuration()->isPermissionXtermjs())
  333. {
  334. $container['serial0'] = 'socket';
  335. }
  336. //cpu flags
  337. if ($this->configuration()->hasCpuFlags() && version_compare($this->api()->getVersion(), "5.2", '>'))
  338. {
  339. $container['cpu'] .= ',flags=' .$this->configuration()->getCpuFlagsAsSource();
  340. }
  341. //start
  342. if ($this->configuration()->isStart())
  343. {
  344. $container['start'] = 1;
  345. }
  346. //bios
  347. if ($this->configuration()->getBios())
  348. {
  349. $container['bios'] = $this->configuration()->getBios();
  350. }
  351. //machine
  352. if ($this->configuration()->getMachine())
  353. {
  354. $container['machine'] = $this->configuration()->getMachine();
  355. }
  356. //Create
  357. $nodeService = new Node($this->getVmModel()->node);
  358. $vm = $nodeService->kvm();
  359. $this->setVm($vm);
  360. $vm = (new VmFactory())->fromVmModel( $vmModel);
  361. //init vm
  362. sl('Vm')->setVm($vm);
  363. $taskId = $vm->create($container);
  364. DB::commit();
  365. }
  366. catch (\Exception $ex)
  367. {
  368. echo $ex->getMessage();
  369. DB::rollBack();
  370. Api::commit();
  371. throw $ex;
  372. }
  373. //task history
  374. $this->createTaskHistory($taskId, "Create");
  375. //save task id
  376. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $nodeService->getNode()]);
  377. //sleep
  378. $this->sleep();
  379. return false;
  380. }
  381. }