]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
lxc/ResoureEdit: move some cpu fields to advanced options
[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
281 render_console_viewer: function(value) {
f2782813 282 if (!value || value === '__default__') {
e7ade592 283 return Proxmox.Utils.defaultText + ' (HTML5)';
b0a6d326
EK
284 } else if (value === 'vv') {
285 return 'SPICE (remote-viewer)';
286 } else if (value === 'html5') {
287 return 'HTML5 (noVNC)';
288 } else {
289 return value;
290 }
291 },
292
755b9083
TL
293 console_viewer_array: function() {
294 return Ext.Array.map(['__default__','vv', 'html5'], function(v) {
295 return [v, PVE.Utils.render_console_viewer(v)];
296 });
297 },
298
b0a6d326
EK
299 render_kvm_vga_driver: function (value) {
300 if (!value) {
e7ade592 301 return Proxmox.Utils.defaultText;
b0a6d326
EK
302 }
303 var text = PVE.Utils.kvm_vga_drivers[value];
0be88ae1 304 if (text) {
b0a6d326
EK
305 return text + ' (' + value + ')';
306 }
307 return value;
308 },
309
310 kvm_vga_driver_array: function() {
f2782813 311 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
312 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
313 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
314 });
315
316 return data;
317 },
318
319 render_kvm_startup: function(value) {
320 var startup = PVE.Parser.parseStartup(value);
321
322 var res = 'order=';
323 if (startup.order === undefined) {
324 res += 'any';
325 } else {
326 res += startup.order;
327 }
328 if (startup.up !== undefined) {
329 res += ',up=' + startup.up;
330 }
331 if (startup.down !== undefined) {
332 res += ',down=' + startup.down;
333 }
334
335 return res;
336 },
337
b0a6d326
EK
338 extractFormActionError: function(action) {
339 var msg;
340 switch (action.failureType) {
341 case Ext.form.action.Action.CLIENT_INVALID:
342 msg = gettext('Form fields may not be submitted with invalid values');
343 break;
344 case Ext.form.action.Action.CONNECT_FAILURE:
345 msg = gettext('Connection error');
346 var resp = action.response;
347 if (resp.status && resp.statusText) {
348 msg += " " + resp.status + ": " + resp.statusText;
349 }
350 break;
351 case Ext.form.action.Action.LOAD_FAILURE:
352 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 353 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
354 break;
355 }
356 return msg;
357 },
358
b0a6d326 359 format_duration_short: function(ut) {
0be88ae1 360
b0a6d326 361 if (ut < 60) {
530ee6b7 362 return ut.toFixed(1) + 's';
b0a6d326
EK
363 }
364
365 if (ut < 3600) {
366 var mins = ut / 60;
530ee6b7 367 return mins.toFixed(1) + 'm';
b0a6d326
EK
368 }
369
370 if (ut < 86400) {
371 var hours = ut / 3600;
530ee6b7 372 return hours.toFixed(1) + 'h';
b0a6d326
EK
373 }
374
375 var days = ut / 86400;
530ee6b7 376 return days.toFixed(1) + 'd';
b0a6d326
EK
377 },
378
b0a6d326
EK
379 imagesText: gettext('Disk image'),
380 backupFileText: gettext('VZDump backup file'),
2c554952 381 vztmplText: gettext('Container template'),
b0a6d326 382 isoImageText: gettext('ISO image'),
2c554952 383 containersText: gettext('Container'),
b0a6d326 384
062a7f49
TL
385 storageSchema: {
386 dir: {
387 name: Proxmox.Utils.directoryText,
388 ipanel: 'DirInputPanel',
389 faIcon: 'folder'
390 },
391 lvm: {
392 name: 'LVM',
393 ipanel: 'LVMInputPanel',
394 faIcon: 'folder'
395 },
396 lvmthin: {
397 name: 'LVM-Thin',
398 ipanel: 'LvmThinInputPanel',
399 faIcon: 'folder'
400 },
401 nfs: {
402 name: 'NFS',
403 ipanel: 'NFSInputPanel',
404 faIcon: 'building'
405 },
406 cifs: {
407 name: 'CIFS',
408 ipanel: 'CIFSInputPanel',
409 faIcon: 'building'
410 },
411 glusterfs: {
412 name: 'GlusterFS',
413 ipanel: 'GlusterFsInputPanel',
414 faIcon: 'building'
415 },
416 iscsi: {
417 name: 'iSCSI',
418 ipanel: 'IScsiInputPanel',
419 faIcon: 'building'
420 },
421 sheepdog: {
422 name: 'Sheepdog',
423 ipanel: 'SheepdogInputPanel',
424 hideAdd: true,
425 faIcon: 'building'
426 },
427 rbd: {
428 name: 'RBD',
429 ipanel: 'RBDInputPanel',
430 hideAdd: true,
431 faIcon: 'building'
432 },
433 rbd_ext: {
434 name: 'RBD (external)',
435 ipanel: 'RBDInputPanel',
436 faIcon: 'building'
437 },
438 pveceph: {
439 name: 'RBD (PVE)',
440 ipanel: 'PVERBDInputPanel',
441 faIcon: 'building'
442 },
443 zfs: {
444 name: 'ZFS over iSCSI',
445 ipanel: 'ZFSInputPanel',
446 faIcon: 'building'
447 },
448 zfspool: {
449 name: 'ZFS',
450 ipanel: 'ZFSPoolInputPanel',
451 faIcon: 'folder'
452 },
453 drbd: {
454 name: 'DRBD',
455 hideAdd: true
456 }
457 },
458
3c23c025 459 format_storage_type: function(value, md, record) {
6760ab92
DC
460 if (value === 'rbd' && record) {
461 value = (record.get('monhost')?'rbd_ext':'pveceph');
3c23c025 462 }
062a7f49
TL
463
464 var schema = PVE.Utils.storageSchema[value];
465 if (schema) {
466 return schema.name;
b0a6d326 467 }
062a7f49 468 return Proxmox.Utils.unknownText;
a3b8efb4
EK
469 },
470
ced1677b 471 format_ha: function(value) {
e7ade592 472 var text = Proxmox.Utils.noneText;
ced1677b
TL
473
474 if (value.managed) {
e7ade592 475 text = value.state || Proxmox.Utils.noneText;
ced1677b 476
e7ade592
DC
477 text += ', ' + Proxmox.Utils.groupText + ': ';
478 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
479 }
480
481 return text;
482 },
483
b0a6d326
EK
484 format_content_types: function(value) {
485 var cta = [];
486
487 Ext.each(value.split(',').sort(), function(ct) {
488 if (ct === 'images') {
489 cta.push(PVE.Utils.imagesText);
490 } else if (ct === 'backup') {
491 cta.push(PVE.Utils.backupFileText);
492 } else if (ct === 'vztmpl') {
493 cta.push(PVE.Utils.vztmplText);
494 } else if (ct === 'iso') {
495 cta.push(PVE.Utils.isoImageText);
496 } else if (ct === 'rootdir') {
497 cta.push(PVE.Utils.containersText);
498 }
499 });
500
501 return cta.join(', ');
502 },
503
504 render_storage_content: function(value, metaData, record) {
505 var data = record.data;
506 if (Ext.isNumber(data.channel) &&
507 Ext.isNumber(data.id) &&
508 Ext.isNumber(data.lun)) {
0be88ae1
DC
509 return "CH " +
510 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
511 " ID " + data.id + " LUN " + data.lun;
512 }
513 return data.volid.replace(/^.*:(.*\/)?/,'');
514 },
515
516 render_serverity: function (value) {
517 return PVE.Utils.log_severity_hash[value] || value;
518 },
519
520 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
521
522 if (!(record.data.uptime && Ext.isNumeric(value))) {
523 return '';
524 }
525
526 var maxcpu = record.data.maxcpu || 1;
527
528 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
529 return '';
530 }
0be88ae1 531
b0a6d326
EK
532 var per = value * 100;
533
534 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
535 },
536
537 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
538 /*jslint confusion: true */
539
540 if (!Ext.isNumeric(value)) {
541 return '';
542 }
543
e7ade592 544 return Proxmox.Utils.format_size(value);
b0a6d326
EK
545 },
546
946730cd
DC
547 render_bandwidth: function(value) {
548 if (!Ext.isNumeric(value)) {
549 return '';
550 }
551
e7ade592 552 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
553 },
554
3f633655
EK
555 render_timestamp_human_readable: function(value) {
556 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
557 },
558
ddd26302
DC
559 render_duration: function(value) {
560 if (value === undefined) {
561 return '-';
562 }
563 return PVE.Utils.format_duration_short(value);
564 },
565
0bfc799f
DC
566 calculate_mem_usage: function(data) {
567 if (!Ext.isNumeric(data.mem) ||
568 data.maxmem === 0 ||
569 data.uptime < 1) {
570 return -1;
571 }
572
573 return (data.mem / data.maxmem);
574 },
575
576 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
577 if (!Ext.isNumeric(value) || value === -1) {
578 return '';
579 }
580 if (value > 1 ) {
581 // we got no percentage but bytes
582 var mem = value;
583 var maxmem = record.data.maxmem;
584 if (!record.data.uptime ||
585 maxmem === 0 ||
586 !Ext.isNumeric(mem)) {
587 return '';
588 }
589
590 return ((mem*100)/maxmem).toFixed(1) + " %";
591 }
592 return (value*100).toFixed(1) + " %";
593 },
594
b0a6d326
EK
595 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
596
597 var mem = value;
598 var maxmem = record.data.maxmem;
0be88ae1 599
b0a6d326
EK
600 if (!record.data.uptime) {
601 return '';
602 }
603
604 if (!(Ext.isNumeric(mem) && maxmem)) {
605 return '';
606 }
607
728f1b97 608 return PVE.Utils.render_size(value);
b0a6d326
EK
609 },
610
0bfc799f
DC
611 calculate_disk_usage: function(data) {
612
613 if (!Ext.isNumeric(data.disk) ||
614 data.type === 'qemu' ||
615 (data.type === 'lxc' && data.uptime === 0) ||
616 data.maxdisk === 0) {
617 return -1;
618 }
619
620 return (data.disk / data.maxdisk);
621 },
622
623 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
624 if (!Ext.isNumeric(value) || value === -1) {
625 return '';
626 }
627
628 return (value * 100).toFixed(1) + " %";
629 },
630
b0a6d326
EK
631 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
632
633 var disk = value;
634 var maxdisk = record.data.maxdisk;
728f1b97 635 var type = record.data.type;
b0a6d326 636
728f1b97
DC
637 if (!Ext.isNumeric(disk) ||
638 type === 'qemu' ||
639 maxdisk === 0 ||
640 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
641 return '';
642 }
643
728f1b97 644 return PVE.Utils.render_size(value);
b0a6d326
EK
645 },
646
4dbc64a7
DC
647 get_object_icon_class: function(type, record) {
648 var status = '';
649 var objType = type;
650
651 if (type === 'type') {
652 // for folder view
653 objType = record.groupbyid;
654 } else if (record.template) {
655 // templates
656 objType = 'template';
657 status = type;
658 } else {
659 // everything else
660 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
661 }
662
4dbc64a7
DC
663 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
664 if (defaults && defaults.iconCls) {
665 var retVal = defaults.iconCls + ' ' + status;
666 return retVal;
b0a6d326
EK
667 }
668
4dbc64a7
DC
669 return '';
670 },
671
672 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
673
674 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 675
4dbc64a7 676 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 677 return fa + value;
b0a6d326
EK
678 },
679
b0a6d326
EK
680 render_support_level: function(value, metaData, record) {
681 return PVE.Utils.support_level_hash[value] || '-';
682 },
683
0be88ae1 684 render_upid: function(value, metaData, record) {
b0a6d326
EK
685 var type = record.data.type;
686 var id = record.data.id;
687
e7ade592 688 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
689 },
690
054ac1b8
DC
691 /* render functions for new status panel */
692
693 render_usage: function(val) {
694 return (val*100).toFixed(2) + '%';
695 },
696
697 render_cpu_usage: function(val, max) {
698 return Ext.String.format(gettext('{0}% of {1}') +
699 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
700 },
701
702 render_size_usage: function(val, max) {
bab64974
DC
703 if (max === 0) {
704 return gettext('N/A');
705 }
054ac1b8
DC
706 return (val*100/max).toFixed(2) + '% '+ '(' +
707 Ext.String.format(gettext('{0} of {1}'),
708 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
709 },
710
711 /* this is different for nodes */
712 render_node_cpu_usage: function(value, record) {
713 return PVE.Utils.render_cpu_usage(value, record.cpus);
714 },
715
716 /* this is different for nodes */
717 render_node_size_usage: function(record) {
718 return PVE.Utils.render_size_usage(record.used, record.total);
719 },
720
aa0819a8 721 windowHostname: function() {
e7ade592 722 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
723 function(m, addr, offset, original) { return addr; });
724 },
0be88ae1 725
b0a6d326
EK
726 openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
727 var dv = PVE.Utils.defaultViewer(allowSpice);
728 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
729 },
730
731 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 732 // kvm, lxc, shell, upgrade
b0a6d326 733
9e361643 734 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
735 throw "missing vmid";
736 }
737
738 if (!nodename) {
739 throw "no nodename specified";
740 }
741
c7218ab3
DC
742 if (viewer === 'html5') {
743 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
c6b2336c 744 } else if (viewer === 'xtermjs') {
e7ade592 745 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
746 } else if (viewer === 'vv') {
747 var url;
aa0819a8 748 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
749 if (vmtype === 'kvm') {
750 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
751 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
752 } else if (vmtype === 'lxc') {
753 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
754 PVE.Utils.openSpiceViewer(url, params);
755 } else if (vmtype === 'shell') {
756 url = '/nodes/' + nodename + '/spiceshell';
757 PVE.Utils.openSpiceViewer(url, params);
758 } else if (vmtype === 'upgrade') {
759 url = '/nodes/' + nodename + '/spiceshell';
760 params.upgrade = 1;
761 PVE.Utils.openSpiceViewer(url, params);
762 }
763 } else {
764 throw "unknown viewer type";
765 }
766 },
767
768 defaultViewer: function(allowSpice) {
769 var vncdefault = 'html5';
770 var dv = PVE.VersionInfo.console || vncdefault;
771 if (dv === 'vv' && !allowSpice) {
772 dv = vncdefault;
773 }
774
775 return dv;
776 },
777
c7218ab3 778 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 779 var url = Ext.urlEncode({
9e361643 780 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 781 novnc: 1,
b0a6d326
EK
782 vmid: vmid,
783 vmname: vmname,
784 node: nodename
785 });
786 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
787 nw.focus();
788 },
789
790 openSpiceViewer: function(url, params){
791
792 var downloadWithName = function(uri, name) {
793 var link = Ext.DomHelper.append(document.body, {
794 tag: 'a',
795 href: uri,
796 css : 'display:none;visibility:hidden;height:0px;'
797 });
798
799 // Note: we need to tell android the correct file name extension
800 // but we do not set 'download' tag for other environments, because
801 // It can have strange side effects (additional user prompt on firefox)
802 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
803 if (andriod) {
804 link.download = name;
805 }
806
807 if (link.fireEvent) {
808 link.fireEvent('onclick');
809 } else {
810 var evt = document.createEvent("MouseEvents");
811 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
812 link.dispatchEvent(evt);
813 }
814 };
815
e7ade592 816 Proxmox.Utils.API2Request({
b0a6d326
EK
817 url: url,
818 params: params,
819 method: 'POST',
820 failure: function(response, opts){
821 Ext.Msg.alert('Error', response.htmlStatus);
822 },
823 success: function(response, opts){
824 var raw = "[virt-viewer]\n";
825 Ext.Object.each(response.result.data, function(k, v) {
826 raw += k + "=" + v + "\n";
827 });
828 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
829 encodeURIComponent(raw);
0be88ae1 830
b0a6d326
EK
831 downloadWithName(url, "pve-spice.vv");
832 }
833 });
834 },
835
e3129443
DC
836 openTreeConsole: function(tree, record, item, index, e) {
837 e.stopEvent();
838 var nodename = record.data.node;
839 var vmid = record.data.vmid;
840 var vmname = record.data.name;
841 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 842 Proxmox.Utils.API2Request({
e3129443
DC
843 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
844 failure: function(response, opts) {
845 Ext.Msg.alert('Error', response.htmlStatus);
846 },
847 success: function(response, opts) {
848 var allowSpice = response.result.data.spice;
849 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
850 }
851 });
852 } else if (record.data.type === 'lxc' && !record.data.template) {
853 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
854 }
855 },
856
fbd60cfd
DM
857 // test automation helper
858 call_menu_handler: function(menu, text) {
859
860 var list = menu.query('menuitem');
861
862 Ext.Array.each(list, function(item) {
863 if (item.text === text) {
864 if (item.handler) {
865 item.handler();
866 return 1;
867 } else {
868 return undefined;
869 }
870 }
871 });
872 },
873
685b7aa4
DC
874 createCmdMenu: function(v, record, item, index, event) {
875 event.stopEvent();
cc1a91be
DC
876 if (!(v instanceof Ext.tree.View)) {
877 v.select(record);
878 }
685b7aa4 879 var menu;
9bad05bd
DC
880 var template = !!record.data.template;
881 var type = record.data.type;
685b7aa4 882
9bad05bd
DC
883 if (template) {
884 if (type === 'qemu' || type == 'lxc') {
885 menu = Ext.create('PVE.menu.TemplateMenu', {
886 pveSelNode: record
887 });
888 }
889 } else if (type === 'qemu' ||
890 type === 'lxc' ||
891 type === 'node') {
892 menu = Ext.create('PVE.' + type + '.CmdMenu', {
893 pveSelNode: record,
c11ab8cb
DC
894 nodename: record.data.node
895 });
685b7aa4
DC
896 } else {
897 return;
898 }
899
900 menu.showAt(event.getXY());
e7ade592 901 },
9fa2e36d 902
fe4f00ad
TL
903 // helper for deleting field which are set to there default values
904 delete_if_default: function(values, fieldname, default_val, create) {
905 if (values[fieldname] === '' || values[fieldname] === default_val) {
906 if (!create) {
907 if (values['delete']) {
908 values['delete'] += ',' + fieldname;
909 } else {
910 values['delete'] = fieldname;
911 }
912 }
913
914 delete values[fieldname];
915 }
857b97a7
TL
916 },
917
918 loadSSHKeyFromFile: function(file, callback) {
919 // ssh-keygen produces 740 bytes for an average 4096 bit rsa key, with
920 // a user@host comment, 1420 for 8192 bits; current max is 16kbit
921 // assume: 740*8 for max. 32kbit (5920 byte file)
922 // round upwards to nearest nice number => 8192 bytes, leaves lots of comment space
923 if (file.size > 8192) {
924 Ext.Msg.alert(gettext('Error'), gettext("Invalid file size: ") + file.size);
925 return;
926 }
927 /*global
928 FileReader
929 */
930 var reader = new FileReader();
931 reader.onload = function(evt) {
932 callback(evt.target.result);
933 };
934 reader.readAsText(file);
abe824aa
DC
935 },
936
937 bus_counts: { ide: 4, sata: 6, scsi: 16, virtio: 16 },
938
939 // types is either undefined (all busses), an array of busses, or a single bus
940 forEachBus: function(types, func) {
941 var busses = Object.keys(PVE.Utils.bus_counts);
942 var i, j, count, cont;
943
944 if (Ext.isArray(types)) {
945 busses = types;
946 } else if (Ext.isDefined(types)) {
947 busses = [ types ];
948 }
949
950 // check if we only have valid busses
951 for (i = 0; i < busses.length; i++) {
952 if (!PVE.Utils.bus_counts[busses[i]]) {
953 throw "invalid bus: '" + busses[i] + "'";
954 }
955 }
956
957 for (i = 0; i < busses.length; i++) {
958 count = PVE.Utils.bus_counts[busses[i]];
959 for (j = 0; j < count; j++) {
960 cont = func(busses[i], j);
961 if (!cont && cont !== undefined) {
962 return;
963 }
964 }
965 }
e7ade592
DC
966 }
967},
fe4f00ad 968
9fa2e36d
EK
969 singleton: true,
970 constructor: function() {
971 var me = this;
972 Ext.apply(me, me.utilities);
685b7aa4 973 }
e7ade592 974
9fa2e36d 975});
b0a6d326 976