ProductProvider.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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", ">", o)->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. //permissionFirewalOptions
  203. $this->availableValues["customconfigoption[permissionFirewalOptions][]"] = [
  204. "enable" => $lang->abtr("Enable/Disable Firewall"),
  205. "dhcp" => $lang->abtr("DHCP"),
  206. "radv" => $lang->abtr("Allow Router Advertisement"),
  207. "ndp" => $lang->abtr("NDP"),
  208. "macfilter" => $lang->abtr("MAC Filter"),
  209. "ipfilter" => $lang->abtr("IP Filter"),
  210. "policy_in" => $lang->abtr("Input Policy"),
  211. "policy_out" => $lang->abtr("Output Policy"),
  212. ];
  213. //buttonSyle
  214. $this->availableValues["customconfigoption[buttonSyle]"] = ["tiles" => $lang->tr('Tiles'), 'buttons' => $lang->tr('Buttons'),];
  215. //detailsView
  216. $this->availableValues["customconfigoption[detailsView]"] = ["standard" => $lang->tr('Standard'),
  217. 'combined' => $lang->tr('Combined'),];
  218. //serverGroup
  219. $this->serverGroupRead();
  220. //firewalOptionPolicyIn
  221. $this->availableValues["customconfigoption[firewalOptionPolicyIn]"] = [
  222. "0" => "",
  223. "DROP" => sl("lang")->abtr("DROP"),
  224. "ACCEPT" => sl("lang")->abtr("ACCEPT"),
  225. "REJECT" => sl("lang")->abtr("REJECT"),
  226. ];
  227. //firewalOptionPolicyOut
  228. $this->availableValues["customconfigoption[firewalOptionPolicyOut]"] = [
  229. "0" => "",
  230. "DROP" => sl("lang")->abtr("DROP"),
  231. "ACCEPT" => sl("lang")->abtr("ACCEPT"),
  232. "REJECT" => sl("lang")->abtr("REJECT"),
  233. ];
  234. //permissionBackupCompress
  235. $this->availableValues["customconfigoption[permissionBackupCompress][]"] = [
  236. "none" => $lang->abtr("None"),
  237. "lzo" => $lang->abtr("LZO (fast)"),
  238. "gzip" => $lang->abtr("GZIP (good)"),
  239. "zstd" => $lang->abtr("ZSTD (fast and good)"),
  240. ];
  241. //permissionSnapshotJobPeriod
  242. $this->availableValues["customconfigoption[permissionSnapshotJobPeriod][]"] = [
  243. JobPeriod::HOURLY => $lang->tr(JobPeriod::HOURLY),
  244. JobPeriod::DAILY => $lang->tr(JobPeriod::DAILY)
  245. ];
  246. }
  247. /**
  248. * KVM
  249. */
  250. private function qemuRead()
  251. {
  252. $lang = sl("lang");
  253. //ostype
  254. $ostype = new Json('ostype.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  255. foreach ($ostype->get() as $k => $ostype) {
  256. $this->availableValues["customconfigoption[ostype]"][$k] = $lang->abtr('ostype', $ostype);
  257. $this->availableValues["customconfigoption[permissionOstype][]"][$k] = $lang->abtr($ostype);
  258. }
  259. //hotplug
  260. $this->availableValues["customconfigoption[hotplug][]"] = ["disk" => $lang->tr('disk'), "network" => $lang->tr('network'), "usb" => $lang->tr('usb'), "cpu" => $lang->tr('cpu'), "memory" => $lang->tr('memory')];
  261. //keyboard
  262. $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')];
  263. //vga
  264. $jsonData = new Json('vga.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  265. foreach ($jsonData->get() as $k => $name) {
  266. $this->availableValues["customconfigoption[vga]"][$k] = $lang->tr($name);
  267. }
  268. //cloneMode
  269. $this->availableValues["customconfigoption[cloneMode]"] = ['1' => $lang->tr('Full Clone'), "0" => $lang->tr("Linked Clone")];
  270. //diskStorage images (Disk Images)
  271. $storageRepository = new StorageRepository();
  272. $storageRepository->findByNodes([$this->node->getNode()])
  273. ->findEnabed();
  274. foreach ($storageRepository->fetch() as $entity) {
  275. if (!in_array("images", $entity->getContentAsArray())) {
  276. continue;
  277. }
  278. $this->availableValues["customconfigoption[diskStorage]"] [$entity->getStorage()] = $lang->tr($entity->getStorage());
  279. $this->availableValues["customconfigoption[additionalDiskStorage]"] [$entity->getStorage()] = $lang->tr($entity->getStorage());
  280. }
  281. //diskType
  282. $this->availableValues["customconfigoption[diskType]"] = ["ide" => $lang->abtr('ide'), "sata" => $lang->abtr('sata'), "virtio" => $lang->abtr('virtio'), "scsi" => $lang->abtr('scsi')];
  283. //diskFormat
  284. $this->availableValues["customconfigoption[diskFormat]"] = ['raw' => $lang->abtr('raw'), 'qcow2' => $lang->abtr('qcow2'), 'vmdk' => $lang->abtr('vmdk')];
  285. //diskCache
  286. $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')];
  287. //scsihw
  288. $this->availableValues["customconfigoption[scsihw]"] = [
  289. '0' => $lang->abtr('Default (LSI 53C895A)'),
  290. "lsi" => $lang->abtr('LSI 53C895A'),
  291. "lsi53c810" => $lang->abtr('LSI 53C810'),
  292. 'virtio-scsi-pci' => $lang->abtr('VirtIO SCSI'),
  293. "virtio-scsi-single" => $lang->abtr('VirtIO SCSI single'),
  294. 'megasas' => $lang->abtr('MegaRAID SAS 8708EM2'),
  295. "pvscsi" => $lang->abtr('VMware PVSCSI'),
  296. ];
  297. //additionalDiskStorage
  298. $this->availableValues["customconfigoption[additionalDiskStorage]"] = $this->availableValues["customconfigoption[diskStorage]"];
  299. //additionalDiskType
  300. $this->availableValues["customconfigoption[additionalDiskType][]"] = $this->availableValues["customconfigoption[diskType]"];
  301. //additionalDiskFormat
  302. $this->availableValues["customconfigoption[additionalDiskFormat][]"] = $this->availableValues["customconfigoption[diskFormat]"];
  303. if ($this->configuration->getAdditionalDiskStorage() && preg_match("/lvm/", $this->configuration->getAdditionalDiskStorage())) {
  304. $this->disabledList["customconfigoption[additionalDiskFormat][]"] = ['qcow2', 'vmdk'];
  305. }
  306. //additionalDiskCache
  307. $this->availableValues["customconfigoption[additionalDiskCache]"] = $this->availableValues["customconfigoption[diskCache]"];
  308. //networkModel
  309. $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')];
  310. //bridge
  311. foreach ($this->api()->get("/nodes/{$this->node->getNode()}/network") as $network) {
  312. if (!in_array($network['type'], ['bridge', 'OVSBridge'])) {
  313. continue;
  314. }
  315. $this->availableValues["customconfigoption[bridge]"] [$network['iface']] = $lang->tr($network['iface']);
  316. }
  317. ksort($this->availableValues["customconfigoption[bridge]"]);
  318. //privateBridge
  319. $this->availableValues["customconfigoption[privateBridge]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[bridge]"];
  320. //networkPrivateModel
  321. $this->availableValues["customconfigoption[networkPrivateModel]"] = $this->availableValues["customconfigoption[networkModel]"];
  322. //bootDevice1
  323. $this->availableValues["customconfigoption[bootDevice1]"] = ['', 'c' => $lang->tr('Hard Disk'), 'd' => $lang->tr('CD-ROM'), 'n' => $lang->tr('Network')];
  324. //bootDevice2
  325. $this->availableValues["customconfigoption[bootDevice2]"] = $this->availableValues["customconfigoption[bootDevice1]"];
  326. //bootDevice3
  327. $this->availableValues["customconfigoption[bootDevice3]"] = $this->availableValues["customconfigoption[bootDevice1]"];
  328. //permissionOsTemplates
  329. $clusterResourcesRepository = new ClusterResourcesRepository();
  330. $clusterResourcesRepository->setApi($this->api());
  331. $clusterResourcesRepository->findKvmTemplate();
  332. foreach ($clusterResourcesRepository->fetch() as $resurce) {
  333. if (preg_match('/^custom[0-9]*/', $resurce->getName())) {
  334. continue;
  335. }
  336. $this->availableValues["customconfigoption[permissionOsTemplates][]"][$resurce->getName()] = $lang->abtr('template', $resurce->getName());
  337. }
  338. //osTemplate
  339. $this->availableValues["customconfigoption[osTemplate]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[permissionOsTemplates][]"];
  340. //permissionIsoImage
  341. $fileRepository = new FileRepository();
  342. $fileRepository->setApi($this->api());
  343. $fileRepository->findIso();
  344. $fileRepository->findByNode($this->node);
  345. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  346. foreach ($fileRepository->fetch() as $file) {
  347. $this->availableValues["customconfigoption[permissionIsoImages][]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  348. $this->availableValues["customconfigoption[permissionSecondaryIsoImages][]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  349. }
  350. //isoImage
  351. $this->availableValues["customconfigoption[isoImage]"] = ["none" => $lang->tr("None")] + (array)$this->availableValues["customconfigoption[permissionIsoImages][]"];
  352. //memoryUnit
  353. $this->availableValues["customconfigoption[memoryUnit]"] = ['mb' => $lang->tr("MB"), "gb" => $lang->tr("GB")];
  354. //storageUnit
  355. $this->availableValues["customconfigoption[storageUnit]"] = [ "gb" => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  356. //cdromType
  357. $this->availableValues["customconfigoption[cdromType]"] = $this->availableValues["customconfigoption[diskType]"];
  358. //bios
  359. $this->availableValues["customconfigoption[bios]"] = [
  360. '0' => "",
  361. 'seabios' => $lang->tr('SeaBIOS'),
  362. 'ovmf' => $lang->tr('OVMF (UEFI)')
  363. ];
  364. //cloudInitScript
  365. $this->availableValues["customconfigoption[cloudInitScript][]"] = CloudInitScript::pluck('name', 'id')->toArray();
  366. //machine
  367. $jsonData = new Json('machine.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  368. foreach ($jsonData->get() as $k => $name) {
  369. $this->availableValues["customconfigoption[machine]"][$k] = $lang->tr($name);
  370. }
  371. //cpu
  372. $jsonData = new Json('cpu.json', ModuleConstants::getFullPathWhmcs('modules', 'addons', 'proxmoxAddon', 'storage', 'app'));
  373. foreach ($jsonData->get() as $k => $name) {
  374. $this->availableValues["customconfigoption[cpu]"][$k] = $lang->tr($name);
  375. }
  376. //archive
  377. $fileRepository->findBackupQemuTemplates();
  378. $fileRepository->findByNodes($this->nodes);
  379. foreach ( $fileRepository->fetch() as $file) {
  380. if($this->vmidExistInWhmcs($file->getVmid())){
  381. continue;
  382. }
  383. $this->availableValues["customconfigoption[archive][]"][$file->getNode().":".$file->getVolid()] = $file->formatVolid();
  384. }
  385. //cloudInitStorage
  386. $this->availableValues["customconfigoption[cloudInitStorage]"] = [0=> ""] + $this->availableValues["customconfigoption[diskStorage]"];
  387. }
  388. /**
  389. * LXC
  390. */
  391. private function lxcRead()
  392. {
  393. $lang = sl("lang");
  394. //storage
  395. $storageRepository = new StorageRepository();
  396. $storageRepository->findByNodes([$this->node->getNode()])
  397. ->findEnabed();
  398. foreach ($storageRepository->fetch() as $storage) {
  399. if (!in_array("rootdir", $storage->getContentAsArray())) {
  400. continue;
  401. }
  402. $this->availableValues["customconfigoption[storage]"][$storage->getStorage()] = $lang->tr($storage->getStorage());
  403. }
  404. //arch
  405. $this->availableValues["customconfigoption[arch]"] = ['0' => "", 'amd64' => $lang->tr("AMD64"), $lang->tr("i386") => $lang->tr("i386")];
  406. //cmode
  407. $this->availableValues["customconfigoption[cmode]"] = ['0' => "", 'shell' => $lang->tr("shell"), "console" => $lang->tr("console"), "tty" => $lang->tr("tty")];
  408. //ostype
  409. $this->availableValues["customconfigoption[ostype]"] = ['0' => "", 'debian' => $lang->tr("Debian"), "ubuntu" => $lang->tr("Ubuntu"), "centos" => $lang->tr("centos"), "archlinux" => $lang->tr("Archlinux")];
  410. //osTemplate
  411. $fileRepository = new FileRepository();
  412. $fileRepository->setApi($this->api());
  413. $fileRepository->findLxcTemplates();
  414. $fileRepository->findByNode($this->node);
  415. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  416. foreach ($fileRepository->fetch() as $file) {
  417. $this->availableValues["customconfigoption[osTemplate]"][$file->getVolid()] = $lang->tr($file->getFriendlyName());
  418. }
  419. //memoryUnit
  420. $this->availableValues["customconfigoption[memoryUnit]"] = ['mb' => $lang->tr("MB"), "gb" => $lang->tr("GB")];
  421. //storageUnit
  422. $this->availableValues["customconfigoption[storageUnit]"] = [ "gb" => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  423. //persimonOsTemplates
  424. $this->availableValues["customconfigoption[permissionOsTemplates][]"] = $this->availableValues["customconfigoption[osTemplate]"];
  425. //mountPointStorage
  426. foreach ($storageRepository->fetch() as $entity) {
  427. if (!in_array("rootdir", $entity->getContentAsArray())) {
  428. continue;
  429. }
  430. $this->availableValues["customconfigoption[mountPointStorage]"][$entity->getStorage()] = $lang->tr($entity->getStorage());
  431. }
  432. //mountPointAcl
  433. $this->availableValues["customconfigoption[mountPointAcl]"] = ["default" => $lang->tr('Default'), "1" => $lang->tr("On"), "0" => $lang->tr("Off")];
  434. //ipv4NetworkMode
  435. $this->availableValues["customconfigoption[ipv4NetworkMode]"] = ["static" => $lang->tr('Static'), "dhcp" => $lang->tr("DHCP")];
  436. //ipv6NetworkMode
  437. $this->availableValues["customconfigoption[ipv6NetworkMode]"] = ["static" => $lang->tr('Static'), "dhcp" => $lang->tr("DHCP"), "slaac" => $lang->tr("SLAAC")];
  438. //swapUnit
  439. $this->availableValues["customconfigoption[swapUnit]"] = ['mb' => $lang->tr("MB"), $lang->tr("gb") => $lang->tr("GB"), "tb" => $lang->tr("TB")];
  440. //bridge
  441. foreach ($this->api()->get("/nodes/{$this->node->getNode()}/network") as $network) {
  442. if (!in_array($network['type'], ['bridge', 'OVSBridge'])) {
  443. continue;
  444. }
  445. $this->availableValues["customconfigoption[bridge]"] [$network['iface']] = $lang->tr($network['iface']);
  446. }
  447. ksort($this->availableValues["customconfigoption[bridge]"]);
  448. //privateBridge
  449. $this->availableValues["customconfigoption[privateBridge]"] = ['0' => ""] + (array)$this->availableValues["customconfigoption[bridge]"];
  450. //archive
  451. $fileRepository->findBackupLxcTemplates();
  452. $fileRepository->findByNodes($this->nodes);
  453. foreach ( $fileRepository->fetch() as $file) {
  454. if($this->vmidExistInWhmcs($file->getVmid())){
  455. continue;
  456. }
  457. $this->availableValues["customconfigoption[archive][]"][$file->getNode().":".$file->getVolid()] = $file->formatVolid();
  458. }
  459. }
  460. public function update()
  461. {
  462. $this->loadRequestObj();
  463. if (empty($this->request->get('customconfigoption'))) {
  464. return;
  465. }
  466. try {
  467. $form = new MainForm();
  468. $switcherFields = $form->getSwitcherFields();
  469. $values = $this->request->get('customconfigoption');
  470. if (!$values['locations']) {
  471. $values['locations'] = [];
  472. }
  473. foreach ($values as $k => $v) {
  474. if (in_array($k, $switcherFields)) {
  475. unset($switcherFields[array_search($k, $switcherFields)]);
  476. }
  477. }
  478. foreach ($switcherFields as $switch) {
  479. $values[$switch] = "off";
  480. }
  481. } catch (\Exception $ex) {
  482. //login to proxmox host failed
  483. }
  484. //delete
  485. $this->configuration->flush();
  486. sleep(1);
  487. //save
  488. $this->configuration->fillAndSave($values);
  489. }
  490. public function delete()
  491. {
  492. $this->configuration->flush();
  493. }
  494. public function replicate($replicateProductId)
  495. {
  496. $_SESSION['ProxmoxCloudVps'] = null;
  497. if (ProductConfiguration::ofProductId($this->productId)->count()) {
  498. return;
  499. }
  500. /**
  501. * @var $entity ProductConfiguration
  502. */
  503. foreach (ProductConfiguration::ofProductId($replicateProductId)->get() as $entity) {
  504. $newEntity = new ProductConfiguration();
  505. $newEntity->setting = $entity->setting;
  506. $newEntity->value = $entity->value;
  507. $newEntity->product_id = $this->productId;
  508. $newEntity->save();
  509. }
  510. }
  511. protected function serverGroupRead(){
  512. $groupId = sl("whmcsParams")->getWhmcsParams()['groupid'];
  513. $sg = (new ServerGroup())->getTable();
  514. $ssg = "tblservergroupsrel";
  515. $query = ServerGroup::select("{$sg}.id", "{$sg}.name")
  516. ->leftJoin($ssg,"{$ssg}.serverid","=","{$sg}.server_id" )
  517. ->where("{$ssg}.groupid", $groupId);
  518. foreach ($query->get() as $row){
  519. $this->availableValues['customconfigoption[serverGroup][]'][$row->id] = $row->name;
  520. }
  521. }
  522. }