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