MigrateSync.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  23. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Symfony\Component\Console\Style\SymfonyStyle;
  27. /**
  28. * Description of MigrateSync
  29. *
  30. * @author Pawel Kopec <pawelk@modulesgardne.com>
  31. */
  32. class MigrateSync extends Command
  33. {
  34. use main\App\Services\BaseService;
  35. /** Command name
  36. * @var string
  37. */
  38. protected $name = 'migrateSync';
  39. /**
  40. * Command description
  41. * @var string
  42. */
  43. protected $description = '';
  44. /**
  45. * Command help text
  46. * @var string
  47. */
  48. protected $help = '';
  49. /**
  50. * Run your custom code
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. * @return int|null|void
  54. */
  55. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  56. {
  57. $io->title('Synchronize migrate: Starting');
  58. if (!function_exists('ModuleBuildParams'))
  59. {
  60. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  61. }
  62. //Get Hostings
  63. $h = (new main\Core\Models\Whmcs\Hosting)->getTable();
  64. $s = (new main\Core\Models\Whmcs\Server)->getTable();
  65. $hostings = main\Core\Models\Whmcs\Hosting::select("{$h}.*")
  66. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  67. ->whereIn("{$h}.domainstatus", ["Active", "Suspended"])
  68. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"])
  69. ->orderBy("{$h}.server");
  70. $i = 0;
  71. foreach ($hostings->get() as $hosting)
  72. {
  73. /* @var $hosting main\Core\Models\Whmcs\Hosting */
  74. $i++;
  75. $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
  76. try
  77. {
  78. $params = \ModuleBuildParams($hosting->id);
  79. $this->setServerId($hosting->server);
  80. if ($this->getServer()->id != $this->getServerId())
  81. {
  82. unset($this->server, $this->api);
  83. }
  84. $this->getApi()->setInstance();
  85. $resurceRepository = new proxmox\repository\ClusterResourcesRepository;
  86. if ($params['moduletype'] == "proxmoxVPS")
  87. {
  88. if (!$params['customfields']['vmid'])
  89. {
  90. throw new \Exception("Custom Field \"vmid\" is empty");
  91. }
  92. if (!$params['customfields']['node'])
  93. {
  94. throw new \Exception("Custom Field \"node\" is empty");
  95. }
  96. foreach ($resurceRepository->fetch() as $resource)
  97. {
  98. if ($resource->getVmid() == $params['customfields']['vmid'] && $params['customfields']['node'] != $resource->getNode())
  99. {
  100. $output->writeln(sprintf("Hosting: %s, migration has been found on VMID %s", $hosting->id, $params['customfields']['vmid']));
  101. $f = (new main\Core\Models\Whmcs\CustomField)->getTable();
  102. $fv = (new main\Core\Models\Whmcs\CustomFieldValue())->getTable();
  103. $cf = main\Core\Models\Whmcs\CustomFieldValue::rightJoin($f, "{$fv}.fieldid", '=', "{$f}.id")
  104. ->where("{$f}.type", "product")
  105. ->where("{$fv}.relid", $hosting->id)
  106. ->where("{$f}.fieldname", "LIKE", "node%")
  107. ->update(["value" => $resource->getNode()]);
  108. break;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. if ($params['moduletype'] == "ProxmoxCloudVps")
  115. {
  116. $vservers = main\App\Models\VmModel::ofHostingId($hosting->id);
  117. foreach ($vservers->get() as $vserver)
  118. {
  119. foreach ($resurceRepository->fetch() as $resource)
  120. {
  121. if ($resource->getVmid() == $vserver->vmid && $vserver->node != $resource->getNode())
  122. {
  123. $output->writeln(sprintf("Hosting: %s, migration has been found on VMID %s", $hosting->id, $vserver->vmid));
  124. $vserver->node = $resource->getNode();
  125. $vserver->save();
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  133. }
  134. catch (\Exception $ex)
  135. {
  136. if ($hosting)
  137. {
  138. $io->error("Hosting Id #{$hosting->id}, " . $ex->getMessage());
  139. }
  140. else
  141. {
  142. $io->error($ex->getMessage());
  143. }
  144. }
  145. (new Hypervisor($this->getName(), $input->getOptions()))
  146. ->ping();
  147. }
  148. $output->writeln("");
  149. $io->success([
  150. sprintf("Synchronize migrate: %s Entries Processed.", $i),
  151. "Synchronize migrate: Done"
  152. ]);
  153. }
  154. }