-
Notifications
You must be signed in to change notification settings - Fork 17
/
proxmox.php
2919 lines (2580 loc) · 109 KB
/
proxmox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Blesta\Core\Util\Validate\Server;
/**
* Proxmox Module
*
* @package blesta
* @subpackage blesta.components.modules.proxmox
* @author Phillips Data, Inc.
* @author Mark Dietzer
* @copyright Copyright (c) 2013, Phillips Data, Inc.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.blesta.com/ Blesta
* @link http://www.fullambit.net/
*/
class Proxmox extends Module
{
/**
* Initializes the module
*/
public function __construct()
{
// Load configuration required by this module
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
// Load components required by this module
Loader::loadComponents($this, ['Input']);
// Load the language required by this module
Language::loadLang('proxmox', null, dirname(__FILE__) . DS . 'language' . DS);
}
/**
* Performs migration of data from $current_version (the current installed version)
* to the given file set version. Sets Input errors on failure, preventing
* the module from being upgraded.
*
* @param string $current_version The current installed version of this module
*/
public function upgrade($current_version)
{
if (version_compare($current_version, '3.0.0', '<')) {
if (!isset($this->ModuleManager)) {
Loader::loadModels($this, ['ModuleManager']);
}
if (!isset($this->Packages)) {
Loader::loadModels($this, ['Packages']);
}
// Update all module rows
$modules = $this->ModuleManager->getByClass('proxmox');
foreach ($modules as $module) {
// Get rows and packages for the module
$rows = $this->ModuleManager->getRows($module->id);
$packages = $this->Packages->getAll(
$module->company_id,
['name' => 'ASC'],
null,
null,
['module_id' => $module->id]
);
foreach ($rows as $row) {
// Get and set module row meta as an in between step for those who have used the community update
$meta = (array)$row->meta;
$meta['gateway'] = $meta['gateway'] ?? '';
$meta['template_storage'] = $meta['template_storage'] ?? 'local';
foreach ($packages as $package) {
if ($package->module_row !== $row->id) {
continue;
}
// Update package meta
$package = $this->Packages->get($package->id);
$package_meta = (array) $package->meta;
$meta_fields = ['gateway', 'storage', 'template_storage', 'default_template'];
foreach ($meta_fields as $meta_field) {
$package_meta[$meta_field] = $meta[$meta_field] ?? '';
}
$this->Packages->edit($package->id, ['meta' => $package_meta]);
}
}
}
}
}
/**
* Attempts to validate service info. This is the top-level error checking method. Sets Input errors on failure.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @return bool True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null)
{
$this->Input->setRules($this->getServiceRules($vars));
return $this->Input->validates($vars);
}
/**
* Attempts to validate an existing service against a set of service info updates. Sets Input errors on failure.
*
* @param stdClass $service A stdClass object representing the service to validate for editing
* @param array $vars An array of user-supplied info to satisfy the request
* @return bool True if the service update validates or false otherwise. Sets Input errors when false.
*/
public function validateServiceEdit($service, array $vars = null)
{
$this->Input->setRules($this->getServiceRules($vars, true));
return $this->Input->validates($vars);
}
/**
* Returns the rule set for adding/editing a service
*
* @param array $vars A list of input vars
* @param bool $edit True to get the edit rules, false for the add rules
* @return array Service rules
*/
private function getServiceRules(array $vars = null, $edit = false)
{
// Set rules
$rules = [
'proxmox_hostname' => [
'format' => [
'if_set' => $edit,
'rule' => [[$this, 'validateHostName']],
'message' => Language::_('Proxmox.!error.proxmox_hostname.format', true)
]
]
];
return $rules;
}
/**
* Adds the service to the remote server. Sets Input errors on failure,
* preventing the service from being added.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being added (if the current service is an addon
* service and parent service has already been provisioned)
* @param string $status The status of the service being added. These include:
* - active
* - canceled
* - pending
* - suspended
* @return array A numerically indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addService(
$package,
array $vars = null,
$parent_package = null,
$parent_service = null,
$status = 'pending'
) {
// Load the API
$row = $this->getModuleRow();
if (!$row) {
$this->Input->setErrors(
['module_row' => ['missing' => Language::_('Proxmox.!error.module_row.missing', true)]]
);
return;
}
$api = $this->getApi($row->meta->user, $row->meta->password, $row->meta->host, $row->meta->port);
// Get the fields for the service
$params = $this->getFieldsFromInput($vars, $package);
$new_vmid = [];
if (empty($row->meta->vmid) || $row->meta->vmid < 200) {
$new_vmid = 200;
} else {
$new_vmid = $row->meta->vmid;
}
$params['vmid'] = '';
$params['storage'] = $package->meta->storage;
$params['ip'] = '';
$params['gateway'] = $package->meta->gateway;
// Validate the service-specific fields
$this->validateService($package, $vars);
if ($this->Input->errors()) {
return;
}
// Only provision the service if 'use_module' is true
if ($vars['use_module'] == 'true') {
$params['vmid'] = $new_vmid;
$available_ips = explode("\n", $row->meta->ips);
$params['ip'] = trim(array_shift($available_ips));
$client_id = (isset($vars['client_id']) ? $vars['client_id'] : '');
// Create a new client (if one does not already exist)
$client = $this->createClient($client_id, $params['userid'], $row);
if ($this->Input->errors()) {
return;
}
// Attempt to create the virtual server
$api->loadCommand('proxmox_vserver');
try {
// Load up the Virtual Server API
$vserver_api = new ProxmoxVserver($api);
$masked_params = $params;
$masked_params['password'] = '***';
// Create the Virtual Server
$this->log($row->meta->host . '|vserver-create', serialize($masked_params), 'input', true);
$response = $this->parseResponse($vserver_api->create($params), $row);
} catch (Exception $e) {
// Internal Error
$this->Input->setErrors(['api' => ['internal' => Language::_('Proxmox.!error.api.internal', true)]]);
}
if ($this->Input->errors()) {
return;
}
$new_mod_row_params = [];
foreach ($row->meta as $key => $value) {
$new_mod_row_params[$key] = $value;
}
$new_mod_row_params['vmid'] = $new_vmid + 1;
$new_mod_row_params['ips'] = implode("\n", $available_ips);
$this->ModuleManager->editRow($row->id, $new_mod_row_params);
sleep(5);
// Attempt to start the VM
$module_row = $this->getModuleRow($package->module_row);
$this->performAction('boot', $params['vmid'], $params['type'], $params['node'], $module_row, [], true);
}
// Return service fields
return [
[
'key' => 'proxmox_vserver_id',
'value' => $params['vmid'],
'encrypted' => 0
],
[
'key' => 'proxmox_ip',
'value' => $params['ip'],
'encrypted' => 0
],
[
'key' => 'proxmox_hostname',
'value' => $params['hostname'],
'encrypted' => 0
],
[
'key' => 'proxmox_node',
'value' => $params['node'],
'encrypted' => 0
],
[
'key' => 'proxmox_type',
'value' => $params['type'],
'encrypted' => 0
],
[
'key' => 'proxmox_username',
'value' => $params['userid'],
'encrypted' => 0
],
[
'key' => 'password',
'value' => $params['password'],
'encrypted' => 1
],
[
'key' => 'proxmox_cpu',
'value' => $params['sockets'],
'encrypted' => 0
],
[
'key' => 'proxmox_memory',
'value' => $params['memory'],
'encrypted' => 0
],
[
'key' => 'proxmox_hdd',
'value' => $params['hdd'],
'encrypted' => 0
],
[
'key' => 'proxmox_storage',
'value' => $params['storage'],
'encrypted' => 0
],
[
'key' => 'proxmox_template_storage',
'value' => $params['template_storage'],
'encrypted' => 0
],
[
'key' => 'proxmox_gateway',
'value' => $params['gateway'],
'encrypted' => 0
],
[
'key' => 'proxmox_netspeed',
'value' => $params['netspeed'],
'encrypted' => 0
],
[
'key' => 'proxmox_cpulimit',
'value' => $params['cpulimit'],
'encrypted' => 0
],
[
'key' => 'proxmox_cpuunits',
'value' => $params['cpuunits'],
'encrypted' => 0
],
[
'key' => 'proxmox_unprivileged',
'value' => $params['unprivileged'],
'encrypted' => 0
],
[
'key' => 'proxmox_swap',
'value' => $params['swap'],
'encrypted' => 0
]
];
}
/**
* Edits the service on the remote server. Sets Input errors on failure,
* preventing the service from being edited.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param array $vars An array of user supplied info to satisfy the request
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being edited (if the current service is an addon service)
* @return array A numerically indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editService($package, $service, array $vars = [], $parent_package = null, $parent_service = null)
{
// Load the API
$row = $this->getModuleRow();
$api = $this->getApi($row->meta->user, $row->meta->password, $row->meta->host, $row->meta->port);
// Validate the service-specific fields
$this->validateServiceEdit($service, $vars);
if ($this->Input->errors()) {
return;
}
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
// Only provision the service if 'use_module' is true
if ($vars['use_module'] == 'true') {
// Nothing to do
}
// Return all the service fields
$fields = [];
$encrypted_fields = ['proxmox_password'];
foreach ($service_fields as $key => $value) {
$fields[] = ['key' => $key, 'value' => $value, 'encrypted' => (in_array($key, $encrypted_fields) ? 1 : 0)];
}
return $fields;
}
/**
* Cancels the service on the remote server. Sets Input errors on failure,
* preventing the service from being canceled.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being canceled (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function cancelService($package, $service, $parent_package = null, $parent_service = null)
{
if (($row = $this->getModuleRow())) {
$api = $this->getApi($row->meta->user, $row->meta->password, $row->meta->host, $row->meta->port);
$api->loadCommand('proxmox_vserver');
$service_fields = $this->serviceFieldsToObject($service->fields);
$available_ips = explode("\n", $row->meta->ips);
$new_mod_row_params = [];
foreach ($row->meta as $key => $value) {
$new_mod_row_params[$key] = $value;
}
$available_ips[] = $service_fields->proxmox_ip;
$new_mod_row_params['ips'] = implode("\n", $available_ips);
$this->ModuleManager->editRow($row->id, $new_mod_row_params);
// Attempt to terminate the virtual server
try {
// Load up the Virtual Server API
$vserver_api = new ProxmoxVserver($api);
$params = [
'vmid' => $service_fields->proxmox_vserver_id,
'type' => $service_fields->proxmox_type,
'node' => $service_fields->proxmox_node
];
// Terminate the Virtual Server
$this->log($row->meta->host . '|vserver-terminate', serialize($params), 'input', true);
if ($service_fields->proxmox_type != 'qemu') {
$this->parseResponse($vserver_api->shutdown($params), $row);
}
sleep(5);
$this->parseResponse($vserver_api->terminate($params), $row);
} catch (Exception $e) {
// Internal Error
$this->Input->setErrors(['api' => ['internal' => Language::_('Proxmox.!error.api.internal', true)]]);
return;
}
}
return null;
}
/**
* Suspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being suspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being suspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function suspendService($package, $service, $parent_package = null, $parent_service = null)
{
// Suspend the service by shutting the server down
$response = null;
if (($row = $this->getModuleRow())) {
$api = $this->getApi($row->meta->user, $row->meta->password, $row->meta->host, $row->meta->port);
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
// Load the virtual server API
$api->loadCommand('proxmox_vserver');
try {
$server_api = new ProxmoxVserver($api);
$params = [
'vmid' => $service_fields->proxmox_vserver_id,
'type' => $service_fields->proxmox_type,
'node' => $service_fields->proxmox_node
];
$this->log($row->meta->host . '|vserver-shutdown', serialize($params), 'input', true);
$response = $this->parseResponse($server_api->shutdown($params), $row);
} catch (Exception $e) {
// Nothing to do
return;
}
}
return null;
}
/**
* Unsuspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being unsuspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being unsuspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function unsuspendService($package, $service, $parent_package = null, $parent_service = null)
{
// Unsuspend the service by booting it up and releasing the suspension lock
$response = null;
if (($row = $this->getModuleRow())) {
$api = $this->getApi($row->meta->user, $row->meta->password, $row->meta->host, $row->meta->port);
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
// Load the virtual server API
$api->loadCommand('proxmox_vserver');
try {
$server_api = new ProxmoxVserver($api);
$params = [
'vmid' => $service_fields->proxmox_vserver_id,
'type' => $service_fields->proxmox_type,
'node' => $service_fields->proxmox_node
];
$this->log($row->meta->host . '|vserver-boot', serialize($params), 'input', true);
$response = $this->parseResponse($server_api->boot($params), $row);
} catch (Exception $e) {
// Nothing to do
return;
}
}
return null;
}
/**
* Allows the module to perform an action when the service is ready to renew.
* Sets Input errors on failure, preventing the service from renewing.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being renewed (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function renewService($package, $service, $parent_package = null, $parent_service = null)
{
// Nothing to do
return null;
}
/**
* Updates the package for the service on the remote server. Sets Input
* errors on failure, preventing the service's package from being changed.
*
* @param stdClass $package_from A stdClass object representing the current package
* @param stdClass $package_to A stdClass object representing the new package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being changed (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function changeServicePackage(
$package_from,
$package_to,
$service,
$parent_package = null,
$parent_service = null
) {
// Nothing to do
return null;
}
/**
* Validates input data when attempting to add a package, returns the meta
* data to save when adding a package. Performs any action required to add
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being added.
*
* @param array An array of key/value pairs used to add the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addPackage(array $vars = null)
{
$this->Input->setRules($this->getPackageRules($vars));
$meta = [];
if ($this->Input->validates($vars)) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
}
return $meta;
}
/**
* Validates input data when attempting to edit a package, returns the meta
* data to save when editing a package. Performs any action required to edit
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being edited.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array An array of key/value pairs used to edit the package
* @return array A numerically indexed array of meta fields to be stored for this package containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editPackage($package, array $vars = null)
{
$this->Input->setRules($this->getPackageRules($vars));
$meta = [];
if ($this->Input->validates($vars)) {
// Return all package meta fields
foreach ($vars['meta'] as $key => $value) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => 0
];
}
}
return $meta;
}
/**
* Deletes the package on the remote server. Sets Input errors on failure,
* preventing the package from being deleted.
*
* @param stdClass $package A stdClass object representing the selected package
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function deletePackage($package)
{
// Nothing to do
return null;
}
/**
* Returns the rendered view of the manage module page
*
* @param mixed $module A stdClass object representing the module and its rows
* @param array $vars An array of post data submitted to or on the
* manage module page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the manager module page
*/
public function manageModule($module, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('manage', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'proxmox' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
$this->view->set('module', $module);
return $this->view->fetch();
}
/**
* Returns the rendered view of the add module row page
*
* @param array $vars An array of post data submitted to or on the add module
* row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the add module row page
*/
public function manageAddRow(array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('add_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'proxmox' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
$this->view->set('vars', (object)$vars);
return $this->view->fetch();
}
/**
* Returns the rendered view of the edit module row page
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of post data submitted to or on the edit module
* row page (used to repopulate fields after an error)
* @return string HTML content containing information to display when viewing the edit module row page
*/
public function manageEditRow($module_row, array &$vars)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('edit_row', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'proxmox' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html', 'Widget']);
if (empty($vars)) {
$vars = $module_row->meta;
// Mask password by replacing with ***
$vars->password = '***';
}
$this->view->set('vars', (object)$vars);
return $this->view->fetch();
}
/**
* Adds the module row on the remote server. Sets Input errors on failure,
* preventing the row from being added.
*
* @param array $vars An array of module info to add
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function addModuleRow(array &$vars)
{
$meta_fields = ['server_name', 'user', 'password', 'host', 'port',
'vmid', 'ips'
];
$encrypted_fields = ['user', 'password'];
$this->Input->setRules($this->getRowRules($vars));
// Validate module row
if ($this->Input->validates($vars)) {
// Build the meta data for this row
$meta = [];
foreach ($vars as $key => $value) {
if (in_array($key, $meta_fields)) {
$meta[] = [
'key' => $key,
'value' => $value,
'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
];
}
}
return $meta;
}
}
/**
* Edits the module row on the remote server. Sets Input errors on failure,
* preventing the row from being updated.
*
* @param stdClass $module_row The stdClass representation of the existing module row
* @param array $vars An array of module info to update
* @return array A numerically indexed array of meta fields for the module row containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
*/
public function editModuleRow($module_row, array &$vars)
{
// If the replacement sting is submitted, replace with the stored password
if (isset($vars['password']) && $vars['password'] == '***') {
$vars['password'] = $module_row->meta->password;
}
// Same as adding
return $this->addModuleRow($vars);
}
/**
* Deletes the module row on the remote server. Sets Input errors on failure,
* preventing the row from being deleted.
*
* @param stdClass $module_row The stdClass representation of the existing module row
*/
public function deleteModuleRow($module_row)
{
return null; // Nothing to do
}
/**
* Returns an array of available service delegation order methods. The module
* will determine how each method is defined. For example, the method "first"
* may be implemented such that it returns the module row with the least number
* of services assigned to it.
*
* @return array An array of order methods in key/value pairs where the key is the
* type to be stored for the group and value is the name for that option
* @see Module::selectModuleRow()
*/
public function getGroupOrderOptions()
{
return ['first' => Language::_('Proxmox.order_options.first', true)];
}
/**
* Determines which module row should be attempted when a service is provisioned
* for the given group based upon the order method set for that group.
*
* @return int The module row ID to attempt to add the service with
* @see Module::getGroupOrderOptions()
*/
public function selectModuleRow($module_group_id)
{
if (!isset($this->ModuleManager)) {
Loader::loadModels($this, ['ModuleManager']);
}
$group = $this->ModuleManager->getGroup($module_group_id);
if ($group) {
switch ($group->add_order) {
default:
case 'first':
foreach ($group->rows as $row) {
return $row->id;
}
break;
}
}
return 0;
}
/**
* Returns all fields used when adding/editing a package, including any
* javascript to execute when the page is rendered with these fields.
*
* @param $vars stdClass A stdClass object representing a set of post fields
* @return ModuleFields A ModuleFields object, containg the fields to render
* as well as any additional HTML markup to include
*/
public function getPackageFields($vars = null)
{
Loader::loadHelpers($this, ['Form', 'Html']);
// Fetch all packages available for the given server or server group
if ($vars->module_group == 'select') {
$vars->module_group = null;
}
$module_row = $this->getModuleRowByServer($vars->module_row ?? 0, $vars->module_group ?? '');
// Load more server info when the type is set
$nodes = [];
if ($module_row && !empty($vars->meta['type'])) {
// Load nodes
$nodes_res = $this->getNodes($vars->meta['type'], $module_row);
foreach ($nodes_res as $node) {
$nodes[$node->node] = $node->node;
}
}
// Remove nodes from 'available' if they are currently 'assigned'
if (isset($vars->meta['nodes'])) {
$this->assignGroups($nodes, $vars->meta['nodes']);
// Set the node value as the node key
$temp = [];
foreach ($vars->meta['nodes'] as $key => $value) {
$temp[$value] = $value;
}
$vars->meta['nodes'] = $temp;
unset($temp, $key, $value);
}
$fields = new ModuleFields();
// Show nodes, and set javascript field toggles
$assigned_nodes = $vars->meta['nodes'] ?? [];
$this->Form->setOutput(true);
$fields->setHtml('
<table id="proxmox_node_selector" style="display: none;">
<tr>
<td>' . Language::_('Proxmox.package_fields.assigned_nodes', true) . '</td>
<td></td>
<td>' . Language::_('Proxmox.package_fields.available_nodes', true) . '</td>
</tr>
<tr>
<td>
'
. $this->Form->fieldMultiSelect(
'meta[nodes][]',
$assigned_nodes,
[],
['id' => 'assigned_nodes']
)
. '
</td>
<td><a href="#" class="move_left"> </a> <a href="#" class="move_right"> </a></td>
<td>
'
. $this->Form->fieldMultiSelect(
'available_nodes[]',
$nodes ?? [],
[],
['id' => 'available_nodes']
)
. "
</td>
</tr>
</table>
<script type=\"text/javascript\">
$(document).ready(function() {
toggleProxmoxFields();
$('#proxmox_type').change(function() {
toggleProxmoxFields();
});
$('#proxmox_type').change(function() {
selectAssignedNodes();
fetchModuleOptions();
});
$('#proxmox_template_storage').change(function() {
selectAssignedNodes();
fetchModuleOptions();
});
// Select all assigned groups on submit
$('#assigned_nodes').closest('form').submit(function() {
selectAssignedNodes();
});
// Move nodes from right to left
$('.move_left').click(function() {
$('#available_nodes option:selected').appendTo($('#assigned_nodes'));
selectAssignedNodes();
fetchModuleOptions();
return false;
});
// Move nodes from left to right
$('.move_right').click(function() {
$('#assigned_nodes option:selected').appendTo($('#available_nodes'));
selectAssignedNodes();
fetchModuleOptions();
return false;