| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- /**********************************************************************
- * ProxmoxVPS developed. (26.03.19)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- **********************************************************************/
- namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Actions;
- use Illuminate\Database\Capsule\Manager as DB;
- use ModulesGarden\ProxmoxAddon\App\Events\Vps\LxcUpdateEvent;
- use ModulesGarden\ProxmoxAddon\App\Events\Vps\QemuUpdateEvent;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Agent\ConfigureNetworkJob;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\LoadBalancer as LoadBalancer;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MoveDiskJob;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RebootVmJob;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\BackupVmJob;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\CreateVmJob;
- use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\DeleteVmJob;
- use ModulesGarden\ProxmoxAddon\App\Libs\Format;
- use ModulesGarden\ProxmoxAddon\App\Models\Job;
- use ModulesGarden\ProxmoxAddon\App\Models\ServerGroupItem;
- use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\ToDoList;
- use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Upgrade;
- use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
- use ModulesGarden\ProxmoxAddon\App\Services\EmailService;
- use ModulesGarden\ProxmoxAddon\App\Services\LoadBalancerService;
- use ModulesGarden\ProxmoxAddon\App\Services\Utility;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\HostingService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
- use ModulesGarden\ProxmoxAddon\App\Services\Vps\UserService;
- use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
- use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
- use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
- use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\fire;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
- use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
- use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
- class ChangePackage extends AddonController
- {
- use WhmcsParams;
- use ApiService;
- use ProductService;
- use UserService;
- use HostingService;
- /**
- * @var NetworkService
- */
- private $networkService;
- /**
- * @var IpSetIpFilterService
- */
- private $ipSetIpFilterService;
- /**
- * @var EmailService
- */
- private $emailService;
- /**
- * ChangePackage constructor.
- * @param $networkService
- */
- public function __construct()
- {
- $this->networkService = new NetworkService();
- $this->ipSetIpFilterService = new IpSetIpFilterService();
- $this->emailService = new EmailService();
- }
- public function execute($params = null)
- {
- if(!ProxmoxAddonValidator::isInstalled()){
- return ProxmoxAddonValidator::failAsString();
- }
- (new AppParams())->initFromWhmcsParams();
- try
- {
- $this->manuallyUpgrade();
- //Disk
- $this->diskSizeValidation();
- $this->createReinstallJob();
- //Add IP Addresses
- if (!Utility::isIpManagerProxmoxVPSIntegration())
- {
- list($requestIPv4, $requestIPv6) = $this->networkService->getIpAddressesRequest();
- $bridge = $this->configuration()->getBridge();
- $this->networkService->hasIp($requestIPv4, $requestIPv6, $this->vm()->getNode(),$bridge)
- ->addIp($requestIPv4, $requestIPv6, $this->vm()->getNode(),$bridge);
- }
- //Unassing ip addresses and delete network
- if(!Utility::isIpManagerProxmoxVPSIntegration())
- {
- $this->networkService->unassignIpAddressesAndDeleteNetwork();
- }
- //Load Balancer
- if (!Job::waiting()->ofHostingId($this->getWhmcsParamByKey("serviceid"))->ofJob(LoadBalancer\UpgradeVmJob::class)->count() && $this->configuration()->isLoadBalancer() && in_array($this->configuration()->getLoadBalancerOnUpgrade(), ["block", "migrate"]))
- {
- $loadBalancer = new LoadBalancerService($this->getWhmcsParamByKey('serverid'));
- $loadBalancer->setApi($this->api());
- //Disk
- $disk = $this->configuration()->getDiskSize();
- Utility::unitFormat($disk, "gb", 'bytes');
- if($this->getWhmcsConfigOption(ConfigurableOption::STORAGE)){
- list($storage,$disk) = explode(":", $this->getWhmcsConfigOption(ConfigurableOption::STORAGE),2);
- Utility::unitFormat($disk, $this->configuration()->getDiskUnit(), 'bytes');
- }
- elseif ($this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
- {
- $disk = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
- Utility::unitFormat($disk, $this->configuration()->getDiskUnit(), 'bytes');
- }
- //RAM
- $ram = $this->configuration()->getMemory();
- Utility::unitFormat($ram, "mb", 'bytes');
- if ($this->getWhmcsConfigOption(ConfigurableOption::MEMORY))
- {
- $ram = $this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
- Utility::unitFormat($ram, $this->configuration()->getMemoryUnit(), 'bytes');
- }
- if ($this->configuration()->isQemu())
- {
- $socket = $this->getWhmcsConfigOption(ConfigurableOption::SOCKETS,$this->configuration()->getSockets() );
- $cores = $this->getWhmcsConfigOption(ConfigurableOption::CORES_PER_SOCKET, $this->configuration()->getCores()) ;
- $cpu = $socket * $cores;
- }
- else if ($this->configuration()->isLxc())
- {
- $cpu = $this->getWhmcsConfigOption(ConfigurableOption::CORES, $this->configuration()->getCores()) ;
- }
- $loadBalancer->setExcludeVmid($this->vm()->getVmid());
- //Upgrade on current node
- if ($this->configuration()->getLoadBalancerOnUpgrade() == "block")
- {
- $loadBalancerNodes = $loadBalancer->findByNode($this->vm()->getNode());
- $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
- ->findByCpu($cpu)
- ->findByDisk($disk);
- if ($loadBalancerNodes->isEmpty())
- {
- throw new \Exception("Load Balancer: Cannot find free resources on node: " . $this->vm()->getNode());
- }
- }
- else
- {
- if ($this->configuration()->getLoadBalancerOnUpgrade() == "migrate")
- { //Allow to upgrade process, after previous migration vm to the server with free space.
- $loadBalancerNodes = $loadBalancer->findByNode($this->vm()->getNode());
- $loadBalancerNodes = $loadBalancerNodes->findByRam($ram)
- ->findByCpu($cpu)
- ->findByDisk($disk);
- if ($loadBalancerNodes->isEmpty())
- {
- if($this->configuration()->getServerGroup()){
- $nodes = ServerGroupItem::ofGroupIdsAndServerId($this->configuration()->getServerGroup(), $this->getWhmcsParamByKey('serverid'))
- ->pluck("node")
- ->toArray();
- if(empty($nodes)){
- throw new \InvalidArgumentException("LoadBalancer: Cannot find group for server #". $this->getWhmcsParamByKey('serverid'));
- }
- $loadBalancer->findByNodes($nodes);
- }
- $loadBalancerNodes = $loadBalancer->findByVmCreate();
- $nodesForUser = $loadBalancer->findNotUser($this->getWhmcsParamByKey('userid'));
- if (!$nodesForUser->isEmpty())
- {
- $loadBalancerNodes = $nodesForUser;
- }
- $targetNode = $loadBalancerNodes->findByRam($ram)
- ->findByCpu($cpu)
- ->findByDisk($disk)
- ->findByVms(1)
- ->nodeLowLoad();
- if ($this->configuration()->isLoadBalancerShutdownOnUpgrade())
- {
- $job = queue(LoadBalancer\ShutdownVmJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid'), "targetNode" => $targetNode], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- $job = queue(LoadBalancer\MigrateVmJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid'), "targetNode" => $targetNode], $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- queue(LoadBalancer\UpgradeVmJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid'), "targetNode" => $targetNode], $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- return true;
- }
- }
- }
- }
- if ($this->configuration()->isQemu())
- {
- $evnet = new QemuUpdateEvent($this->vm());
- $evnet->setChangeVmPassword(false);
- fire($evnet);
- }
- elseif ($this->configuration()->isLxc())
- {
- fire(new LxcUpdateEvent($this->vm()));
- }
- //hosting limit
- $this->saveUsageLimit();
- //move disk
- if($this->getWhmcsConfigOption(ConfigurableOption::STORAGE)){
- list($storage,$diskSize) = explode(":", $this->getWhmcsConfigOption(ConfigurableOption::STORAGE),2);
- $hdd = $this->vm()->getMasterHardDisk();
- if($hdd->getStorage() != $storage && !Job::waiting()->ofJob(MoveDiskJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count()){
- $parent = queue(MoveDiskJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid')], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- }
- if($this->configuration()->isAgentConfigureNetwork()){
- $parent = queue(ConfigureNetworkJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid')], $parent->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- //reboot
- if ($this->configuration()->isRebootVmAfterChangePackage() &&
- $this->vm()->isRunning() &&
- !Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
- {
- $parent = queue(RebootVmJob::class, ['hostingId' => $this->getWhmcsParamByKey('serviceid')], $parent->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- //IP Filter
- if ($this->configuration()->isIpsetIpFilter())
- {
- $this->ipSetIpFilterService->create();
- }
- return "success";
- }
- catch (\Exception $ex)
- {
- return $ex->getMessage();
- }
- }
- private function manuallyUpgrade()
- {
- $scriptFile = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], DIRECTORY_SEPARATOR) + 1);
- if (!$this->configuration()->getUpgradeNotificationTemplateId() || $scriptFile == "clientsservices.php")
- {
- return;
- }
- if ($this->configuration()->isToDoList())
- {
- $title = sprintf('Manually Upgrade - Service ID: %s', $this->getWhmcsParamByKey('serviceid'));
- if (!ToDoList::ofTitle($title)->pending()->count())
- {
- $entity = new ToDoList();
- $entity->fill(
- [
- 'date' => date("Y-m-d H:i:s"),
- "duedate" => date("Y-m-d H:i:s"),
- 'title' => $title,
- "status" => "Pending",
- "description" => "An upgrade order has received its payment, automatic upgrades has been disabled and requires manually processing.",
- "admin" => 0,
- ]
- );
- $entity->save();
- }
- }
- //Send Admin notification
- $this->emailService->template($this->configuration()->getUpgradeNotificationTemplateId());
- global $customadminpath;
- $adminDir = $customadminpath ? $customadminpath : "admin";
- $adminAreaLink = $GLOBALS['CONFIG']['SystemURL'] . "/{$adminDir}";
- $hosting = $GLOBALS['CONFIG']['SystemURL'] . "/{$adminDir}/clientsservices.php?id=" . $this->getWhmcsParamByKey('serviceid');
- $emailVars = [
- "client_id" => $this->getWhmcsParamByKey('clientsdetails')['id'],
- "service_id" => $this->getWhmcsParamByKey('serviceid'),
- "service_product" => "<a href='{$hosting}/'>{$hosting}</a>",
- "service_domain" => $this->getWhmcsParamByKey('domain'),
- "whmcs_admin_link" => "<a href='{$adminAreaLink}/'>{$adminAreaLink}</a>",
- ];
- $this->emailService->vars($emailVars)->sendToAdmin();
- throw new \Exception("Automaticall upgrade has been disabled by admin");
- }
- private function createReinstallJob()
- {
- if (!$this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE))
- {
- return;
- }
- if (!Upgrade::ofHostingId($this->getWhmcsParamByKey('serviceid'))->pending()->today()->count())
- {
- return;
- }
- $pg = "tblproductconfiggroups";
- $pl = "tblproductconfiglinks";
- $po = "tblproductconfigoptions";
- $optionId = DB::table($po)
- ->select("{$po}.id")
- ->leftJoin($pg, "{$pg}.id", "=", "{$po}.gid")
- ->leftJoin($pl, "{$pl}.gid", "=", "{$pg}.id")
- ->where("{$pl}.pid", $this->getWhmcsParamByKey("pid"))
- ->where("{$po}.optionname", "like", "VM Template|%")
- ->value("id");
- if (!$optionId)
- {
- return;
- }
- if (!Upgrade::ofHostingId($this->getWhmcsParamByKey('serviceid'))->pending()->today()->ofOptionId($optionId)->count())
- {
- return;
- }
- if ($this->configuration()->isQemu())
- {
- if ($this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE)=== "0")
- {
- return;
- }
- }
- $arguments = [
- 'hostingId' => $this->getWhmcsParamByKey('serviceid'),
- "osTemplate" => $this->getWhmcsConfigOption(ConfigurableOption::OS_TEMPLATE),
- "password" => encrypt($this->getWhmcsParamByKey("password"))
- ];
- if ($this->configuration()->isBackupVmBeforeReinstall())
- {
- $job = queue(BackupVmJob::class, $arguments, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- $job = queue(DeleteVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- $job = queue(CreateVmJob::class, $arguments, $job->id, "hosting", $this->getWhmcsParamByKey("serviceid"));
- }
- protected function diskSizeValidation(){
- $diskSize = $this->configuration()->getDiskSize();
- if($this->getWhmcsConfigOption(ConfigurableOption::STORAGE)){
- list($storage,$diskSize) = explode(":", $this->getWhmcsConfigOption(ConfigurableOption::STORAGE),2);
- Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'bytes');
- }
- elseif ($this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE))
- {
- $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::DISK_SIZE);
- Utility::unitFormat($diskSize, $this->configuration()->getDiskUnit(), 'bytes');
- }else{
- Utility::unitFormat($diskSize, "gb", 'bytes');
- }
- $masterHddSize = $this->vm()->getMasterHddSize();
- Utility::unitFormat($masterHddSize, "gb", 'bytes');
- if ($diskSize < $masterHddSize)
- {
- throw new \Exception(sprintf('Downgrading disk size is restricted. Request Disk size: %s, VM disk size: %s',
- Format::convertBytes($diskSize), Format::convertBytes($masterHddSize)));
- }
- }
- }
|