CreateLxcJob.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\Features;
  6. use MGProvision\Proxmox\v2\VmFactory;
  7. use ModulesGarden\ProxmoxAddon\App\Events\Cloud\VmCreatedEvent;
  8. use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
  9. use ModulesGarden\ProxmoxAddon\App\Models\NodeSetting;
  10. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AdditionalMountPointService;
  11. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\AgentService;
  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 CreateLxcJob extends BaseJob
  21. {
  22. use ProductService;
  23. use UserService;
  24. use VmNetwork;
  25. /**
  26. * @var ContainerService
  27. */
  28. protected $containerService;
  29. protected function initServices()
  30. {
  31. $this->emailService = new EmailService();
  32. $this->containerService = new ContainerService();
  33. $this->agentService = new AgentService();
  34. $this->additionalMountPointService = new AdditionalMountPointService();
  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->additionalMountPointService->hasMountPoint()){
  46. $this->additionalMountPointService->create($this->getModelData());
  47. }
  48. fire(new VmCreatedEvent($this->getVmModel(), $this->getModelData()));
  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. $this->getVmModel();
  64. $this->vmModel->vmid = $vmid;
  65. $this->vmModel->save();
  66. sl('Vm')->setVm(null);
  67. sl('Vm')->setVmModel( $this->vmModel);
  68. $container = [
  69. 'vmid' => $vmid,
  70. 'ostemplate' => $this->getModelData()['osTemplate'],
  71. 'hostname' => $this->getVmModel()->name,
  72. 'password' => $this->getVmModel()->getPassword(),
  73. ];
  74. //Configurable Options
  75. if ($this->getVmModel()->cpulimit )
  76. {
  77. $container['cpulimit'] = $this->getVmModel()->cpulimit;
  78. }
  79. //Memory
  80. $container['memory'] = $this->getVmModel()->memory;
  81. //SWAP
  82. $container['swap'] = $this->getVmModel()->swap;
  83. //cpuunits
  84. $container['cpuunits'] = $this->getVmModel()->cpuunits;
  85. //Name servers
  86. $ns = [];
  87. for ($i = 0; $i <= 1; $i++)
  88. {
  89. $n = trim($this->getModelData()['nameserver'][$i]);
  90. if (!empty($n) && !filter_var($n, FILTER_VALIDATE_IP))
  91. {
  92. $n = gethostbyname($n);
  93. }
  94. if (!empty($n) && filter_var($n, FILTER_VALIDATE_IP))
  95. {
  96. $ns[] = $n;
  97. }
  98. }
  99. if ($ns)
  100. {
  101. $container['nameserver'] = implode(" ", $ns);
  102. }
  103. //cores
  104. if ($this->getVmModel()->cores)
  105. {
  106. $container['cores'] = $this->getVmModel()->cores;
  107. }
  108. //arch
  109. if ($this->configuration()->getArch())
  110. {
  111. $container['arch'] = $this->configuration()->getArch();
  112. }
  113. //cmode
  114. if ($this->configuration()->getCmode())
  115. {
  116. $container['cmode'] = $this->configuration()->getCmode();
  117. }
  118. //console
  119. if ($this->configuration()->isConsole())
  120. {
  121. $container['console'] = 1;
  122. }
  123. //description
  124. if ($this->getModelData()['description'])
  125. {
  126. $container['description'] = $this->getModelData()['description'];
  127. }
  128. //onboot
  129. if ($this->configuration()->isOnboot())
  130. {
  131. $container['onboot'] = 1;
  132. }
  133. //ostype
  134. if ($this->configuration()->getOsType())
  135. {
  136. $container['ostype'] = $this->configuration()->getOsType();
  137. }
  138. //pool
  139. if ($this->configuration()->getPool())
  140. {
  141. $container['pool'] = $this->configuration()->getPool();
  142. }
  143. //protection
  144. if ($this->configuration()->isProtection())
  145. {
  146. $container['protection'] = 1;
  147. }
  148. //startup
  149. if ($this->configuration()->getStartup())
  150. {
  151. $container['startup'] = $this->configuration()->getStartup();
  152. }
  153. //storage
  154. $dafaultStorage = NodeSetting::ofServer($this->getWhmcsParamByKey('serverid'))
  155. ->ofNode($this->getNode()->getNode())
  156. ->ofSetting('defaultStorage')
  157. ->value("value");
  158. $container['storage'] = $dafaultStorage ? $dafaultStorage: $this->configuration()->getStorage();
  159. //tty
  160. if ($this->configuration()->getTty())
  161. {
  162. $container['tty'] = $this->configuration()->getTty();
  163. }
  164. //unprivileged
  165. $container['unprivileged'] = $this->configuration()->isUnprivileged() ? 1 : 0;
  166. //Disk
  167. $diskSize = $this->getVmModel()->disk;
  168. //Rootfs
  169. $container['rootfs'] = "{$container['storage']}:{$diskSize}";
  170. //SSH Public key.
  171. if ($this->configuration()->isSshKeyPairs())
  172. {
  173. $keyPairs = $this->containerService->makeKeyPairs();
  174. $keyPairs->vm_id = $this->getVmModel()->id;
  175. $keyPairs->save();
  176. $container['ssh-public-keys'] = $keyPairs->getPublic();
  177. }
  178. //Network
  179. $networkService = new NetworkService();
  180. $container += $this->createNetwork();
  181. //start
  182. if ($this->configuration()->isStart() && version_compare($this->api()->getVersion(), "4.4", '>'))
  183. {
  184. $container['start'] = 1;
  185. }
  186. //features
  187. $features = new Features();
  188. //Keyctl
  189. if($this->configuration()->isFeatureKeyctl() && $this->configuration()->isUnprivileged()){
  190. $features->setKeyctl(1);
  191. }
  192. //Nesting
  193. if($this->configuration()->isFeatureNesting() ){
  194. $features->setNesting(1);
  195. }
  196. //NFS
  197. if($this->configuration()->isFeatureNfs() ){
  198. $features->addNfs();
  199. }
  200. //CIFS
  201. if($this->configuration()->isFeatureCifs() ){
  202. $features->addCifs();
  203. }
  204. //Fuse
  205. if($this->configuration()->isFeatureFuse() ){
  206. $features->setFuse(1);
  207. }
  208. //Mknod
  209. if($this->configuration()->isFeatureMknod() ){
  210. $features->setMknod(1);
  211. }
  212. if(!$features->isEmpty()){
  213. $container[Features::ID] = $features->asConfig();
  214. }
  215. //Create
  216. $vm = (new VmFactory())->fromVmModel($this->getVmModel());
  217. $taskId = $vm->create($container);
  218. DB::commit();
  219. }
  220. catch (\Exception $ex)
  221. {
  222. DB::rollBack();
  223. Api::commit();
  224. $this->failed($ex->getMessage());
  225. throw $ex;
  226. }
  227. //task history
  228. $this->createTaskHistory($taskId, "Create");
  229. //save task id
  230. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->getVmModel()->node]);
  231. //sleep
  232. $this->sleep();
  233. return false;
  234. }
  235. }