]> git.proxmox.com Git - pve-manager.git/blob - www/manager/Utils.js
09788cfc8c855346e28c9f232466739b46ee1cb8
[pve-manager.git] / www / manager / Utils.js
1 Ext.ns('PVE');
2
3 // avoid errors when running without development tools
4 if (!Ext.isDefined(Ext.global.console)) {
5 var console = {
6 dir: function() {},
7 log: function() {}
8 };
9 }
10 console.log("Starting PVE Manager");
11
12 Ext.Ajax.defaultHeaders = {
13 'Accept': 'application/json'
14 };
15
16 Ext.Ajax.on('beforerequest', function(conn, options) {
17 if (PVE.CSRFPreventionToken) {
18 if (!options.headers) {
19 options.headers = {};
20 }
21 options.headers.CSRFPreventionToken = PVE.CSRFPreventionToken;
22 }
23 });
24
25 var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
26 var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
27 var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
28 var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
29
30 var IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
31 var IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/[1-3]?[0-9]$");
32
33 var IPV6_REGEXP = "(?:" +
34 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
35 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
36 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
37 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
38 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
39 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
40 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
41 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
42 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
43 ")";
44
45 var IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
46 var IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/[0-9]{1,3}?$");
47 var IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
48
49 var IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
50
51 Ext.define('PVE.Utils', { statics: {
52
53 // this class only contains static functions
54
55 toolkit: undefined, // (extjs|touch), set inside Toolkit.js
56
57 log_severity_hash: {
58 0: "panic",
59 1: "alert",
60 2: "critical",
61 3: "error",
62 4: "warning",
63 5: "notice",
64 6: "info",
65 7: "debug"
66 },
67
68 support_level_hash: {
69 'c': gettext('Community'),
70 'b': gettext('Basic'),
71 's': gettext('Standard'),
72 'p': gettext('Premium')
73 },
74
75 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.',
76
77 kvm_ostypes: {
78 other: gettext('Other OS types'),
79 wxp: 'Microsoft Windows XP/2003',
80 w2k: 'Microsoft Windows 2000',
81 w2k8: 'Microsoft Windows Vista/2008',
82 win7: 'Microsoft Windows 7/2008r2',
83 win8: 'Microsoft Windows 8/10/2012',
84 l24: 'Linux 2.4 Kernel',
85 l26: 'Linux 4.X/3.X/2.6 Kernel',
86 solaris: 'Solaris Kernel'
87 },
88
89 render_kvm_ostype: function (value) {
90 if (!value) {
91 return gettext('Other OS types');
92 }
93 var text = PVE.Utils.kvm_ostypes[value];
94 if (text) {
95 return text + ' (' + value + ')';
96 }
97 return value;
98 },
99
100 render_hotplug_features: function (value) {
101 var fa = [];
102
103 if (!value || (value === '0')) {
104 return gettext('disabled');
105 }
106
107 Ext.each(value.split(','), function(el) {
108 if (el === 'disk') {
109 fa.push(gettext('Disk'));
110 } else if (el === 'network') {
111 fa.push(gettext('Network'));
112 } else if (el === 'usb') {
113 fa.push(gettext('USB'));
114 } else if (el === 'memory') {
115 fa.push(gettext('Memory'));
116 } else if (el === 'cpu') {
117 fa.push(gettext('CPU'));
118 } else {
119 fa.push(el);
120 }
121 });
122
123 return fa.join(', ');
124 },
125
126 network_iface_types: {
127 eth: gettext("Network Device"),
128 bridge: 'Linux Bridge',
129 bond: 'Linux Bond',
130 vlan: 'Linux Vlan interface',
131 OVSBridge: 'OVS Bridge',
132 OVSBond: 'OVS Bond',
133 OVSPort: 'OVS Port',
134 OVSIntPort: 'OVS IntPort'
135 },
136
137 render_network_iface_type: function(value) {
138 return PVE.Utils.network_iface_types[value] ||
139 PVE.Utils.unknownText;
140 },
141
142 render_qemu_bios: function(value) {
143 if (!value) {
144 return PVE.Utils.defaultText + ' (SeaBIOS)';
145 } else if (value === 'seabios') {
146 return "SeaBIOS";
147 } else if (value === 'ovmf') {
148 return "OVMF (UEFI)";
149 } else {
150 return value;
151 }
152 },
153
154 render_scsihw: function(value) {
155 if (!value) {
156 return PVE.Utils.defaultText + ' (LSI 53C895A)';
157 } else if (value === 'lsi') {
158 return 'LSI 53C895A';
159 } else if (value === 'lsi53c810') {
160 return 'LSI 53C810';
161 } else if (value === 'megasas') {
162 return 'MegaRAID SAS 8708EM2';
163 } else if (value === 'virtio-scsi-pci') {
164 return 'VIRTIO';
165 } else if (value === 'virtio-scsi-single') {
166 return 'VIRTIO single';
167 } else if (value === 'pvscsi') {
168 return 'VMware PVSCSI';
169 } else {
170 return value;
171 }
172 },
173
174 // fixme: auto-generate this
175 // for now, please keep in sync with PVE::Tools::kvmkeymaps
176 kvm_keymaps: {
177 //ar: 'Arabic',
178 da: 'Danish',
179 de: 'German',
180 'de-ch': 'German (Swiss)',
181 'en-gb': 'English (UK)',
182 'en-us': 'English (USA)',
183 es: 'Spanish',
184 //et: 'Estonia',
185 fi: 'Finnish',
186 //fo: 'Faroe Islands',
187 fr: 'French',
188 'fr-be': 'French (Belgium)',
189 'fr-ca': 'French (Canada)',
190 'fr-ch': 'French (Swiss)',
191 //hr: 'Croatia',
192 hu: 'Hungarian',
193 is: 'Icelandic',
194 it: 'Italian',
195 ja: 'Japanese',
196 lt: 'Lithuanian',
197 //lv: 'Latvian',
198 mk: 'Macedonian',
199 nl: 'Dutch',
200 //'nl-be': 'Dutch (Belgium)',
201 no: 'Norwegian',
202 pl: 'Polish',
203 pt: 'Portuguese',
204 'pt-br': 'Portuguese (Brazil)',
205 //ru: 'Russian',
206 sl: 'Slovenian',
207 sv: 'Swedish',
208 //th: 'Thai',
209 tr: 'Turkish'
210 },
211
212 kvm_vga_drivers: {
213 std: gettext('Standard VGA'),
214 vmware: gettext('VMWare compatible'),
215 cirrus: 'Cirrus Logic GD5446',
216 qxl: 'SPICE',
217 qxl2: 'SPICE dual monitor',
218 qxl3: 'SPICE three monitors',
219 qxl4: 'SPICE four monitors',
220 serial0: gettext('Serial terminal') + ' 0',
221 serial1: gettext('Serial terminal') + ' 1',
222 serial2: gettext('Serial terminal') + ' 2',
223 serial3: gettext('Serial terminal') + ' 3'
224 },
225
226 render_kvm_language: function (value) {
227 if (!value) {
228 return PVE.Utils.defaultText;
229 }
230 var text = PVE.Utils.kvm_keymaps[value];
231 if (text) {
232 return text + ' (' + value + ')';
233 }
234 return value;
235 },
236
237 kvm_keymap_array: function() {
238 var data = [['', PVE.Utils.render_kvm_language('')]];
239 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
240 data.push([key, PVE.Utils.render_kvm_language(value)]);
241 });
242
243 return data;
244 },
245
246 render_console_viewer: function(value) {
247 if (!value) {
248 return PVE.Utils.defaultText + ' (HTML5)';
249 } else if (value === 'vv') {
250 return 'SPICE (remote-viewer)';
251 } else if (value === 'applet' || value === 'html5') {
252 return 'HTML5 (noVNC)';
253 } else {
254 return value;
255 }
256 },
257
258 language_map: {
259 zh_CN: 'Chinese',
260 ca: 'Catalan',
261 da: 'Danish',
262 en: 'English',
263 eu: 'Euskera (Basque)',
264 fr: 'French',
265 gl: 'Galician',
266 de: 'German',
267 hu: 'Hungarian',
268 it: 'Italian',
269 ja: 'Japanese',
270 nb: 'Norwegian (Bokmal)',
271 nn: 'Norwegian (Nynorsk)',
272 fa: 'Persian (Farsi)',
273 pl: 'Polish',
274 pt_BR: 'Portuguese (Brazil)',
275 ru: 'Russian',
276 sl: 'Slovenian',
277 es: 'Spanish',
278 sv: 'Swedish',
279 tr: 'Turkish'
280 },
281
282 render_language: function (value) {
283 if (!value) {
284 return PVE.Utils.defaultText + ' (English)';
285 }
286 var text = PVE.Utils.language_map[value];
287 if (text) {
288 return text + ' (' + value + ')';
289 }
290 return value;
291 },
292
293 language_array: function() {
294 var data = [['', PVE.Utils.render_language('')]];
295 Ext.Object.each(PVE.Utils.language_map, function(key, value) {
296 data.push([key, PVE.Utils.render_language(value)]);
297 });
298
299 return data;
300 },
301
302 render_kvm_vga_driver: function (value) {
303 if (!value) {
304 return PVE.Utils.defaultText;
305 }
306 var text = PVE.Utils.kvm_vga_drivers[value];
307 if (text) {
308 return text + ' (' + value + ')';
309 }
310 return value;
311 },
312
313 kvm_vga_driver_array: function() {
314 var data = [['', PVE.Utils.render_kvm_vga_driver('')]];
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
341 authOK: function() {
342 return Ext.util.Cookies.get('PVEAuthCookie');
343 },
344
345 authClear: function() {
346 Ext.util.Cookies.clear("PVEAuthCookie");
347 },
348
349 // fixme: remove - not needed?
350 gridLineHeigh: function() {
351 return 21;
352
353 //if (Ext.isGecko)
354 //return 23;
355 //return 21;
356 },
357
358 extractRequestError: function(result, verbose) {
359 var msg = gettext('Successful');
360
361 if (!result.success) {
362 msg = gettext("Unknown error");
363 if (result.message) {
364 msg = result.message;
365 if (result.status) {
366 msg += ' (' + result.status + ')';
367 }
368 }
369 if (verbose && Ext.isObject(result.errors)) {
370 msg += "<br>";
371 Ext.Object.each(result.errors, function(prop, desc) {
372 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
373 Ext.htmlEncode(desc);
374 });
375 }
376 }
377
378 return msg;
379 },
380
381 extractFormActionError: function(action) {
382 var msg;
383 switch (action.failureType) {
384 case Ext.form.action.Action.CLIENT_INVALID:
385 msg = gettext('Form fields may not be submitted with invalid values');
386 break;
387 case Ext.form.action.Action.CONNECT_FAILURE:
388 msg = gettext('Connection error');
389 var resp = action.response;
390 if (resp.status && resp.statusText) {
391 msg += " " + resp.status + ": " + resp.statusText;
392 }
393 break;
394 case Ext.form.action.Action.LOAD_FAILURE:
395 case Ext.form.action.Action.SERVER_INVALID:
396 msg = PVE.Utils.extractRequestError(action.result, true);
397 break;
398 }
399 return msg;
400 },
401
402 // Ext.Ajax.request
403 API2Request: function(reqOpts) {
404
405 var newopts = Ext.apply({
406 waitMsg: gettext('Please wait...')
407 }, reqOpts);
408
409 if (!newopts.url.match(/^\/api2/)) {
410 newopts.url = '/api2/extjs' + newopts.url;
411 }
412 delete newopts.callback;
413
414 var createWrapper = function(successFn, callbackFn, failureFn) {
415 Ext.apply(newopts, {
416 success: function(response, options) {
417 if (options.waitMsgTarget) {
418 if (PVE.Utils.toolkit === 'touch') {
419 options.waitMsgTarget.setMasked(false);
420 } else {
421 options.waitMsgTarget.setLoading(false);
422 }
423 }
424 var result = Ext.decode(response.responseText);
425 response.result = result;
426 if (!result.success) {
427 response.htmlStatus = PVE.Utils.extractRequestError(result, true);
428 Ext.callback(callbackFn, options.scope, [options, false, response]);
429 Ext.callback(failureFn, options.scope, [response, options]);
430 return;
431 }
432 Ext.callback(callbackFn, options.scope, [options, true, response]);
433 Ext.callback(successFn, options.scope, [response, options]);
434 },
435 failure: function(response, options) {
436 if (options.waitMsgTarget) {
437 if (PVE.Utils.toolkit === 'touch') {
438 options.waitMsgTarget.setMasked(false);
439 } else {
440 options.waitMsgTarget.setLoading(false);
441 }
442 }
443 response.result = {};
444 try {
445 response.result = Ext.decode(response.responseText);
446 } catch(e) {}
447 var msg = gettext('Connection error') + ' - server offline?';
448 if (response.aborted) {
449 msg = gettext('Connection error') + ' - aborted.';
450 } else if (response.timedout) {
451 msg = gettext('Connection error') + ' - Timeout.';
452 } else if (response.status && response.statusText) {
453 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
454 }
455 response.htmlStatus = msg;
456 Ext.callback(callbackFn, options.scope, [options, false, response]);
457 Ext.callback(failureFn, options.scope, [response, options]);
458 }
459 });
460 };
461
462 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
463
464 var target = newopts.waitMsgTarget;
465 if (target) {
466 if (PVE.Utils.toolkit === 'touch') {
467 target.setMasked({ xtype: 'loadmask', message: newopts.waitMsg} );
468 } else {
469 // Note: ExtJS bug - this does not work when component is not rendered
470 target.setLoading(newopts.waitMsg);
471 }
472 }
473 Ext.Ajax.request(newopts);
474 },
475
476 assemble_field_data: function(values, data) {
477 if (Ext.isObject(data)) {
478 Ext.Object.each(data, function(name, val) {
479 if (values.hasOwnProperty(name)) {
480 var bucket = values[name];
481 if (!Ext.isArray(bucket)) {
482 bucket = values[name] = [bucket];
483 }
484 if (Ext.isArray(val)) {
485 values[name] = bucket.concat(val);
486 } else {
487 bucket.push(val);
488 }
489 } else {
490 values[name] = val;
491 }
492 });
493 }
494 },
495
496 checked_command: function(orig_cmd) {
497 PVE.Utils.API2Request({
498 url: '/nodes/localhost/subscription',
499 method: 'GET',
500 //waitMsgTarget: me,
501 failure: function(response, opts) {
502 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
503 },
504 success: function(response, opts) {
505 var data = response.result.data;
506
507 if (data.status !== 'Active') {
508 Ext.Msg.show({
509 title: gettext('No valid subscription'),
510 icon: Ext.Msg.WARNING,
511 msg: PVE.Utils.noSubKeyHtml,
512 buttons: Ext.Msg.OK,
513 callback: function(btn) {
514 if (btn !== 'ok') {
515 return;
516 }
517 orig_cmd();
518 }
519 });
520 } else {
521 orig_cmd();
522 }
523 }
524 });
525 },
526
527 task_desc_table: {
528 vncproxy: [ 'VM/CT', gettext('Console') ],
529 spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ],
530 vncshell: [ '', gettext('Shell') ],
531 spiceshell: [ '', gettext('Shell') + ' (Spice)' ],
532 qmsnapshot: [ 'VM', gettext('Snapshot') ],
533 qmrollback: [ 'VM', gettext('Rollback') ],
534 qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ],
535 qmcreate: [ 'VM', gettext('Create') ],
536 qmrestore: [ 'VM', gettext('Restore') ],
537 qmdestroy: [ 'VM', gettext('Destroy') ],
538 qmigrate: [ 'VM', gettext('Migrate') ],
539 qmclone: [ 'VM', gettext('Clone') ],
540 qmmove: [ 'VM', gettext('Move disk') ],
541 qmtemplate: [ 'VM', gettext('Convert to template') ],
542 qmstart: [ 'VM', gettext('Start') ],
543 qmstop: [ 'VM', gettext('Stop') ],
544 qmreset: [ 'VM', gettext('Reset') ],
545 qmshutdown: [ 'VM', gettext('Shutdown') ],
546 qmsuspend: [ 'VM', gettext('Suspend') ],
547 qmresume: [ 'VM', gettext('Resume') ],
548 qmconfig: [ 'VM', gettext('Configure') ],
549 vzcreate: ['CT', gettext('Create') ],
550 vzrestore: ['CT', gettext('Restore') ],
551 vzdestroy: ['CT', gettext('Destroy') ],
552 vzmigrate: [ 'CT', gettext('Migrate') ],
553 vzstart: ['CT', gettext('Start') ],
554 vzstop: ['CT', gettext('Stop') ],
555 vzmount: ['CT', gettext('Mount') ],
556 vzumount: ['CT', gettext('Unmount') ],
557 vzshutdown: ['CT', gettext('Shutdown') ],
558 vzsuspend: [ 'CT', gettext('Suspend') ],
559 vzresume: [ 'CT', gettext('Resume') ],
560 vzsnapshot: [ 'CT', gettext('Snapshot') ],
561 vzrollback: [ 'CT', gettext('Rollback') ],
562 vzdelsnapshot: [ 'CT', gettext('Delete Snapshot') ],
563 vztemplate: [ 'CT', gettext('Convert to template') ],
564 vzclone: [ 'CT', gettext('Clone') ],
565 hamigrate: [ 'HA', gettext('Migrate') ],
566 hastart: [ 'HA', gettext('Start') ],
567 hastop: [ 'HA', gettext('Stop') ],
568 srvstart: ['SRV', gettext('Start') ],
569 srvstop: ['SRV', gettext('Stop') ],
570 srvrestart: ['SRV', gettext('Restart') ],
571 srvreload: ['SRV', gettext('Reload') ],
572 cephcreatemon: ['Ceph Monitor', gettext('Create') ],
573 cephdestroymon: ['Ceph Monitor', gettext('Destroy') ],
574 cephcreateosd: ['Ceph OSD', gettext('Create') ],
575 cephdestroyosd: ['Ceph OSD', gettext('Destroy') ],
576 imgcopy: ['', gettext('Copy data') ],
577 imgdel: ['', gettext('Erase data') ],
578 download: ['', gettext('Download') ],
579 vzdump: ['', gettext('Backup') ],
580 aptupdate: ['', gettext('Update package database') ],
581 startall: [ '', gettext('Start all VMs and Containers') ],
582 stopall: [ '', gettext('Stop all VMs and Containers') ],
583 migrateall: [ '', gettext('Migrate all VMs and Containers') ]
584 },
585
586 format_task_description: function(type, id) {
587 var farray = PVE.Utils.task_desc_table[type];
588 if (!farray) {
589 return type;
590 }
591 var prefix = farray[0];
592 var text = farray[1];
593 if (prefix) {
594 return prefix + ' ' + id + ' - ' + text;
595 }
596 return text;
597 },
598
599 parse_task_upid: function(upid) {
600 var task = {};
601
602 var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
603 if (!res) {
604 throw "unable to parse upid '" + upid + "'";
605 }
606 task.node = res[1];
607 task.pid = parseInt(res[2], 16);
608 task.pstart = parseInt(res[3], 16);
609 task.starttime = parseInt(res[4], 16);
610 task.type = res[5];
611 task.id = res[6];
612 task.user = res[7];
613
614 task.desc = PVE.Utils.format_task_description(task.type, task.id);
615
616 return task;
617 },
618
619 format_size: function(size) {
620 /*jslint confusion: true */
621
622 if (size < 1024) {
623 return size;
624 }
625
626 var kb = size / 1024;
627
628 if (kb < 1024) {
629 return kb.toFixed(0) + "KB";
630 }
631
632 var mb = size / (1024*1024);
633
634 if (mb < 1024) {
635 return mb.toFixed(0) + "MB";
636 }
637
638 var gb = mb / 1024;
639
640 if (gb < 1024) {
641 return gb.toFixed(2) + "GB";
642 }
643
644 var tb = gb / 1024;
645
646 return tb.toFixed(2) + "TB";
647
648 },
649
650 format_html_bar: function(per, text) {
651
652 return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" +
653 "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" +
654 "</div></div>";
655
656 },
657
658 format_cpu_bar: function(per1, per2, text) {
659
660 return "<div class='pve-bar-border'>" +
661 "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" +
662 "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" +
663 "<div class='pve-bar-text'>" + text + "</div>" +
664 "</div>";
665 },
666
667 format_large_bar: function(per, text) {
668
669 if (!text) {
670 text = per.toFixed(1) + "%";
671 }
672
673 return "<div class='pve-largebar-border'>" +
674 "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" +
675 "<div class='pve-largebar-text'>" + text + "</div>" +
676 "</div>";
677 },
678
679 format_duration_long: function(ut) {
680
681 var days = Math.floor(ut / 86400);
682 ut -= days*86400;
683 var hours = Math.floor(ut / 3600);
684 ut -= hours*3600;
685 var mins = Math.floor(ut / 60);
686 ut -= mins*60;
687
688 var hours_str = '00' + hours.toString();
689 hours_str = hours_str.substr(hours_str.length - 2);
690 var mins_str = "00" + mins.toString();
691 mins_str = mins_str.substr(mins_str.length - 2);
692 var ut_str = "00" + ut.toString();
693 ut_str = ut_str.substr(ut_str.length - 2);
694
695 if (days) {
696 var ds = days > 1 ? PVE.Utils.daysText : PVE.Utils.dayText;
697 return days.toString() + ' ' + ds + ' ' +
698 hours_str + ':' + mins_str + ':' + ut_str;
699 } else {
700 return hours_str + ':' + mins_str + ':' + ut_str;
701 }
702 },
703
704 format_duration_short: function(ut) {
705
706 if (ut < 60) {
707 return ut.toString() + 's';
708 }
709
710 if (ut < 3600) {
711 var mins = ut / 60;
712 return mins.toFixed(0) + 'm';
713 }
714
715 if (ut < 86400) {
716 var hours = ut / 3600;
717 return hours.toFixed(0) + 'h';
718 }
719
720 var days = ut / 86400;
721 return days.toFixed(0) + 'd';
722 },
723
724 yesText: gettext('Yes'),
725 noText: gettext('No'),
726 noneText: gettext('none'),
727 errorText: gettext('Error'),
728 unknownText: gettext('Unknown'),
729 defaultText: gettext('Default'),
730 daysText: gettext('days'),
731 dayText: gettext('day'),
732 runningText: gettext('running'),
733 stoppedText: gettext('stopped'),
734 neverText: gettext('never'),
735 totalText: gettext('Total'),
736 usedText: gettext('Used'),
737 directoryText: gettext('Directory'),
738 imagesText: gettext('Disk image'),
739 backupFileText: gettext('VZDump backup file'),
740 vztmplText: gettext('Container template'),
741 isoImageText: gettext('ISO image'),
742 containersText: gettext('Container'),
743
744 format_expire: function(date) {
745 if (!date) {
746 return PVE.Utils.neverText;
747 }
748 return Ext.Date.format(date, "Y-m-d");
749 },
750
751 format_storage_type: function(value) {
752 if (value === 'dir') {
753 return PVE.Utils.directoryText;
754 } else if (value === 'nfs') {
755 return 'NFS';
756 } else if (value === 'glusterfs') {
757 return 'GlusterFS';
758 } else if (value === 'lvm') {
759 return 'LVM';
760 } else if (value === 'lvmthin') {
761 return 'LVM-Thin';
762 } else if (value === 'iscsi') {
763 return 'iSCSI';
764 } else if (value === 'rbd') {
765 return 'RBD';
766 } else if (value === 'sheepdog') {
767 return 'Sheepdog';
768 } else if (value === 'zfs') {
769 return 'ZFS over iSCSI';
770 } else if (value === 'zfspool') {
771 return 'ZFS';
772 } else if (value === 'iscsidirect') {
773 return 'iSCSIDirect';
774 } else if (value === 'drbd') {
775 return 'DRBD';
776 } else {
777 return PVE.Utils.unknownText;
778 }
779 },
780
781 format_boolean_with_default: function(value) {
782 if (Ext.isDefined(value) && value !== '') {
783 return value ? PVE.Utils.yesText : PVE.Utils.noText;
784 }
785 return PVE.Utils.defaultText;
786 },
787
788 format_boolean: function(value) {
789 return value ? PVE.Utils.yesText : PVE.Utils.noText;
790 },
791
792 format_neg_boolean: function(value) {
793 return !value ? PVE.Utils.yesText : PVE.Utils.noText;
794 },
795
796 format_content_types: function(value) {
797 var cta = [];
798
799 Ext.each(value.split(',').sort(), function(ct) {
800 if (ct === 'images') {
801 cta.push(PVE.Utils.imagesText);
802 } else if (ct === 'backup') {
803 cta.push(PVE.Utils.backupFileText);
804 } else if (ct === 'vztmpl') {
805 cta.push(PVE.Utils.vztmplText);
806 } else if (ct === 'iso') {
807 cta.push(PVE.Utils.isoImageText);
808 } else if (ct === 'rootdir') {
809 cta.push(PVE.Utils.containersText);
810 }
811 });
812
813 return cta.join(', ');
814 },
815
816 render_storage_content: function(value, metaData, record) {
817 var data = record.data;
818 if (Ext.isNumber(data.channel) &&
819 Ext.isNumber(data.id) &&
820 Ext.isNumber(data.lun)) {
821 return "CH " +
822 Ext.String.leftPad(data.channel,2, '0') +
823 " ID " + data.id + " LUN " + data.lun;
824 }
825 return data.volid.replace(/^.*:(.*\/)?/,'');
826 },
827
828 render_serverity: function (value) {
829 return PVE.Utils.log_severity_hash[value] || value;
830 },
831
832 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
833
834 if (!(record.data.uptime && Ext.isNumeric(value))) {
835 return '';
836 }
837
838 var maxcpu = record.data.maxcpu || 1;
839
840 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
841 return '';
842 }
843
844 var per = value * 100;
845
846 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
847 },
848
849 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
850 /*jslint confusion: true */
851
852 if (!Ext.isNumeric(value)) {
853 return '';
854 }
855
856 return PVE.Utils.format_size(value);
857 },
858
859 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
860 var servertime = new Date(value * 1000);
861 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
862 },
863
864 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
865
866 var mem = value;
867 var maxmem = record.data.maxmem;
868
869 if (!record.data.uptime) {
870 return '';
871 }
872
873 if (!(Ext.isNumeric(mem) && maxmem)) {
874 return '';
875 }
876
877 var per = (mem * 100) / maxmem;
878
879 return per.toFixed(1) + '%';
880 },
881
882 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
883
884 var disk = value;
885 var maxdisk = record.data.maxdisk;
886
887 if (!(Ext.isNumeric(disk) && maxdisk)) {
888 return '';
889 }
890
891 var per = (disk * 100) / maxdisk;
892
893 return per.toFixed(1) + '%';
894 },
895
896 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
897
898 var cls = 'pve-itype-icon-' + value;
899
900 if (record.data.running) {
901 metaData.tdCls = cls + "-running";
902 } else if (record.data.template) {
903 metaData.tdCls = cls + "-template";
904 } else {
905 metaData.tdCls = cls;
906 }
907
908 return value;
909 },
910
911 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
912
913 var uptime = value;
914
915 if (uptime === undefined) {
916 return '';
917 }
918
919 if (uptime <= 0) {
920 return '-';
921 }
922
923 return PVE.Utils.format_duration_long(uptime);
924 },
925
926 render_support_level: function(value, metaData, record) {
927 return PVE.Utils.support_level_hash[value] || '-';
928 },
929
930 render_upid: function(value, metaData, record) {
931 var type = record.data.type;
932 var id = record.data.id;
933
934 return PVE.Utils.format_task_description(type, id);
935 },
936
937 dialog_title: function(subject, create, isAdd) {
938 if (create) {
939 if (isAdd) {
940 return gettext('Add') + ': ' + subject;
941 } else {
942 return gettext('Create') + ': ' + subject;
943 }
944 } else {
945 return gettext('Edit') + ': ' + subject;
946 }
947 },
948
949 windowHostname: function() {
950 return window.location.hostname.replace(IP6_bracket_match,
951 function(m, addr, offset, original) { return addr; });
952 },
953
954 openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
955 var dv = PVE.Utils.defaultViewer(allowSpice);
956 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
957 },
958
959 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
960 // kvm, lxc, shell, upgrade
961
962 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
963 throw "missing vmid";
964 }
965
966 if (!nodename) {
967 throw "no nodename specified";
968 }
969
970 if (viewer === 'applet' || viewer === 'html5') {
971 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname, true);
972 } else if (viewer === 'vv') {
973 var url;
974 var params = { proxy: PVE.Utils.windowHostname() };
975 if (vmtype === 'kvm') {
976 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
977 PVE.Utils.openSpiceViewer(url, params);
978 } else if (vmtype === 'lxc') {
979 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
980 PVE.Utils.openSpiceViewer(url, params);
981 } else if (vmtype === 'shell') {
982 url = '/nodes/' + nodename + '/spiceshell';
983 PVE.Utils.openSpiceViewer(url, params);
984 } else if (vmtype === 'upgrade') {
985 url = '/nodes/' + nodename + '/spiceshell';
986 params.upgrade = 1;
987 PVE.Utils.openSpiceViewer(url, params);
988 }
989 } else {
990 throw "unknown viewer type";
991 }
992 },
993
994 defaultViewer: function(allowSpice) {
995 var vncdefault = 'html5';
996 var dv = PVE.VersionInfo.console || vncdefault;
997 if (dv === 'vv' && !allowSpice) {
998 dv = vncdefault;
999 }
1000
1001 return dv;
1002 },
1003
1004 openVNCViewer: function(vmtype, vmid, nodename, vmname, novnc) {
1005 var url = Ext.urlEncode({
1006 console: vmtype, // kvm, lxc, upgrade or shell
1007 novnc: novnc ? 1 : 0,
1008 vmid: vmid,
1009 vmname: vmname,
1010 node: nodename
1011 });
1012 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
1013 nw.focus();
1014 },
1015
1016 openSpiceViewer: function(url, params){
1017
1018 var downloadWithName = function(uri, name) {
1019 var link = Ext.DomHelper.append(document.body, {
1020 tag: 'a',
1021 href: uri,
1022 css : 'display:none;visibility:hidden;height:0px;'
1023 });
1024
1025 // Note: we need to tell android the correct file name extension
1026 // but we do not set 'download' tag for other environments, because
1027 // It can have strange side effects (additional user prompt on firefox)
1028 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1029 if (andriod) {
1030 link.download = name;
1031 }
1032
1033 if (link.fireEvent) {
1034 link.fireEvent('onclick');
1035 } else {
1036 var evt = document.createEvent("MouseEvents");
1037 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1038 link.dispatchEvent(evt);
1039 }
1040 };
1041
1042 PVE.Utils.API2Request({
1043 url: url,
1044 params: params,
1045 method: 'POST',
1046 failure: function(response, opts){
1047 Ext.Msg.alert('Error', response.htmlStatus);
1048 },
1049 success: function(response, opts){
1050 var raw = "[virt-viewer]\n";
1051 Ext.Object.each(response.result.data, function(k, v) {
1052 raw += k + "=" + v + "\n";
1053 });
1054 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1055 encodeURIComponent(raw);
1056
1057 downloadWithName(url, "pve-spice.vv");
1058 }
1059 });
1060 },
1061
1062 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
1063 // use el.mask() instead
1064 setErrorMask: function(comp, msg) {
1065 var el = comp.el;
1066 if (!el) {
1067 return;
1068 }
1069 if (!msg) {
1070 el.unmask();
1071 } else {
1072 if (msg === true) {
1073 el.mask(gettext("Loading..."));
1074 } else {
1075 el.mask(msg);
1076 }
1077 }
1078 },
1079
1080 monStoreErrors: function(me, store) {
1081 me.mon(store, 'beforeload', function(s, operation, eOpts) {
1082 if (!me.loadCount) {
1083 me.loadCount = 0; // make sure it is numeric
1084 PVE.Utils.setErrorMask(me, true);
1085 }
1086 });
1087
1088 // only works with 'pve' proxy
1089 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
1090 me.loadCount++;
1091
1092 if (success) {
1093 PVE.Utils.setErrorMask(me, false);
1094 return;
1095 }
1096
1097 var msg;
1098 var operation = request.operation;
1099 var error = operation.getError();
1100 if (error.statusText) {
1101 msg = error.statusText + ' (' + error.status + ')';
1102 } else {
1103 msg = gettext('Connection error');
1104 }
1105 PVE.Utils.setErrorMask(me, msg);
1106 });
1107 }
1108
1109 }});
1110