]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
ceph: add get api call for single pool
[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
945 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
b0a6d326
EK
946 if (!Ext.isNumeric(value)) {
947 return '';
948 }
949
e7ade592 950 return Proxmox.Utils.format_size(value);
b0a6d326
EK
951 },
952
946730cd
DC
953 render_bandwidth: function(value) {
954 if (!Ext.isNumeric(value)) {
955 return '';
956 }
957
e7ade592 958 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
959 },
960
3f633655
EK
961 render_timestamp_human_readable: function(value) {
962 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
963 },
964
0bfc799f
DC
965 calculate_mem_usage: function(data) {
966 if (!Ext.isNumeric(data.mem) ||
967 data.maxmem === 0 ||
968 data.uptime < 1) {
969 return -1;
970 }
971
53e3ea84 972 return data.mem / data.maxmem;
0bfc799f
DC
973 },
974
975 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
976 if (!Ext.isNumeric(value) || value === -1) {
977 return '';
978 }
8058410f 979 if (value > 1) {
0bfc799f
DC
980 // we got no percentage but bytes
981 var mem = value;
982 var maxmem = record.data.maxmem;
983 if (!record.data.uptime ||
984 maxmem === 0 ||
985 !Ext.isNumeric(mem)) {
986 return '';
987 }
988
53e3ea84 989 return (mem*100/maxmem).toFixed(1) + " %";
0bfc799f
DC
990 }
991 return (value*100).toFixed(1) + " %";
992 },
993
b0a6d326 994 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
b0a6d326
EK
995 var mem = value;
996 var maxmem = record.data.maxmem;
0be88ae1 997
b0a6d326
EK
998 if (!record.data.uptime) {
999 return '';
1000 }
1001
1002 if (!(Ext.isNumeric(mem) && maxmem)) {
1003 return '';
1004 }
1005
728f1b97 1006 return PVE.Utils.render_size(value);
b0a6d326
EK
1007 },
1008
0bfc799f 1009 calculate_disk_usage: function(data) {
0bfc799f
DC
1010 if (!Ext.isNumeric(data.disk) ||
1011 data.type === 'qemu' ||
53e3ea84 1012 data.type === 'lxc' && data.uptime === 0 ||
0bfc799f
DC
1013 data.maxdisk === 0) {
1014 return -1;
1015 }
1016
53e3ea84 1017 return data.disk / data.maxdisk;
0bfc799f
DC
1018 },
1019
1020 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
1021 if (!Ext.isNumeric(value) || value === -1) {
1022 return '';
1023 }
1024
1025 return (value * 100).toFixed(1) + " %";
1026 },
1027
b0a6d326 1028 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
b0a6d326
EK
1029 var disk = value;
1030 var maxdisk = record.data.maxdisk;
728f1b97 1031 var type = record.data.type;
b0a6d326 1032
728f1b97
DC
1033 if (!Ext.isNumeric(disk) ||
1034 type === 'qemu' ||
1035 maxdisk === 0 ||
53e3ea84 1036 type === 'lxc' && record.data.uptime === 0) {
b0a6d326
EK
1037 return '';
1038 }
1039
728f1b97 1040 return PVE.Utils.render_size(value);
b0a6d326
EK
1041 },
1042
4dbc64a7
DC
1043 get_object_icon_class: function(type, record) {
1044 var status = '';
1045 var objType = type;
1046
1047 if (type === 'type') {
1048 // for folder view
1049 objType = record.groupbyid;
1050 } else if (record.template) {
1051 // templates
1052 objType = 'template';
1053 status = type;
1054 } else {
1055 // everything else
1056 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
1057 }
1058
6284a48a
DC
1059 if (record.lock) {
1060 status += ' locked lock-' + record.lock;
1061 }
1062
4dbc64a7
DC
1063 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
1064 if (defaults && defaults.iconCls) {
1065 var retVal = defaults.iconCls + ' ' + status;
1066 return retVal;
b0a6d326
EK
1067 }
1068
4dbc64a7
DC
1069 return '';
1070 },
1071
1072 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
f6710aac 1073 var cls = PVE.Utils.get_object_icon_class(value, record.data);
2b2fe160 1074
8058410f 1075 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 1076 return fa + value;
b0a6d326
EK
1077 },
1078
b0a6d326
EK
1079 render_support_level: function(value, metaData, record) {
1080 return PVE.Utils.support_level_hash[value] || '-';
1081 },
1082
0be88ae1 1083 render_upid: function(value, metaData, record) {
b0a6d326
EK
1084 var type = record.data.type;
1085 var id = record.data.id;
1086
e7ade592 1087 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
1088 },
1089
054ac1b8
DC
1090 /* render functions for new status panel */
1091
1092 render_usage: function(val) {
1093 return (val*100).toFixed(2) + '%';
1094 },
1095
1096 render_cpu_usage: function(val, max) {
1097 return Ext.String.format(gettext('{0}% of {1}') +
1098 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
1099 },
1100
1101 render_size_usage: function(val, max) {
bab64974
DC
1102 if (max === 0) {
1103 return gettext('N/A');
1104 }
054ac1b8
DC
1105 return (val*100/max).toFixed(2) + '% '+ '(' +
1106 Ext.String.format(gettext('{0} of {1}'),
1107 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
1108 },
1109
1110 /* this is different for nodes */
1111 render_node_cpu_usage: function(value, record) {
1112 return PVE.Utils.render_cpu_usage(value, record.cpus);
1113 },
1114
1115 /* this is different for nodes */
1116 render_node_size_usage: function(record) {
1117 return PVE.Utils.render_size_usage(record.used, record.total);
1118 },
1119
27809975
DC
1120 render_optional_url: function(value) {
1121 var match;
1122 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 1123 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
1124 }
1125 return value;
1126 },
1127
1128 render_san: function(value) {
1129 var names = [];
1130 if (Ext.isArray(value)) {
1131 value.forEach(function(val) {
1132 if (!Ext.isNumber(val)) {
1133 names.push(val);
1134 }
1135 });
1136 return names.join('<br>');
1137 }
1138 return value;
1139 },
1140
6ad4be69
DC
1141 render_full_name: function(firstname, metaData, record) {
1142 var first = firstname || '';
1143 var last = record.data.lastname || '';
1144 return Ext.htmlEncode(first + " " + last);
1145 },
1146
2d41c7e6
TL
1147 render_u2f_error: function(error) {
1148 var ErrorNames = {
1149 '1': gettext('Other Error'),
1150 '2': gettext('Bad Request'),
1151 '3': gettext('Configuration Unsupported'),
1152 '4': gettext('Device Ineligible'),
f6710aac 1153 '5': gettext('Timeout'),
2d41c7e6 1154 };
8058410f 1155 return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
2d41c7e6
TL
1156 },
1157
aa0819a8 1158 windowHostname: function() {
e7ade592 1159 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
1160 function(m, addr, offset, original) { return addr; });
1161 },
0be88ae1 1162
953f6e9b 1163 openDefaultConsoleWindow: function(consoles, consoleType, vmid, nodename, vmname, cmd) {
3438c27e 1164 var dv = PVE.Utils.defaultViewer(consoles);
953f6e9b 1165 PVE.Utils.openConsoleWindow(dv, consoleType, vmid, nodename, vmname, cmd);
b0a6d326
EK
1166 },
1167
953f6e9b
TL
1168 openConsoleWindow: function(viewer, consoleType, vmid, nodename, vmname, cmd) {
1169 if (vmid == undefined && (consoleType === 'kvm' || consoleType === 'lxc')) {
b0a6d326
EK
1170 throw "missing vmid";
1171 }
b0a6d326
EK
1172 if (!nodename) {
1173 throw "no nodename specified";
1174 }
1175
c7218ab3 1176 if (viewer === 'html5') {
953f6e9b 1177 PVE.Utils.openVNCViewer(consoleType, vmid, nodename, vmname, cmd);
c6b2336c 1178 } else if (viewer === 'xtermjs') {
953f6e9b 1179 Proxmox.Utils.openXtermJsViewer(consoleType, vmid, nodename, vmname, cmd);
b0a6d326 1180 } else if (viewer === 'vv') {
953f6e9b
TL
1181 let url = '/nodes/' + nodename + '/spiceshell';
1182 let params = {
1183 proxy: PVE.Utils.windowHostname(),
1184 };
1185 if (consoleType === 'kvm') {
b0a6d326 1186 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
953f6e9b 1187 } else if (consoleType === 'lxc') {
9e361643 1188 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
953f6e9b
TL
1189 } else if (consoleType === 'upgrade') {
1190 params.cmd = 'upgrade';
1191 } else if (consoleType === 'cmd') {
8eccc68f 1192 params.cmd = cmd;
953f6e9b
TL
1193 } else if (consoleType !== 'shell') {
1194 throw `unknown spice viewer type '${consoleType}'`;
b0a6d326 1195 }
953f6e9b 1196 PVE.Utils.openSpiceViewer(url, params);
b0a6d326 1197 } else {
953f6e9b 1198 throw `unknown viewer type '${viewer}'`;
b0a6d326
EK
1199 }
1200 },
1201
3438c27e 1202 defaultViewer: function(consoles) {
3438c27e
DC
1203 var allowSpice, allowXtermjs;
1204
1205 if (consoles === true) {
1206 allowSpice = true;
1207 allowXtermjs = true;
1208 } else if (typeof consoles === 'object') {
1209 allowSpice = consoles.spice;
4ace5c6f 1210 allowXtermjs = !!consoles.xtermjs;
3438c27e 1211 }
da9d14cd 1212 var dv = PVE.VersionInfo.console || 'xtermjs';
f932cffa 1213 if (dv === 'vv' && !allowSpice) {
53e3ea84 1214 dv = allowXtermjs ? 'xtermjs' : 'html5';
f932cffa 1215 } else if (dv === 'xtermjs' && !allowXtermjs) {
53e3ea84 1216 dv = allowSpice ? 'vv' : 'html5';
b0a6d326
EK
1217 }
1218
1219 return dv;
1220 },
1221
8eccc68f 1222 openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
af89f682
TL
1223 let scaling = 'off';
1224 if (Proxmox.Utils.toolkit !== 'touch') {
1225 var sp = Ext.state.Manager.getProvider();
1226 scaling = sp.get('novnc-scaling', 'off');
1227 }
8eccc68f 1228 var url = Ext.Object.toQueryString({
9e361643 1229 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 1230 novnc: 1,
b0a6d326
EK
1231 vmid: vmid,
1232 vmname: vmname,
16e64c97 1233 node: nodename,
af89f682 1234 resize: scaling,
f6710aac 1235 cmd: cmd,
b0a6d326
EK
1236 });
1237 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
7af1ab47
DC
1238 if (nw) {
1239 nw.focus();
1240 }
b0a6d326
EK
1241 },
1242
8058410f 1243 openSpiceViewer: function(url, params) {
b0a6d326
EK
1244 var downloadWithName = function(uri, name) {
1245 var link = Ext.DomHelper.append(document.body, {
1246 tag: 'a',
1247 href: uri,
8058410f 1248 css: 'display:none;visibility:hidden;height:0px;',
b0a6d326
EK
1249 });
1250
1251 // Note: we need to tell android the correct file name extension
1252 // but we do not set 'download' tag for other environments, because
1253 // It can have strange side effects (additional user prompt on firefox)
ef725143 1254 var andriod = !!navigator.userAgent.match(/Android/i);
b0a6d326
EK
1255 if (andriod) {
1256 link.download = name;
1257 }
1258
1259 if (link.fireEvent) {
1260 link.fireEvent('onclick');
1261 } else {
953f6e9b
TL
1262 let evt = document.createEvent("MouseEvents");
1263 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
b0a6d326
EK
1264 link.dispatchEvent(evt);
1265 }
1266 };
1267
e7ade592 1268 Proxmox.Utils.API2Request({
b0a6d326
EK
1269 url: url,
1270 params: params,
1271 method: 'POST',
8058410f 1272 failure: function(response, opts) {
b0a6d326
EK
1273 Ext.Msg.alert('Error', response.htmlStatus);
1274 },
8058410f 1275 success: function(response, opts) {
b0a6d326
EK
1276 var raw = "[virt-viewer]\n";
1277 Ext.Object.each(response.result.data, function(k, v) {
1278 raw += k + "=" + v + "\n";
1279 });
1280 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1281 encodeURIComponent(raw);
0be88ae1 1282
b0a6d326 1283 downloadWithName(url, "pve-spice.vv");
f6710aac 1284 },
b0a6d326
EK
1285 });
1286 },
1287
e3129443
DC
1288 openTreeConsole: function(tree, record, item, index, e) {
1289 e.stopEvent();
1290 var nodename = record.data.node;
1291 var vmid = record.data.vmid;
1292 var vmname = record.data.name;
1293 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 1294 Proxmox.Utils.API2Request({
e3129443
DC
1295 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1296 failure: function(response, opts) {
1297 Ext.Msg.alert('Error', response.htmlStatus);
1298 },
1299 success: function(response, opts) {
bd9537d7 1300 let conf = response.result.data;
54453c38 1301 var consoles = {
bd9537d7
TL
1302 spice: !!conf.spice,
1303 xtermjs: !!conf.serial,
54453c38
DC
1304 };
1305 PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
f6710aac 1306 },
e3129443
DC
1307 });
1308 } else if (record.data.type === 'lxc' && !record.data.template) {
1309 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1310 }
1311 },
1312
fbd60cfd
DM
1313 // test automation helper
1314 call_menu_handler: function(menu, text) {
fbd60cfd
DM
1315 var list = menu.query('menuitem');
1316
1317 Ext.Array.each(list, function(item) {
1318 if (item.text === text) {
1319 if (item.handler) {
1320 item.handler();
1321 return 1;
1322 } else {
1323 return undefined;
1324 }
1325 }
1326 });
1327 },
1328
685b7aa4
DC
1329 createCmdMenu: function(v, record, item, index, event) {
1330 event.stopEvent();
cc1a91be
DC
1331 if (!(v instanceof Ext.tree.View)) {
1332 v.select(record);
1333 }
685b7aa4 1334 var menu;
9bad05bd
DC
1335 var template = !!record.data.template;
1336 var type = record.data.type;
685b7aa4 1337
9bad05bd
DC
1338 if (template) {
1339 if (type === 'qemu' || type == 'lxc') {
1340 menu = Ext.create('PVE.menu.TemplateMenu', {
f6710aac 1341 pveSelNode: record,
9bad05bd
DC
1342 });
1343 }
1344 } else if (type === 'qemu' ||
1345 type === 'lxc' ||
1346 type === 'node') {
1347 menu = Ext.create('PVE.' + type + '.CmdMenu', {
1348 pveSelNode: record,
f6710aac 1349 nodename: record.data.node,
c11ab8cb 1350 });
685b7aa4
DC
1351 } else {
1352 return;
1353 }
1354
1355 menu.showAt(event.getXY());
9f0b4e04 1356 return menu;
e7ade592 1357 },
9fa2e36d 1358
fe4f00ad
TL
1359 // helper for deleting field which are set to there default values
1360 delete_if_default: function(values, fieldname, default_val, create) {
1361 if (values[fieldname] === '' || values[fieldname] === default_val) {
1362 if (!create) {
399ffa76
TL
1363 if (values.delete) {
1364 if (Ext.isArray(values.delete)) {
1365 values.delete.push(fieldname);
2db8e90d 1366 } else {
399ffa76 1367 values.delete += ',' + fieldname;
2db8e90d 1368 }
fe4f00ad 1369 } else {
399ffa76 1370 values.delete = fieldname;
fe4f00ad
TL
1371 }
1372 }
1373
1374 delete values[fieldname];
1375 }
857b97a7
TL
1376 },
1377
1378 loadSSHKeyFromFile: function(file, callback) {
1379 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1380 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1381 // assume: 740*8 for max. 32kbit (5920 byte file)
1382 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1383 if (file.size > 8192) {
1384 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1385 return;
1386 }
1387 /*global
1388 FileReader
1389 */
1390 var reader = new FileReader();
1391 reader.onload = function(evt) {
1392 callback(evt.target.result);
1393 };
1394 reader.readAsText(file);
abe824aa
DC
1395 },
1396
7dda153c
TL
1397 loadTextFromFile: function(file, callback, maxBytes) {
1398 let maxSize = maxBytes || 8192;
1399 if (file.size > maxSize) {
1400 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1401 return;
1402 }
1403 /*global
1404 FileReader
1405 */
1406 let reader = new FileReader();
1407 reader.onload = evt => callback(evt.target.result);
1408 reader.readAsText(file);
1409 },
1410
8c4ec8c7
TL
1411 diskControllerMaxIDs: {
1412 ide: 4,
1413 sata: 6,
cf0d139e 1414 scsi: 31,
8c4ec8c7
TL
1415 virtio: 16,
1416 },
abe824aa
DC
1417
1418 // types is either undefined (all busses), an array of busses, or a single bus
1419 forEachBus: function(types, func) {
8c4ec8c7 1420 var busses = Object.keys(PVE.Utils.diskControllerMaxIDs);
abe824aa
DC
1421 var i, j, count, cont;
1422
1423 if (Ext.isArray(types)) {
1424 busses = types;
1425 } else if (Ext.isDefined(types)) {
8058410f 1426 busses = [types];
abe824aa
DC
1427 }
1428
1429 // check if we only have valid busses
1430 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1431 if (!PVE.Utils.diskControllerMaxIDs[busses[i]]) {
abe824aa
DC
1432 throw "invalid bus: '" + busses[i] + "'";
1433 }
1434 }
1435
1436 for (i = 0; i < busses.length; i++) {
8c4ec8c7 1437 count = PVE.Utils.diskControllerMaxIDs[busses[i]];
abe824aa
DC
1438 for (j = 0; j < count; j++) {
1439 cont = func(busses[i], j);
1440 if (!cont && cont !== undefined) {
1441 return;
1442 }
1443 }
1444 }
14a845bc
DC
1445 },
1446
483bd394 1447 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1448
1449 forEachMP: function(func, includeUnused) {
1450 var i, cont;
1451 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1452 cont = func('mp', i);
1453 if (!cont && cont !== undefined) {
1454 return;
1455 }
1456 }
1457
1458 if (!includeUnused) {
1459 return;
1460 }
1461
1462 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1463 cont = func('unused', i);
1464 if (!cont && cont !== undefined) {
1465 return;
1466 }
1467 }
b945c7c1
TM
1468 },
1469
6c1d8ead 1470 hardware_counts: { net: 32, usb: 5, hostpci: 16, audio: 1, efidisk: 1, serial: 4, rng: 1 },
9d855398 1471
8058410f 1472 cleanEmptyObjectKeys: function(obj) {
b945c7c1
TM
1473 var propName;
1474 for (propName in obj) {
1475 if (obj.hasOwnProperty(propName)) {
1476 if (obj[propName] === null || obj[propName] === undefined) {
1477 delete obj[propName];
1478 }
1479 }
1480 }
4616a55b
TM
1481 },
1482
eadbbb4a
DC
1483 acmedomain_count: 5,
1484
1485 add_domain_to_acme: function(acme, domain) {
1486 if (acme.domains === undefined) {
1487 acme.domains = [domain];
1488 } else {
1489 acme.domains.push(domain);
8058410f 1490 acme.domains = acme.domains.filter((value, index, self) => self.indexOf(value) === index);
eadbbb4a
DC
1491 }
1492 return acme;
1493 },
1494
1495 remove_domain_from_acme: function(acme, domain) {
1496 if (acme.domains !== undefined) {
8058410f 1497 acme.domains = acme.domains.filter((value, index, self) => self.indexOf(value) === index && value !== domain);
eadbbb4a
DC
1498 }
1499 return acme;
1500 },
1501
4616a55b 1502 handleStoreErrorOrMask: function(me, store, regex, callback) {
8058410f 1503 me.mon(store, 'load', function(proxy, response, success, operation) {
4616a55b
TM
1504 if (success) {
1505 Proxmox.Utils.setErrorMask(me, false);
1506 return;
1507 }
1508 var msg;
1509
1510 if (operation.error.statusText) {
1511 if (operation.error.statusText.match(regex)) {
1512 callback(me, operation.error);
1513 return;
1514 } else {
1515 msg = operation.error.statusText + ' (' + operation.error.status + ')';
1516 }
1517 } else {
1518 msg = gettext('Connection error');
1519 }
1520 Proxmox.Utils.setErrorMask(me, msg);
1521 });
1522 },
1523
8058410f 1524 showCephInstallOrMask: function(container, msg, nodename, callback) {
4616a55b
TM
1525 var regex = new RegExp("not (installed|initialized)", "i");
1526 if (msg.match(regex)) {
1527 if (Proxmox.UserName === 'root@pam') {
1528 container.el.mask();
8058410f 1529 if (!container.down('pveCephInstallWindow')) {
ef725143 1530 var isInstalled = !!msg.match(/not initialized/i);
4616a55b 1531 var win = Ext.create('PVE.ceph.Install', {
f6710aac 1532 nodename: nodename,
4616a55b 1533 });
f992ef80 1534 win.getViewModel().set('isInstalled', isInstalled);
4616a55b
TM
1535 container.add(win);
1536 win.show();
1537 callback(win);
1538 }
1539 } else {
a7e8b87b
TL
1540 container.mask(Ext.String.format(gettext('{0} not installed.') +
1541 ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
4616a55b
TM
1542 }
1543 return true;
1544 } else {
1545 return false;
1546 }
49dfba72
DC
1547 },
1548
1549 propertyStringSet: function(target, source, name, value) {
1550 if (source) {
1551 if (value === undefined) {
1552 target[name] = source;
1553 } else {
1554 target[name] = value;
1555 }
1556 } else {
1557 delete target[name];
1558 }
bbc83309
DC
1559 },
1560
1561 updateColumns: function(container) {
f973c5b2
DC
1562 let mode = Ext.state.Manager.get('summarycolumns') || 'auto';
1563 let factor;
1564 if (mode !== 'auto') {
1565 factor = parseInt(mode, 10);
1566 if (Number.isNaN(factor)) {
1567 factor = 1;
1568 }
1569 } else {
1570 factor = container.getSize().width < 1400 ? 1 : 2;
1571 }
bbc83309
DC
1572
1573 if (container.oldFactor === factor) {
1574 return;
1575 }
1576
1577 let items = container.query('>'); // direct childs
1578 factor = Math.min(factor, items.length);
1579 container.oldFactor = factor;
1580
1581 items.forEach((item) => {
1582 item.columnWidth = 1 / factor;
1583 });
1584
1585 // we have to update the layout twice, since the first layout change
1586 // can trigger the scrollbar which reduces the amount of space left
1587 container.updateLayout();
1588 container.updateLayout();
1589 },
e65817a1
SR
1590
1591 forEachCorosyncLink: function(nodeinfo, cb) {
1592 let re = /(?:ring|link)(\d+)_addr/;
1593 Ext.iterate(nodeinfo, (prop, val) => {
1594 let match = re.exec(prop);
1595 if (match) {
1596 cb(Number(match[1]), val);
1597 }
1598 });
1599 },
4546808c
SR
1600
1601 cpu_vendor_map: {
1602 'default': 'QEMU',
1603 'AuthenticAMD': 'AMD',
f6710aac 1604 'GenuineIntel': 'Intel',
4546808c
SR
1605 },
1606
1607 cpu_vendor_order: {
1608 "AMD": 1,
1609 "Intel": 2,
1610 "QEMU": 3,
1611 "Host": 4,
1612 "_default_": 5, // includes custom models
1613 },
5865b597
WB
1614
1615 verify_ip64_address_list: function(value, with_suffix) {
1616 for (let addr of value.split(/[ ,;]+/)) {
1617 if (addr === '') {
1618 continue;
1619 }
1620
1621 if (with_suffix) {
1622 let parts = addr.split('%');
1623 addr = parts[0];
1624
1625 if (parts.length > 2) {
1626 return false;
1627 }
1628
1629 if (parts.length > 1 && !addr.startsWith('fe80:')) {
1630 return false;
1631 }
1632 }
1633
1634 if (!Proxmox.Utils.IP64_match.test(addr)) {
1635 return false;
1636 }
1637 }
1638
1639 return true;
1640 },
e7ade592 1641},
fe4f00ad 1642
9fa2e36d
EK
1643 singleton: true,
1644 constructor: function() {
1645 var me = this;
1646 Ext.apply(me, me.utilities);
d18e15dd
DC
1647
1648 Proxmox.Utils.override_task_descriptions({
1649 acmedeactivate: ['ACME Account', gettext('Deactivate')],
1650 acmenewcert: ['SRV', gettext('Order Certificate')],
1651 acmerefresh: ['ACME Account', gettext('Refresh')],
1652 acmeregister: ['ACME Account', gettext('Register')],
1653 acmerenew: ['SRV', gettext('Renew Certificate')],
1654 acmerevoke: ['SRV', gettext('Revoke Certificate')],
1655 acmeupdate: ['ACME Account', gettext('Update')],
1656 'auth-realm-sync': [gettext('Realm'), gettext('Sync')],
1657 'auth-realm-sync-test': [gettext('Realm'), gettext('Sync Preview')],
1658 cephcreatemds: ['Ceph Metadata Server', gettext('Create')],
1659 cephcreatemgr: ['Ceph Manager', gettext('Create')],
1660 cephcreatemon: ['Ceph Monitor', gettext('Create')],
1661 cephcreateosd: ['Ceph OSD', gettext('Create')],
1662 cephcreatepool: ['Ceph Pool', gettext('Create')],
1663 cephdestroymds: ['Ceph Metadata Server', gettext('Destroy')],
1664 cephdestroymgr: ['Ceph Manager', gettext('Destroy')],
1665 cephdestroymon: ['Ceph Monitor', gettext('Destroy')],
1666 cephdestroyosd: ['Ceph OSD', gettext('Destroy')],
1667 cephdestroypool: ['Ceph Pool', gettext('Destroy')],
1668 cephfscreate: ['CephFS', gettext('Create')],
1669 clustercreate: ['', gettext('Create Cluster')],
1670 clusterjoin: ['', gettext('Join Cluster')],
1671 dircreate: [gettext('Directory Storage'), gettext('Create')],
1672 dirremove: [gettext('Directory'), gettext('Remove')],
1673 download: ['', gettext('Download')],
1674 hamigrate: ['HA', gettext('Migrate')],
1675 hashutdown: ['HA', gettext('Shutdown')],
1676 hastart: ['HA', gettext('Start')],
1677 hastop: ['HA', gettext('Stop')],
1678 imgcopy: ['', gettext('Copy data')],
1679 imgdel: ['', gettext('Erase data')],
1680 lvmcreate: [gettext('LVM Storage'), gettext('Create')],
1681 lvmthincreate: [gettext('LVM-Thin Storage'), gettext('Create')],
1682 migrateall: ['', gettext('Migrate all VMs and Containers')],
1683 'move_volume': ['CT', gettext('Move Volume')],
1684 pull_file: ['CT', gettext('Pull file')],
1685 push_file: ['CT', gettext('Push file')],
1686 qmclone: ['VM', gettext('Clone')],
1687 qmconfig: ['VM', gettext('Configure')],
1688 qmcreate: ['VM', gettext('Create')],
1689 qmdelsnapshot: ['VM', gettext('Delete Snapshot')],
1690 qmdestroy: ['VM', gettext('Destroy')],
1691 qmigrate: ['VM', gettext('Migrate')],
1692 qmmove: ['VM', gettext('Move disk')],
1693 qmpause: ['VM', gettext('Pause')],
1694 qmreboot: ['VM', gettext('Reboot')],
1695 qmreset: ['VM', gettext('Reset')],
1696 qmrestore: ['VM', gettext('Restore')],
1697 qmresume: ['VM', gettext('Resume')],
1698 qmrollback: ['VM', gettext('Rollback')],
1699 qmshutdown: ['VM', gettext('Shutdown')],
1700 qmsnapshot: ['VM', gettext('Snapshot')],
1701 qmstart: ['VM', gettext('Start')],
1702 qmstop: ['VM', gettext('Stop')],
1703 qmsuspend: ['VM', gettext('Hibernate')],
1704 qmtemplate: ['VM', gettext('Convert to template')],
1705 spiceproxy: ['VM/CT', gettext('Console') + ' (Spice)'],
1706 spiceshell: ['', gettext('Shell') + ' (Spice)'],
1707 startall: ['', gettext('Start all VMs and Containers')],
1708 stopall: ['', gettext('Stop all VMs and Containers')],
1709 unknownimgdel: ['', gettext('Destroy image from unknown guest')],
1710 vncproxy: ['VM/CT', gettext('Console')],
1711 vncshell: ['', gettext('Shell')],
1712 vzclone: ['CT', gettext('Clone')],
1713 vzcreate: ['CT', gettext('Create')],
1714 vzdelsnapshot: ['CT', gettext('Delete Snapshot')],
1715 vzdestroy: ['CT', gettext('Destroy')],
1716 vzdump: (type, id) => id ? `VM/CT ${id} - ${gettext('Backup')}` : gettext('Backup Job'),
1717 vzmigrate: ['CT', gettext('Migrate')],
1718 vzmount: ['CT', gettext('Mount')],
1719 vzreboot: ['CT', gettext('Reboot')],
1720 vzrestore: ['CT', gettext('Restore')],
1721 vzresume: ['CT', gettext('Resume')],
1722 vzrollback: ['CT', gettext('Rollback')],
1723 vzshutdown: ['CT', gettext('Shutdown')],
1724 vzsnapshot: ['CT', gettext('Snapshot')],
1725 vzstart: ['CT', gettext('Start')],
1726 vzstop: ['CT', gettext('Stop')],
1727 vzsuspend: ['CT', gettext('Suspend')],
1728 vztemplate: ['CT', gettext('Convert to template')],
1729 vzumount: ['CT', gettext('Unmount')],
1730 zfscreate: [gettext('ZFS Storage'), gettext('Create')],
1731 });
f6710aac 1732 },
e7ade592 1733
9fa2e36d 1734});