| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /* * ********************************************************************
- * ProxmoxAddon product developed. (Sep 19, 2018)
- * *
- *
- * 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\ProxmoxAddon\App\Cron;
- use MGProvision\Proxmox\v2\Factory;
- use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
- use ModulesGarden\ProxmoxAddon as main;
- use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
- use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Style\SymfonyStyle;
- use ModulesGarden\ProxmoxAddon\App\Enum\Vps;
- use ModulesGarden\ProxmoxAddon\App\Enum\Cloud;
- /**
- * Description of Users
- *
- * @author Pawel Kopec <pawelk@modulesgardne.com>
- */
- class VmNamesCron extends Command
- {
- /**
- * Command name
- * @var string
- */
- protected $name = 'synchronize-vm-names';
- /**
- * Command description
- * @var string
- */
- protected $description = '';
- /**
- * Command help text
- * @var string
- */
- protected $help = '';
- protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
- {
- $io->title('Synchronize users: Starting');
- if (!function_exists('ModuleBuildParams'))
- {
- require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
- }
- //Get Hostings
- $h = (new main\Core\Models\Whmcs\Hosting)->getTable();
- $s = (new main\Core\Models\Whmcs\Server)->getTable();
- $hostings = main\Core\Models\Whmcs\Hosting::select("{$h}.*")
- ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
- ->where("{$h}.domainstatus", "Active")
- ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"])
- ->orderBy("{$h}.server");
- $i = 0;
- foreach ($hostings->get() as $hosting)
- {
- /* @var $hosting main\Core\Models\Whmcs\Hosting */
- $i++;
- $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
- try
- {
- $params = \ModuleBuildParams($hosting->id);
- $api = Factory::api($params);
- $resurceRepository = new ClusterResourcesRepository();
- $resurceRepository->setApi($api);
- $resurceRepository->findVm();
- if ($params['moduletype'] == "proxmoxVPS")
- {
- if (!$params['customfields'][Vps\CustomField::VMID])
- {
- throw new \Exception("Custom Field \"vmid\" is empty");
- }
- if (!$params['customfields'][Vps\CustomField::NODE])
- {
- throw new \Exception("Custom Field \"node\" is empty");
- }
- $resurceRepository->findVmid($params['customfields'][Vps\CustomField::VMID]);
- foreach ( $resurceRepository->fetch() as $resource){
- if($resource->getName() != $hosting->domain ){
- $output->writeln(sprintf("Hosting: %s, new VM name %s has been found on VMID %s", $hosting->id, $resource->getName() ,$resource->getVmid() ));
- $hosting->update(['domain' => $resource->getName() ]);
- }
- }
- }
- elseif ($params['moduletype'] == "ProxmoxCloudVps")
- {
- $vservers = main\App\Models\VmModel::ofHostingId($hosting->id);
- foreach ($vservers->get() as $vserver)
- {
- foreach ($resurceRepository->fetch() as $resource)
- {
- if ($vserver->vmid && $resource->getVmid() == $vserver->vmid && $vserver->name != $resource->getName())
- {
- $vserver->update(['name' => $resource->getName()]);
- $output->writeln(sprintf("Hosting: %s, new VM name %s has been found on VMID %s", $hosting->id, $resource->getName(),$resource->getVmid() ));
- }
- }
- }
- }
- $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
- }
- catch (\Exception $ex)
- {
- if ($hosting)
- {
- $io->error("Hosting Id #{$hosting->id}, " . $ex->getMessage());
- }
- else
- {
- $io->error($ex->getMessage());
- }
- }
- (new Hypervisor($this->getName(), $input->getOptions()))
- ->ping();
- }
- $output->writeln("");
- $io->success([
- sprintf("Synchronize hostings: %s Entries Processed.", $i),
- "Synchronize hostings: Done"
- ]);
- }
- }
|