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