ProductProvider.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. /**********************************************************************
  3. * ProxmoxVPS developed. (26.03.19)
  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\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Providers;
  20. use MGProvision\Proxmox\v2\models\Node;
  21. use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
  22. use MGProvision\Proxmox\v2\repository\FileRepository;
  23. use MGProvision\Proxmox\v2\repository\NodeRepository;
  24. use MGProvision\Proxmox\v2\repository\StorageRepository;
  25. use ModulesGarden\ProxmoxAddon\App\Helper\VmidHelper;
  26. use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
  27. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  28. use ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration;
  29. use ModulesGarden\ProxmoxAddon\App\Models\ServerGroup;
  30. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  31. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Product;
  32. use ModulesGarden\ProxmoxAddon\App\Repositories\Cloud\ProductConfigurationRepository;
  33. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  34. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  35. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\JobPeriod;
  36. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\Product\Forms\MainForm;
  37. use ModulesGarden\Servers\ProxmoxCloudVps\Core\FileReader\Reader\Json;
  38. use ModulesGarden\Servers\ProxmoxCloudVps\Core\Models\Whmcs\EmailTemplate;
  39. use ModulesGarden\Servers\ProxmoxCloudVps\Core\ModuleConstants;
  40. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
  41. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  42. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  43. class ProductProvider extends BaseDataProvider implements AdminArea
  44. {
  45. use ApiService;
  46. use ProductService;
  47. use VmidHelper;
  48. private $productId;
  49. /**
  50. * @var ProductConfigurationRepository
  51. */
  52. protected $configuration;
  53. /**
  54. * @var Node
  55. */
  56. private $node;
  57. /**
  58. * ProductConfigurationProvider constructor.
  59. * @param $productId
  60. */
  61. public function __construct($productId)
  62. {
  63. if (!is_numeric($productId)) {
  64. throw new \InvalidArgumentException("The product id must be definded.");
  65. }
  66. $this->configuration = new ProductConfigurationRepository($productId);
  67. $this->productId = $productId;
  68. }
  69. public function isSupportedModule()
  70. {
  71. return Product::where("id", $this->configuration->getProductId())
  72. ->where("servertype", "ProxmoxCloudVps")->count() == 1;
  73. }
  74. public function read()
  75. {
  76. foreach ($this->configuration->all() as $key => $value) {
  77. //multiselect
  78. if (is_array($value)) {
  79. $this->data[sprintf("customconfigoption[%s][]", $key)] = $value;
  80. continue;
  81. }
  82. if (is_null($value)) {
  83. $value = "";
  84. }
  85. $this->data[sprintf("customconfigoption[%s]", $key)] = $value;
  86. }
  87. $this->initApi();
  88. $this->defaultRead();
  89. if (!$this->configuration->getVirtualization() || $this->configuration->isQemu()) {
  90. //kvm
  91. $this->qemuRead();
  92. } else {
  93. //lxc
  94. $this->lxcRead();
  95. }
  96. }
  97. private function initApi()
  98. {
  99. $lang = sl("lang");
  100. $product = Product::where("id", $this->productId)->firstOrFail();
  101. sl("whmcsParams")->setParams($product->getParams());
  102. }
  103. private function defaultRead()
  104. {
  105. $lang = sl("lang");
  106. //Virtualization
  107. $this->availableValues["customconfigoption[virtualization]"] = ["qemu" => $lang->tr("KVM"), "lxc" => $lang->tr("LXC")];
  108. if ($this->getWhmcsParamByKey('packageid') && Hosting::ofProductId($this->getWhmcsParamByKey('packageid'))->activeAndSuspended()->count()) {
  109. if ($this->configuration->isQemu()) {
  110. unset($this->availableValues["customconfigoption[virtualization]"]["lxc"]);
  111. } else {
  112. unset($this->availableValues["customconfigoption[virtualization]"]["qemu"]);
  113. }
  114. }
  115. //Default Node
  116. $this->availableValues["customconfigoption[defaultNode]"] = ["serverNode" => $lang->tr('Server-Node'), "autoNode" => $lang->tr('Auto-Node')];
  117. $nodeRepository = new NodeRepository();
  118. $nodeRepository->setApi($this->api());
  119. $nodeRepository->findOnline(true);
  120. foreach ($nodeRepository->fetch() as $node) {
  121. $this->nodes[] = $node->getNode();
  122. $this->availableValues["customconfigoption[defaultNode]"][$node->getNode()] = $node->getNode();
  123. //locations
  124. $this->availableValues["customconfigoption[locations][]"][$node->getNode()] = $node->getNode();
  125. }
  126. //realm
  127. foreach ($this->api()->get("/access/domains") as $d) {
  128. if (!$d['realm']) {
  129. continue;
  130. }
  131. $this->availableValues["customconfigoption[realm]"][$d['realm']] = $lang->tr($d['comment']);
  132. }
  133. //userRole
  134. foreach ($this->api()->get("/access/roles") as $r) {
  135. if (!$r['roleid']) {
  136. continue;
  137. }
  138. $this->availableValues["customconfigoption[userRole]"][$r['roleid']] = $lang->tr($r['roleid']);
  139. }
  140. //welcomeEmailTemplateId
  141. $this->availableValues["customconfigoption[welcomeEmailTemplateId]"][0] = "";
  142. foreach (EmailTemplate::where('type', "product")->pluck("name", "id")->all() as $key => $value) {
  143. $this->availableValues["customconfigoption[welcomeEmailTemplateId]"][$key] = $value;
  144. }
  145. //reinstallEmailTemplateId
  146. $this->availableValues["customconfigoption[reinstallEmailTemplateId]"][0] = "";
  147. foreach (EmailTemplate::where('type', "product")->pluck("name", "id")->all() as $key => $value) {
  148. $this->availableValues["customconfigoption[reinstallEmailTemplateId]"][$key] = $value;
  149. }
  150. //upgradeNotificationTemplateId
  151. $this->availableValues["customconfigoption[serviceCreationFailedTemplateId]"][0] = "";
  152. foreach (EmailTemplate::where('type', "admin")->where("custom", 1)->pluck("name", "id")->all() as $key => $value) {
  153. $this->availableValues["customconfigoption[serviceCreationFailedTemplateId]"][$key] = $value;
  154. }
  155. //upgradeNotificationTemplateId
  156. $this->availableValues["customconfigoption[upgradeNotificationTemplateId]"] = $this->availableValues["customconfigoption[serviceCreationFailedTemplateId]"];
  157. //loadBalancerOnUpgrade
  158. $this->availableValues["customconfigoption[loadBalancerOnUpgrade]"] = ['0' => $lang->tr('None'), "block" => $lang->tr("Block"), "migrate" => $lang->tr("Migrate")];
  159. //backupStorage
  160. if (in_array($this->configuration->getDefaultNode(), $this->availableValues["customconfigoption[defaultNode]"])) {
  161. $this->node = new Node($this->configuration->getDefaultNode());
  162. } else {//"Auto-Node" or 'Server-Node' or empty
  163. $servePrivateIP = $this->getServerPrivateIpAddress();
  164. $serverIp = $servePrivateIP ? $servePrivateIP : $this->getWhmcsParamByKey('serverip');
  165. $this->node = $nodeRepository->findWithHostOrIp($this->getWhmcsParamByKey('serverhostname'), $serverIp);
  166. }
  167. $this->node->setApi($this->api());
  168. $storageRepository = new StorageRepository();
  169. $storageRepository->findByNodes([$this->node->getNode()])
  170. ->findEnabed();
  171. foreach ($storageRepository->fetch() as $entity) {
  172. if (!in_array("backup", $entity->getContentAsArray())) {
  173. continue;
  174. }
  175. $this->availableValues["customconfigoption[backupStorage]"] [$entity->getStorage()] = $lang->tr($entity->getStorage());
  176. }
  177. //clusterState
  178. $this->availableValues["customconfigoption[clusterState]"] = ['' => "", 'started' => $lang->tr('Started'), 'stopped' => $lang->tr('Stopped'), 'enabled' => $lang->tr('Enabled'), 'disabled' => $lang->tr('Disabled'), 'ignored' => $lang->tr('Ignored')];
  179. //firewallInterfaces
  180. $this->availableValues["customconfigoption[firewallInterfaces][]"] = ["venet" => $lang->tr("venet"), "eth" => $lang->tr("eth\d+")];
  181. //clusterGroup
  182. $this->availableValues["customconfigoption[clusterGroup]"] = ['' => ""];
  183. foreach ($this->api()->get('/cluster/ha/groups') as $g) {
  184. $this->availableValues["customconfigoption[clusterGroup]"][$g['group']] = ucfirst($g['group']);
  185. }
  186. //suspensionAction
  187. $this->availableValues["customconfigoption[suspensionAction]"] = ['0' => $lang->tr('Default'), "stop" => $lang->tr('Stop VM'), 'shutdown' => $lang->tr('Shutdown VM')];
  188. if ($this->configuration()->isQemu()) {
  189. $this->availableValues["customconfigoption[suspensionAction]"]["suspend"] = $lang->tr("Pause VM");
  190. $this->availableValues["customconfigoption[suspensionAction]"]["hibernate"] = $lang->tr("Hibernate VM");
  191. }
  192. //tag
  193. $this->availableValues["customconfigoption[tags][]"];
  194. foreach (IpAddress::select("tag")->whereNotNull("tag")->where("tag", ">", 0)->groupBy("tag")->pluck("tag")->all() as $tag) {
  195. $this->availableValues["customconfigoption[tags][]"][$tag] = $tag;
  196. }
  197. //pool
  198. $this->availableValues["customconfigoption[pool]"] = [""];
  199. foreach ($this->api()->get('/pools') as $pool) {
  200. $this->availableValues["customconfigoption[pool]"][$pool['poolid']] = $lang->tr($pool['poolid']);
  201. }
  202. //permissionSnapshotJobPeriod
  203. $this->availableValues["customconfigoption[permissionSnapshotJobPeriod][]"] = [
  204. JobPeriod::HOURLY => $lang->tr(JobPeriod::HOURLY),
  205. JobPeriod::DAILY => $lang->tr(JobPeriod::DAILY)
  206. ];
  207. //permissionFirewalOptions
  208. $this->availableValues["customconfigoption[permissionFirewalOptions][]"] = [
  209. "enable" => $lang->abtr("Enable/Disable Firewall"),
  210. "dhcp" => $lang->abtr("DHCP"),
  211. "radv" => $lang->abtr("Allow Router Advertisement"),
  212. "ndp" => $lang->abtr("NDP"),
  213. "macfilter" => $lang->abtr("MAC Filter"),
  214. "ipfilter" => $lang->abtr("IP Filter"),
  215. "policy_in" => $lang->abtr("Input Policy"),
  216. "policy_out" => $lang->abtr("Output Policy"),
  217. ];
  218. //buttonSyle
  219. $this->availableValues["customconfigoption[buttonSyle]"] = ["tiles" => $lang->tr('Tiles'), 'buttons' => $lang->tr('Buttons'),];
  220. //detailsView
  221. $this->availableValues["customconfigoption[detailsView]"] = ["standard" => $lang->tr('Standard'),
  222. 'combined' => $lang->tr('Combined'),];
  223. //serverGroup
  224. $this->serverGroupRead();
  225. //firewalOptionPolicyIn
  226. $this->availableValues["customconfigoption[firewalOptionPolicyIn]"] = [
  227. "0" => "",
  228. "DROP" => sl("lang")->abtr("DROP"),
  229. "ACCEPT" => sl("lang")->abtr("ACCEPT"),
  230. "REJECT" => sl("lang")->abtr("REJECT"),
  231. ];
  232. //firewalOptionPolicyOut
  233. $this->availableValues["customconfigoption[firewalOptionPolicyOut]"] = [
  234. "0" => "",
  235. "DROP" => sl("lang")->abtr("DROP"),
  236. "ACCEPT" => sl("lang")->abtr("ACCEPT"),
  237. "REJECT" => sl("lang")->abtr("REJECT"),
  238. ];
  239. //permissionBackupCompress
  240. $this->availableValues["customconfigoption[permissionBackupCompress][]"] = [
  241. "none" => $lang->abtr("None"),
  242. "lzo" => $lang->abtr("LZO (fast)"),
  243. "gzip" => $lang->abtr("GZIP (good)"),
  244. "zstd" => $lang->abtr("ZSTD (fast and good)"),
  245. ];
  246. //permissionSnapshotJobPeriod
  247. $this->availableValues["customconfigoption[permissionSnapshotJobPeriod][]"] = [
  248. JobPeriod::HOURLY => $lang->tr(JobPeriod::HOURLY),
  249. JobPeriod::DAILY => $lang->tr(JobPeriod::DAILY)
  250. ];
  251. }
  252. /**
  253. * KVM
  254. */
  255. private function qemuRead()
  256. {
  257. $lang = sl("lang");
  258. //ostype
  259. $ostype = new Json('ostype.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  260. foreach ($ostype->get() as $k => $ostype) {
  261. $this->availableValues["customconfigoption[ostype]"][$k] = $lang->abtr('ostype', $ostype);
  262. $this->availableValues["customconfigoption[permissionOstype][]"][$k] = $lang->abtr($ostype);
  263. }
  264. //hotplug
  265. $this->availableValues["customconfigoption[hotplug][]"] = ["disk" => $lang->tr('disk'), "network" => $lang->tr('network'), "usb" => $lang->tr('usb'), "cpu" => $lang->tr('cpu'), "memory" => $lang->tr('memory')];
  266. //keyboard
  267. $this->availableValues["customconfigoption[keyboard]"] = ['', 'pt' => $lang->tr('pt'), "tr" => $lang->tr('tr'), 'ja' => $lang->tr('ja'), 'es' => $lang->tr('es'), 'no' => $lang->tr('no'), 'is' => $lang->tr('is'), 'fr-ca' => $lang->tr('fr-ca'), 'fr' => $lang->tr('fr'), 'pt-br' => $lang->tr('pt-br'), 'da' => $lang->tr('da'), 'fr-ch' => $lang->tr('fr-ch'), 'sl' => $lang->tr('sl'), 'de-ch' => $lang->tr('de-ch'), 'en-gb' => $lang->tr('en-gb'), 'it' => $lang->tr('it'), 'en-us' => $lang->tr('en-us'), 'fr-be' => $lang->tr('fr-be'), 'hu' => $lang->tr('hu'), 'pl' => $lang->tr('pl'), 'nl' => $lang->tr('nl'), 'mk' => $lang->tr('mk'), 'fi' => $lang->tr('fi'), 'lt' => $lang->tr('lt'), 'sv' => $lang->tr('sv'), 'de' => $lang->tr('de')];
  268. //vga
  269. $jsonData = new Json('vga.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  270. foreach ($jsonData->get() as $k => $name) {
  271. $this->availableValues["customconfigoption[vga]"][$k] = $lang->tr($name);
  272. }
  273. //cloneMode
  274. $this->availableValues["customconfigoption[cloneMode]"] = ['1' => $lang->tr('Full Clone'), "0" => $lang->tr("Linked Clone")];
  275. //diskStorage images (Disk Images)
  276. $storageRepository = new StorageRepository();
  277. $storageRepository->findByNodes([$this->node->getNode()])
  278. ->findEnabed();
  279. foreach ($storageRepository->fetch() as $entity) {
  280. if (!in_array("images", $entity->getContentAsArray())) {
  281. continue;
  282. }
  283. $this->availableValues["customconfigoption[diskStorage]"] [$entity->getStorage()] = $lang->tr($entity->getStorage());
  284. $this->availableValues["customconfigoption[additionalDiskStorage]"] [$entity->getStorage()] = $lang->tr($entity->getStorage());
  285. }
  286. //diskType
  287. $this->availableValues["customconfigoption[diskType]"] = ["ide" => $lang->abtr('ide'), "sata" => $lang->abtr('sata'), "virtio" => $lang->abtr('virtio'), "scsi" => $lang->abtr('scsi')];
  288. //diskFormat
  289. $this->availableValues["customconfigoption[diskFormat]"] = ['raw' => $lang->abtr('raw'), 'qcow2' => $lang->abtr('qcow2'), 'vmdk' => $lang->abtr('vmdk')];
  290. //diskCache
  291. $this->availableValues["customconfigoption[diskCache]"] = ['none' => $lang->tr('Default (No Cache)'), 'writethrough' => $lang->tr('Write Through'), 'writeback' => $lang->tr('Write Back'), 'unsafe' => $lang->tr('Write Back (Unsafe)'), 'directsync' => $lang->tr('Direct Sync')];
  292. //scsihw
  293. $this->availableValues["customconfigoption[scsihw]"] = [
  294. '0' => $lang->abtr('Default (LSI 53C895A)'),
  295. "lsi" => $lang->abtr('LSI 53C895A'),
  296. "lsi53c810" => $lang->abtr('LSI 53C810'),
  297. 'virtio-scsi-pci' => $lang->abtr('VirtIO SCSI'),
  298. "virtio-scsi-single" => $lang->abtr('VirtIO SCSI single'),
  299. 'megasas' => $lang->abtr('MegaRAID SAS 8708EM2'),
  300. "pvscsi" => $lang->abtr('VMware PVSCSI'),
  301. ];
  302. //additionalDiskStorage
  303. $this->availableValues["customconfigoption[additionalDiskStorage]"] = $this->availableValues["customconfigoption[diskStorage]"];
  304. //additionalDiskType
  305. $this->availableValues["customconfigoption[additionalDiskType][]"] = $this->availableValues["customconfigoption[diskType]"];
  306. //additionalDiskFormat
  307. $this->availableValues["customconfigoption[additionalDiskFormat][]"] = $this->availableValues["customconfigoption[diskFormat]"];
  308. if ($this->configuration->getAdditionalDiskStorage() && preg_match("/lvm/", $this->configuration->getAdditionalDiskStorage())) {
  309. $this->disabledList["customconfigoption[additionalDiskFormat][]"] = ['qcow2', 'vmdk'];
  310. }
  311. //additionalDiskCache
  312. $this->availableValues["customconfigoption[additionalDiskCache]"] = $this->availableValues["customconfigoption[diskCache]"];
  313. //networkModel
  314. $this->availableValues["customconfigoption[networkModel]"] = ['e1000' => $lang->tr('e1000'), 'i82551' => $lang->tr('i82551'), 'i82557b' => $lang->tr('i82557b'), 'i82559er' => $lang->tr('i82559er'), 'ne2k_isa' => $lang->tr('ne2k_isa'), 'ne2k_pci' => $lang->tr('ne2k_pci'), 'pcnet' => $lang->tr('pcnet'), 'rtl8139' => $lang->tr('rtl8139'), 'virtio' => $lang->tr('virtio'), 'vmxnet3' => $lang->tr('vmxnet3')];
  315. //bridge
  316. foreach ($this->api()->get("/nodes/{$this->node->getNode()}/network") as $network) {
  317. if (!in_array($network['type'], ['bridge', 'OVSBridge'])) {
  318. continue;
  319. }
  320. $this->availableValues["customconfigoption[bridge]"] [$network['iface']] = $lang->tr($network['iface']);
  321. }
  322. ksort($this->availableValues["customconfigoption[bridge]"]);
  323. //privateBridge
  324. $this->availableValues["customconfigoption[privateBridge]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[bridge]"];
  325. //networkPrivateModel
  326. $this->availableValues["customconfigoption[networkPrivateModel]"] = $this->availableValues["customconfigoption[networkModel]"];
  327. //bootDevice1
  328. $this->availableValues["customconfigoption[bootDevice1]"] = ['', 'c' => $lang->tr('Hard Disk'), 'd' => $lang->tr('CD-ROM'), 'n' => $lang->tr('Network')];
  329. //bootDevice2
  330. $this->availableValues["customconfigoption[bootDevice2]"] = $this->availableValues["customconfigoption[bootDevice1]"];
  331. //bootDevice3
  332. $this->availableValues["customconfigoption[bootDevice3]"] = $this->availableValues["customconfigoption[bootDevice1]"];
  333. //permissionOsTemplates
  334. $clusterResourcesRepository = new ClusterResourcesRepository();
  335. $clusterResourcesRepository->setApi($this->api());
  336. $clusterResourcesRepository->findKvmTemplate();
  337. foreach ($clusterResourcesRepository->fetch() as $resurce) {
  338. if (preg_match('/^custom[0-9]*/', $resurce->getName())) {
  339. continue;
  340. }
  341. $this->availableValues["customconfigoption[permissionOsTemplates][]"][$resurce->getName()] = $lang->abtr('template', $resurce->getName());
  342. }
  343. //osTemplate
  344. $this->availableValues["customconfigoption[osTemplate]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[permissionOsTemplates][]"];
  345. //permissionIsoImage
  346. $fileRepository = new FileRepository();
  347. $fileRepository->setApi($this->api());
  348. $fileRepository->findIso();
  349. $fileRepository->findByNode($this->node);
  350. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  351. foreach ($fileRepository->fetch() as $file) {
  352. $this->availableValues["customconfigoption[permissionIsoImages][]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  353. $this->availableValues["customconfigoption[permissionSecondaryIsoImages][]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  354. }
  355. //isoImage
  356. $this->availableValues["customconfigoption[isoImage]"] = ["none" => $lang->tr("None")] + (array)$this->availableValues["customconfigoption[permissionIsoImages][]"];
  357. //memoryUnit
  358. $this->availableValues["customconfigoption[memoryUnit]"] = ['mb' => $lang->tr("MB"), "gb" => $lang->tr("GB")];
  359. //storageUnit
  360. $this->availableValues["customconfigoption[storageUnit]"] = [ "gb" => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  361. //cdromType
  362. $this->availableValues["customconfigoption[cdromType]"] = $this->availableValues["customconfigoption[diskType]"];
  363. //bios
  364. $this->availableValues["customconfigoption[bios]"] = [
  365. '0' => "",
  366. 'seabios' => $lang->tr('SeaBIOS'),
  367. 'ovmf' => $lang->tr('OVMF (UEFI)')
  368. ];
  369. //cloudInitScript
  370. $this->availableValues["customconfigoption[cloudInitScript][]"] = CloudInitScript::pluck('name', 'id')->toArray();
  371. //machine
  372. $jsonData = new Json('machine.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  373. foreach ($jsonData->get() as $k => $name) {
  374. $this->availableValues["customconfigoption[machine]"][$k] = $lang->tr($name);
  375. }
  376. //cpu
  377. $jsonData = new Json('cpu.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  378. foreach ($jsonData->get() as $k => $name) {
  379. $this->availableValues["customconfigoption[cpu]"][$k] = $lang->tr($name);
  380. }
  381. //archive
  382. $fileRepository->findBackupQemuTemplates();
  383. $fileRepository->findByNodes($this->nodes);
  384. foreach ( $fileRepository->fetch() as $file) {
  385. if($this->vmidExistInWhmcs($file->getVmid())){
  386. continue;
  387. }
  388. $this->availableValues["customconfigoption[archive][]"][$file->getNode().":".$file->getVolid()] = $file->formatVolid();
  389. }
  390. //cloudInitStorage
  391. $this->availableValues["customconfigoption[cloudInitStorage]"] = [0=> ""] + $this->availableValues["customconfigoption[diskStorage]"];
  392. }
  393. /**
  394. * LXC
  395. */
  396. private function lxcRead()
  397. {
  398. $lang = sl("lang");
  399. //storage
  400. $storageRepository = new StorageRepository();
  401. $storageRepository->findByNodes([$this->node->getNode()])
  402. ->findEnabed();
  403. foreach ($storageRepository->fetch() as $storage) {
  404. if (!in_array("rootdir", $storage->getContentAsArray())) {
  405. continue;
  406. }
  407. $this->availableValues["customconfigoption[storage]"][$storage->getStorage()] = $lang->tr($storage->getStorage());
  408. }
  409. //arch
  410. $this->availableValues["customconfigoption[arch]"] = ['0' => "", 'amd64' => $lang->tr("AMD64"), $lang->tr("i386") => $lang->tr("i386")];
  411. //cmode
  412. $this->availableValues["customconfigoption[cmode]"] = ['0' => "", 'shell' => $lang->tr("shell"), "console" => $lang->tr("console"), "tty" => $lang->tr("tty")];
  413. //ostype
  414. $this->availableValues["customconfigoption[ostype]"] = ['0' => "", 'debian' => $lang->tr("Debian"), "ubuntu" => $lang->tr("Ubuntu"), "centos" => $lang->tr("centos"), "archlinux" => $lang->tr("Archlinux")];
  415. //osTemplate
  416. $fileRepository = new FileRepository();
  417. $fileRepository->setApi($this->api());
  418. $fileRepository->findLxcTemplates();
  419. $fileRepository->findByNode($this->node);
  420. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  421. foreach ($fileRepository->fetch() as $file) {
  422. $this->availableValues["customconfigoption[osTemplate]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  423. }
  424. //memoryUnit
  425. $this->availableValues["customconfigoption[memoryUnit]"] = ['mb' => $lang->tr("MB"), "gb" => $lang->tr("GB")];
  426. //storageUnit
  427. $this->availableValues["customconfigoption[storageUnit]"] = [ "gb" => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  428. //persimonOsTemplates
  429. $this->availableValues["customconfigoption[permissionOsTemplates][]"] = $this->availableValues["customconfigoption[osTemplate]"];
  430. //mountPointStorage
  431. foreach ($storageRepository->fetch() as $entity) {
  432. if (!in_array("rootdir", $entity->getContentAsArray())) {
  433. continue;
  434. }
  435. $this->availableValues["customconfigoption[mountPointStorage]"][$entity->getStorage()] = $lang->tr($entity->getStorage());
  436. }
  437. //mountPointAcl
  438. $this->availableValues["customconfigoption[mountPointAcl]"] = ["default" => $lang->tr('Default'), "1" => $lang->tr("On"), "0" => $lang->tr("Off")];
  439. //ipv4NetworkMode
  440. $this->availableValues["customconfigoption[ipv4NetworkMode]"] = ["static" => $lang->tr('Static'), "dhcp" => $lang->tr("DHCP")];
  441. //ipv6NetworkMode
  442. $this->availableValues["customconfigoption[ipv6NetworkMode]"] = ["static" => $lang->tr('Static'), "dhcp" => $lang->tr("DHCP"), "slaac" => $lang->tr("SLAAC")];
  443. //swapUnit
  444. $this->availableValues["customconfigoption[swapUnit]"] = ['mb' => $lang->tr("MB"), $lang->tr("gb") => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  445. //bridge
  446. foreach ($this->api()->get("/nodes/{$this->node->getNode()}/network") as $network) {
  447. if (!in_array($network['type'], ['bridge', 'OVSBridge'])) {
  448. continue;
  449. }
  450. $this->availableValues["customconfigoption[bridge]"] [$network['iface']] = $lang->tr($network['iface']);
  451. }
  452. ksort($this->availableValues["customconfigoption[bridge]"]);
  453. //privateBridge
  454. $this->availableValues["customconfigoption[privateBridge]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[bridge]"];
  455. //archive
  456. $fileRepository->findBackupLxcTemplates();
  457. $fileRepository->findByNodes($this->nodes);
  458. foreach ( $fileRepository->fetch() as $file) {
  459. if($this->vmidExistInWhmcs($file->getVmid())){
  460. continue;
  461. }
  462. $this->availableValues["customconfigoption[archive][]"][$file->getNode().":".$file->getVolid()] = $file->formatVolid();
  463. }
  464. }
  465. public function update()
  466. {
  467. $this->loadRequestObj();
  468. if (empty($this->request->get('customconfigoption'))) {
  469. return;
  470. }
  471. try {
  472. $form = new MainForm();
  473. $switcherFields = $form->getSwitcherFields();
  474. $values = $this->request->get('customconfigoption');
  475. if (!$values['locations']) {
  476. $values['locations'] = [];
  477. }
  478. foreach ($values as $k => $v) {
  479. if (in_array($k, $switcherFields)) {
  480. unset($switcherFields[array_search($k, $switcherFields)]);
  481. }
  482. }
  483. foreach ($switcherFields as $switch) {
  484. $values[$switch] = "off";
  485. }
  486. } catch (\Exception $ex) {
  487. //login to proxmox host failed
  488. }
  489. //delete
  490. $this->configuration->flush();
  491. sleep(1);
  492. //save
  493. $this->configuration->fillAndSave((array)$values);
  494. }
  495. public function delete()
  496. {
  497. $this->configuration->flush();
  498. }
  499. public function replicate($replicateProductId)
  500. {
  501. $_SESSION['ProxmoxCloudVps'] = null;
  502. if (ProductConfiguration::ofProductId($this->productId)->count()) {
  503. return;
  504. }
  505. /**
  506. * @var $entity ProductConfiguration
  507. */
  508. foreach (ProductConfiguration::ofProductId($replicateProductId)->get() as $entity) {
  509. $newEntity = new ProductConfiguration();
  510. $newEntity->setting = $entity->setting;
  511. $newEntity->value = $entity->value;
  512. $newEntity->product_id = $this->productId;
  513. $newEntity->save();
  514. }
  515. }
  516. protected function serverGroupRead(){
  517. $groupId = sl("whmcsParams")->getWhmcsParams()['groupid'];
  518. $sg = (new ServerGroup())->getTable();
  519. $ssg = "tblservergroupsrel";
  520. $query = ServerGroup::select("{$sg}.id", "{$sg}.name")
  521. ->leftJoin($ssg,"{$ssg}.serverid","=","{$sg}.server_id" )
  522. ->where("{$ssg}.groupid", $groupId);
  523. foreach ($query->get() as $row){
  524. $this->availableValues['customconfigoption[serverGroup][]'][$row->id] = $row->name;
  525. }
  526. }
  527. }