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