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