DefaultPatch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. foreach ($query->get() as $entery)
  137. {
  138. if(VmModel::ofHostingId($entery->hosting_id)->ofVmid($entery->vmid)->count()){
  139. continue;
  140. }
  141. if(!Hosting::ofId($entery->hosting_id)->activeAndSuspended()->count()){
  142. continue;
  143. }
  144. $vm = new VmModel;
  145. $vm->fill((array)$entery);
  146. $vm->name = $entery->hostname;
  147. $vm->disk = $entery->storage_usage;
  148. $vm->disks = (int) $entery->hdds;
  149. $vm->virtualization = $entery->type;
  150. $vm->memory = $entery->memory_usage;
  151. $vm->sockets = $entery->sockets_usage;
  152. $vm->cores = $entery->cpu_cores_usage;
  153. $vm->cpuunits = $entery->cpu_weigh_usage;
  154. $vm->swap = $entery->swap_usage;
  155. $vm->cpulimit = $entery->cpu_limit;
  156. $vm->virtualization = str_replace('kvm','qemu',$entery->type);
  157. $vm->save();
  158. //ips
  159. $this->vmIps($vm, $entery);
  160. $this->vmPrivateInterfaces($vm, $entery);
  161. //keys
  162. $this->vmKeyPairs($vm, $entery);
  163. }
  164. }
  165. catch (\Exception $ex)
  166. {
  167. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  168. {
  169. throw $ex;
  170. }
  171. }
  172. }
  173. /**
  174. * @throws \Exception
  175. * Import ip addresses from proxmox cloud
  176. */
  177. public function vmIps(VmModel $vmModel, $vserver)
  178. {
  179. try
  180. {
  181. $query = DB::table("proxmoxcloud_ips")->where('vid', $vserver->id);
  182. foreach ($query->get() as $entery)
  183. {
  184. if (VmIpAddress::ofHostingId($vmModel->hosting_id)->ofIp($entery->ip)->count()) {
  185. continue;
  186. }
  187. //public ip only
  188. if(!filter_var($entery->ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)){
  189. continue;
  190. }
  191. $entity = new VmIpAddress();
  192. $entery->hosting_id = $vmModel->hosting_id;
  193. $entery->server_id = $entery->sid;
  194. $entery->vm_id = $vmModel->id;
  195. $entity->fill((array)$entery)
  196. ->save();
  197. //VI
  198. $vi = new VirtualInterface();
  199. $vi->vn_id = 0;
  200. $vi->ip = $entity->ip;
  201. if(preg_match('/\./', $entity->ip)){
  202. $vi->ip_long = ip2long($entity->ip);
  203. }
  204. $vi->vm_id = $vmModel->id;
  205. $vi->hosting_id = $vmModel->hosting_id;
  206. $vi->net = $entity->net;
  207. $vi->save();
  208. }
  209. }
  210. catch (\Exception $ex)
  211. {
  212. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  213. {
  214. throw $ex;
  215. }
  216. }
  217. }
  218. public function vmKeyPairs(VmModel $vmModel, $vserver)
  219. {
  220. try
  221. {
  222. $query = DB::table("proxmoxcloud_key_pairs")->where('vserver_id', $vserver->id);
  223. foreach ($query->get() as $entery)
  224. {
  225. if(KeyPair::ofHostingId($entery->hosting_id)->where('vm_id',$vmModel->id)->count()){
  226. continue;
  227. }
  228. $entity = new KeyPair();
  229. $entery->vm_id = $vmModel->id;
  230. $entity->fill((array)$entery)
  231. ->save();
  232. }
  233. }
  234. catch (\Exception $ex)
  235. {
  236. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  237. {
  238. throw $ex;
  239. }
  240. }
  241. }
  242. public function productCloud()
  243. {
  244. try
  245. {
  246. $productConveter = new ProductCloudConveter();
  247. $query = DB::table("proxmoxCloud_prodConfig");
  248. foreach ($query->get() as $row)
  249. {
  250. if(!Product::where('id',$row->product_id )->count()){
  251. continue;
  252. }
  253. $setting = $productConveter->convert($row->setting, $row->value, $row->product_id);
  254. if (is_null($setting))
  255. {
  256. continue;
  257. }
  258. if ($productConveter->exist($setting))
  259. {
  260. continue;
  261. }
  262. $setting->save();
  263. }
  264. }
  265. catch (\Exception $ex)
  266. {
  267. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  268. {
  269. throw $ex;
  270. }
  271. }
  272. }
  273. protected function usersCloud(){
  274. try {
  275. foreach (Hosting::ofProxmoxCloudAndStatusActiveAndSuspended()->get() as $hosting) {
  276. if (User::ofHostingId($hosting->id)->count()) {
  277. continue;
  278. }
  279. $realm = $query = DB::table("proxmoxCloud_prodConfig")
  280. ->where('product_id', $hosting->packageid)
  281. ->where('setting','realm')
  282. ->value('value');
  283. $entity = new User();
  284. $entity->user_id = $hosting->userid;
  285. $entity->hosting_id = $hosting->id;
  286. $entity->username = $hosting->username;
  287. $entity->password = $hosting->password;
  288. $entity->realm = $realm ? $realm : 'pve';
  289. $entity->save();
  290. }
  291. }catch (\Exception $ex)
  292. {
  293. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  294. {
  295. throw $ex;
  296. }
  297. }
  298. }
  299. public function vmPrivateInterfaces(VmModel $vmModel, $vserver)
  300. {
  301. try
  302. {
  303. $query = DB::table("proxmoxcloud_private_interfaces")->where('vserver_id', $vserver->id);
  304. foreach ($query->get() as $entery)
  305. {
  306. if (VirtualInterface::ofHostingId($vmModel->hosting_id)->ofIp($entery->ip)->ofVmId($vmModel->id)->count()) {
  307. continue;
  308. }
  309. $network = DB::table("proxmoxcloud_private_networks")->where('id', $entery->network_id)->first();
  310. $ip = IpAddress::find($entery->ip_id);
  311. if(VirtualNetwork::ofHostingId( $vmModel->hosting_id)->ofTag($network->vlan_tag)->count() ==0){
  312. $vn = new VirtualNetwork;
  313. $vn->name = $network->name;
  314. $vn->tag = $network->vlan_tag;
  315. $vn->hosting_id = $network->hosting_id;
  316. $vn->pool = $entery->ip;
  317. $vn->cidr = $ip->cidr;
  318. $vn->gateway = $ip->gateway;
  319. $vn->save();
  320. }else{
  321. $vn = VirtualNetwork::ofHostingId( $vmModel->hosting_id)->ofTag($network->vlan_tag)->first();
  322. }
  323. //VI
  324. $vi = new VirtualInterface();
  325. $vi->vn_id = $vn->id;
  326. $vi->ip = $entery->ip;
  327. if(preg_match('/\./', $entery->ip)){
  328. $vi->ip_long = ip2long( $entery->ip);
  329. }
  330. $vi->vm_id = $vmModel->id;
  331. $vi->hosting_id = $vmModel->hosting_id;
  332. $vi->net = $entery->device;
  333. $vi->save();
  334. $ip->hosting_id = 0 ;
  335. $ip->save();
  336. }
  337. }
  338. catch (\Exception $ex)
  339. {
  340. print_r($ex); die();
  341. if (!preg_match("/doesn\'t/", $ex->getMessage()))
  342. {
  343. throw $ex;
  344. }
  345. }
  346. }
  347. }