ProductProvider.php 25 KB

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