]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
test: allow running replication tests in parallel
[pve-manager.git] / www / manager6 / Utils.js
CommitLineData
b0a6d326
EK
1Ext.ns('PVE');
2
63b9faae
EK
3// avoid errors related to Accessible Rich Internet Applications
4// (access for people with disabilities)
0be88ae1 5// TODO reenable after all components are upgraded
63b9faae
EK
6Ext.enableAria = false;
7Ext.enableAriaButtons = false;
8Ext.enableAriaPanels = false;
9
b0a6d326 10// avoid errors when running without development tools
0be88ae1
DC
11if (!Ext.isDefined(Ext.global.console)) {
12 var console = {
0be88ae1 13 log: function() {}
b0a6d326
EK
14 };
15}
0be88ae1 16console.log("Starting PVE Manager");
b0a6d326
EK
17
18Ext.Ajax.defaultHeaders = {
19 'Accept': 'application/json'
20};
21
9fa2e36d 22Ext.define('PVE.Utils', { utilities: {
b0a6d326 23
9fa2e36d 24 // this singleton contains miscellaneous utilities
b0a6d326 25
0be88ae1 26 toolkit: undefined, // (extjs|touch), set inside Toolkit.js
b0a6d326 27
98a01af2
EK
28 bus_match: /^(ide|sata|virtio|scsi)\d+$/,
29
b0a6d326
EK
30 log_severity_hash: {
31 0: "panic",
32 1: "alert",
33 2: "critical",
34 3: "error",
35 4: "warning",
36 5: "notice",
37 6: "info",
38 7: "debug"
39 },
40
41 support_level_hash: {
42 'c': gettext('Community'),
43 'b': gettext('Basic'),
44 's': gettext('Standard'),
45 'p': gettext('Premium')
46 },
47
8ec8af9c 48 noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="https://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
b0a6d326
EK
49
50 kvm_ostypes: {
45d6c71a 51 'Linux': [
ca71cd5d 52 { desc: '5.x - 2.6 Kernel', val: 'l26' },
45d6c71a
TL
53 { desc: '2.4 Kernel', val: 'l24' }
54 ],
55 'Microsoft Windows': [
beb3c8a4 56 { desc: '10/2016/2019', val: 'win10' },
45d6c71a
TL
57 { desc: '8.x/2012/2012r2', val: 'win8' },
58 { desc: '7/2008r2', val: 'win7' },
59 { desc: 'Vista/2008', val: 'w2k8' },
60 { desc: 'XP/2003', val: 'wxp' },
61 { desc: '2000', val: 'w2k' }
62 ],
63 'Solaris Kernel': [
64 { desc: '-', val: 'solaris'}
65 ],
66 'Other': [
67 { desc: '-', val: 'other'}
68 ]
b0a6d326
EK
69 },
70
428bc4a2
DC
71 get_health_icon: function(state, circle) {
72 if (circle === undefined) {
73 circle = false;
74 }
75
76 if (state === undefined) {
77 state = 'uknown';
78 }
79
80 var icon = 'faded fa-question';
81 switch(state) {
82 case 'good':
83 icon = 'good fa-check';
84 break;
23c83a3a
DC
85 case 'upgrade':
86 icon = 'warning fa-upload';
87 break;
aa240b29
DC
88 case 'old':
89 icon = 'warning fa-refresh';
90 break;
428bc4a2
DC
91 case 'warning':
92 icon = 'warning fa-exclamation';
93 break;
94 case 'critical':
95 icon = 'critical fa-times';
96 break;
97 default: break;
98 }
99
100 if (circle) {
101 icon += '-circle';
102 }
103
104 return icon;
105 },
106
95d0de89
DC
107 parse_ceph_version: function(service) {
108 if (service.ceph_version_short) {
109 return service.ceph_version_short;
110 }
111
112 if (service.ceph_version) {
35306f47 113 var match = service.ceph_version.match(/version (\d+(\.\d+)*)/);
95d0de89
DC
114 if (match) {
115 return match[1];
116 }
117 }
118
119 return undefined;
120 },
121
005e0a60 122 compare_ceph_versions: function(a, b) {
3a08795a
DC
123 let avers = [];
124 let bvers = [];
125
005e0a60
DC
126 if (a === b) {
127 return 0;
128 }
3a08795a
DC
129
130 if (Ext.isArray(a)) {
131 avers = a.slice(); // copy array
132 } else {
133 avers = a.toString().split('.');
134 }
135
136 if (Ext.isArray(b)) {
137 bvers = b.slice(); // copy array
138 } else {
139 bvers = b.toString().split('.');
140 }
35306f47
TL
141
142 while (true) {
143 let av = avers.shift();
144 let bv = bvers.shift();
145
146 if (av === undefined && bv === undefined) {
147 return 0;
148 } else if (av === undefined) {
149 return -1;
150 } else if (bv === undefined) {
151 return 1;
152 } else {
153 let diff = parseInt(av, 10) - parseInt(bv, 10);
154 if (diff != 0) return diff;
155 // else we need to look at the next parts
005e0a60
DC
156 }
157 }
158
005e0a60
DC
159 },
160
2f5b82ae
DC
161 get_ceph_icon_html: function(health, fw) {
162 var state = PVE.Utils.map_ceph_health[health];
163 var cls = PVE.Utils.get_health_icon(state);
164 if (fw) {
165 cls += ' fa-fw';
166 }
167 return "<i class='fa " + cls + "'></i> ";
168 },
169
046e640c
DC
170 map_ceph_health: {
171 'HEALTH_OK':'good',
23c83a3a 172 'HEALTH_UPGRADE':'upgrade',
aa240b29 173 'HEALTH_OLD':'old',
046e640c
DC
174 'HEALTH_WARN':'warning',
175 'HEALTH_ERR':'critical'
176 },
177
dfe6d184 178 render_ceph_health: function(healthObj) {
046e640c
DC
179 var state = {
180 iconCls: PVE.Utils.get_health_icon(),
181 text: ''
182 };
183
dfe6d184 184 if (!healthObj || !healthObj.status) {
046e640c
DC
185 return state;
186 }
187
dfe6d184 188 var health = PVE.Utils.map_ceph_health[healthObj.status];
046e640c
DC
189
190 state.iconCls = PVE.Utils.get_health_icon(health, true);
dfe6d184 191 state.text = healthObj.status;
046e640c
DC
192
193 return state;
194 },
195
fee716d3 196 render_zfs_health: function(value) {
8c8604ba
TM
197 if (typeof value == 'undefined'){
198 return "";
199 }
fee716d3
DC
200 var iconCls = 'question-circle';
201 switch (value) {
8c8604ba 202 case 'AVAIL':
fee716d3
DC
203 case 'ONLINE':
204 iconCls = 'check-circle good';
205 break;
206 case 'REMOVED':
207 case 'DEGRADED':
208 iconCls = 'exclamation-circle warning';
209 break;
210 case 'UNAVAIL':
211 case 'FAULTED':
212 case 'OFFLINE':
213 iconCls = 'times-circle critical';
214 break;
215 default: //unknown
216 }
217
218 return '<i class="fa fa-' + iconCls + '"></i> ' + value;
8c8604ba 219
fee716d3
DC
220 },
221
14ba33fb
TL
222 render_pbs_fingerprint: fp => fp.substring(0, 23),
223
3003a59d
TL
224 render_backup_encryption: function(v, meta, record) {
225 if (!v) {
226 return gettext('No');
227 }
228
229 let tip = '';
230 if (v.match(/^[a-fA-F0-9]{2}:/)) { // fingerprint
231 tip = `Key fingerprint ${PVE.Utils.render_pbs_fingerprint(v)}`;
232 }
233 let icon = `<i class="fa fa-fw fa-lock good"></i>`;
234 return `<span data-qtip="${tip}">${icon} ${gettext('Encrypted')}</span>`;
235 },
236
770d614f
TL
237 render_backup_verification: function(v, meta, record) {
238 let i = (cls, txt) => `<i class="fa fa-fw fa-${cls}"></i> ${txt}`;
239 if (v === undefined || v === null) {
240 return i('question-circle-o warning', gettext('None'));
241 }
242 let tip = ""
243 let txt = gettext('Failed');
244 let iconCls = 'times critical';
245 if (v.state === 'ok') {
246 txt = gettext('OK');
247 iconCls = 'check good';
248 let now = Date.now() / 1000;
249 let task = Proxmox.Utils.parse_task_upid(v.upid);
250 let verify_time = Proxmox.Utils.render_timestamp(task.starttime);
251 tip = `Last verify task started on ${verify_time}`;
252 if (now - v.starttime > 30 * 24 * 60 * 60) {
253 tip = `Last verify task over 30 days ago: ${verify_time}`;
254 iconCls = 'check warning';
255 }
256 }
257 return `<span data-qtip="${tip}"> ${i(iconCls, txt)} </span>`;
258 },
259
01ad47af
AL
260 render_backup_status: function(value, meta, record) {
261 if (typeof value == 'undefined') {
262 return "";
263 }
264
265 let iconCls = 'check-circle good';
266 let text = gettext('Yes');
267
268 if (!PVE.Parser.parseBoolean(value.toString())) {
269 iconCls = 'times-circle critical';
270
271 text = gettext('No');
272
273 let reason = record.get('reason');
274 if (typeof reason !== 'undefined') {
275 if (reason in PVE.Utils.backup_reasons_table) {
276 reason = PVE.Utils.backup_reasons_table[record.get('reason')];
277 }
278 text = `${text} - ${reason}`;
279 }
280 }
281
282 return `<i class="fa fa-${iconCls}"></i> ${text}`;
283 },
284
7f08d0d1
AL
285 render_backup_days_of_week: function(val) {
286 var dows = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
287 var selected = [];
288 var cur = -1;
289 val.split(',').forEach(function(day){
290 cur++;
291 var dow = (dows.indexOf(day)+6)%7;
292 if (cur === dow) {
293 if (selected.length === 0 || selected[selected.length-1] === 0) {
294 selected.push(1);
295 } else {
296 selected[selected.length-1]++;
297 }
298 } else {
299 while (cur < dow) {
300 cur++;
301 selected.push(0);
302 }
303 selected.push(1);
304 }
305 });
306
307 cur = -1;
308 var days = [];
309 selected.forEach(function(item) {
310 cur++;
311 if (item > 2) {
312 days.push(Ext.Date.dayNames[(cur+1)] + '-' + Ext.Date.dayNames[(cur+item)%7]);
313 cur += item-1;
314 } else if (item == 2) {
315 days.push(Ext.Date.dayNames[cur+1]);
316 days.push(Ext.Date.dayNames[(cur+2)%7]);
317 cur++;
318 } else if (item == 1) {
319 days.push(Ext.Date.dayNames[(cur+1)%7]);
320 }
321 });
322 return days.join(', ');
323 },
324
325 render_backup_selection: function(value, metaData, record) {
326 let allExceptText = gettext('All except {0}');
327 let allText = '-- ' + gettext('All') + ' --';
328 if (record.data.all) {
329 if (record.data.exclude) {
330 return Ext.String.format(allExceptText, record.data.exclude);
331 }
332 return allText;
333 }
334 if (record.data.vmid) {
335 return record.data.vmid;
336 }
337
338 if (record.data.pool) {
339 return "Pool '"+ record.data.pool + "'";
340 }
341
342 return "-";
343 },
344
01ad47af
AL
345 backup_reasons_table: {
346 'backup=yes': gettext('Enabled'),
347 'backup=no': gettext('Disabled'),
348 'enabled': gettext('Enabled'),
349 'disabled': gettext('Disabled'),
350 'not a volume': gettext('Not a volume'),
351 'efidisk but no OMVF BIOS': gettext('EFI Disk without OMVF BIOS'),
352 },
353
45d6c71a
TL
354 get_kvm_osinfo: function(value) {
355 var info = { base: 'Other' }; // default
356 if (value) {
357 Ext.each(Object.keys(PVE.Utils.kvm_ostypes), function(k) {
358 Ext.each(PVE.Utils.kvm_ostypes[k], function(e) {
359 if (e.val === value) {
360 info = { desc: e.desc, base: k };
361 }
362 });
363 });
b0a6d326 364 }
45d6c71a
TL
365 return info;
366 },
367
368 render_kvm_ostype: function (value) {
369 var osinfo = PVE.Utils.get_kvm_osinfo(value);
370 if (osinfo.desc && osinfo.desc !== '-') {
371 return osinfo.base + ' ' + osinfo.desc;
372 } else {
373 return osinfo.base;
b0a6d326 374 }
b0a6d326
EK
375 },
376
377 render_hotplug_features: function (value) {
23d3881a 378 var fa = [];
b0a6d326
EK
379
380 if (!value || (value === '0')) {
ec99b1c3 381 return gettext('Disabled');
b0a6d326
EK
382 }
383
4dcca8a4
DC
384 if (value === '1') {
385 value = 'disk,network,usb';
386 }
387
b0a6d326
EK
388 Ext.each(value.split(','), function(el) {
389 if (el === 'disk') {
390 fa.push(gettext('Disk'));
391 } else if (el === 'network') {
392 fa.push(gettext('Network'));
393 } else if (el === 'usb') {
b9628aa5 394 fa.push('USB');
b0a6d326
EK
395 } else if (el === 'memory') {
396 fa.push(gettext('Memory'));
397 } else if (el === 'cpu') {
398 fa.push(gettext('CPU'));
399 } else {
400 fa.push(el);
401 }
402 });
403
404 return fa.join(', ');
405 },
406
0beff18b
FE
407 render_localtime: function(value) {
408 if (value === '__default__') {
409 return Proxmox.Utils.defaultText + ' (' + gettext('Enabled for Windows') + ')';
410 }
411 return Proxmox.Utils.format_boolean(value);
412 },
413
1662ccdb
SI
414 render_qga_features: function(value) {
415 if (!value) {
416 return Proxmox.Utils.defaultText + ' (' + Proxmox.Utils.disabledText + ')';
417 }
418 var props = PVE.Parser.parsePropertyString(value, 'enabled');
419 if (!PVE.Parser.parseBoolean(props.enabled)) {
420 return Proxmox.Utils.disabledText;
421 }
422
423 delete props.enabled;
424 var agentstring = Proxmox.Utils.enabledText;
425
426 Ext.Object.each(props, function(key, value) {
427 var keystring = '' ;
428 agentstring += ', ' + key + ': ';
429
5a6c563c
MD
430 if (key === 'type') {
431 let map = {
432 isa: "ISA",
176a62d3 433 virtio: "VirtIO",
5a6c563c
MD
434 };
435 agentstring += map[value] || Proxmox.Utils.unknownText;
1662ccdb 436 } else {
5a6c563c
MD
437 if (PVE.Parser.parseBoolean(value)) {
438 agentstring += Proxmox.Utils.enabledText;
439 } else {
440 agentstring += Proxmox.Utils.disabledText;
441 }
1662ccdb
SI
442 }
443 });
444
445 return agentstring;
446 },
447
a1ee14a2
DC
448 render_qemu_machine: function(value) {
449 return value || (Proxmox.Utils.defaultText + ' (i440fx)');
450 },
451
17c71f27
DC
452 render_qemu_bios: function(value) {
453 if (!value) {
e7ade592 454 return Proxmox.Utils.defaultText + ' (SeaBIOS)';
17c71f27
DC
455 } else if (value === 'seabios') {
456 return "SeaBIOS";
457 } else if (value === 'ovmf') {
458 return "OVMF (UEFI)";
459 } else {
460 return value;
461 }
462 },
463
8f17b496
TL
464 render_dc_ha_opts: function(value) {
465 if (!value) {
41ca3465 466 return Proxmox.Utils.defaultText;
8f17b496
TL
467 } else {
468 return PVE.Parser.printPropertyString(value);
469 }
470 },
bb469a11
TL
471 render_as_property_string: function(value) {
472 return (!value) ? Proxmox.Utils.defaultText
473 : PVE.Parser.printPropertyString(value);
474 },
8f17b496 475
b0a6d326
EK
476 render_scsihw: function(value) {
477 if (!value) {
e7ade592 478 return Proxmox.Utils.defaultText + ' (LSI 53C895A)';
b0a6d326
EK
479 } else if (value === 'lsi') {
480 return 'LSI 53C895A';
481 } else if (value === 'lsi53c810') {
482 return 'LSI 53C810';
483 } else if (value === 'megasas') {
484 return 'MegaRAID SAS 8708EM2';
485 } else if (value === 'virtio-scsi-pci') {
49f5d1ab
EK
486 return 'VirtIO SCSI';
487 } else if (value === 'virtio-scsi-single') {
488 return 'VirtIO SCSI single';
b0a6d326
EK
489 } else if (value === 'pvscsi') {
490 return 'VMware PVSCSI';
491 } else {
492 return value;
493 }
494 },
495
9c22da32 496 render_spice_enhancements: function(values) {
9c22da32
AL
497 let props = PVE.Parser.parsePropertyString(values);
498 if (Ext.Object.isEmpty(props)) {
3b0facc9 499 return Proxmox.Utils.noneText;
9c22da32
AL
500 }
501
502 let output = [];
503 if (PVE.Parser.parseBoolean(props.foldersharing)) {
504 output.push('Folder Sharing: ' + gettext('Enabled'));
505 }
506 if (props.videostreaming === 'all' || props.videostreaming === 'filter') {
507 output.push('Video Streaming: ' + props.videostreaming);
508 }
509 return output.join(', ');
510 },
511
b0a6d326
EK
512 // fixme: auto-generate this
513 // for now, please keep in sync with PVE::Tools::kvmkeymaps
514 kvm_keymaps: {
515 //ar: 'Arabic',
516 da: 'Danish',
0be88ae1
DC
517 de: 'German',
518 'de-ch': 'German (Swiss)',
519 'en-gb': 'English (UK)',
0a5b8747 520 'en-us': 'English (USA)',
b0a6d326
EK
521 es: 'Spanish',
522 //et: 'Estonia',
523 fi: 'Finnish',
0be88ae1
DC
524 //fo: 'Faroe Islands',
525 fr: 'French',
526 'fr-be': 'French (Belgium)',
b0a6d326
EK
527 'fr-ca': 'French (Canada)',
528 'fr-ch': 'French (Swiss)',
529 //hr: 'Croatia',
530 hu: 'Hungarian',
531 is: 'Icelandic',
0be88ae1 532 it: 'Italian',
b0a6d326
EK
533 ja: 'Japanese',
534 lt: 'Lithuanian',
535 //lv: 'Latvian',
0be88ae1 536 mk: 'Macedonian',
b0a6d326
EK
537 nl: 'Dutch',
538 //'nl-be': 'Dutch (Belgium)',
0be88ae1 539 no: 'Norwegian',
b0a6d326
EK
540 pl: 'Polish',
541 pt: 'Portuguese',
542 'pt-br': 'Portuguese (Brazil)',
543 //ru: 'Russian',
544 sl: 'Slovenian',
545 sv: 'Swedish',
546 //th: 'Thai',
547 tr: 'Turkish'
548 },
549
550 kvm_vga_drivers: {
551 std: gettext('Standard VGA'),
5ca366f2 552 vmware: gettext('VMware compatible'),
b0a6d326
EK
553 qxl: 'SPICE',
554 qxl2: 'SPICE dual monitor',
555 qxl3: 'SPICE three monitors',
556 qxl4: 'SPICE four monitors',
557 serial0: gettext('Serial terminal') + ' 0',
558 serial1: gettext('Serial terminal') + ' 1',
559 serial2: gettext('Serial terminal') + ' 2',
89ae1bb1
DC
560 serial3: gettext('Serial terminal') + ' 3',
561 virtio: 'VirtIO-GPU',
562 none: Proxmox.Utils.noneText
b0a6d326
EK
563 },
564
565 render_kvm_language: function (value) {
e7ade592
DC
566 if (!value || value === '__default__') {
567 return Proxmox.Utils.defaultText;
b0a6d326
EK
568 }
569 var text = PVE.Utils.kvm_keymaps[value];
570 if (text) {
571 return text + ' (' + value + ')';
572 }
573 return value;
574 },
575
576 kvm_keymap_array: function() {
f2782813 577 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
578 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
579 data.push([key, PVE.Utils.render_kvm_language(value)]);
580 });
581
582 return data;
583 },
584
3438c27e 585 console_map: {
da9d14cd 586 '__default__': Proxmox.Utils.defaultText + ' (xterm.js)',
3438c27e
DC
587 'vv': 'SPICE (remote-viewer)',
588 'html5': 'HTML5 (noVNC)',
4ace5c6f 589 'xtermjs': 'xterm.js'
3438c27e
DC
590 },
591
b0a6d326 592 render_console_viewer: function(value) {
3438c27e
DC
593 value = value || '__default__';
594 if (PVE.Utils.console_map[value]) {
595 return PVE.Utils.console_map[value];
b0a6d326 596 }
3438c27e 597 return value;
b0a6d326
EK
598 },
599
755b9083 600 console_viewer_array: function() {
3438c27e 601 return Ext.Array.map(Object.keys(PVE.Utils.console_map), function(v) {
755b9083
TL
602 return [v, PVE.Utils.render_console_viewer(v)];
603 });
604 },
605
b0a6d326
EK
606 render_kvm_vga_driver: function (value) {
607 if (!value) {
e7ade592 608 return Proxmox.Utils.defaultText;
b0a6d326 609 }
4f3e66d8
DC
610 var vga = PVE.Parser.parsePropertyString(value, 'type');
611 var text = PVE.Utils.kvm_vga_drivers[vga.type];
612 if (!vga.type) {
613 text = Proxmox.Utils.defaultText;
614 }
0be88ae1 615 if (text) {
b0a6d326
EK
616 return text + ' (' + value + ')';
617 }
618 return value;
619 },
620
621 kvm_vga_driver_array: function() {
f2782813 622 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
623 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
624 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
625 });
626
627 return data;
628 },
629
630 render_kvm_startup: function(value) {
631 var startup = PVE.Parser.parseStartup(value);
632
633 var res = 'order=';
634 if (startup.order === undefined) {
635 res += 'any';
636 } else {
637 res += startup.order;
638 }
639 if (startup.up !== undefined) {
640 res += ',up=' + startup.up;
641 }
642 if (startup.down !== undefined) {
643 res += ',down=' + startup.down;
644 }
645
646 return res;
647 },
648
b0a6d326
EK
649 extractFormActionError: function(action) {
650 var msg;
651 switch (action.failureType) {
652 case Ext.form.action.Action.CLIENT_INVALID:
653 msg = gettext('Form fields may not be submitted with invalid values');
654 break;
655 case Ext.form.action.Action.CONNECT_FAILURE:
656 msg = gettext('Connection error');
657 var resp = action.response;
658 if (resp.status && resp.statusText) {
659 msg += " " + resp.status + ": " + resp.statusText;
660 }
661 break;
662 case Ext.form.action.Action.LOAD_FAILURE:
663 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 664 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
665 break;
666 }
667 return msg;
668 },
669
0e244a29
DC
670 contentTypes: {
671 'images': gettext('Disk image'),
672 'backup': gettext('VZDump backup file'),
673 'vztmpl': gettext('Container template'),
674 'iso': gettext('ISO image'),
aef28e04
DC
675 'rootdir': gettext('Container'),
676 'snippets': gettext('Snippets')
0e244a29 677 },
b0a6d326 678
3b8f599b
DM
679 volume_is_qemu_backup: function(volid, format) {
680 return format === 'pbs-vm' || volid.match(':backup/vzdump-qemu-');
681 },
682
683 volume_is_lxc_backup: function(volid, format) {
684 return format === 'pbs-ct' || volid.match(':backup/vzdump-(lxc|openvz)-');
685 },
686
efff7eab
DC
687 authSchema: {
688 ad: {
689 name: gettext('Active Directory Server'),
690 ipanel: 'pveAuthADPanel',
822fb26d 691 syncipanel: 'pveAuthLDAPSyncPanel',
efff7eab
DC
692 add: true,
693 },
694 ldap: {
695 name: gettext('LDAP Server'),
696 ipanel: 'pveAuthLDAPPanel',
822fb26d 697 syncipanel: 'pveAuthLDAPSyncPanel',
efff7eab
DC
698 add: true,
699 },
700 pam: {
701 name: 'Linux PAM',
702 ipanel: 'pveAuthBasePanel',
703 add: false,
704 },
705 pve: {
706 name: 'Proxmox VE authentication server',
707 ipanel: 'pveAuthBasePanel',
708 add: false,
709 },
710 },
711
062a7f49
TL
712 storageSchema: {
713 dir: {
714 name: Proxmox.Utils.directoryText,
715 ipanel: 'DirInputPanel',
716 faIcon: 'folder'
717 },
718 lvm: {
719 name: 'LVM',
720 ipanel: 'LVMInputPanel',
721 faIcon: 'folder'
722 },
723 lvmthin: {
724 name: 'LVM-Thin',
725 ipanel: 'LvmThinInputPanel',
726 faIcon: 'folder'
727 },
728 nfs: {
729 name: 'NFS',
730 ipanel: 'NFSInputPanel',
731 faIcon: 'building'
732 },
733 cifs: {
734 name: 'CIFS',
735 ipanel: 'CIFSInputPanel',
736 faIcon: 'building'
737 },
738 glusterfs: {
739 name: 'GlusterFS',
740 ipanel: 'GlusterFsInputPanel',
741 faIcon: 'building'
742 },
743 iscsi: {
744 name: 'iSCSI',
745 ipanel: 'IScsiInputPanel',
746 faIcon: 'building'
747 },
4a4b2b6e
TL
748 cephfs: {
749 name: 'CephFS',
750 ipanel: 'CephFSInputPanel',
751 faIcon: 'building'
752 },
753 pvecephfs: {
754 name: 'CephFS (PVE)',
755 ipanel: 'CephFSInputPanel',
756 hideAdd: true,
757 faIcon: 'building'
758 },
062a7f49
TL
759 rbd: {
760 name: 'RBD',
761 ipanel: 'RBDInputPanel',
062a7f49
TL
762 faIcon: 'building'
763 },
764 pveceph: {
765 name: 'RBD (PVE)',
0d1ac958
TL
766 ipanel: 'RBDInputPanel',
767 hideAdd: true,
062a7f49
TL
768 faIcon: 'building'
769 },
770 zfs: {
771 name: 'ZFS over iSCSI',
772 ipanel: 'ZFSInputPanel',
773 faIcon: 'building'
774 },
775 zfspool: {
776 name: 'ZFS',
777 ipanel: 'ZFSPoolInputPanel',
778 faIcon: 'folder'
779 },
8b966034
TL
780 pbs: {
781 name: 'Proxmox Backup Server',
ee19d331
TL
782 ipanel: 'PBSInputPanel',
783 faIcon: 'floppy-o',
8b966034 784 },
062a7f49
TL
785 drbd: {
786 name: 'DRBD',
8b966034
TL
787 hideAdd: true,
788 },
062a7f49
TL
789 },
790
9233148b
AD
791 sdnvnetSchema: {
792 vnet: {
793 name: 'vnet',
794 faIcon: 'folder'
795 },
796 },
797
798 sdnzoneSchema: {
799 zone: {
800 name: 'zone',
801 hideAdd: true
802 },
1b4cce60
AD
803 simple: {
804 name: 'Simple',
805 ipanel: 'SimpleInputPanel',
806 faIcon: 'th'
807 },
9233148b 808 vlan: {
f3c1eac7 809 name: 'VLAN',
9233148b 810 ipanel: 'VlanInputPanel',
f3c1eac7 811 faIcon: 'th'
9233148b
AD
812 },
813 qinq: {
f3c1eac7 814 name: 'QinQ',
9233148b 815 ipanel: 'QinQInputPanel',
f3c1eac7 816 faIcon: 'th'
9233148b
AD
817 },
818 vxlan: {
f3c1eac7 819 name: 'VXLAN',
9233148b 820 ipanel: 'VxlanInputPanel',
f3c1eac7 821 faIcon: 'th'
9233148b
AD
822 },
823 evpn: {
f3c1eac7 824 name: 'EVPN',
9233148b 825 ipanel: 'EvpnInputPanel',
f3c1eac7 826 faIcon: 'th'
9233148b
AD
827 },
828 },
829
830 sdncontrollerSchema: {
831 controller: {
832 name: 'controller',
833 hideAdd: true
834 },
835 evpn: {
836 name: 'evpn',
837 ipanel: 'EvpnInputPanel',
f3c1eac7 838 faIcon: 'crosshairs'
9233148b
AD
839 },
840 },
841
842 format_sdnvnet_type: function(value, md, record) {
843 var schema = PVE.Utils.sdnvnetSchema[value];
844 if (schema) {
845 return schema.name;
846 }
847 return Proxmox.Utils.unknownText;
848 },
849
850 format_sdnzone_type: function(value, md, record) {
851 var schema = PVE.Utils.sdnzoneSchema[value];
852 if (schema) {
f3c1eac7 853 return schema.name;
9233148b
AD
854 }
855 return Proxmox.Utils.unknownText;
856 },
857
858 format_sdncontroller_type: function(value, md, record) {
859 var schema = PVE.Utils.sdncontrollerSchema[value];
860 if (schema) {
861 return schema.name;
862 }
863 return Proxmox.Utils.unknownText;
864 },
865
3c23c025 866 format_storage_type: function(value, md, record) {
4a4b2b6e
TL
867 if (value === 'rbd') {
868 value = (!record || record.get('monhost') ? 'rbd' : 'pveceph');
869 } else if (value === 'cephfs') {
870 value = (!record || record.get('monhost') ? 'cephfs' : 'pvecephfs');
3c23c025 871 }
062a7f49
TL
872
873 var schema = PVE.Utils.storageSchema[value];
874 if (schema) {
875 return schema.name;
b0a6d326 876 }
062a7f49 877 return Proxmox.Utils.unknownText;
a3b8efb4
EK
878 },
879
ced1677b 880 format_ha: function(value) {
e7ade592 881 var text = Proxmox.Utils.noneText;
ced1677b
TL
882
883 if (value.managed) {
e7ade592 884 text = value.state || Proxmox.Utils.noneText;
ced1677b 885
e7ade592
DC
886 text += ', ' + Proxmox.Utils.groupText + ': ';
887 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
888 }
889
890 return text;
891 },
892
b0a6d326 893 format_content_types: function(value) {
0e244a29
DC
894 return value.split(',').sort().map(function(ct) {
895 return PVE.Utils.contentTypes[ct] || ct;
896 }).join(', ');
b0a6d326
EK
897 },
898
899 render_storage_content: function(value, metaData, record) {
900 var data = record.data;
901 if (Ext.isNumber(data.channel) &&
902 Ext.isNumber(data.id) &&
903 Ext.isNumber(data.lun)) {
0be88ae1
DC
904 return "CH " +
905 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
906 " ID " + data.id + " LUN " + data.lun;
907 }
3b8f599b 908 return data.volid.replace(/^.*?:(.*?\/)?/,'');
b0a6d326
EK
909 },
910
911 render_serverity: function (value) {
912 return PVE.Utils.log_severity_hash[value] || value;
913 },
914
915 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
916
917 if (!(record.data.uptime && Ext.isNumeric(value))) {
918 return '';
919 }
920
921 var maxcpu = record.data.maxcpu || 1;
922
923 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
924 return '';
925 }
0be88ae1 926
b0a6d326
EK
927 var per = value * 100;
928
929 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
930 },
931
932 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
b0a6d326
EK
933
934 if (!Ext.isNumeric(value)) {
935 return '';
936 }
937
e7ade592 938 return Proxmox.Utils.format_size(value);
b0a6d326
EK
939 },
940
946730cd
DC
941 render_bandwidth: function(value) {
942 if (!Ext.isNumeric(value)) {
943 return '';
944 }
945
e7ade592 946 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
947 },
948
3f633655
EK
949 render_timestamp_human_readable: function(value) {
950 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
951 },
952
0bfc799f
DC
953 calculate_mem_usage: function(data) {
954 if (!Ext.isNumeric(data.mem) ||
955 data.maxmem === 0 ||
956 data.uptime < 1) {
957 return -1;
958 }
959
960 return (data.mem / data.maxmem);
961 },
962
963 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
964 if (!Ext.isNumeric(value) || value === -1) {
965 return '';
966 }
967 if (value > 1 ) {
968 // we got no percentage but bytes
969 var mem = value;
970 var maxmem = record.data.maxmem;
971 if (!record.data.uptime ||
972 maxmem === 0 ||
973 !Ext.isNumeric(mem)) {
974 return '';
975 }
976
977 return ((mem*100)/maxmem).toFixed(1) + " %";
978 }
979 return (value*100).toFixed(1) + " %";
980 },
981
b0a6d326
EK
982 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
983
984 var mem = value;
985 var maxmem = record.data.maxmem;
0be88ae1 986
b0a6d326
EK
987 if (!record.data.uptime) {
988 return '';
989 }
990
991 if (!(Ext.isNumeric(mem) && maxmem)) {
992 return '';
993 }
994
728f1b97 995 return PVE.Utils.render_size(value);
b0a6d326
EK
996 },
997
0bfc799f
DC
998 calculate_disk_usage: function(data) {
999
1000 if (!Ext.isNumeric(data.disk) ||
1001 data.type === 'qemu' ||
1002 (data.type === 'lxc' && data.uptime === 0) ||
1003 data.maxdisk === 0) {
1004 return -1;
1005 }
1006
1007 return (data.disk / data.maxdisk);
1008 },
1009
1010 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
1011 if (!Ext.isNumeric(value) || value === -1) {
1012 return '';
1013 }
1014
1015 return (value * 100).toFixed(1) + " %";
1016 },
1017
b0a6d326
EK
1018 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
1019
1020 var disk = value;
1021 var maxdisk = record.data.maxdisk;
728f1b97 1022 var type = record.data.type;
b0a6d326 1023
728f1b97
DC
1024 if (!Ext.isNumeric(disk) ||
1025 type === 'qemu' ||
1026 maxdisk === 0 ||
1027 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
1028 return '';
1029 }
1030
728f1b97 1031 return PVE.Utils.render_size(value);
b0a6d326
EK
1032 },
1033
4dbc64a7
DC
1034 get_object_icon_class: function(type, record) {
1035 var status = '';
1036 var objType = type;
1037
1038 if (type === 'type') {
1039 // for folder view
1040 objType = record.groupbyid;
1041 } else if (record.template) {
1042 // templates
1043 objType = 'template';
1044 status = type;
1045 } else {
1046 // everything else
1047 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
1048 }
1049
6284a48a
DC
1050 if (record.lock) {
1051 status += ' locked lock-' + record.lock;
1052 }
1053
4dbc64a7
DC
1054 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
1055 if (defaults && defaults.iconCls) {
1056 var retVal = defaults.iconCls + ' ' + status;
1057 return retVal;
b0a6d326
EK
1058 }
1059
4dbc64a7
DC
1060 return '';
1061 },
1062
1063 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
1064
1065 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 1066
4dbc64a7 1067 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 1068 return fa + value;
b0a6d326
EK
1069 },
1070
b0a6d326
EK
1071 render_support_level: function(value, metaData, record) {
1072 return PVE.Utils.support_level_hash[value] || '-';
1073 },
1074
0be88ae1 1075 render_upid: function(value, metaData, record) {
b0a6d326
EK
1076 var type = record.data.type;
1077 var id = record.data.id;
1078
e7ade592 1079 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
1080 },
1081
054ac1b8
DC
1082 /* render functions for new status panel */
1083
1084 render_usage: function(val) {
1085 return (val*100).toFixed(2) + '%';
1086 },
1087
1088 render_cpu_usage: function(val, max) {
1089 return Ext.String.format(gettext('{0}% of {1}') +
1090 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
1091 },
1092
1093 render_size_usage: function(val, max) {
bab64974
DC
1094 if (max === 0) {
1095 return gettext('N/A');
1096 }
054ac1b8
DC
1097 return (val*100/max).toFixed(2) + '% '+ '(' +
1098 Ext.String.format(gettext('{0} of {1}'),
1099 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
1100 },
1101
1102 /* this is different for nodes */
1103 render_node_cpu_usage: function(value, record) {
1104 return PVE.Utils.render_cpu_usage(value, record.cpus);
1105 },
1106
1107 /* this is different for nodes */
1108 render_node_size_usage: function(record) {
1109 return PVE.Utils.render_size_usage(record.used, record.total);
1110 },
1111
27809975
DC
1112 render_optional_url: function(value) {
1113 var match;
1114 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 1115 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
1116 }
1117 return value;
1118 },
1119
1120 render_san: function(value) {
1121 var names = [];
1122 if (Ext.isArray(value)) {
1123 value.forEach(function(val) {
1124 if (!Ext.isNumber(val)) {
1125 names.push(val);
1126 }
1127 });
1128 return names.join('<br>');
1129 }
1130 return value;
1131 },
1132
6ad4be69
DC
1133 render_full_name: function(firstname, metaData, record) {
1134 var first = firstname || '';
1135 var last = record.data.lastname || '';
1136 return Ext.htmlEncode(first + " " + last);
1137 },
1138
2d41c7e6
TL
1139 render_u2f_error: function(error) {
1140 var ErrorNames = {
1141 '1': gettext('Other Error'),
1142 '2': gettext('Bad Request'),
1143 '3': gettext('Configuration Unsupported'),
1144 '4': gettext('Device Ineligible'),
1145 '5': gettext('Timeout')
1146 };
1147 return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
1148 },
1149
aa0819a8 1150 windowHostname: function() {
e7ade592 1151 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
1152 function(m, addr, offset, original) { return addr; });
1153 },
0be88ae1 1154
953f6e9b 1155 openDefaultConsoleWindow: function(consoles, consoleType, vmid, nodename, vmname, cmd) {
3438c27e 1156 var dv = PVE.Utils.defaultViewer(consoles);
953f6e9b 1157 PVE.Utils.openConsoleWindow(dv, consoleType, vmid, nodename, vmname, cmd);
b0a6d326
EK
1158 },
1159
953f6e9b
TL
1160 openConsoleWindow: function(viewer, consoleType, vmid, nodename, vmname, cmd) {
1161 if (vmid == undefined && (consoleType === 'kvm' || consoleType === 'lxc')) {
b0a6d326
EK
1162 throw "missing vmid";
1163 }
b0a6d326
EK
1164 if (!nodename) {
1165 throw "no nodename specified";
1166 }
1167
c7218ab3 1168 if (viewer === 'html5') {
953f6e9b 1169 PVE.Utils.openVNCViewer(consoleType, vmid, nodename, vmname, cmd);
c6b2336c 1170 } else if (viewer === 'xtermjs') {
953f6e9b 1171 Proxmox.Utils.openXtermJsViewer(consoleType, vmid, nodename, vmname, cmd);
b0a6d326 1172 } else if (viewer === 'vv') {
953f6e9b
TL
1173 let url = '/nodes/' + nodename + '/spiceshell';
1174 let params = {
1175 proxy: PVE.Utils.windowHostname(),
1176 };
1177 if (consoleType === 'kvm') {
b0a6d326 1178 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
953f6e9b 1179 } else if (consoleType === 'lxc') {
9e361643 1180 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
953f6e9b
TL
1181 } else if (consoleType === 'upgrade') {
1182 params.cmd = 'upgrade';
1183 } else if (consoleType === 'cmd') {
8eccc68f 1184 params.cmd = cmd;
953f6e9b
TL
1185 } else if (consoleType !== 'shell') {
1186 throw `unknown spice viewer type '${consoleType}'`;
b0a6d326 1187 }
953f6e9b 1188 PVE.Utils.openSpiceViewer(url, params);
b0a6d326 1189 } else {
953f6e9b 1190 throw `unknown viewer type '${viewer}'`;
b0a6d326
EK
1191 }
1192 },
1193
3438c27e
DC
1194 defaultViewer: function(consoles) {
1195
1196 var allowSpice, allowXtermjs;
1197
1198 if (consoles === true) {
1199 allowSpice = true;
1200 allowXtermjs = true;
1201 } else if (typeof consoles === 'object') {
1202 allowSpice = consoles.spice;
4ace5c6f 1203 allowXtermjs = !!consoles.xtermjs;
3438c27e 1204 }
da9d14cd 1205 var dv = PVE.VersionInfo.console || 'xtermjs';
f932cffa
DC
1206 if (dv === 'vv' && !allowSpice) {
1207 dv = (allowXtermjs) ? 'xtermjs' : 'html5';
1208 } else if (dv === 'xtermjs' && !allowXtermjs) {
1209 dv = (allowSpice) ? 'vv' : 'html5';
b0a6d326
EK
1210 }
1211
1212 return dv;
1213 },
1214
8eccc68f 1215 openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
af89f682
TL
1216 let scaling = 'off';
1217 if (Proxmox.Utils.toolkit !== 'touch') {
1218 var sp = Ext.state.Manager.getProvider();
1219 scaling = sp.get('novnc-scaling', 'off');
1220 }
8eccc68f 1221 var url = Ext.Object.toQueryString({
9e361643 1222 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 1223 novnc: 1,
b0a6d326
EK
1224 vmid: vmid,
1225 vmname: vmname,
16e64c97 1226 node: nodename,
af89f682 1227 resize: scaling,
8eccc68f 1228 cmd: cmd
b0a6d326
EK
1229 });
1230 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
7af1ab47
DC
1231 if (nw) {
1232 nw.focus();
1233 }
b0a6d326
EK
1234 },
1235
1236 openSpiceViewer: function(url, params){
1237
1238 var downloadWithName = function(uri, name) {
1239 var link = Ext.DomHelper.append(document.body, {
1240 tag: 'a',
1241 href: uri,
1242 css : 'display:none;visibility:hidden;height:0px;'
1243 });
1244
1245 // Note: we need to tell android the correct file name extension
1246 // but we do not set 'download' tag for other environments, because
1247 // It can have strange side effects (additional user prompt on firefox)
1248 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1249 if (andriod) {
1250 link.download = name;
1251 }
1252
1253 if (link.fireEvent) {
1254 link.fireEvent('onclick');
1255 } else {
953f6e9b
TL
1256 let evt = document.createEvent("MouseEvents");
1257 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
b0a6d326
EK
1258 link.dispatchEvent(evt);
1259 }
1260 };
1261
e7ade592 1262 Proxmox.Utils.API2Request({
b0a6d326
EK
1263 url: url,
1264 params: params,
1265 method: 'POST',
1266 failure: function(response, opts){
1267 Ext.Msg.alert('Error', response.htmlStatus);
1268 },
1269 success: function(response, opts){
1270 var raw = "[virt-viewer]\n";
1271 Ext.Object.each(response.result.data, function(k, v) {
1272 raw += k + "=" + v + "\n";
1273 });
1274 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1275 encodeURIComponent(raw);
0be88ae1 1276
b0a6d326
EK
1277 downloadWithName(url, "pve-spice.vv");
1278 }
1279 });
1280 },
1281
e3129443
DC
1282 openTreeConsole: function(tree, record, item, index, e) {
1283 e.stopEvent();
1284 var nodename = record.data.node;
1285 var vmid = record.data.vmid;
1286 var vmname = record.data.name;
1287 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 1288 Proxmox.Utils.API2Request({
e3129443
DC
1289 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1290 failure: function(response, opts) {
1291 Ext.Msg.alert('Error', response.htmlStatus);
1292 },
1293 success: function(response, opts) {
bd9537d7 1294 let conf = response.result.data;
54453c38 1295 var consoles = {
bd9537d7
TL
1296 spice: !!conf.spice,
1297 xtermjs: !!conf.serial,
54453c38
DC
1298 };
1299 PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
e3129443
DC
1300 }
1301 });
1302 } else if (record.data.type === 'lxc' && !record.data.template) {
1303 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1304 }
1305 },
1306
fbd60cfd
DM
1307 // test automation helper
1308 call_menu_handler: function(menu, text) {
1309
1310 var list = menu.query('menuitem');
1311
1312 Ext.Array.each(list, function(item) {
1313 if (item.text === text) {
1314 if (item.handler) {
1315 item.handler();
1316 return 1;
1317 } else {
1318 return undefined;
1319 }
1320 }
1321 });
1322 },
1323
685b7aa4
DC
1324 createCmdMenu: function(v, record, item, index, event) {
1325 event.stopEvent();
cc1a91be
DC
1326 if (!(v instanceof Ext.tree.View)) {
1327 v.select(record);
1328 }
685b7aa4 1329 var menu;
9bad05bd
DC
1330 var template = !!record.data.template;
1331 var type = record.data.type;
685b7aa4 1332
9bad05bd
DC
1333 if (template) {
1334 if (type === 'qemu' || type == 'lxc') {
1335 menu = Ext.create('PVE.menu.TemplateMenu', {
1336 pveSelNode: record
1337 });
1338 }
1339 } else if (type === 'qemu' ||
1340 type === 'lxc' ||
1341 type === 'node') {
1342 menu = Ext.create('PVE.' + type + '.CmdMenu', {
1343 pveSelNode: record,
c11ab8cb
DC
1344 nodename: record.data.node
1345 });
685b7aa4
DC
1346 } else {
1347 return;
1348 }
1349
1350 menu.showAt(event.getXY());
9f0b4e04 1351 return menu;
e7ade592 1352 },
9fa2e36d 1353
fe4f00ad
TL
1354 // helper for deleting field which are set to there default values
1355 delete_if_default: function(values, fieldname, default_val, create) {
1356 if (values[fieldname] === '' || values[fieldname] === default_val) {
1357 if (!create) {
1358 if (values['delete']) {
2db8e90d
DC
1359 if (Ext.isArray(values['delete'])) {
1360 values['delete'].push(fieldname);
1361 } else {
1362 values['delete'] += ',' + fieldname;
1363 }
fe4f00ad
TL
1364 } else {
1365 values['delete'] = fieldname;
1366 }
1367 }
1368
1369 delete values[fieldname];
1370 }
857b97a7
TL
1371 },
1372
1373 loadSSHKeyFromFile: function(file, callback) {
1374 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1375 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1376 // assume: 740*8 for max. 32kbit (5920 byte file)
1377 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1378 if (file.size > 8192) {
1379 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1380 return;
1381 }
1382 /*global
1383 FileReader
1384 */
1385 var reader = new FileReader();
1386 reader.onload = function(evt) {
1387 callback(evt.target.result);
1388 };
1389 reader.readAsText(file);
abe824aa
DC
1390 },
1391
7dda153c
TL
1392 loadTextFromFile: function(file, callback, maxBytes) {
1393 let maxSize = maxBytes || 8192;
1394 if (file.size > maxSize) {
1395 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1396 return;
1397 }
1398 /*global
1399 FileReader
1400 */
1401 let reader = new FileReader();
1402 reader.onload = evt => callback(evt.target.result);
1403 reader.readAsText(file);
1404 },
1405
8c4ec8c7
TL
1406 diskControllerMaxIDs: {
1407 ide: 4,
1408 sata: 6,
cf0d139e 1409 scsi: 31,
8c4ec8c7
TL
1410 virtio: 16,
1411 },
abe824aa
DC
1412
1413 // types is either undefined (all busses), an array of busses, or a single bus
1414 forEachBus: function(types, func) {
8c4ec8c7 1415 var busses = Object.keys(PVE.Utils.diskControllerMaxIDs);
abe824aa
DC
1416 var i, j, count, cont;
1417
1418 if (Ext.isArray(types)) {
1419 busses = types;
1420 } else if (Ext.isDefined(types)) {
1421 busses = [ types ];
1422 }
1423
1424 // check if we only have valid busses
1425 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1426 if (!PVE.Utils.diskControllerMaxIDs[busses[i]]) {
abe824aa
DC
1427 throw "invalid bus: '" + busses[i] + "'";
1428 }
1429 }
1430
1431 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1432 count = PVE.Utils.diskControllerMaxIDs[busses[i]];
abe824aa
DC
1433 for (j = 0; j < count; j++) {
1434 cont = func(busses[i], j);
1435 if (!cont && cont !== undefined) {
1436 return;
1437 }
1438 }
1439 }
14a845bc
DC
1440 },
1441
483bd394 1442 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1443
1444 forEachMP: function(func, includeUnused) {
1445 var i, cont;
1446 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1447 cont = func('mp', i);
1448 if (!cont && cont !== undefined) {
1449 return;
1450 }
1451 }
1452
1453 if (!includeUnused) {
1454 return;
1455 }
1456
1457 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1458 cont = func('unused', i);
1459 if (!cont && cont !== undefined) {
1460 return;
1461 }
1462 }
b945c7c1
TM
1463 },
1464
6c1d8ead 1465 hardware_counts: { net: 32, usb: 5, hostpci: 16, audio: 1, efidisk: 1, serial: 4, rng: 1 },
9d855398 1466
b945c7c1
TM
1467 cleanEmptyObjectKeys: function (obj) {
1468 var propName;
1469 for (propName in obj) {
1470 if (obj.hasOwnProperty(propName)) {
1471 if (obj[propName] === null || obj[propName] === undefined) {
1472 delete obj[propName];
1473 }
1474 }
1475 }
4616a55b
TM
1476 },
1477
eadbbb4a
DC
1478 acmedomain_count: 5,
1479
1480 add_domain_to_acme: function(acme, domain) {
1481 if (acme.domains === undefined) {
1482 acme.domains = [domain];
1483 } else {
1484 acme.domains.push(domain);
1485 acme.domains = acme.domains.filter((value, index, self) => {
1486 return self.indexOf(value) === index;
1487 });
1488 }
1489 return acme;
1490 },
1491
1492 remove_domain_from_acme: function(acme, domain) {
1493 if (acme.domains !== undefined) {
1494 acme.domains = acme.domains.filter((value, index, self) => {
1495 return self.indexOf(value) === index && value !== domain;
1496 });
1497 }
1498 return acme;
1499 },
1500
4616a55b
TM
1501 handleStoreErrorOrMask: function(me, store, regex, callback) {
1502
1503 me.mon(store, 'load', function (proxy, response, success, operation) {
1504
1505 if (success) {
1506 Proxmox.Utils.setErrorMask(me, false);
1507 return;
1508 }
1509 var msg;
1510
1511 if (operation.error.statusText) {
1512 if (operation.error.statusText.match(regex)) {
1513 callback(me, operation.error);
1514 return;
1515 } else {
1516 msg = operation.error.statusText + ' (' + operation.error.status + ')';
1517 }
1518 } else {
1519 msg = gettext('Connection error');
1520 }
1521 Proxmox.Utils.setErrorMask(me, msg);
1522 });
1523 },
1524
1525 showCephInstallOrMask: function(container, msg, nodename, callback){
1526 var regex = new RegExp("not (installed|initialized)", "i");
1527 if (msg.match(regex)) {
1528 if (Proxmox.UserName === 'root@pam') {
1529 container.el.mask();
1530 if (!container.down('pveCephInstallWindow')){
f992ef80 1531 var isInstalled = msg.match(/not initialized/i) ? true : false;
4616a55b
TM
1532 var win = Ext.create('PVE.ceph.Install', {
1533 nodename: nodename
1534 });
f992ef80 1535 win.getViewModel().set('isInstalled', isInstalled);
4616a55b
TM
1536 container.add(win);
1537 win.show();
1538 callback(win);
1539 }
1540 } else {
a7e8b87b
TL
1541 container.mask(Ext.String.format(gettext('{0} not installed.') +
1542 ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
4616a55b
TM
1543 }
1544 return true;
1545 } else {
1546 return false;
1547 }
49dfba72
DC
1548 },
1549
1550 propertyStringSet: function(target, source, name, value) {
1551 if (source) {
1552 if (value === undefined) {
1553 target[name] = source;
1554 } else {
1555 target[name] = value;
1556 }
1557 } else {
1558 delete target[name];
1559 }
bbc83309
DC
1560 },
1561
1562 updateColumns: function(container) {
f973c5b2
DC
1563 let mode = Ext.state.Manager.get('summarycolumns') || 'auto';
1564 let factor;
1565 if (mode !== 'auto') {
1566 factor = parseInt(mode, 10);
1567 if (Number.isNaN(factor)) {
1568 factor = 1;
1569 }
1570 } else {
1571 factor = container.getSize().width < 1400 ? 1 : 2;
1572 }
bbc83309
DC
1573
1574 if (container.oldFactor === factor) {
1575 return;
1576 }
1577
1578 let items = container.query('>'); // direct childs
1579 factor = Math.min(factor, items.length);
1580 container.oldFactor = factor;
1581
1582 items.forEach((item) => {
1583 item.columnWidth = 1 / factor;
1584 });
1585
1586 // we have to update the layout twice, since the first layout change
1587 // can trigger the scrollbar which reduces the amount of space left
1588 container.updateLayout();
1589 container.updateLayout();
1590 },
e65817a1
SR
1591
1592 forEachCorosyncLink: function(nodeinfo, cb) {
1593 let re = /(?:ring|link)(\d+)_addr/;
1594 Ext.iterate(nodeinfo, (prop, val) => {
1595 let match = re.exec(prop);
1596 if (match) {
1597 cb(Number(match[1]), val);
1598 }
1599 });
1600 },
4546808c
SR
1601
1602 cpu_vendor_map: {
1603 'default': 'QEMU',
1604 'AuthenticAMD': 'AMD',
1605 'GenuineIntel': 'Intel'
1606 },
1607
1608 cpu_vendor_order: {
1609 "AMD": 1,
1610 "Intel": 2,
1611 "QEMU": 3,
1612 "Host": 4,
1613 "_default_": 5, // includes custom models
1614 },
5865b597
WB
1615
1616 verify_ip64_address_list: function(value, with_suffix) {
1617 for (let addr of value.split(/[ ,;]+/)) {
1618 if (addr === '') {
1619 continue;
1620 }
1621
1622 if (with_suffix) {
1623 let parts = addr.split('%');
1624 addr = parts[0];
1625
1626 if (parts.length > 2) {
1627 return false;
1628 }
1629
1630 if (parts.length > 1 && !addr.startsWith('fe80:')) {
1631 return false;
1632 }
1633 }
1634
1635 if (!Proxmox.Utils.IP64_match.test(addr)) {
1636 return false;
1637 }
1638 }
1639
1640 return true;
1641 },
e7ade592 1642},
fe4f00ad 1643
9fa2e36d
EK
1644 singleton: true,
1645 constructor: function() {
1646 var me = this;
1647 Ext.apply(me, me.utilities);
d18e15dd
DC
1648
1649 Proxmox.Utils.override_task_descriptions({
1650 acmedeactivate: ['ACME Account', gettext('Deactivate')],
1651 acmenewcert: ['SRV', gettext('Order Certificate')],
1652 acmerefresh: ['ACME Account', gettext('Refresh')],
1653 acmeregister: ['ACME Account', gettext('Register')],
1654 acmerenew: ['SRV', gettext('Renew Certificate')],
1655 acmerevoke: ['SRV', gettext('Revoke Certificate')],
1656 acmeupdate: ['ACME Account', gettext('Update')],
1657 'auth-realm-sync': [gettext('Realm'), gettext('Sync')],
1658 'auth-realm-sync-test': [gettext('Realm'), gettext('Sync Preview')],
1659 cephcreatemds: ['Ceph Metadata Server', gettext('Create')],
1660 cephcreatemgr: ['Ceph Manager', gettext('Create')],
1661 cephcreatemon: ['Ceph Monitor', gettext('Create')],
1662 cephcreateosd: ['Ceph OSD', gettext('Create')],
1663 cephcreatepool: ['Ceph Pool', gettext('Create')],
1664 cephdestroymds: ['Ceph Metadata Server', gettext('Destroy')],
1665 cephdestroymgr: ['Ceph Manager', gettext('Destroy')],
1666 cephdestroymon: ['Ceph Monitor', gettext('Destroy')],
1667 cephdestroyosd: ['Ceph OSD', gettext('Destroy')],
1668 cephdestroypool: ['Ceph Pool', gettext('Destroy')],
1669 cephfscreate: ['CephFS', gettext('Create')],
1670 clustercreate: ['', gettext('Create Cluster')],
1671 clusterjoin: ['', gettext('Join Cluster')],
1672 dircreate: [gettext('Directory Storage'), gettext('Create')],
1673 dirremove: [gettext('Directory'), gettext('Remove')],
1674 download: ['', gettext('Download')],
1675 hamigrate: ['HA', gettext('Migrate')],
1676 hashutdown: ['HA', gettext('Shutdown')],
1677 hastart: ['HA', gettext('Start')],
1678 hastop: ['HA', gettext('Stop')],
1679 imgcopy: ['', gettext('Copy data')],
1680 imgdel: ['', gettext('Erase data')],
1681 lvmcreate: [gettext('LVM Storage'), gettext('Create')],
1682 lvmthincreate: [gettext('LVM-Thin Storage'), gettext('Create')],
1683 migrateall: ['', gettext('Migrate all VMs and Containers')],
1684 'move_volume': ['CT', gettext('Move Volume')],
1685 pull_file: ['CT', gettext('Pull file')],
1686 push_file: ['CT', gettext('Push file')],
1687 qmclone: ['VM', gettext('Clone')],
1688 qmconfig: ['VM', gettext('Configure')],
1689 qmcreate: ['VM', gettext('Create')],
1690 qmdelsnapshot: ['VM', gettext('Delete Snapshot')],
1691 qmdestroy: ['VM', gettext('Destroy')],
1692 qmigrate: ['VM', gettext('Migrate')],
1693 qmmove: ['VM', gettext('Move disk')],
1694 qmpause: ['VM', gettext('Pause')],
1695 qmreboot: ['VM', gettext('Reboot')],
1696 qmreset: ['VM', gettext('Reset')],
1697 qmrestore: ['VM', gettext('Restore')],
1698 qmresume: ['VM', gettext('Resume')],
1699 qmrollback: ['VM', gettext('Rollback')],
1700 qmshutdown: ['VM', gettext('Shutdown')],
1701 qmsnapshot: ['VM', gettext('Snapshot')],
1702 qmstart: ['VM', gettext('Start')],
1703 qmstop: ['VM', gettext('Stop')],
1704 qmsuspend: ['VM', gettext('Hibernate')],
1705 qmtemplate: ['VM', gettext('Convert to template')],
1706 spiceproxy: ['VM/CT', gettext('Console') + ' (Spice)'],
1707 spiceshell: ['', gettext('Shell') + ' (Spice)'],
1708 startall: ['', gettext('Start all VMs and Containers')],
1709 stopall: ['', gettext('Stop all VMs and Containers')],
1710 unknownimgdel: ['', gettext('Destroy image from unknown guest')],
1711 vncproxy: ['VM/CT', gettext('Console')],
1712 vncshell: ['', gettext('Shell')],
1713 vzclone: ['CT', gettext('Clone')],
1714 vzcreate: ['CT', gettext('Create')],
1715 vzdelsnapshot: ['CT', gettext('Delete Snapshot')],
1716 vzdestroy: ['CT', gettext('Destroy')],
1717 vzdump: (type, id) => id ? `VM/CT ${id} - ${gettext('Backup')}` : gettext('Backup Job'),
1718 vzmigrate: ['CT', gettext('Migrate')],
1719 vzmount: ['CT', gettext('Mount')],
1720 vzreboot: ['CT', gettext('Reboot')],
1721 vzrestore: ['CT', gettext('Restore')],
1722 vzresume: ['CT', gettext('Resume')],
1723 vzrollback: ['CT', gettext('Rollback')],
1724 vzshutdown: ['CT', gettext('Shutdown')],
1725 vzsnapshot: ['CT', gettext('Snapshot')],
1726 vzstart: ['CT', gettext('Start')],
1727 vzstop: ['CT', gettext('Stop')],
1728 vzsuspend: ['CT', gettext('Suspend')],
1729 vztemplate: ['CT', gettext('Convert to template')],
1730 vzumount: ['CT', gettext('Unmount')],
1731 zfscreate: [gettext('ZFS Storage'), gettext('Create')],
1732 });
685b7aa4 1733 }
e7ade592 1734
9fa2e36d 1735});