CreateLxcJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. //os
  48. if($this->isWhmcsConfigOption(\ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption::OS_TEMPLATE) && $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE) !="0"){
  49. $osTemplate = $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE);
  50. }else if( $this->isWhmcsConfigOption(ConfigurableOption::ARCHIVE) && $this->getWhmcsConfigOption(ConfigurableOption::ARCHIVE) !="0" ){
  51. $archive = $this->getWhmcsConfigOption(ConfigurableOption::ARCHIVE);
  52. } else if($this->configuration()->getOsTemplate() && $this->configuration()->getOsTemplate()!="0"){
  53. $osTemplate = $this->configuration()->getOsTemplate();
  54. } else if($this->configuration()->archive && $this->configuration()->archive!="0"){
  55. $archive = $this->configuration()->archive;
  56. }
  57. $container = [
  58. 'vmid' => $vmid,
  59. 'ostemplate' => $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE, $this->configuration()->getOsTemplate()),
  60. 'hostname' => $this->containerService->hostname(),
  61. 'password' => $this->getWhmcsParamByKey('password'),
  62. ];
  63. if(isset($osTemplate) && $osTemplate){
  64. $container['ostemplate'] = $osTemplate;
  65. }elseif (isset($archive) && $archive){
  66. list($backupNode, $volid) = explode(":", $archive, 2);
  67. $container['ostemplate'] = $volid;
  68. }
  69. //default node
  70. if(isset($backupNode) && $backupNode){
  71. $nodeService = new Node($backupNode);
  72. }else{
  73. $nodeService = $this->getWhmcsCustomField(CustomField::NODE)? new Node($this->getWhmcsCustomField(CustomField::NODE)) : $this->getNode();
  74. }
  75. //Configurable Options
  76. if ($this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit()) )
  77. {
  78. $container['cpulimit'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit());
  79. }
  80. //Memory
  81. $container['memory'] = $this->configuration()->getMemory();
  82. if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
  83. {
  84. $container['memory'] = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
  85. Utility::unitFormat($container['memory'], $this->configuration()->getMemoryUnit(), 'mb');
  86. }
  87. //SWAP
  88. $container['swap'] = $this->configuration()->getSwap();
  89. if ($this->isWhmcsConfigOption(ConfigurableOption::SWAP))
  90. {
  91. $container['swap'] = $this->getWhmcsConfigOption(ConfigurableOption::SWAP);
  92. Utility::unitFormat($container['swap'], $this->configuration()->getSwapUnit(), 'mb');
  93. }
  94. //cpuunits
  95. $container['cpuunits'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_UNITS, $this->configuration()->getCpuunits() );
  96. //Name servers
  97. $ns = [];
  98. for ($i = 1; $i <= 2; $i++)
  99. {
  100. $n = trim($this->hosting()->{"ns{$i}"});
  101. if (!empty($n) && !filter_var($n, FILTER_VALIDATE_IP))
  102. {
  103. $n = gethostbyname($n);
  104. }
  105. if (!empty($n) && filter_var($n, FILTER_VALIDATE_IP))
  106. {
  107. $ns[] = $n;
  108. }
  109. }
  110. if ($ns)
  111. {
  112. $container['nameserver'] = implode(" ", $ns);
  113. }
  114. //cores
  115. if ($this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores()))
  116. {
  117. $container['cores'] =$this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores());
  118. }
  119. //arch
  120. if ($this->configuration()->getArch())
  121. {
  122. $container['arch'] = $this->configuration()->getArch();
  123. }
  124. //cmode
  125. if ($this->configuration()->getCmode())
  126. {
  127. $container['cmode'] = $this->configuration()->getCmode();
  128. }
  129. //console
  130. if ($this->configuration()->isConsole())
  131. {
  132. $container['console'] = 1;
  133. }
  134. //description
  135. if ($this->configuration()->getDescription())
  136. {
  137. $container['description'] = $this->containerService->description();
  138. }
  139. //onboot
  140. if ($this->configuration()->isOnboot())
  141. {
  142. $container['onboot'] = 1;
  143. }
  144. //ostype
  145. if ($this->configuration()->getOsType())
  146. {
  147. $container['ostype'] = $this->configuration()->getOsType();
  148. }
  149. //pool
  150. if ($this->configuration()->getPool())
  151. {
  152. $container['pool'] = $this->configuration()->getPool();
  153. }
  154. //protection
  155. if ($this->configuration()->isProtection())
  156. {
  157. $container['protection'] = 1;
  158. }
  159. //startup
  160. if ($this->configuration()->getStartup())
  161. {
  162. $container['startup'] = $this->configuration()->getStartup();
  163. }
  164. //storage
  165. $dafaultStorage = NodeSetting::ofServer($this->getWhmcsParamByKey('serverid'))
  166. ->ofNode($nodeService->getNode())
  167. ->ofSetting('defaultStorage')
  168. ->value("value");
  169. $container['storage'] = $dafaultStorage ? $dafaultStorage: $this->configuration()->getStorage();
  170. //tty
  171. if ($this->configuration()->getTty())
  172. {
  173. $container['tty'] = $this->configuration()->getTty();
  174. }
  175. //unprivileged
  176. $container['unprivileged'] = $this->configuration()->isUnprivileged() ? 1 : 0;
  177. //Disk
  178. $diskSize = $this->configuration()->getDiskSize();
  179. if($this->getWhmcsConfigOption(ConfigurableOption::STORAGE)){
  180. list($storage,$diskSize) = explode(":", $this->getWhmcsConfigOption(ConfigurableOption::STORAGE),2);
  181. Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'gb');
  182. $container['storage'] = $storage;
  183. }
  184. elseif ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
  185. {
  186. $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
  187. Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'gb');
  188. }
  189. //Rootfs
  190. $container['rootfs'] = "{$container['storage']}:{$diskSize}";
  191. //SSH Public key.
  192. if ($this->configuration()->isSshKeyPairs())
  193. {
  194. $container['ssh-public-keys'] = $this->containerService->makeKeyPairs()->getPublic();
  195. }
  196. //Network
  197. $networkService = new NetworkService();
  198. $container += $networkService->buildLxc();
  199. //start
  200. if ($this->configuration()->isStart() && version_compare($this->api()->getVersion(), "4.4", '>'))
  201. {
  202. $container['start'] = 1;
  203. }
  204. //features
  205. $features = new Features();
  206. //Keyctl
  207. if($this->configuration()->isFeatureKeyctl() && $this->configuration()->isUnprivileged()){
  208. $features->setKeyctl(1);
  209. }
  210. //Nesting
  211. if($this->configuration()->isFeatureNesting() ){
  212. $features->setNesting(1);
  213. }
  214. //NFS
  215. if($this->configuration()->isFeatureNfs() ){
  216. $features->addNfs();
  217. }
  218. //CIFS
  219. if($this->configuration()->isFeatureCifs() ){
  220. $features->addCifs();
  221. }
  222. //Fuse
  223. if($this->configuration()->isFeatureFuse() ){
  224. $features->setFuse(1);
  225. }
  226. //Mknod
  227. if($this->configuration()->isFeatureMknod() ){
  228. $features->setMknod(1);
  229. }
  230. if(!$features->isEmpty()){
  231. $container[Features::ID] = $features->asConfig();
  232. }
  233. //Create
  234. $vm = $nodeService->lxc();
  235. $this->setVm($vm);
  236. $taskId = $vm->create($container);
  237. $this->customFieldUpdate("node", $nodeService->getNode());
  238. DB::commit();
  239. }
  240. catch (\Exception $ex)
  241. {
  242. DB::rollBack();
  243. Api::commit();
  244. $this->failed($ex->getMessage());
  245. throw $ex;
  246. }
  247. //task history
  248. $this->createTaskHistory($taskId, "Create");
  249. //save task id
  250. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $nodeService->getNode()]);
  251. //sleep
  252. $this->sleep();
  253. return false;
  254. }
  255. }