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