CreateLxcJob.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Features;
  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\Utility;
  11. use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
  12. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  13. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
  14. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  15. use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
  16. class CreateLxcJob extends BaseJob
  17. {
  18. use ProductService;
  19. use UserService;
  20. public function handle()
  21. {
  22. $this->initParams();
  23. $this->initServices();
  24. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  25. //create task validation
  26. if ($this->isDone())
  27. {
  28. if($this->configuration()->isMountPoint()){
  29. $this->additionalMountPointService->create();
  30. }
  31. fire(new VmCreatedEvent($this->vm()));
  32. return true;
  33. }
  34. elseif ($this->isTaskRunning())
  35. {
  36. //sleep
  37. $this->sleep(5);
  38. return false;
  39. }
  40. try
  41. {
  42. Api::beginTransaction();
  43. DB::beginTransaction();
  44. //vmid
  45. $vmid = $this->nextVmid();
  46. $this->customFieldUpdate("vmid", $vmid);
  47. $container = [
  48. 'vmid' => $vmid,
  49. 'ostemplate' => $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE, $this->configuration()->getOsTemplate()),
  50. 'hostname' => $this->containerService->hostname(),
  51. 'password' => $this->getWhmcsParamByKey('password'),
  52. ];
  53. //Configurable Options
  54. if ($this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit()) )
  55. {
  56. $container['cpulimit'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit());
  57. }
  58. //Memory
  59. $container['memory'] = $this->configuration()->getMemory();
  60. if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
  61. {
  62. $container['memory'] = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
  63. Utility::unitFormat($container['memory'], $this->configuration()->getMemoryUnit(), 'mb');
  64. }
  65. //SWAP
  66. $container['swap'] = $this->configuration()->getSwap();
  67. if ($this->isWhmcsConfigOption(ConfigurableOption::SWAP))
  68. {
  69. $container['swap'] = $this->getWhmcsConfigOption(ConfigurableOption::SWAP);
  70. Utility::unitFormat($container['swap'], $this->configuration()->getSwapUnit(), 'mb');
  71. }
  72. //cpuunits
  73. $container['cpuunits'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_UNITS, $this->configuration()->getCpuunits() );
  74. //Name servers
  75. $ns = [];
  76. for ($i = 1; $i <= 2; $i++)
  77. {
  78. $n = trim($this->hosting()->{"ns{$i}"});
  79. if (!empty($n) && !filter_var($n, FILTER_VALIDATE_IP))
  80. {
  81. $n = gethostbyname($n);
  82. }
  83. if (!empty($n) && filter_var($n, FILTER_VALIDATE_IP))
  84. {
  85. $ns[] = $n;
  86. }
  87. }
  88. if ($ns)
  89. {
  90. $container['nameserver'] = implode(" ", $ns);
  91. }
  92. //cores
  93. if ($this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores()))
  94. {
  95. $container['cores'] =$this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores());
  96. }
  97. //arch
  98. if ($this->configuration()->getArch())
  99. {
  100. $container['arch'] = $this->configuration()->getArch();
  101. }
  102. //cmode
  103. if ($this->configuration()->getCmode())
  104. {
  105. $container['cmode'] = $this->configuration()->getCmode();
  106. }
  107. //console
  108. if ($this->configuration()->isConsole())
  109. {
  110. $container['console'] = 1;
  111. }
  112. //description
  113. if ($this->configuration()->getDescription())
  114. {
  115. $container['description'] = $this->containerService->description();
  116. }
  117. //onboot
  118. if ($this->configuration()->isOnboot())
  119. {
  120. $container['onboot'] = 1;
  121. }
  122. //ostype
  123. if ($this->configuration()->getOsType())
  124. {
  125. $container['ostype'] = $this->configuration()->getOsType();
  126. }
  127. //pool
  128. if ($this->configuration()->getPool())
  129. {
  130. $container['pool'] = $this->configuration()->getPool();
  131. }
  132. //protection
  133. if ($this->configuration()->isProtection())
  134. {
  135. $container['protection'] = 1;
  136. }
  137. //startup
  138. if ($this->configuration()->getStartup())
  139. {
  140. $container['startup'] = $this->configuration()->getStartup();
  141. }
  142. //storage
  143. $dafaultStorage = NodeSetting::ofServer($this->getWhmcsParamByKey('serverid'))
  144. ->ofNode($this->getNode()->getNode())
  145. ->ofSetting('defaultStorage')
  146. ->value("value");
  147. $container['storage'] = $dafaultStorage ? $dafaultStorage: $this->configuration()->getStorage();
  148. //tty
  149. if ($this->configuration()->getTty())
  150. {
  151. $container['tty'] = $this->configuration()->getTty();
  152. }
  153. //unprivileged
  154. $container['unprivileged'] = $this->configuration()->isUnprivileged() ? 1 : 0;
  155. //Disk
  156. $diskSize = $this->configuration()->getDiskSize();
  157. if ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  158. {
  159. $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  160. Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'gb');
  161. }
  162. //Rootfs
  163. $container['rootfs'] = "{$container['storage']}:{$diskSize}";
  164. //SSH Public key.
  165. if ($this->configuration()->isSshKeyPairs())
  166. {
  167. $container['ssh-public-keys'] = $this->containerService->makeKeyPairs()->getPublic();
  168. }
  169. //Network
  170. $networkService = new NetworkService();
  171. $container += $networkService->buildLxc();
  172. //start
  173. if ($this->configuration()->isStart() && version_compare($this->api()->getVersion(), "4.4", '>'))
  174. {
  175. $container['start'] = 1;
  176. }
  177. //features
  178. $features = new Features();
  179. //Keyctl
  180. if($this->configuration()->isFeatureKeyctl() && $this->configuration()->isUnprivileged()){
  181. $features->setKeyctl(1);
  182. }
  183. //Nesting
  184. if($this->configuration()->isFeatureNesting() ){
  185. $features->setNesting(1);
  186. }
  187. //NFS
  188. if($this->configuration()->isFeatureNfs() ){
  189. $features->addNfs();
  190. }
  191. //CIFS
  192. if($this->configuration()->isFeatureCifs() ){
  193. $features->addCifs();
  194. }
  195. //Fuse
  196. if($this->configuration()->isFeatureFuse() ){
  197. $features->setFuse(1);
  198. }
  199. //Mknod
  200. if($this->configuration()->isFeatureMknod() ){
  201. $features->setMknod(1);
  202. }
  203. if(!$features->isEmpty()){
  204. $container[Features::ID] = $features->asConfig();
  205. }
  206. //Create
  207. $nodeService = $this->getWhmcsCustomField(CustomField::NODE)? new Node($this->getWhmcsCustomField(CustomField::NODE)) : $this->getNode();
  208. $vm = $nodeService->lxc();
  209. $this->setVm($vm);
  210. $taskId = $vm->create($container);
  211. $this->customFieldUpdate("node", $nodeService->getNode());
  212. DB::commit();
  213. }
  214. catch (\Exception $ex)
  215. {
  216. DB::rollBack();
  217. Api::commit();
  218. $this->failed($ex->getMessage());
  219. throw $ex;
  220. }
  221. //task history
  222. $this->createTaskHistory($taskId, "Create");
  223. //save task id
  224. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $nodeService->getNode()]);
  225. //sleep
  226. $this->sleep();
  227. return false;
  228. }
  229. }