ProductProvider.php 25 KB

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