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