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