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