]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
bump version to 5.1-52
[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)',
285 'xtermjs': 'xterm.js',
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',
433 hideAdd: true,
434 faIcon: 'building'
435 },
436 rbd_ext: {
437 name: 'RBD (external)',
438 ipanel: 'RBDInputPanel',
439 faIcon: 'building'
440 },
441 pveceph: {
442 name: 'RBD (PVE)',
443 ipanel: 'PVERBDInputPanel',
444 faIcon: 'building'
445 },
446 zfs: {
447 name: 'ZFS over iSCSI',
448 ipanel: 'ZFSInputPanel',
449 faIcon: 'building'
450 },
451 zfspool: {
452 name: 'ZFS',
453 ipanel: 'ZFSPoolInputPanel',
454 faIcon: 'folder'
455 },
456 drbd: {
457 name: 'DRBD',
458 hideAdd: true
459 }
460 },
461
3c23c025 462 format_storage_type: function(value, md, record) {
6760ab92
DC
463 if (value === 'rbd' && record) {
464 value = (record.get('monhost')?'rbd_ext':'pveceph');
3c23c025 465 }
062a7f49
TL
466
467 var schema = PVE.Utils.storageSchema[value];
468 if (schema) {
469 return schema.name;
b0a6d326 470 }
062a7f49 471 return Proxmox.Utils.unknownText;
a3b8efb4
EK
472 },
473
ced1677b 474 format_ha: function(value) {
e7ade592 475 var text = Proxmox.Utils.noneText;
ced1677b
TL
476
477 if (value.managed) {
e7ade592 478 text = value.state || Proxmox.Utils.noneText;
ced1677b 479
e7ade592
DC
480 text += ', ' + Proxmox.Utils.groupText + ': ';
481 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
482 }
483
484 return text;
485 },
486
b0a6d326
EK
487 format_content_types: function(value) {
488 var cta = [];
489
490 Ext.each(value.split(',').sort(), function(ct) {
491 if (ct === 'images') {
492 cta.push(PVE.Utils.imagesText);
493 } else if (ct === 'backup') {
494 cta.push(PVE.Utils.backupFileText);
495 } else if (ct === 'vztmpl') {
496 cta.push(PVE.Utils.vztmplText);
497 } else if (ct === 'iso') {
498 cta.push(PVE.Utils.isoImageText);
499 } else if (ct === 'rootdir') {
500 cta.push(PVE.Utils.containersText);
501 }
502 });
503
504 return cta.join(', ');
505 },
506
507 render_storage_content: function(value, metaData, record) {
508 var data = record.data;
509 if (Ext.isNumber(data.channel) &&
510 Ext.isNumber(data.id) &&
511 Ext.isNumber(data.lun)) {
0be88ae1
DC
512 return "CH " +
513 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
514 " ID " + data.id + " LUN " + data.lun;
515 }
516 return data.volid.replace(/^.*:(.*\/)?/,'');
517 },
518
519 render_serverity: function (value) {
520 return PVE.Utils.log_severity_hash[value] || value;
521 },
522
523 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
524
525 if (!(record.data.uptime && Ext.isNumeric(value))) {
526 return '';
527 }
528
529 var maxcpu = record.data.maxcpu || 1;
530
531 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
532 return '';
533 }
0be88ae1 534
b0a6d326
EK
535 var per = value * 100;
536
537 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
538 },
539
540 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
541 /*jslint confusion: true */
542
543 if (!Ext.isNumeric(value)) {
544 return '';
545 }
546
e7ade592 547 return Proxmox.Utils.format_size(value);
b0a6d326
EK
548 },
549
946730cd
DC
550 render_bandwidth: function(value) {
551 if (!Ext.isNumeric(value)) {
552 return '';
553 }
554
e7ade592 555 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
556 },
557
3f633655
EK
558 render_timestamp_human_readable: function(value) {
559 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
560 },
561
ddd26302
DC
562 render_duration: function(value) {
563 if (value === undefined) {
564 return '-';
565 }
566 return PVE.Utils.format_duration_short(value);
567 },
568
0bfc799f
DC
569 calculate_mem_usage: function(data) {
570 if (!Ext.isNumeric(data.mem) ||
571 data.maxmem === 0 ||
572 data.uptime < 1) {
573 return -1;
574 }
575
576 return (data.mem / data.maxmem);
577 },
578
579 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
580 if (!Ext.isNumeric(value) || value === -1) {
581 return '';
582 }
583 if (value > 1 ) {
584 // we got no percentage but bytes
585 var mem = value;
586 var maxmem = record.data.maxmem;
587 if (!record.data.uptime ||
588 maxmem === 0 ||
589 !Ext.isNumeric(mem)) {
590 return '';
591 }
592
593 return ((mem*100)/maxmem).toFixed(1) + " %";
594 }
595 return (value*100).toFixed(1) + " %";
596 },
597
b0a6d326
EK
598 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
599
600 var mem = value;
601 var maxmem = record.data.maxmem;
0be88ae1 602
b0a6d326
EK
603 if (!record.data.uptime) {
604 return '';
605 }
606
607 if (!(Ext.isNumeric(mem) && maxmem)) {
608 return '';
609 }
610
728f1b97 611 return PVE.Utils.render_size(value);
b0a6d326
EK
612 },
613
0bfc799f
DC
614 calculate_disk_usage: function(data) {
615
616 if (!Ext.isNumeric(data.disk) ||
617 data.type === 'qemu' ||
618 (data.type === 'lxc' && data.uptime === 0) ||
619 data.maxdisk === 0) {
620 return -1;
621 }
622
623 return (data.disk / data.maxdisk);
624 },
625
626 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
627 if (!Ext.isNumeric(value) || value === -1) {
628 return '';
629 }
630
631 return (value * 100).toFixed(1) + " %";
632 },
633
b0a6d326
EK
634 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
635
636 var disk = value;
637 var maxdisk = record.data.maxdisk;
728f1b97 638 var type = record.data.type;
b0a6d326 639
728f1b97
DC
640 if (!Ext.isNumeric(disk) ||
641 type === 'qemu' ||
642 maxdisk === 0 ||
643 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
644 return '';
645 }
646
728f1b97 647 return PVE.Utils.render_size(value);
b0a6d326
EK
648 },
649
4dbc64a7
DC
650 get_object_icon_class: function(type, record) {
651 var status = '';
652 var objType = type;
653
654 if (type === 'type') {
655 // for folder view
656 objType = record.groupbyid;
657 } else if (record.template) {
658 // templates
659 objType = 'template';
660 status = type;
661 } else {
662 // everything else
663 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
664 }
665
4dbc64a7
DC
666 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
667 if (defaults && defaults.iconCls) {
668 var retVal = defaults.iconCls + ' ' + status;
669 return retVal;
b0a6d326
EK
670 }
671
4dbc64a7
DC
672 return '';
673 },
674
675 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
676
677 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 678
4dbc64a7 679 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 680 return fa + value;
b0a6d326
EK
681 },
682
b0a6d326
EK
683 render_support_level: function(value, metaData, record) {
684 return PVE.Utils.support_level_hash[value] || '-';
685 },
686
0be88ae1 687 render_upid: function(value, metaData, record) {
b0a6d326
EK
688 var type = record.data.type;
689 var id = record.data.id;
690
e7ade592 691 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
692 },
693
054ac1b8
DC
694 /* render functions for new status panel */
695
696 render_usage: function(val) {
697 return (val*100).toFixed(2) + '%';
698 },
699
700 render_cpu_usage: function(val, max) {
701 return Ext.String.format(gettext('{0}% of {1}') +
702 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
703 },
704
705 render_size_usage: function(val, max) {
bab64974
DC
706 if (max === 0) {
707 return gettext('N/A');
708 }
054ac1b8
DC
709 return (val*100/max).toFixed(2) + '% '+ '(' +
710 Ext.String.format(gettext('{0} of {1}'),
711 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
712 },
713
714 /* this is different for nodes */
715 render_node_cpu_usage: function(value, record) {
716 return PVE.Utils.render_cpu_usage(value, record.cpus);
717 },
718
719 /* this is different for nodes */
720 render_node_size_usage: function(record) {
721 return PVE.Utils.render_size_usage(record.used, record.total);
722 },
723
aa0819a8 724 windowHostname: function() {
e7ade592 725 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
726 function(m, addr, offset, original) { return addr; });
727 },
0be88ae1 728
3438c27e
DC
729 openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname) {
730 var dv = PVE.Utils.defaultViewer(consoles);
b0a6d326
EK
731 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
732 },
733
734 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 735 // kvm, lxc, shell, upgrade
b0a6d326 736
9e361643 737 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
738 throw "missing vmid";
739 }
740
741 if (!nodename) {
742 throw "no nodename specified";
743 }
744
c7218ab3
DC
745 if (viewer === 'html5') {
746 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
c6b2336c 747 } else if (viewer === 'xtermjs') {
e7ade592 748 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
749 } else if (viewer === 'vv') {
750 var url;
aa0819a8 751 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
752 if (vmtype === 'kvm') {
753 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
754 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
755 } else if (vmtype === 'lxc') {
756 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
757 PVE.Utils.openSpiceViewer(url, params);
758 } else if (vmtype === 'shell') {
759 url = '/nodes/' + nodename + '/spiceshell';
760 PVE.Utils.openSpiceViewer(url, params);
761 } else if (vmtype === 'upgrade') {
762 url = '/nodes/' + nodename + '/spiceshell';
763 params.upgrade = 1;
764 PVE.Utils.openSpiceViewer(url, params);
765 }
766 } else {
767 throw "unknown viewer type";
768 }
769 },
770
3438c27e
DC
771 defaultViewer: function(consoles) {
772
773 var allowSpice, allowXtermjs;
774
775 if (consoles === true) {
776 allowSpice = true;
777 allowXtermjs = true;
778 } else if (typeof consoles === 'object') {
779 allowSpice = consoles.spice;
780 allowXtermjs = consoles.xtermjs;
781 }
b0a6d326
EK
782 var vncdefault = 'html5';
783 var dv = PVE.VersionInfo.console || vncdefault;
3438c27e 784 if ((dv === 'vv' && !allowSpice) || (dv === 'xtermjs' && !allowXtermjs)) {
b0a6d326
EK
785 dv = vncdefault;
786 }
787
788 return dv;
789 },
790
c7218ab3 791 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 792 var url = Ext.urlEncode({
9e361643 793 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 794 novnc: 1,
b0a6d326
EK
795 vmid: vmid,
796 vmname: vmname,
797 node: nodename
798 });
799 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
800 nw.focus();
801 },
802
803 openSpiceViewer: function(url, params){
804
805 var downloadWithName = function(uri, name) {
806 var link = Ext.DomHelper.append(document.body, {
807 tag: 'a',
808 href: uri,
809 css : 'display:none;visibility:hidden;height:0px;'
810 });
811
812 // Note: we need to tell android the correct file name extension
813 // but we do not set 'download' tag for other environments, because
814 // It can have strange side effects (additional user prompt on firefox)
815 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
816 if (andriod) {
817 link.download = name;
818 }
819
820 if (link.fireEvent) {
821 link.fireEvent('onclick');
822 } else {
823 var evt = document.createEvent("MouseEvents");
824 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
825 link.dispatchEvent(evt);
826 }
827 };
828
e7ade592 829 Proxmox.Utils.API2Request({
b0a6d326
EK
830 url: url,
831 params: params,
832 method: 'POST',
833 failure: function(response, opts){
834 Ext.Msg.alert('Error', response.htmlStatus);
835 },
836 success: function(response, opts){
837 var raw = "[virt-viewer]\n";
838 Ext.Object.each(response.result.data, function(k, v) {
839 raw += k + "=" + v + "\n";
840 });
841 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
842 encodeURIComponent(raw);
0be88ae1 843
b0a6d326
EK
844 downloadWithName(url, "pve-spice.vv");
845 }
846 });
847 },
848
e3129443
DC
849 openTreeConsole: function(tree, record, item, index, e) {
850 e.stopEvent();
851 var nodename = record.data.node;
852 var vmid = record.data.vmid;
853 var vmname = record.data.name;
854 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 855 Proxmox.Utils.API2Request({
e3129443
DC
856 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
857 failure: function(response, opts) {
858 Ext.Msg.alert('Error', response.htmlStatus);
859 },
860 success: function(response, opts) {
861 var allowSpice = response.result.data.spice;
862 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
863 }
864 });
865 } else if (record.data.type === 'lxc' && !record.data.template) {
866 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
867 }
868 },
869
fbd60cfd
DM
870 // test automation helper
871 call_menu_handler: function(menu, text) {
872
873 var list = menu.query('menuitem');
874
875 Ext.Array.each(list, function(item) {
876 if (item.text === text) {
877 if (item.handler) {
878 item.handler();
879 return 1;
880 } else {
881 return undefined;
882 }
883 }
884 });
885 },
886
685b7aa4
DC
887 createCmdMenu: function(v, record, item, index, event) {
888 event.stopEvent();
cc1a91be
DC
889 if (!(v instanceof Ext.tree.View)) {
890 v.select(record);
891 }
685b7aa4 892 var menu;
9bad05bd
DC
893 var template = !!record.data.template;
894 var type = record.data.type;
685b7aa4 895
9bad05bd
DC
896 if (template) {
897 if (type === 'qemu' || type == 'lxc') {
898 menu = Ext.create('PVE.menu.TemplateMenu', {
899 pveSelNode: record
900 });
901 }
902 } else if (type === 'qemu' ||
903 type === 'lxc' ||
904 type === 'node') {
905 menu = Ext.create('PVE.' + type + '.CmdMenu', {
906 pveSelNode: record,
c11ab8cb
DC
907 nodename: record.data.node
908 });
685b7aa4
DC
909 } else {
910 return;
911 }
912
913 menu.showAt(event.getXY());
e7ade592 914 },
9fa2e36d 915
fe4f00ad
TL
916 // helper for deleting field which are set to there default values
917 delete_if_default: function(values, fieldname, default_val, create) {
918 if (values[fieldname] === '' || values[fieldname] === default_val) {
919 if (!create) {
920 if (values['delete']) {
921 values['delete'] += ',' + fieldname;
922 } else {
923 values['delete'] = fieldname;
924 }
925 }
926
927 delete values[fieldname];
928 }
857b97a7
TL
929 },
930
931 loadSSHKeyFromFile: function(file, callback) {
932 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
933 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
934 // assume: 740*8 for max. 32kbit (5920 byte file)
935 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
936 if (file.size > 8192) {
937 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
938 return;
939 }
940 /*global
941 FileReader
942 */
943 var reader = new FileReader();
944 reader.onload = function(evt) {
945 callback(evt.target.result);
946 };
947 reader.readAsText(file);
abe824aa
DC
948 },
949
950 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
951
952 // types is either undefined (all busses), an array of busses, or a single bus
953 forEachBus: function(types, func) {
954 var busses = Object.keys(PVE.Utils.bus_counts);
955 var i, j, count, cont;
956
957 if (Ext.isArray(types)) {
958 busses = types;
959 } else if (Ext.isDefined(types)) {
960 busses = [ types ];
961 }
962
963 // check if we only have valid busses
964 for (i = 0; i < busses.length; i++) {
965 if (!PVE.Utils.bus_counts[busses[i]]) {
966 throw "invalid bus: '" + busses[i] + "'";
967 }
968 }
969
970 for (i = 0; i < busses.length; i++) {
971 count = PVE.Utils.bus_counts[busses[i]];
972 for (j = 0; j < count; j++) {
973 cont = func(busses[i], j);
974 if (!cont && cont !== undefined) {
975 return;
976 }
977 }
978 }
14a845bc
DC
979 },
980
981 mp_counts: { mps: 10, unused: 10 },
982
983 forEachMP: function(func, includeUnused) {
984 var i, cont;
985 for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
986 cont = func('mp', i);
987 if (!cont && cont !== undefined) {
988 return;
989 }
990 }
991
992 if (!includeUnused) {
993 return;
994 }
995
996 for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
997 cont = func('unused', i);
998 if (!cont && cont !== undefined) {
999 return;
1000 }
1001 }
e7ade592
DC
1002 }
1003},
fe4f00ad 1004
9fa2e36d
EK
1005 singleton: true,
1006 constructor: function() {
1007 var me = this;
1008 Ext.apply(me, me.utilities);
685b7aa4 1009 }
e7ade592 1010
9fa2e36d 1011});
b0a6d326 1012