| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps;
- use Illuminate\Database\Capsule\Manager as DB;
- use MGProvision\Proxmox\v2\Api;
- use MGProvision\Proxmox\v2\models\Features;
- use MGProvision\Proxmox\v2\models\Node;
- use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
- use ModulesGarden\ProxmoxAddon\App\Events\Vps\VmCreatedEvent;
- use ModulesGarden\ProxmoxAddon\App\Models\NodeSetting;
- use ModulesGarden\ProxmoxAddon\App\Services\Utility;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
- use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
- class CreateLxcJob extends BaseJob
- {
- use ProductService;
- use UserService;
- public function handle()
- {
- $this->initParams();
- $this->initServices();
- $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
- //create task validation
- if ($this->isDone())
- {
- if($this->configuration()->isMountPoint()){
- $this->additionalMountPointService->create();
- }
- fire(new VmCreatedEvent($this->vm()));
- return true;
- }
- elseif ($this->isTaskRunning())
- {
- //sleep
- $this->sleep(5);
- return false;
- }
- try
- {
- Api::beginTransaction();
- DB::beginTransaction();
- //vmid
- $vmid = $this->nextVmid();
- $this->customFieldUpdate("vmid", $vmid);
- $container = [
- 'vmid' => $vmid,
- 'ostemplate' => $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE, $this->configuration()->getOsTemplate()),
- 'hostname' => $this->containerService->hostname(),
- 'password' => $this->getWhmcsParamByKey('password'),
- ];
- //Configurable Options
- if ($this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit()) )
- {
- $container['cpulimit'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT, $this->configuration()->getCpulimit());
- }
- //Memory
- $container['memory'] = $this->configuration()->getMemory();
- if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
- {
- $container['memory'] = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
- Utility::unitFormat($container['memory'], $this->configuration()->getMemoryUnit(), 'mb');
- }
- //SWAP
- $container['swap'] = $this->configuration()->getSwap();
- if ($this->isWhmcsConfigOption(ConfigurableOption::SWAP))
- {
- $container['swap'] = $this->getWhmcsConfigOption(ConfigurableOption::SWAP);
- Utility::unitFormat($container['swap'], $this->configuration()->getSwapUnit(), 'mb');
- }
- //cpuunits
- $container['cpuunits'] = $this->getWhmcsConfigOption(ConfigurableOption::CPU_UNITS, $this->configuration()->getCpuunits() );
- //Name servers
- $ns = [];
- for ($i = 1; $i <= 2; $i++)
- {
- $n = trim($this->hosting()->{"ns{$i}"});
- if (!empty($n) && !filter_var($n, FILTER_VALIDATE_IP))
- {
- $n = gethostbyname($n);
- }
- if (!empty($n) && filter_var($n, FILTER_VALIDATE_IP))
- {
- $ns[] = $n;
- }
- }
- if ($ns)
- {
- $container['nameserver'] = implode(" ", $ns);
- }
- //cores
- if ($this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores()))
- {
- $container['cores'] =$this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores());
- }
- //arch
- if ($this->configuration()->getArch())
- {
- $container['arch'] = $this->configuration()->getArch();
- }
- //cmode
- if ($this->configuration()->getCmode())
- {
- $container['cmode'] = $this->configuration()->getCmode();
- }
- //console
- if ($this->configuration()->isConsole())
- {
- $container['console'] = 1;
- }
- //description
- if ($this->configuration()->getDescription())
- {
- $container['description'] = $this->containerService->description();
- }
- //onboot
- if ($this->configuration()->isOnboot())
- {
- $container['onboot'] = 1;
- }
- //ostype
- if ($this->configuration()->getOsType())
- {
- $container['ostype'] = $this->configuration()->getOsType();
- }
- //pool
- if ($this->configuration()->getPool())
- {
- $container['pool'] = $this->configuration()->getPool();
- }
- //protection
- if ($this->configuration()->isProtection())
- {
- $container['protection'] = 1;
- }
- //startup
- if ($this->configuration()->getStartup())
- {
- $container['startup'] = $this->configuration()->getStartup();
- }
- //storage
- $dafaultStorage = NodeSetting::ofServer($this->getWhmcsParamByKey('serverid'))
- ->ofNode($this->getNode()->getNode())
- ->ofSetting('defaultStorage')
- ->value("value");
- $container['storage'] = $dafaultStorage ? $dafaultStorage: $this->configuration()->getStorage();
- //tty
- if ($this->configuration()->getTty())
- {
- $container['tty'] = $this->configuration()->getTty();
- }
- //unprivileged
- $container['unprivileged'] = $this->configuration()->isUnprivileged() ? 1 : 0;
- //Disk
- $diskSize = $this->configuration()->getDiskSize();
- if ($this->isWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
- {
- $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
- Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'gb');
- }
- //Rootfs
- $container['rootfs'] = "{$container['storage']}:{$diskSize}";
- //SSH Public key.
- if ($this->configuration()->isSshKeyPairs())
- {
- $container['ssh-public-keys'] = $this->containerService->makeKeyPairs()->getPublic();
- }
- //Network
- $networkService = new NetworkService();
- $container += $networkService->buildLxc();
- //start
- if ($this->configuration()->isStart() && version_compare($this->api()->getVersion(), "4.4", '>'))
- {
- $container['start'] = 1;
- }
- //features
- $features = new Features();
- //Keyctl
- if($this->configuration()->isFeatureKeyctl() && $this->configuration()->isUnprivileged()){
- $features->setKeyctl(1);
- }
- //Nesting
- if($this->configuration()->isFeatureNesting() ){
- $features->setNesting(1);
- }
- //NFS
- if($this->configuration()->isFeatureNfs() ){
- $features->addNfs();
- }
- //CIFS
- if($this->configuration()->isFeatureCifs() ){
- $features->addCifs();
- }
- //Fuse
- if($this->configuration()->isFeatureFuse() ){
- $features->setFuse(1);
- }
- //Mknod
- if($this->configuration()->isFeatureMknod() ){
- $features->setMknod(1);
- }
- if(!$features->isEmpty()){
- $container[Features::ID] = $features->asConfig();
- }
- //Create
- $nodeService = $this->getWhmcsCustomField(CustomField::NODE)? new Node($this->getWhmcsCustomField(CustomField::NODE)) : $this->getNode();
- $vm = $nodeService->lxc();
- $this->setVm($vm);
- $taskId = $vm->create($container);
- $this->customFieldUpdate("node", $nodeService->getNode());
- DB::commit();
- }
- catch (\Exception $ex)
- {
- DB::rollBack();
- Api::commit();
- $this->failed($ex->getMessage());
- throw $ex;
- }
- //task history
- $this->createTaskHistory($taskId, "Create");
- //save task id
- $this->putModelDataAndSave(["taskId" => $taskId, "node" => $nodeService->getNode()]);
- //sleep
- $this->sleep();
- return false;
- }
- }
|