]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
Add 'type' option to AgentFeatureSelector
[pve-manager.git] / www / manager6 / Utils.js
CommitLineData
b0a6d326
EK
1Ext.ns('PVE');
2
63b9faae
EK
3// avoid errors related to Accessible Rich Internet Applications
4// (access for people with disabilities)
0be88ae1 5// TODO reenable after all components are upgraded
63b9faae
EK
6Ext.enableAria = false;
7Ext.enableAriaButtons = false;
8Ext.enableAriaPanels = false;
9
b0a6d326 10// avoid errors when running without development tools
0be88ae1
DC
11if (!Ext.isDefined(Ext.global.console)) {
12 var console = {
0be88ae1 13 log: function() {}
b0a6d326
EK
14 };
15}
0be88ae1 16console.log("Starting PVE Manager");
b0a6d326
EK
17
18Ext.Ajax.defaultHeaders = {
19 'Accept': 'application/json'
20};
21
89ae1bb1 22/*jslint confusion: true */
9fa2e36d 23Ext.define('PVE.Utils', { 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",
39 7: "debug"
40 },
41
42 support_level_hash: {
43 'c': gettext('Community'),
44 'b': gettext('Basic'),
45 's': gettext('Standard'),
46 'p': gettext('Premium')
47 },
48
8ec8af9c 49 noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="https://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
b0a6d326
EK
50
51 kvm_ostypes: {
45d6c71a 52 'Linux': [
ca71cd5d 53 { desc: '5.x - 2.6 Kernel', val: 'l26' },
45d6c71a
TL
54 { desc: '2.4 Kernel', val: 'l24' }
55 ],
56 'Microsoft Windows': [
beb3c8a4 57 { desc: '10/2016/2019', val: 'win10' },
45d6c71a
TL
58 { desc: '8.x/2012/2012r2', val: 'win8' },
59 { desc: '7/2008r2', val: 'win7' },
60 { desc: 'Vista/2008', val: 'w2k8' },
61 { desc: 'XP/2003', val: 'wxp' },
62 { desc: '2000', val: 'w2k' }
63 ],
64 'Solaris Kernel': [
65 { desc: '-', val: 'solaris'}
66 ],
67 'Other': [
68 { desc: '-', val: 'other'}
69 ]
b0a6d326
EK
70 },
71
428bc4a2
DC
72 get_health_icon: function(state, circle) {
73 if (circle === undefined) {
74 circle = false;
75 }
76
77 if (state === undefined) {
78 state = 'uknown';
79 }
80
81 var icon = 'faded fa-question';
82 switch(state) {
83 case 'good':
84 icon = 'good fa-check';
85 break;
23c83a3a
DC
86 case 'upgrade':
87 icon = 'warning fa-upload';
88 break;
aa240b29
DC
89 case 'old':
90 icon = 'warning fa-refresh';
91 break;
428bc4a2
DC
92 case 'warning':
93 icon = 'warning fa-exclamation';
94 break;
95 case 'critical':
96 icon = 'critical fa-times';
97 break;
98 default: break;
99 }
100
101 if (circle) {
102 icon += '-circle';
103 }
104
105 return icon;
106 },
107
95d0de89
DC
108 parse_ceph_version: function(service) {
109 if (service.ceph_version_short) {
110 return service.ceph_version_short;
111 }
112
113 if (service.ceph_version) {
35306f47 114 var match = service.ceph_version.match(/version (\d+(\.\d+)*)/);
95d0de89
DC
115 if (match) {
116 return match[1];
117 }
118 }
119
120 return undefined;
121 },
122
005e0a60
DC
123 compare_ceph_versions: function(a, b) {
124 if (a === b) {
125 return 0;
126 }
35306f47
TL
127 let avers = a.toString().split('.');
128 let bvers = b.toString().split('.');
129
130 while (true) {
131 let av = avers.shift();
132 let bv = bvers.shift();
133
134 if (av === undefined && bv === undefined) {
135 return 0;
136 } else if (av === undefined) {
137 return -1;
138 } else if (bv === undefined) {
139 return 1;
140 } else {
141 let diff = parseInt(av, 10) - parseInt(bv, 10);
142 if (diff != 0) return diff;
143 // else we need to look at the next parts
005e0a60
DC
144 }
145 }
146
005e0a60
DC
147 },
148
2f5b82ae
DC
149 get_ceph_icon_html: function(health, fw) {
150 var state = PVE.Utils.map_ceph_health[health];
151 var cls = PVE.Utils.get_health_icon(state);
152 if (fw) {
153 cls += ' fa-fw';
154 }
155 return "<i class='fa " + cls + "'></i> ";
156 },
157
046e640c
DC
158 map_ceph_health: {
159 'HEALTH_OK':'good',
23c83a3a 160 'HEALTH_UPGRADE':'upgrade',
aa240b29 161 'HEALTH_OLD':'old',
046e640c
DC
162 'HEALTH_WARN':'warning',
163 'HEALTH_ERR':'critical'
164 },
165
dfe6d184 166 render_ceph_health: function(healthObj) {
046e640c
DC
167 var state = {
168 iconCls: PVE.Utils.get_health_icon(),
169 text: ''
170 };
171
dfe6d184 172 if (!healthObj || !healthObj.status) {
046e640c
DC
173 return state;
174 }
175
dfe6d184 176 var health = PVE.Utils.map_ceph_health[healthObj.status];
046e640c
DC
177
178 state.iconCls = PVE.Utils.get_health_icon(health, true);
dfe6d184 179 state.text = healthObj.status;
046e640c
DC
180
181 return state;
182 },
183
fee716d3 184 render_zfs_health: function(value) {
8c8604ba
TM
185 if (typeof value == 'undefined'){
186 return "";
187 }
fee716d3
DC
188 var iconCls = 'question-circle';
189 switch (value) {
8c8604ba 190 case 'AVAIL':
fee716d3
DC
191 case 'ONLINE':
192 iconCls = 'check-circle good';
193 break;
194 case 'REMOVED':
195 case 'DEGRADED':
196 iconCls = 'exclamation-circle warning';
197 break;
198 case 'UNAVAIL':
199 case 'FAULTED':
200 case 'OFFLINE':
201 iconCls = 'times-circle critical';
202 break;
203 default: //unknown
204 }
205
206 return '<i class="fa fa-' + iconCls + '"></i> ' + value;
8c8604ba 207
fee716d3
DC
208 },
209
45d6c71a
TL
210 get_kvm_osinfo: function(value) {
211 var info = { base: 'Other' }; // default
212 if (value) {
213 Ext.each(Object.keys(PVE.Utils.kvm_ostypes), function(k) {
214 Ext.each(PVE.Utils.kvm_ostypes[k], function(e) {
215 if (e.val === value) {
216 info = { desc: e.desc, base: k };
217 }
218 });
219 });
b0a6d326 220 }
45d6c71a
TL
221 return info;
222 },
223
224 render_kvm_ostype: function (value) {
225 var osinfo = PVE.Utils.get_kvm_osinfo(value);
226 if (osinfo.desc && osinfo.desc !== '-') {
227 return osinfo.base + ' ' + osinfo.desc;
228 } else {
229 return osinfo.base;
b0a6d326 230 }
b0a6d326
EK
231 },
232
233 render_hotplug_features: function (value) {
23d3881a 234 var fa = [];
b0a6d326
EK
235
236 if (!value || (value === '0')) {
ec99b1c3 237 return gettext('Disabled');
b0a6d326
EK
238 }
239
4dcca8a4
DC
240 if (value === '1') {
241 value = 'disk,network,usb';
242 }
243
b0a6d326
EK
244 Ext.each(value.split(','), function(el) {
245 if (el === 'disk') {
246 fa.push(gettext('Disk'));
247 } else if (el === 'network') {
248 fa.push(gettext('Network'));
249 } else if (el === 'usb') {
b9628aa5 250 fa.push('USB');
b0a6d326
EK
251 } else if (el === 'memory') {
252 fa.push(gettext('Memory'));
253 } else if (el === 'cpu') {
254 fa.push(gettext('CPU'));
255 } else {
256 fa.push(el);
257 }
258 });
259
260 return fa.join(', ');
261 },
262
1662ccdb
SI
263 render_qga_features: function(value) {
264 if (!value) {
265 return Proxmox.Utils.defaultText + ' (' + Proxmox.Utils.disabledText + ')';
266 }
267 var props = PVE.Parser.parsePropertyString(value, 'enabled');
268 if (!PVE.Parser.parseBoolean(props.enabled)) {
269 return Proxmox.Utils.disabledText;
270 }
271
272 delete props.enabled;
273 var agentstring = Proxmox.Utils.enabledText;
274
275 Ext.Object.each(props, function(key, value) {
276 var keystring = '' ;
277 agentstring += ', ' + key + ': ';
278
5a6c563c
MD
279 if (key === 'type') {
280 let map = {
281 isa: "ISA",
282 };
283 agentstring += map[value] || Proxmox.Utils.unknownText;
1662ccdb 284 } else {
5a6c563c
MD
285 if (PVE.Parser.parseBoolean(value)) {
286 agentstring += Proxmox.Utils.enabledText;
287 } else {
288 agentstring += Proxmox.Utils.disabledText;
289 }
1662ccdb
SI
290 }
291 });
292
293 return agentstring;
294 },
295
a1ee14a2
DC
296 render_qemu_machine: function(value) {
297 return value || (Proxmox.Utils.defaultText + ' (i440fx)');
298 },
299
17c71f27
DC
300 render_qemu_bios: function(value) {
301 if (!value) {
e7ade592 302 return Proxmox.Utils.defaultText + ' (SeaBIOS)';
17c71f27
DC
303 } else if (value === 'seabios') {
304 return "SeaBIOS";
305 } else if (value === 'ovmf') {
306 return "OVMF (UEFI)";
307 } else {
308 return value;
309 }
310 },
311
8f17b496
TL
312 render_dc_ha_opts: function(value) {
313 if (!value) {
41ca3465 314 return Proxmox.Utils.defaultText;
8f17b496
TL
315 } else {
316 return PVE.Parser.printPropertyString(value);
317 }
318 },
bb469a11
TL
319 render_as_property_string: function(value) {
320 return (!value) ? Proxmox.Utils.defaultText
321 : PVE.Parser.printPropertyString(value);
322 },
8f17b496 323
b0a6d326
EK
324 render_scsihw: function(value) {
325 if (!value) {
e7ade592 326 return Proxmox.Utils.defaultText + ' (LSI 53C895A)';
b0a6d326
EK
327 } else if (value === 'lsi') {
328 return 'LSI 53C895A';
329 } else if (value === 'lsi53c810') {
330 return 'LSI 53C810';
331 } else if (value === 'megasas') {
332 return 'MegaRAID SAS 8708EM2';
333 } else if (value === 'virtio-scsi-pci') {
49f5d1ab
EK
334 return 'VirtIO SCSI';
335 } else if (value === 'virtio-scsi-single') {
336 return 'VirtIO SCSI single';
b0a6d326
EK
337 } else if (value === 'pvscsi') {
338 return 'VMware PVSCSI';
339 } else {
340 return value;
341 }
342 },
343
9c22da32 344 render_spice_enhancements: function(values) {
9c22da32
AL
345 let props = PVE.Parser.parsePropertyString(values);
346 if (Ext.Object.isEmpty(props)) {
3b0facc9 347 return Proxmox.Utils.noneText;
9c22da32
AL
348 }
349
350 let output = [];
351 if (PVE.Parser.parseBoolean(props.foldersharing)) {
352 output.push('Folder Sharing: ' + gettext('Enabled'));
353 }
354 if (props.videostreaming === 'all' || props.videostreaming === 'filter') {
355 output.push('Video Streaming: ' + props.videostreaming);
356 }
357 return output.join(', ');
358 },
359
b0a6d326
EK
360 // fixme: auto-generate this
361 // for now, please keep in sync with PVE::Tools::kvmkeymaps
362 kvm_keymaps: {
363 //ar: 'Arabic',
364 da: 'Danish',
0be88ae1
DC
365 de: 'German',
366 'de-ch': 'German (Swiss)',
367 'en-gb': 'English (UK)',
0a5b8747 368 'en-us': 'English (USA)',
b0a6d326
EK
369 es: 'Spanish',
370 //et: 'Estonia',
371 fi: 'Finnish',
0be88ae1
DC
372 //fo: 'Faroe Islands',
373 fr: 'French',
374 'fr-be': 'French (Belgium)',
b0a6d326
EK
375 'fr-ca': 'French (Canada)',
376 'fr-ch': 'French (Swiss)',
377 //hr: 'Croatia',
378 hu: 'Hungarian',
379 is: 'Icelandic',
0be88ae1 380 it: 'Italian',
b0a6d326
EK
381 ja: 'Japanese',
382 lt: 'Lithuanian',
383 //lv: 'Latvian',
0be88ae1 384 mk: 'Macedonian',
b0a6d326
EK
385 nl: 'Dutch',
386 //'nl-be': 'Dutch (Belgium)',
0be88ae1 387 no: 'Norwegian',
b0a6d326
EK
388 pl: 'Polish',
389 pt: 'Portuguese',
390 'pt-br': 'Portuguese (Brazil)',
391 //ru: 'Russian',
392 sl: 'Slovenian',
393 sv: 'Swedish',
394 //th: 'Thai',
395 tr: 'Turkish'
396 },
397
398 kvm_vga_drivers: {
399 std: gettext('Standard VGA'),
5ca366f2 400 vmware: gettext('VMware compatible'),
b0a6d326
EK
401 qxl: 'SPICE',
402 qxl2: 'SPICE dual monitor',
403 qxl3: 'SPICE three monitors',
404 qxl4: 'SPICE four monitors',
405 serial0: gettext('Serial terminal') + ' 0',
406 serial1: gettext('Serial terminal') + ' 1',
407 serial2: gettext('Serial terminal') + ' 2',
89ae1bb1
DC
408 serial3: gettext('Serial terminal') + ' 3',
409 virtio: 'VirtIO-GPU',
410 none: Proxmox.Utils.noneText
b0a6d326
EK
411 },
412
413 render_kvm_language: function (value) {
e7ade592
DC
414 if (!value || value === '__default__') {
415 return Proxmox.Utils.defaultText;
b0a6d326
EK
416 }
417 var text = PVE.Utils.kvm_keymaps[value];
418 if (text) {
419 return text + ' (' + value + ')';
420 }
421 return value;
422 },
423
424 kvm_keymap_array: function() {
f2782813 425 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
426 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
427 data.push([key, PVE.Utils.render_kvm_language(value)]);
428 });
429
430 return data;
431 },
432
3438c27e 433 console_map: {
da9d14cd 434 '__default__': Proxmox.Utils.defaultText + ' (xterm.js)',
3438c27e
DC
435 'vv': 'SPICE (remote-viewer)',
436 'html5': 'HTML5 (noVNC)',
4ace5c6f 437 'xtermjs': 'xterm.js'
3438c27e
DC
438 },
439
b0a6d326 440 render_console_viewer: function(value) {
3438c27e
DC
441 value = value || '__default__';
442 if (PVE.Utils.console_map[value]) {
443 return PVE.Utils.console_map[value];
b0a6d326 444 }
3438c27e 445 return value;
b0a6d326
EK
446 },
447
755b9083 448 console_viewer_array: function() {
3438c27e 449 return Ext.Array.map(Object.keys(PVE.Utils.console_map), function(v) {
755b9083
TL
450 return [v, PVE.Utils.render_console_viewer(v)];
451 });
452 },
453
b0a6d326
EK
454 render_kvm_vga_driver: function (value) {
455 if (!value) {
e7ade592 456 return Proxmox.Utils.defaultText;
b0a6d326 457 }
4f3e66d8
DC
458 var vga = PVE.Parser.parsePropertyString(value, 'type');
459 var text = PVE.Utils.kvm_vga_drivers[vga.type];
460 if (!vga.type) {
461 text = Proxmox.Utils.defaultText;
462 }
0be88ae1 463 if (text) {
b0a6d326
EK
464 return text + ' (' + value + ')';
465 }
466 return value;
467 },
468
469 kvm_vga_driver_array: function() {
f2782813 470 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
471 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
472 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
473 });
474
475 return data;
476 },
477
478 render_kvm_startup: function(value) {
479 var startup = PVE.Parser.parseStartup(value);
480
481 var res = 'order=';
482 if (startup.order === undefined) {
483 res += 'any';
484 } else {
485 res += startup.order;
486 }
487 if (startup.up !== undefined) {
488 res += ',up=' + startup.up;
489 }
490 if (startup.down !== undefined) {
491 res += ',down=' + startup.down;
492 }
493
494 return res;
495 },
496
b0a6d326
EK
497 extractFormActionError: function(action) {
498 var msg;
499 switch (action.failureType) {
500 case Ext.form.action.Action.CLIENT_INVALID:
501 msg = gettext('Form fields may not be submitted with invalid values');
502 break;
503 case Ext.form.action.Action.CONNECT_FAILURE:
504 msg = gettext('Connection error');
505 var resp = action.response;
506 if (resp.status && resp.statusText) {
507 msg += " " + resp.status + ": " + resp.statusText;
508 }
509 break;
510 case Ext.form.action.Action.LOAD_FAILURE:
511 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 512 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
513 break;
514 }
515 return msg;
516 },
517
b0a6d326 518 format_duration_short: function(ut) {
0be88ae1 519
b0a6d326 520 if (ut < 60) {
530ee6b7 521 return ut.toFixed(1) + 's';
b0a6d326
EK
522 }
523
524 if (ut < 3600) {
525 var mins = ut / 60;
530ee6b7 526 return mins.toFixed(1) + 'm';
b0a6d326
EK
527 }
528
529 if (ut < 86400) {
530 var hours = ut / 3600;
530ee6b7 531 return hours.toFixed(1) + 'h';
b0a6d326
EK
532 }
533
534 var days = ut / 86400;
530ee6b7 535 return days.toFixed(1) + 'd';
b0a6d326
EK
536 },
537
0e244a29
DC
538 contentTypes: {
539 'images': gettext('Disk image'),
540 'backup': gettext('VZDump backup file'),
541 'vztmpl': gettext('Container template'),
542 'iso': gettext('ISO image'),
aef28e04
DC
543 'rootdir': gettext('Container'),
544 'snippets': gettext('Snippets')
0e244a29 545 },
b0a6d326 546
062a7f49
TL
547 storageSchema: {
548 dir: {
549 name: Proxmox.Utils.directoryText,
550 ipanel: 'DirInputPanel',
551 faIcon: 'folder'
552 },
553 lvm: {
554 name: 'LVM',
555 ipanel: 'LVMInputPanel',
556 faIcon: 'folder'
557 },
558 lvmthin: {
559 name: 'LVM-Thin',
560 ipanel: 'LvmThinInputPanel',
561 faIcon: 'folder'
562 },
563 nfs: {
564 name: 'NFS',
565 ipanel: 'NFSInputPanel',
566 faIcon: 'building'
567 },
568 cifs: {
569 name: 'CIFS',
570 ipanel: 'CIFSInputPanel',
571 faIcon: 'building'
572 },
573 glusterfs: {
574 name: 'GlusterFS',
575 ipanel: 'GlusterFsInputPanel',
576 faIcon: 'building'
577 },
578 iscsi: {
579 name: 'iSCSI',
580 ipanel: 'IScsiInputPanel',
581 faIcon: 'building'
582 },
4a4b2b6e
TL
583 cephfs: {
584 name: 'CephFS',
585 ipanel: 'CephFSInputPanel',
586 faIcon: 'building'
587 },
588 pvecephfs: {
589 name: 'CephFS (PVE)',
590 ipanel: 'CephFSInputPanel',
591 hideAdd: true,
592 faIcon: 'building'
593 },
062a7f49
TL
594 rbd: {
595 name: 'RBD',
596 ipanel: 'RBDInputPanel',
062a7f49
TL
597 faIcon: 'building'
598 },
599 pveceph: {
600 name: 'RBD (PVE)',
0d1ac958
TL
601 ipanel: 'RBDInputPanel',
602 hideAdd: true,
062a7f49
TL
603 faIcon: 'building'
604 },
605 zfs: {
606 name: 'ZFS over iSCSI',
607 ipanel: 'ZFSInputPanel',
608 faIcon: 'building'
609 },
610 zfspool: {
611 name: 'ZFS',
612 ipanel: 'ZFSPoolInputPanel',
613 faIcon: 'folder'
614 },
615 drbd: {
616 name: 'DRBD',
617 hideAdd: true
618 }
619 },
620
3c23c025 621 format_storage_type: function(value, md, record) {
4a4b2b6e
TL
622 if (value === 'rbd') {
623 value = (!record || record.get('monhost') ? 'rbd' : 'pveceph');
624 } else if (value === 'cephfs') {
625 value = (!record || record.get('monhost') ? 'cephfs' : 'pvecephfs');
3c23c025 626 }
062a7f49
TL
627
628 var schema = PVE.Utils.storageSchema[value];
629 if (schema) {
630 return schema.name;
b0a6d326 631 }
062a7f49 632 return Proxmox.Utils.unknownText;
a3b8efb4
EK
633 },
634
ced1677b 635 format_ha: function(value) {
e7ade592 636 var text = Proxmox.Utils.noneText;
ced1677b
TL
637
638 if (value.managed) {
e7ade592 639 text = value.state || Proxmox.Utils.noneText;
ced1677b 640
e7ade592
DC
641 text += ', ' + Proxmox.Utils.groupText + ': ';
642 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
643 }
644
645 return text;
646 },
647
b0a6d326 648 format_content_types: function(value) {
0e244a29
DC
649 return value.split(',').sort().map(function(ct) {
650 return PVE.Utils.contentTypes[ct] || ct;
651 }).join(', ');
b0a6d326
EK
652 },
653
654 render_storage_content: function(value, metaData, record) {
655 var data = record.data;
656 if (Ext.isNumber(data.channel) &&
657 Ext.isNumber(data.id) &&
658 Ext.isNumber(data.lun)) {
0be88ae1
DC
659 return "CH " +
660 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
661 " ID " + data.id + " LUN " + data.lun;
662 }
663 return data.volid.replace(/^.*:(.*\/)?/,'');
664 },
665
666 render_serverity: function (value) {
667 return PVE.Utils.log_severity_hash[value] || value;
668 },
669
670 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
671
672 if (!(record.data.uptime && Ext.isNumeric(value))) {
673 return '';
674 }
675
676 var maxcpu = record.data.maxcpu || 1;
677
678 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
679 return '';
680 }
0be88ae1 681
b0a6d326
EK
682 var per = value * 100;
683
684 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
685 },
686
687 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
688 /*jslint confusion: true */
689
690 if (!Ext.isNumeric(value)) {
691 return '';
692 }
693
e7ade592 694 return Proxmox.Utils.format_size(value);
b0a6d326
EK
695 },
696
946730cd
DC
697 render_bandwidth: function(value) {
698 if (!Ext.isNumeric(value)) {
699 return '';
700 }
701
e7ade592 702 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
703 },
704
3f633655
EK
705 render_timestamp_human_readable: function(value) {
706 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
707 },
708
ddd26302
DC
709 render_duration: function(value) {
710 if (value === undefined) {
711 return '-';
712 }
713 return PVE.Utils.format_duration_short(value);
714 },
715
0bfc799f
DC
716 calculate_mem_usage: function(data) {
717 if (!Ext.isNumeric(data.mem) ||
718 data.maxmem === 0 ||
719 data.uptime < 1) {
720 return -1;
721 }
722
723 return (data.mem / data.maxmem);
724 },
725
726 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
727 if (!Ext.isNumeric(value) || value === -1) {
728 return '';
729 }
730 if (value > 1 ) {
731 // we got no percentage but bytes
732 var mem = value;
733 var maxmem = record.data.maxmem;
734 if (!record.data.uptime ||
735 maxmem === 0 ||
736 !Ext.isNumeric(mem)) {
737 return '';
738 }
739
740 return ((mem*100)/maxmem).toFixed(1) + " %";
741 }
742 return (value*100).toFixed(1) + " %";
743 },
744
b0a6d326
EK
745 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
746
747 var mem = value;
748 var maxmem = record.data.maxmem;
0be88ae1 749
b0a6d326
EK
750 if (!record.data.uptime) {
751 return '';
752 }
753
754 if (!(Ext.isNumeric(mem) && maxmem)) {
755 return '';
756 }
757
728f1b97 758 return PVE.Utils.render_size(value);
b0a6d326
EK
759 },
760
0bfc799f
DC
761 calculate_disk_usage: function(data) {
762
763 if (!Ext.isNumeric(data.disk) ||
764 data.type === 'qemu' ||
765 (data.type === 'lxc' && data.uptime === 0) ||
766 data.maxdisk === 0) {
767 return -1;
768 }
769
770 return (data.disk / data.maxdisk);
771 },
772
773 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
774 if (!Ext.isNumeric(value) || value === -1) {
775 return '';
776 }
777
778 return (value * 100).toFixed(1) + " %";
779 },
780
b0a6d326
EK
781 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
782
783 var disk = value;
784 var maxdisk = record.data.maxdisk;
728f1b97 785 var type = record.data.type;
b0a6d326 786
728f1b97
DC
787 if (!Ext.isNumeric(disk) ||
788 type === 'qemu' ||
789 maxdisk === 0 ||
790 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
791 return '';
792 }
793
728f1b97 794 return PVE.Utils.render_size(value);
b0a6d326
EK
795 },
796
4dbc64a7
DC
797 get_object_icon_class: function(type, record) {
798 var status = '';
799 var objType = type;
800
801 if (type === 'type') {
802 // for folder view
803 objType = record.groupbyid;
804 } else if (record.template) {
805 // templates
806 objType = 'template';
807 status = type;
808 } else {
809 // everything else
810 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
811 }
812
6284a48a
DC
813 if (record.lock) {
814 status += ' locked lock-' + record.lock;
815 }
816
4dbc64a7
DC
817 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
818 if (defaults && defaults.iconCls) {
819 var retVal = defaults.iconCls + ' ' + status;
820 return retVal;
b0a6d326
EK
821 }
822
4dbc64a7
DC
823 return '';
824 },
825
826 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
827
828 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 829
4dbc64a7 830 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 831 return fa + value;
b0a6d326
EK
832 },
833
b0a6d326
EK
834 render_support_level: function(value, metaData, record) {
835 return PVE.Utils.support_level_hash[value] || '-';
836 },
837
0be88ae1 838 render_upid: function(value, metaData, record) {
b0a6d326
EK
839 var type = record.data.type;
840 var id = record.data.id;
841
e7ade592 842 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
843 },
844
054ac1b8
DC
845 /* render functions for new status panel */
846
847 render_usage: function(val) {
848 return (val*100).toFixed(2) + '%';
849 },
850
851 render_cpu_usage: function(val, max) {
852 return Ext.String.format(gettext('{0}% of {1}') +
853 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
854 },
855
856 render_size_usage: function(val, max) {
bab64974
DC
857 if (max === 0) {
858 return gettext('N/A');
859 }
054ac1b8
DC
860 return (val*100/max).toFixed(2) + '% '+ '(' +
861 Ext.String.format(gettext('{0} of {1}'),
862 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
863 },
864
865 /* this is different for nodes */
866 render_node_cpu_usage: function(value, record) {
867 return PVE.Utils.render_cpu_usage(value, record.cpus);
868 },
869
870 /* this is different for nodes */
871 render_node_size_usage: function(record) {
872 return PVE.Utils.render_size_usage(record.used, record.total);
873 },
874
27809975
DC
875 render_optional_url: function(value) {
876 var match;
877 if (value && (match = value.match(/^https?:\/\//)) !== null) {
cdd9b6c0 878 return '<a target="_blank" href="' + value + '">' + value + '</a>';
27809975
DC
879 }
880 return value;
881 },
882
883 render_san: function(value) {
884 var names = [];
885 if (Ext.isArray(value)) {
886 value.forEach(function(val) {
887 if (!Ext.isNumber(val)) {
888 names.push(val);
889 }
890 });
891 return names.join('<br>');
892 }
893 return value;
894 },
895
6ad4be69
DC
896 render_full_name: function(firstname, metaData, record) {
897 var first = firstname || '';
898 var last = record.data.lastname || '';
899 return Ext.htmlEncode(first + " " + last);
900 },
901
2d41c7e6
TL
902 render_u2f_error: function(error) {
903 var ErrorNames = {
904 '1': gettext('Other Error'),
905 '2': gettext('Bad Request'),
906 '3': gettext('Configuration Unsupported'),
907 '4': gettext('Device Ineligible'),
908 '5': gettext('Timeout')
909 };
910 return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
911 },
912
aa0819a8 913 windowHostname: function() {
e7ade592 914 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
915 function(m, addr, offset, original) { return addr; });
916 },
0be88ae1 917
8eccc68f 918 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname, cmd) {
3438c27e 919 var dv = PVE.Utils.defaultViewer(consoles);
8eccc68f 920 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
921 },
922
8eccc68f 923 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname, cmd) {
9e361643 924 // kvm, lxc, shell, upgrade
b0a6d326 925
9e361643 926 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
927 throw "missing vmid";
928 }
929
930 if (!nodename) {
931 throw "no nodename specified";
932 }
933
c7218ab3 934 if (viewer === 'html5') {
8eccc68f 935 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname, cmd);
c6b2336c 936 } else if (viewer === 'xtermjs') {
8eccc68f 937 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname, cmd);
b0a6d326
EK
938 } else if (viewer === 'vv') {
939 var url;
aa0819a8 940 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
941 if (vmtype === 'kvm') {
942 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
943 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
944 } else if (vmtype === 'lxc') {
945 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
946 PVE.Utils.openSpiceViewer(url, params);
947 } else if (vmtype === 'shell') {
948 url = '/nodes/' + nodename + '/spiceshell';
949 PVE.Utils.openSpiceViewer(url, params);
950 } else if (vmtype === 'upgrade') {
951 url = '/nodes/' + nodename + '/spiceshell';
952 params.upgrade = 1;
953 PVE.Utils.openSpiceViewer(url, params);
8eccc68f
TM
954 } else if (vmtype === 'cmd') {
955 url = '/nodes/' + nodename + '/spiceshell';
956 params.cmd = cmd;
957 PVE.Utils.openSpiceViewer(url, params);
b0a6d326
EK
958 }
959 } else {
960 throw "unknown viewer type";
961 }
962 },
963
3438c27e
DC
964 defaultViewer: function(consoles) {
965
966 var allowSpice, allowXtermjs;
967
968 if (consoles === true) {
969 allowSpice = true;
970 allowXtermjs = true;
971 } else if (typeof consoles === 'object') {
972 allowSpice = consoles.spice;
4ace5c6f 973 allowXtermjs = !!consoles.xtermjs;
3438c27e 974 }
da9d14cd 975 var dv = PVE.VersionInfo.console || 'xtermjs';
f932cffa
DC
976 if (dv === 'vv' && !allowSpice) {
977 dv = (allowXtermjs) ? 'xtermjs' : 'html5';
978 } else if (dv === 'xtermjs' && !allowXtermjs) {
979 dv = (allowSpice) ? 'vv' : 'html5';
b0a6d326
EK
980 }
981
982 return dv;
983 },
984
8eccc68f 985 openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
af89f682
TL
986 let scaling = 'off';
987 if (Proxmox.Utils.toolkit !== 'touch') {
988 var sp = Ext.state.Manager.getProvider();
989 scaling = sp.get('novnc-scaling', 'off');
990 }
8eccc68f 991 var url = Ext.Object.toQueryString({
9e361643 992 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 993 novnc: 1,
b0a6d326
EK
994 vmid: vmid,
995 vmname: vmname,
16e64c97 996 node: nodename,
af89f682 997 resize: scaling,
8eccc68f 998 cmd: cmd
b0a6d326
EK
999 });
1000 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
7af1ab47
DC
1001 if (nw) {
1002 nw.focus();
1003 }
b0a6d326
EK
1004 },
1005
1006 openSpiceViewer: function(url, params){
1007
1008 var downloadWithName = function(uri, name) {
1009 var link = Ext.DomHelper.append(document.body, {
1010 tag: 'a',
1011 href: uri,
1012 css : 'display:none;visibility:hidden;height:0px;'
1013 });
1014
1015 // Note: we need to tell android the correct file name extension
1016 // but we do not set 'download' tag for other environments, because
1017 // It can have strange side effects (additional user prompt on firefox)
1018 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1019 if (andriod) {
1020 link.download = name;
1021 }
1022
1023 if (link.fireEvent) {
1024 link.fireEvent('onclick');
1025 } else {
1026 var evt = document.createEvent("MouseEvents");
1027 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1028 link.dispatchEvent(evt);
1029 }
1030 };
1031
e7ade592 1032 Proxmox.Utils.API2Request({
b0a6d326
EK
1033 url: url,
1034 params: params,
1035 method: 'POST',
1036 failure: function(response, opts){
1037 Ext.Msg.alert('Error', response.htmlStatus);
1038 },
1039 success: function(response, opts){
1040 var raw = "[virt-viewer]\n";
1041 Ext.Object.each(response.result.data, function(k, v) {
1042 raw += k + "=" + v + "\n";
1043 });
1044 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1045 encodeURIComponent(raw);
0be88ae1 1046
b0a6d326
EK
1047 downloadWithName(url, "pve-spice.vv");
1048 }
1049 });
1050 },
1051
e3129443
DC
1052 openTreeConsole: function(tree, record, item, index, e) {
1053 e.stopEvent();
1054 var nodename = record.data.node;
1055 var vmid = record.data.vmid;
1056 var vmname = record.data.name;
1057 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 1058 Proxmox.Utils.API2Request({
e3129443
DC
1059 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1060 failure: function(response, opts) {
1061 Ext.Msg.alert('Error', response.htmlStatus);
1062 },
1063 success: function(response, opts) {
bd9537d7 1064 let conf = response.result.data;
54453c38 1065 var consoles = {
bd9537d7
TL
1066 spice: !!conf.spice,
1067 xtermjs: !!conf.serial,
54453c38
DC
1068 };
1069 PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
e3129443
DC
1070 }
1071 });
1072 } else if (record.data.type === 'lxc' && !record.data.template) {
1073 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1074 }
1075 },
1076
fbd60cfd
DM
1077 // test automation helper
1078 call_menu_handler: function(menu, text) {
1079
1080 var list = menu.query('menuitem');
1081
1082 Ext.Array.each(list, function(item) {
1083 if (item.text === text) {
1084 if (item.handler) {
1085 item.handler();
1086 return 1;
1087 } else {
1088 return undefined;
1089 }
1090 }
1091 });
1092 },
1093
685b7aa4
DC
1094 createCmdMenu: function(v, record, item, index, event) {
1095 event.stopEvent();
cc1a91be
DC
1096 if (!(v instanceof Ext.tree.View)) {
1097 v.select(record);
1098 }
685b7aa4 1099 var menu;
9bad05bd
DC
1100 var template = !!record.data.template;
1101 var type = record.data.type;
685b7aa4 1102
9bad05bd
DC
1103 if (template) {
1104 if (type === 'qemu' || type == 'lxc') {
1105 menu = Ext.create('PVE.menu.TemplateMenu', {
1106 pveSelNode: record
1107 });
1108 }
1109 } else if (type === 'qemu' ||
1110 type === 'lxc' ||
1111 type === 'node') {
1112 menu = Ext.create('PVE.' + type + '.CmdMenu', {
1113 pveSelNode: record,
c11ab8cb
DC
1114 nodename: record.data.node
1115 });
685b7aa4
DC
1116 } else {
1117 return;
1118 }
1119
1120 menu.showAt(event.getXY());
9f0b4e04 1121 return menu;
e7ade592 1122 },
9fa2e36d 1123
fe4f00ad
TL
1124 // helper for deleting field which are set to there default values
1125 delete_if_default: function(values, fieldname, default_val, create) {
1126 if (values[fieldname] === '' || values[fieldname] === default_val) {
1127 if (!create) {
1128 if (values['delete']) {
1129 values['delete'] += ',' + fieldname;
1130 } else {
1131 values['delete'] = fieldname;
1132 }
1133 }
1134
1135 delete values[fieldname];
1136 }
857b97a7
TL
1137 },
1138
1139 loadSSHKeyFromFile: function(file, callback) {
1140 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
1141 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
1142 // assume: 740*8 for max. 32kbit (5920 byte file)
1143 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
1144 if (file.size > 8192) {
1145 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
1146 return;
1147 }
1148 /*global
1149 FileReader
1150 */
1151 var reader = new FileReader();
1152 reader.onload = function(evt) {
1153 callback(evt.target.result);
1154 };
1155 reader.readAsText(file);
abe824aa
DC
1156 },
1157
1158 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
1159
1160 // types is either undefined (all busses), an array of busses, or a single bus
1161 forEachBus: function(types, func) {
1162 var busses = Object.keys(PVE.Utils.bus_counts);
1163 var i, j, count, cont;
1164
1165 if (Ext.isArray(types)) {
1166 busses = types;
1167 } else if (Ext.isDefined(types)) {
1168 busses = [ types ];
1169 }
1170
1171 // check if we only have valid busses
1172 for (i = 0; i < busses.length; i++) {
1173 if (!PVE.Utils.bus_counts[busses[i]]) {
1174 throw "invalid bus: '" + busses[i] + "'";
1175 }
1176 }
1177
1178 for (i = 0; i < busses.length; i++) {
1179 count = PVE.Utils.bus_counts[busses[i]];
1180 for (j = 0; j < count; j++) {
1181 cont = func(busses[i], j);
1182 if (!cont && cont !== undefined) {
1183 return;
1184 }
1185 }
1186 }
14a845bc
DC
1187 },
1188
483bd394 1189 mp_counts: { mps: 256, unused: 256 },
14a845bc
DC
1190
1191 forEachMP: function(func, includeUnused) {
1192 var i, cont;
1193 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
1194 cont = func('mp', i);
1195 if (!cont && cont !== undefined) {
1196 return;
1197 }
1198 }
1199
1200 if (!includeUnused) {
1201 return;
1202 }
1203
1204 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
1205 cont = func('unused', i);
1206 if (!cont && cont !== undefined) {
1207 return;
1208 }
1209 }
b945c7c1
TM
1210 },
1211
1212 cleanEmptyObjectKeys: function (obj) {
1213 var propName;
1214 for (propName in obj) {
1215 if (obj.hasOwnProperty(propName)) {
1216 if (obj[propName] === null || obj[propName] === undefined) {
1217 delete obj[propName];
1218 }
1219 }
1220 }
4616a55b
TM
1221 },
1222
1223 handleStoreErrorOrMask: function(me, store, regex, callback) {
1224
1225 me.mon(store, 'load', function (proxy, response, success, operation) {
1226
1227 if (success) {
1228 Proxmox.Utils.setErrorMask(me, false);
1229 return;
1230 }
1231 var msg;
1232
1233 if (operation.error.statusText) {
1234 if (operation.error.statusText.match(regex)) {
1235 callback(me, operation.error);
1236 return;
1237 } else {
1238 msg = operation.error.statusText + ' (' + operation.error.status + ')';
1239 }
1240 } else {
1241 msg = gettext('Connection error');
1242 }
1243 Proxmox.Utils.setErrorMask(me, msg);
1244 });
1245 },
1246
1247 showCephInstallOrMask: function(container, msg, nodename, callback){
1248 var regex = new RegExp("not (installed|initialized)", "i");
1249 if (msg.match(regex)) {
1250 if (Proxmox.UserName === 'root@pam') {
1251 container.el.mask();
1252 if (!container.down('pveCephInstallWindow')){
f992ef80 1253 var isInstalled = msg.match(/not initialized/i) ? true : false;
4616a55b
TM
1254 var win = Ext.create('PVE.ceph.Install', {
1255 nodename: nodename
1256 });
f992ef80 1257 win.getViewModel().set('isInstalled', isInstalled);
4616a55b
TM
1258 container.add(win);
1259 win.show();
1260 callback(win);
1261 }
1262 } else {
a7e8b87b
TL
1263 container.mask(Ext.String.format(gettext('{0} not installed.') +
1264 ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
4616a55b
TM
1265 }
1266 return true;
1267 } else {
1268 return false;
1269 }
49dfba72
DC
1270 },
1271
1272 propertyStringSet: function(target, source, name, value) {
1273 if (source) {
1274 if (value === undefined) {
1275 target[name] = source;
1276 } else {
1277 target[name] = value;
1278 }
1279 } else {
1280 delete target[name];
1281 }
e7ade592
DC
1282 }
1283},
fe4f00ad 1284
9fa2e36d
EK
1285 singleton: true,
1286 constructor: function() {
1287 var me = this;
1288 Ext.apply(me, me.utilities);
685b7aa4 1289 }
e7ade592 1290
9fa2e36d 1291});
b0a6d326 1292