ProductProvider.php 25 KB

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