CreateLxcJob.php 9.2 KB

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