ProductProvider.php 28 KB

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