VmNamesCron.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Sep 19, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Cron;
  20. use MGProvision\Proxmox\v2\Factory;
  21. use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
  22. use ModulesGarden\ProxmoxAddon as main;
  23. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  24. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Output\OutputInterface;
  27. use Symfony\Component\Console\Style\SymfonyStyle;
  28. use ModulesGarden\ProxmoxAddon\App\Enum\Vps;
  29. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud;
  30. /**
  31. * Description of Users
  32. *
  33. * @author Pawel Kopec <pawelk@modulesgardne.com>
  34. */
  35. class VmNamesCron extends Command
  36. {
  37. /**
  38. * Command name
  39. * @var string
  40. */
  41. protected $name = 'synchronize-vm-names';
  42. /**
  43. * Command description
  44. * @var string
  45. */
  46. protected $description = '';
  47. /**
  48. * Command help text
  49. * @var string
  50. */
  51. protected $help = '';
  52. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  53. {
  54. $io->title('Synchronize users: Starting');
  55. if (!function_exists('ModuleBuildParams'))
  56. {
  57. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  58. }
  59. //Get Hostings
  60. $h = (new main\Core\Models\Whmcs\Hosting)->getTable();
  61. $s = (new main\Core\Models\Whmcs\Server)->getTable();
  62. $hostings = main\Core\Models\Whmcs\Hosting::select("{$h}.*")
  63. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  64. ->where("{$h}.domainstatus", "Active")
  65. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"])
  66. ->orderBy("{$h}.server");
  67. $i = 0;
  68. foreach ($hostings->get() as $hosting)
  69. {
  70. /* @var $hosting main\Core\Models\Whmcs\Hosting */
  71. $i++;
  72. $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
  73. try
  74. {
  75. $params = \ModuleBuildParams($hosting->id);
  76. $api = Factory::api($params);
  77. $resurceRepository = new ClusterResourcesRepository();
  78. $resurceRepository->setApi($api);
  79. $resurceRepository->findVm();
  80. if ($params['moduletype'] == "proxmoxVPS")
  81. {
  82. if (!$params['customfields'][Vps\CustomField::VMID])
  83. {
  84. throw new \Exception("Custom Field \"vmid\" is empty");
  85. }
  86. if (!$params['customfields'][Vps\CustomField::NODE])
  87. {
  88. throw new \Exception("Custom Field \"node\" is empty");
  89. }
  90. $resurceRepository->findVmid($params['customfields'][Vps\CustomField::VMID]);
  91. foreach ( $resurceRepository->fetch() as $resource){
  92. if($resource->getName() != $hosting->domain ){
  93. $output->writeln(sprintf("Hosting: %s, new VM name %s has been found on VMID %s", $hosting->id, $resource->getName() ,$resource->getVmid() ));
  94. $hosting->update(['domain' => $resource->getName() ]);
  95. }
  96. }
  97. }
  98. elseif ($params['moduletype'] == "ProxmoxCloudVps")
  99. {
  100. $vservers = main\App\Models\VmModel::ofHostingId($hosting->id);
  101. foreach ($vservers->get() as $vserver)
  102. {
  103. foreach ($resurceRepository->fetch() as $resource)
  104. {
  105. if ($vserver->vmid && $resource->getVmid() == $vserver->vmid && $vserver->name != $resource->getName())
  106. {
  107. $vserver->update(['name' => $resource->getName()]);
  108. $output->writeln(sprintf("Hosting: %s, new VM name %s has been found on VMID %s", $hosting->id, $resource->getName(),$resource->getVmid() ));
  109. }
  110. }
  111. }
  112. }
  113. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  114. }
  115. catch (\Exception $ex)
  116. {
  117. if ($hosting)
  118. {
  119. $io->error("Hosting Id #{$hosting->id}, " . $ex->getMessage());
  120. }
  121. else
  122. {
  123. $io->error($ex->getMessage());
  124. }
  125. }
  126. (new Hypervisor($this->getName(), $input->getOptions()))
  127. ->ping();
  128. }
  129. $output->writeln("");
  130. $io->success([
  131. sprintf("Synchronize hostings: %s Entries Processed.", $i),
  132. "Synchronize hostings: Done"
  133. ]);
  134. }
  135. }