RecoveryList.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Illuminate\Database\Capsule\Manager as DB;
  21. use MGProvision\Proxmox\v2 as proxmox;
  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. /**
  29. * Description of RecoveryList
  30. *
  31. * @author Pawel Kopec <pawelk@modulesgardne.com>
  32. */
  33. class RecoveryList extends Command
  34. {
  35. use main\App\Services\BaseService;
  36. /**
  37. * Command name
  38. * @var string
  39. */
  40. protected $name = 'recoveryList';
  41. /**
  42. * Command description
  43. * @var string
  44. */
  45. protected $description = '';
  46. /**
  47. * Command help text
  48. * @var string
  49. */
  50. protected $help = '';
  51. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  52. {
  53. $io->title('Synchronize recovery list: Starting');
  54. if (!function_exists('ModuleBuildParams'))
  55. {
  56. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  57. }
  58. $activeHostings = [];
  59. $activeVservers = [];
  60. //Get Hostings
  61. $h = (new main\Core\Models\Whmcs\Hosting)->getTable();
  62. $s = (new main\Core\Models\Whmcs\Server)->getTable();
  63. $hostings = main\Core\Models\Whmcs\Hosting::select("{$h}.*")
  64. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  65. ->where("{$h}.domainstatus", "Active")
  66. ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"])
  67. ->orderBy("{$h}.server");
  68. $i = 0;
  69. foreach ($hostings->get() as $hosting)
  70. {
  71. /* @var $hosting main\Core\Models\Whmcs\Hosting */
  72. $i++;
  73. try
  74. {
  75. $activeHostings[] = (int)$hosting->id;
  76. $params = \ModuleBuildParams($hosting->id);
  77. $this->setServerId($hosting->server);
  78. if ($this->getServer()->id != $this->getServerId())
  79. {
  80. unset($this->server, $this->api);
  81. }
  82. $this->getApi()->setInstance();
  83. if ($params['moduletype'] == "proxmoxVPS")
  84. {
  85. if (!$params['customfields']['vmid'])
  86. {
  87. throw new \Exception("Custom Field \"vmid\" is empty");
  88. }
  89. if (!$params['customfields']['node'])
  90. {
  91. throw new \Exception("Custom Field \"node\" is empty");
  92. }
  93. $virtualization = \json_decode(DB::table('ProxmoxAddon_ProductConfiguration')
  94. ->where('product_id', $hosting->packageid)
  95. ->where('setting', 'virtualization')
  96. ->value('value'));
  97. $vm = proxmox\Factory::vmVps($virtualization, $params);
  98. if (main\App\Models\RecoveryVm::where('service_id', $hosting->id)->count())
  99. {
  100. $recovery = main\App\Models\RecoveryVm::where('service_id', $hosting->id)->first();
  101. }
  102. else
  103. {
  104. $recovery = new main\App\Models\RecoveryVm();
  105. }
  106. $recovery->client_id = $hosting->userid;
  107. $recovery->service_id = $hosting->id;
  108. $recovery->server_id = $this->getServer()->id;
  109. $recovery->vserver_id = 0;
  110. $recovery->last_update = date('Y-m-d H:i:s', strtotime('now'));
  111. $recovery->status = json_encode($vm->status());
  112. $recovery->config = json_encode($vm->config());
  113. $recovery->virtualization = $vm->getVirtualization();
  114. $recovery->vmid = $vm->getVmid();
  115. $recovery->node = $vm->getNode();
  116. try
  117. {
  118. $recovery->dns = json_encode($vm->node()->getDns());
  119. }
  120. catch (\Exception $ex)
  121. {
  122. $recovery->dns = "Error: " . $ex->getMessage();
  123. }
  124. $recovery->save();
  125. }
  126. else
  127. {
  128. if ($params['moduletype'] == "ProxmoxCloudVps")
  129. {
  130. $vservers = main\App\Models\VmModel::ofHostingId($hosting->id);
  131. foreach ($vservers->get() as $vserver)
  132. {
  133. $activeVservers[] = $vserver->id;
  134. $vm = (new proxmox\VmFactory())->fromVmModel($vserver);
  135. if (main\App\Models\RecoveryVm::where('service_id', $hosting->id)->where('vserver_id', $vserver->id)->count())
  136. {
  137. $recovery = main\App\Models\RecoveryVm::where('service_id', $hosting->id)->where('vserver_id', $vserver->id)->first();
  138. }
  139. else
  140. {
  141. $recovery = new main\App\Models\RecoveryVm();
  142. }
  143. $recovery->client_id = $hosting->userid;
  144. $recovery->service_id = $hosting->id;
  145. $recovery->server_id = $this->getServer()->id;
  146. $recovery->vserver_id = $vserver->id;
  147. $recovery->last_update = date('Y-m-d H:i:s', strtotime('now'));
  148. $recovery->status = json_encode($vm->status());
  149. $recovery->config = json_encode($vm->config());
  150. $recovery->virtualization = $vm->getVirtualization();
  151. $recovery->vmid = $vm->getVmid();
  152. $recovery->node = $vm->getNode();
  153. try
  154. {
  155. $recovery->dns = json_encode($vm->node()->getDns());
  156. }
  157. catch (\Exception $ex)
  158. {
  159. $recovery->dns = "Error: " . $ex->getMessage();
  160. }
  161. $recovery->save();
  162. }
  163. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  164. }
  165. }
  166. }
  167. catch (\Exception $ex)
  168. {
  169. if ($hosting)
  170. {
  171. $io->error("Hosting Id #{$hosting->id}, " . $ex->getMessage());
  172. }
  173. else
  174. {
  175. $io->error($ex->getMessage());
  176. }
  177. }
  178. (new Hypervisor($this->getName(), $input->getOptions()))
  179. ->ping();
  180. }
  181. //Remove entries which does not exist on $activeHostings
  182. if (!empty($activeHostings))
  183. {
  184. main\App\Models\RecoveryVm::whereNotIn('service_id', $activeHostings)->delete();
  185. }
  186. //Remove entries which does not exist on $activeVservers
  187. if (!empty($activeVservers))
  188. {
  189. $activeVservers[] = '0';
  190. main\App\Models\RecoveryVm::whereNotIn('vserver_id', $activeVservers)->delete();
  191. }
  192. $output->writeln("");
  193. $io->success([
  194. sprintf("Synchronize recovery list: %s Entries Processed.", $i),
  195. "Synchronize recovery list: Done"
  196. ]);
  197. }
  198. }