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