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