CreateQemuJob.php 14 KB

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