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