MigrateSync.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\App\Jobs\Vps\CloneQemuJob;
  23. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\CreateLxcJob;
  24. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\CreateQemuJob;
  25. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\LoadBalancer\UpgradeVmJob;
  26. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
  27. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\CreateVmJob;
  28. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RestoreVm;
  29. use ModulesGarden\ProxmoxAddon\App\Models\Job;
  30. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  31. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  32. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\Console\Style\SymfonyStyle;
  36. /**
  37. * Description of MigrateSync
  38. *
  39. * @author Pawel Kopec <pawelk@modulesgardne.com>
  40. */
  41. class MigrateSync extends Command
  42. {
  43. use main\App\Services\BaseService;
  44. /** Command name
  45. * @var string
  46. */
  47. protected $name = 'migrateSync';
  48. /**
  49. * Command description
  50. * @var string
  51. */
  52. protected $description = '';
  53. /**
  54. * Command help text
  55. * @var string
  56. */
  57. protected $help = '';
  58. /**
  59. * Run your custom code
  60. * @param InputInterface $input
  61. * @param OutputInterface $output
  62. * @return int|null|void
  63. */
  64. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  65. {
  66. $io->title('Synchronize migrate: Starting');
  67. if (!function_exists('ModuleBuildParams'))
  68. {
  69. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  70. }
  71. //Get Hostings
  72. $h = (new main\Core\Models\Whmcs\Hosting)->getTable();
  73. $s = (new main\Core\Models\Whmcs\Server)->getTable();
  74. $hostings = main\Core\Models\Whmcs\Hosting::select("{$h}.*")
  75. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  76. ->whereIn("{$h}.domainstatus", ["Active", "Suspended"])
  77. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"])
  78. ->orderBy("{$h}.server");
  79. $i = 0;
  80. foreach ($hostings->get() as $hosting)
  81. {
  82. /* @var $hosting main\Core\Models\Whmcs\Hosting */
  83. $i++;
  84. $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
  85. try
  86. {
  87. $params = \ModuleBuildParams($hosting->id);
  88. $this->setServerId($hosting->server);
  89. if ($this->getServer()->id != $this->getServerId())
  90. {
  91. unset($this->server, $this->api);
  92. }
  93. $this->getApi()->setInstance();
  94. $resurceRepository = new proxmox\repository\ClusterResourcesRepository;
  95. if ($params['moduletype'] == "proxmoxVPS")
  96. {
  97. if (!$params['customfields']['vmid'])
  98. {
  99. throw new \Exception("Custom Field \"vmid\" is empty");
  100. }
  101. if (!$params['customfields']['node'])
  102. {
  103. throw new \Exception("Custom Field \"node\" is empty");
  104. }
  105. if(!$this->isVpsCreated($params)){
  106. continue;
  107. }
  108. foreach ($resurceRepository->fetch() as $resource)
  109. {
  110. if ($resource->getVmid() == $params['customfields']['vmid'] && $params['customfields']['node'] != $resource->getNode())
  111. {
  112. $output->writeln(sprintf("Hosting: %s, migration has been found on VMID %s", $hosting->id, $params['customfields']['vmid']));
  113. $f = (new main\Core\Models\Whmcs\CustomField)->getTable();
  114. $fv = (new main\Core\Models\Whmcs\CustomFieldValue())->getTable();
  115. $cf = main\Core\Models\Whmcs\CustomFieldValue::rightJoin($f, "{$fv}.fieldid", '=', "{$f}.id")
  116. ->where("{$f}.type", "product")
  117. ->where("{$fv}.relid", $hosting->id)
  118. ->where("{$f}.fieldname", "LIKE", "node%")
  119. ->update(["value" => $resource->getNode()]);
  120. break;
  121. }
  122. }
  123. }
  124. else
  125. {
  126. if ($params['moduletype'] == "ProxmoxCloudVps")
  127. {
  128. $vservers = main\App\Models\VmModel::ofHostingId($hosting->id);
  129. foreach ($vservers->get() as $vserver)
  130. {
  131. if(!$this->isVmCreated($vserver)){
  132. continue;
  133. }
  134. foreach ($resurceRepository->fetch() as $resource)
  135. {
  136. if ($resource->getVmid() == $vserver->vmid && $vserver->node != $resource->getNode())
  137. {
  138. $output->writeln(sprintf("Hosting: %s, migration has been found on VMID %s", $hosting->id, $vserver->vmid));
  139. $vserver->node = $resource->getNode();
  140. $vserver->save();
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  148. }
  149. catch (\Exception $ex)
  150. {
  151. if ($hosting)
  152. {
  153. $io->error("Hosting Id #{$hosting->id}, " . $ex->getMessage());
  154. }
  155. else
  156. {
  157. $io->error($ex->getMessage());
  158. }
  159. }
  160. (new Hypervisor($this->getName(), $input->getOptions()))
  161. ->ping();
  162. }
  163. $output->writeln("");
  164. $io->success([
  165. sprintf("Synchronize migrate: %s Entries Processed.", $i),
  166. "Synchronize migrate: Done"
  167. ]);
  168. }
  169. public function isVpsCreated($params)
  170. {
  171. $jobs = [
  172. CloneQemuJob::class,
  173. CreateQemuJob::class,
  174. CreateLxcJob::class,
  175. MigrateVmJob::class,
  176. RestoreVm::class,
  177. \ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Reinstall\RestoreVm::class,
  178. UpgradeVmJob::class,
  179. CreateVmJob::class
  180. ];
  181. if(Job::waiting()->ofHostingId($params["serviceid"])->ofJobs($jobs)->count() > 0){
  182. return false;
  183. }else if (!$params['customfields']['vmid']){
  184. return false;
  185. }
  186. return true;
  187. }
  188. public function isVmCreated( VmModel $vmModel)
  189. {
  190. $jobs = [
  191. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\CloneQemuJob::class,
  192. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\CreateQemuJob::class,
  193. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\CreateLxcJob::class,
  194. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\MigrateVmJob::class,
  195. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\RestoreVm::class,
  196. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Reinstall\RestoreVm::class,
  197. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\LoadBalancer\UpgradeVmJob::class,
  198. \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Reinstall\CreateVmJob::class
  199. ];
  200. if (!$vmModel->vmid || $vmModel->vmid == 0 ){
  201. return false;
  202. }
  203. if(Job::waiting()->ofHostingId($vmModel->hosting_id)->ofJobs($jobs)->ofCustomId($vmModel->id)->count() > 0){
  204. return false;
  205. }
  206. return true;
  207. }
  208. }