DefaultPatch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Configuration\Addon\Update;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  5. use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
  6. use ModulesGarden\ProxmoxAddon\App\Models\User;
  7. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  8. use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
  9. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  10. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  11. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\EmailTemplate;
  12. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  13. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Product;
  14. use ModulesGarden\ProxmoxAddon\Core\Configuration\Addon\Update\Patch\AbstractPatch;
  15. class DefaultPatch extends AbstractPatch
  16. {
  17. public function up()
  18. {
  19. //install email templates
  20. $this->templates();
  21. }
  22. protected function templates()
  23. {
  24. //Service Creation Failed
  25. if (EmailTemplate::ofAdmin()->ofName('Service Creation Failed')->count() == 0)
  26. {
  27. $entity = new EmailTemplate();
  28. $entity->fill(
  29. [
  30. "type" => "admin",
  31. "name" => "Service Creation Failed",
  32. "subject" => "Service Creation Failed",
  33. "custom" => 1,
  34. "message" => "<p>This product/service automatic provisioning has failed and requires you to manually check & resolve.</p>\n"
  35. . "<p>Client ID: {\$client_id}<br />Service ID: {\$service_id}<br />Product/Service: {\$service_product}<br />Domain: {\$service_domain}<br />Error: {\$error_msg}</p>\n"
  36. . "<p>{\$whmcs_admin_link}</p>\n"
  37. ]
  38. )->save();
  39. }
  40. //Manual Upgrade Request
  41. if (EmailTemplate::ofAdmin()->ofName('Manual Upgrade Request')->count() == 0)
  42. {
  43. $entity = new EmailTemplate();
  44. $entity->fill(
  45. [
  46. "type" => "admin",
  47. "name" => "Manual Upgrade Request",
  48. "subject" => "Manual Upgrade Request",
  49. "custom" => 1,
  50. "message" => "<p>An upgrade order has received its payment, automatic upgrades has been disabled and requires manually processing.</p>\n"
  51. . "<p>Client ID: {\$client_id}<br />Service ID: {\$service_id}<br />Product/Service: {\$service_product}<br />Domain: {\$service_domain}</p>\n"
  52. . "<p>{\$whmcs_admin_link}</p>\n"
  53. ]
  54. )->save();
  55. }
  56. }
  57. public function vmIpAddresses()
  58. {
  59. try
  60. {
  61. $query = DB::table("proxmoxVPS_IP");
  62. foreach ($query->get() as $entery)
  63. {
  64. if (VmIpAddress::ofHostingId($entery->hid)->ofIp($entery->ip)->count()) {
  65. continue;
  66. }
  67. $entity = new VmIpAddress();
  68. $entery->hosting_id = $entery->hid;
  69. $entity->fill((array)$entery)
  70. ->save();
  71. }
  72. }
  73. catch (\Exception $ex)
  74. {
  75. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  76. {
  77. throw $ex;
  78. }
  79. }
  80. }
  81. public function keyPairs()
  82. {
  83. try
  84. {
  85. $query = DB::table("proxmoxvps_key_pairs");
  86. foreach ($query->get() as $entery)
  87. {
  88. if(KeyPair::ofHostingId($entery->hosting_id)->count()){
  89. continue;
  90. }
  91. $entity = new KeyPair();
  92. $entity->fill((array)$entery)
  93. ->save();
  94. }
  95. }
  96. catch (\Exception $ex)
  97. {
  98. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  99. {
  100. throw $ex;
  101. }
  102. }
  103. }
  104. public function users()
  105. {
  106. try
  107. {
  108. $query = DB::table("proxmoxVPS_Users");
  109. foreach ($query->get() as $entery)
  110. {
  111. if(User::ofHostingId($entery->hosting_id)->where("username", $entery->username )->count()){
  112. continue;
  113. }
  114. $entity = new User();
  115. $entity->fill((array)$entery)
  116. ->save();
  117. }
  118. }
  119. catch (\Exception $ex)
  120. {
  121. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  122. {
  123. throw $ex;
  124. }
  125. }
  126. }
  127. /**
  128. * Import VMs from Proxmox Cloud
  129. * @throws \Exception
  130. */
  131. public function vms()
  132. {
  133. try
  134. {
  135. $query = DB::table("proxmoxcloud_vservers");
  136. $query->where('hosting_id', 8053);
  137. foreach ($query->get() as $entery)
  138. {
  139. if(VmModel::ofHostingId($entery->hosting_id)->ofVmid($entery->vmid)->count()){
  140. continue;
  141. }
  142. if(!Hosting::ofId($entery->hosting_id)->activeAndSuspended()->count()){
  143. continue;
  144. }
  145. $vm = new VmModel;
  146. $vm->fill((array)$entery);
  147. $vm->name = $entery->hostname;
  148. $vm->disk = $entery->storage_usage;
  149. $vm->disks = (int) $entery->hdds;
  150. $vm->virtualization = $entery->type;
  151. $vm->memory = $entery->memory_usage;
  152. $vm->sockets = $entery->sockets_usage;
  153. $vm->cores = $entery->cpu_cores_usage;
  154. $vm->cpuunits = $entery->cpu_weigh_usage;
  155. $vm->swap = $entery->swap_usage;
  156. $vm->cpulimit = $entery->cpu_limit;
  157. $vm->virtualization = str_replace('kvm','qemu',$entery->type);
  158. $vm->save();
  159. //ips
  160. $this->vmIps($vm, $entery);
  161. $this->vmPrivateInterfaces($vm, $entery);
  162. //keys
  163. $this->vmKeyPairs($vm, $entery);
  164. }
  165. }
  166. catch (\Exception $ex)
  167. {
  168. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  169. {
  170. throw $ex;
  171. }
  172. }
  173. }
  174. /**
  175. * @throws \Exception
  176. * Import ip addresses from proxmox cloud
  177. */
  178. public function vmIps(VmModel $vmModel, $vserver)
  179. {
  180. try
  181. {
  182. $query = DB::table("proxmoxcloud_ips")->where('vid', $vserver->id);
  183. foreach ($query->get() as $entery)
  184. {
  185. if (VmIpAddress::ofHostingId($vmModel->hosting_id)->ofIp($entery->ip)->count()) {
  186. continue;
  187. }
  188. //public ip only
  189. if(!filter_var($entery->ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)){
  190. continue;
  191. }
  192. $entity = new VmIpAddress();
  193. $entery->hosting_id = $vmModel->hosting_id;
  194. $entery->server_id = $entery->sid;
  195. $entery->vm_id = $vmModel->id;
  196. $entity->fill((array)$entery)
  197. ->save();
  198. //VI
  199. $vi = new VirtualInterface();
  200. $vi->vn_id = 0;
  201. $vi->ip = $entity->ip;
  202. if(preg_match('/\./', $entity->ip)){
  203. $vi->ip_long = ip2long($entity->ip);
  204. }
  205. $vi->vm_id = $vmModel->id;
  206. $vi->hosting_id = $vmModel->hosting_id;
  207. $vi->net = (string)$entity->net;
  208. $vi->save();
  209. }
  210. }
  211. catch (\Exception $ex)
  212. {
  213. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  214. {
  215. throw $ex;
  216. }
  217. }
  218. }
  219. public function vmKeyPairs(VmModel $vmModel, $vserver)
  220. {
  221. try
  222. {
  223. $query = DB::table("proxmoxcloud_key_pairs")->where('vserver_id', $vserver->id);
  224. foreach ($query->get() as $entery)
  225. {
  226. if(KeyPair::ofHostingId($entery->hosting_id)->where('vm_id',$vmModel->id)->count()){
  227. continue;
  228. }
  229. $entity = new KeyPair();
  230. $entery->vm_id = $vmModel->id;
  231. $entity->fill((array)$entery)
  232. ->save();
  233. }
  234. }
  235. catch (\Exception $ex)
  236. {
  237. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  238. {
  239. throw $ex;
  240. }
  241. }
  242. }
  243. public function productCloud()
  244. {
  245. try
  246. {
  247. $productConveter = new ProductCloudConveter();
  248. $query = DB::table("proxmoxCloud_prodConfig");
  249. foreach ($query->get() as $row)
  250. {
  251. if(!Product::where('id',$row->product_id )->count()){
  252. continue;
  253. }
  254. $setting = $productConveter->convert($row->setting, $row->value, $row->product_id);
  255. if (is_null($setting))
  256. {
  257. continue;
  258. }
  259. if ($productConveter->exist($setting))
  260. {
  261. continue;
  262. }
  263. $setting->save();
  264. }
  265. }
  266. catch (\Exception $ex)
  267. {
  268. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  269. {
  270. throw $ex;
  271. }
  272. }
  273. }
  274. protected function usersCloud(){
  275. try {
  276. foreach (Hosting::ofProxmoxCloudAndStatusActiveAndSuspended()->get() as $hosting) {
  277. if (User::ofHostingId($hosting->id)->count()) {
  278. continue;
  279. }
  280. $realm = $query = DB::table("proxmoxCloud_prodConfig")
  281. ->where('product_id', $hosting->packageid)
  282. ->where('setting','realm')
  283. ->value('value');
  284. $entity = new User();
  285. $entity->user_id = $hosting->userid;
  286. $entity->hosting_id = $hosting->id;
  287. $entity->username = $hosting->username;
  288. $entity->password = $hosting->password;
  289. $entity->realm = $realm ? $realm : 'pve';
  290. $entity->save();
  291. }
  292. }catch (\Exception $ex)
  293. {
  294. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  295. {
  296. throw $ex;
  297. }
  298. }
  299. }
  300. public function vmPrivateInterfaces(VmModel $vmModel, $vserver)
  301. {
  302. try
  303. {
  304. $query = DB::table("proxmoxcloud_private_interfaces")->where('vserver_id', $vserver->id);
  305. foreach ($query->get() as $entery)
  306. {
  307. if (VirtualInterface::ofHostingId($vmModel->hosting_id)->ofIp($entery->ip)->ofVmId($vmModel->id)->count()) {
  308. continue;
  309. }
  310. $network = DB::table("proxmoxcloud_private_networks")->where('id', $entery->network_id)->first();
  311. $ip = IpAddress::find($entery->ip_id);
  312. if(VirtualNetwork::ofHostingId( $vmModel->hosting_id)->ofTag($network->vlan_tag)->count() ==0){
  313. $vn = new VirtualNetwork;
  314. $vn->name = $network->name;
  315. $vn->tag = $network->vlan_tag;
  316. $vn->hosting_id = $network->hosting_id;
  317. $vn->pool = $entery->ip;
  318. $vn->cidr = $ip->cidr;
  319. $vn->gateway = $ip->gateway;
  320. $vn->save();
  321. }else{
  322. $vn = VirtualNetwork::ofHostingId( $vmModel->hosting_id)->ofTag($network->vlan_tag)->first();
  323. }
  324. //VI
  325. $vi = new VirtualInterface();
  326. $vi->vn_id = $vn->id;
  327. $vi->ip = $entery->ip;
  328. if(preg_match('/\./', $entery->ip)){
  329. $vi->ip_long = ip2long( $entery->ip);
  330. }
  331. $vi->vm_id = $vmModel->id;
  332. $vi->hosting_id = $vmModel->hosting_id;
  333. $vi->net = $entery->device;
  334. $vi->save();
  335. $ip->hosting_id = 0 ;
  336. $ip->save();
  337. }
  338. }
  339. catch (\Exception $ex)
  340. {
  341. print_r($ex); die();
  342. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  343. {
  344. throw $ex;
  345. }
  346. }
  347. }
  348. }