]> git.proxmox.com Git - pve-manager.git/blame - www/manager/Utils.js
fix typo
[pve-manager.git] / www / manager / Utils.js
CommitLineData
aff192e6
DM
1Ext.ns('PVE');
2
3// avoid errors when running without development tools
4if (!Ext.isDefined(Ext.global.console)) {
5 var console = {
6 dir: function() {},
7 log: function() {}
8 };
9}
10console.log("Starting PVE Manager");
11
12Ext.Ajax.defaultHeaders = {
13 'Accept': 'application/json'
14};
15
16// do not send '_dc' parameter
17Ext.Ajax.disableCaching = false;
18
19Ext.Ajax.on('beforerequest', function(conn, options) {
20 if (PVE.CSRFPreventionToken) {
21 if (!options.headers) {
22 options.headers = {};
23 }
24 options.headers.CSRFPreventionToken = PVE.CSRFPreventionToken;
25 }
26});
27
10b4a988 28// custom Vtypes
aff192e6
DM
29Ext.apply(Ext.form.field.VTypes, {
30 IPAddress: function(v) {
31 return (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/).test(v);
32 },
33 IPAddressText: 'Must be a numeric IP address',
34 IPAddressMask: /[\d\.]/i,
35
36 MacAddress: function(v) {
37 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v);
38 },
39 MacAddressMask: /[a-fA-F0-9:]/,
40 MacAddressText: 'Must be a valid MAC address (example: "01:23:45:67:89:ab")',
41
42 BridgeName: function(v) {
43 return (/^vmbr\d{1,4}$/).test(v);
44 },
45 BridgeNameText: 'Allowable bridge names: vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
46
47 BondName: function(v) {
48 return (/^bond\d{1,4}$/).test(v);
49 },
50 BondNameText: 'Allowable bond names: bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
51
52 QemuStartDate: function(v) {
53 return (/^(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)$/).test(v);
54 },
55 QemuStartDateText: 'Valid format for date are: "now" or "2006-06-17T16:01:21" or "2006-06-17"',
56
57 StorageId: function(v) {
58 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
59 },
10b4a988
DM
60 StorageIdText: "ID contains illegal characters (allowed characters: 'a-z', '0-9', '-', '_' and '.')",
61
62 HttpProxy: function(v) {
63 return (/^http:\/\/.*$/).test(v);
64 },
e7b95416 65 HttpProxyText: "Must confirm to schema 'http://.*' (example: 'http://username:password@host:port/')"
aff192e6
DM
66});
67
68// we dont want that a displayfield set the form dirty flag!
69Ext.override(Ext.form.field.Display, {
70 isDirty: function() { return false; }
71});
72
73// hack: ExtJS does not display the correct value if we
74// call setValue while the store is loading, so we need
75// to call it again after loading
76Ext.override(Ext.form.field.ComboBox, {
77 onLoad: function() {
78 this.setValue(this.value, false);
79 this.callOverridden(arguments);
80 }
81});
82
83Ext.define('PVE.Utils', { statics: {
84
85 // this class only contains static functions
86
87 log_severity_hash: {
88 0: "panic",
89 1: "alert",
90 2: "critical",
91 3: "error",
92 4: "warning",
93 5: "notice",
94 6: "info",
95 7: "debug"
96 },
97
98 kvm_ostypes: {
99 other: 'Other',
100 wxp: 'Microsoft Windows XP/2003',
101 w2k: 'Microsoft Windows 2000',
102 w2k8: 'Microsoft Windows Vista/2008',
103 win7: 'Microsoft Windows 7/2008r2',
104 l24: 'Linux 2.4 Kernel',
1ea7a234 105 l26: 'Linux 3.X/2.6 Kernel'
aff192e6
DM
106 },
107
108 render_kvm_ostype: function (value) {
109 if (!value) {
110 return 'Other';
111 }
112 var text = PVE.Utils.kvm_ostypes[value];
113 if (text) {
114 return text + ' (' + value + ')';
115 }
116 return value;
117 },
118
119 // fixme: auto-generate this
120 // for now, please keep in sync with PVE::Tools::kvmkeymaps
121 kvm_keymaps: {
122 //ar: 'Arabic',
123 dk: 'Danish',
124 de: 'German',
125 'de-ch': 'German (Swiss)',
126 'en-gb': 'English (UK)',
127 'en-us': 'English (USA',
128 es: 'Spanish',
129 //et: 'Estonia',
130 fi: 'Finnish',
131 //fo: 'Faroe Islands',
132 fr: 'French',
133 'fr-be': 'French (Belgium)',
134 'fr-ca': 'French (Canada)',
135 'fr-ch': 'French (Swiss)',
136 //hr: 'Croatia',
137 hu: 'Hungarian',
138 is: 'Icelandic',
139 it: 'Italian',
140 ja: 'Japanese',
141 lt: 'Lithuanian',
142 //lv: 'Latvian',
143 mk: 'Macedonian',
144 nl: 'Dutch',
145 //'nl-be': 'Dutch (Belgium)',
146 no: 'Norwegian',
147 pl: 'Polish',
148 pt: 'Portuguese',
149 'pt-br': 'Portuguese (Brazil)',
150 //ru: 'Russian',
151 si: 'Slovenian'
152 //sv: 'Swedish',
153 //th: 'Thai',
154 //tr: 'Turkish'
155 },
156
157 kvm_vga_drivers: {
158 std: 'Standard VGA',
159 vmware: 'VMWare compatible',
160 cirrus: 'Cirrus Logic GD5446'
161 },
162
163 render_kvm_language: function (value) {
164 if (!value) {
165 return 'Default';
166 }
167 var text = PVE.Utils.kvm_keymaps[value];
168 if (text) {
169 return text + ' (' + value + ')';
170 }
171 return value;
172 },
173
174 kvm_keymap_array: function() {
175 var data = [['', PVE.Utils.render_kvm_language('')]];
176 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
177 data.push([key, PVE.Utils.render_kvm_language(value)]);
178 });
179
180 return data;
181 },
182
19fa0647
DM
183 language_map: {
184 en: 'English',
185 de: 'German'
186 },
187
188 render_language: function (value) {
189 if (!value) {
190 return 'Default (English)';
191 }
192 var text = PVE.Utils.language_map[value];
193 if (text) {
194 return text + ' (' + value + ')';
195 }
196 return value;
197 },
198
199 language_array: function() {
200 var data = [['', PVE.Utils.render_language('')]];
201 Ext.Object.each(PVE.Utils.language_map, function(key, value) {
202 data.push([key, PVE.Utils.render_language(value)]);
203 });
204
205 return data;
206 },
207
aff192e6
DM
208 render_kvm_vga_driver: function (value) {
209 if (!value) {
133362ca 210 return 'OS default';
aff192e6
DM
211 }
212 var text = PVE.Utils.kvm_vga_drivers[value];
213 if (text) {
214 return text + ' (' + value + ')';
215 }
216 return value;
217 },
218
219 kvm_vga_driver_array: function() {
220 var data = [['', PVE.Utils.render_kvm_vga_driver('')]];
221 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
222 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
223 });
224
225 return data;
226 },
227
228 authOK: function() {
229 return Ext.util.Cookies.get('PVEAuthCookie');
230 },
231
232 authClear: function() {
233 Ext.util.Cookies.clear("PVEAuthCookie");
234 },
235
236 // fixme: remove - not needed?
237 gridLineHeigh: function() {
238 return 21;
239
240 //if (Ext.isGecko)
241 //return 23;
242 //return 21;
243 },
244
245 extractRequestError: function(result, verbose) {
246 var msg = 'Successful';
247
248 if (!result.success) {
249 msg = "Unknown error";
250 if (result.message) {
251 msg = result.message;
252 if (result.status) {
253 msg += ' (' + result.status + ')';
254 }
255 }
256 if (verbose && Ext.isObject(result.errors)) {
257 msg += "<br>";
258 Ext.Object.each(result.errors, function(prop, desc) {
259 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
260 Ext.htmlEncode(desc);
261 });
262 }
263 }
264
265 return msg;
266 },
267
268 extractFormActionError: function(action) {
269 var msg;
270 switch (action.failureType) {
271 case Ext.form.action.Action.CLIENT_INVALID:
272 msg = 'Form fields may not be submitted with invalid values';
273 break;
274 case Ext.form.action.Action.CONNECT_FAILURE:
275 msg = 'Connect failure';
276 var resp = action.response;
277 if (resp.status && resp.statusText) {
278 msg += " " + resp.status + ": " + resp.statusText;
279 }
280 break;
281 case Ext.form.action.Action.LOAD_FAILURE:
282 case Ext.form.action.Action.SERVER_INVALID:
283 msg = PVE.Utils.extractRequestError(action.result, true);
284 break;
285 }
286 return msg;
287 },
288
289 // Ext.Ajax.request
290 API2Request: function(reqOpts) {
291
292 var newopts = Ext.apply({
293 waitMsg: 'Please wait...'
294 }, reqOpts);
295
296 if (!newopts.url.match(/^\/api2/)) {
297 newopts.url = '/api2/extjs' + newopts.url;
298 }
299 delete newopts.callback;
300
301 var createWrapper = function(successFn, callbackFn, failureFn) {
302 Ext.apply(newopts, {
303 success: function(response, options) {
304 if (options.waitMsgTarget) {
305 options.waitMsgTarget.setLoading(false);
306 }
307 var result = Ext.decode(response.responseText);
308 response.result = result;
309 if (!result.success) {
310 response.htmlStatus = PVE.Utils.extractRequestError(result, true);
311 Ext.callback(callbackFn, options.scope, [options, false, response]);
312 Ext.callback(failureFn, options.scope, [response, options]);
313 return;
314 }
315 Ext.callback(callbackFn, options.scope, [options, true, response]);
316 Ext.callback(successFn, options.scope, [response, options]);
317 },
318 failure: function(response, options) {
319 if (options.waitMsgTarget) {
320 options.waitMsgTarget.setLoading(false);
321 }
322 var result = Ext.decode(response.responseText);
323 response.result = result || {};
324 var msg = "Connection error - server offline?";
325 if (response.aborted) {
326 msg = 'Transaction aborted.';
327 } else if (response.timedout) {
328 msg = 'Communication failure: Timeout.';
329 } else if (response.status && response.statusText) {
330 msg = 'Connection error ' + response.status + ': ' + response.statusText;
331 }
332 response.htmlStatus = msg;
333 Ext.callback(callbackFn, options.scope, [options, false, response]);
334 Ext.callback(failureFn, options.scope, [response, options]);
335 }
336 });
337 };
338
339 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
340
341 var target = newopts.waitMsgTarget;
342 if (target) {
343 // Note: ExtJS bug - this does not work when component is not rendered
344 target.setLoading(newopts.waitMsg, true);
345 }
346 Ext.Ajax.request(newopts);
347 },
348
349 assemble_field_data: function(values, data) {
350 if (Ext.isObject(data)) {
351 Ext.Object.each(data, function(name, val) {
352 if (values.hasOwnProperty(name)) {
353 var bucket = values[name];
354 if (!Ext.isArray(bucket)) {
355 bucket = values[name] = [bucket];
356 }
357 if (Ext.isArray(val)) {
358 values[name] = bucket.concat(val);
359 } else {
360 bucket.push(val);
361 }
362 } else {
363 values[name] = val;
364 }
365 });
366 }
367 },
368
acc0c13a
DM
369 task_desc_table: {
370 vncproxy: 'VNC connection to VM/CT {0}',
371 vncshell: 'VNC shell',
997b5dd6
DM
372 qmcreate: 'Create VM {0}',
373 qmrestore: 'Restore VM {0}',
374 qmdestroy: 'Destroy VM {0}',
acc0c13a
DM
375 qmigrate: 'Migrate VM {0}',
376 qmstart: 'Start VM {0}',
377 qmstop: 'Stop VM {0}',
378 qmreset: 'Reset VM {0}',
379 qmshutdown: 'Shutdown VM {0}',
380 qmsuspend: 'Suspend VM {0}',
381 qmresume: 'Resume VM {0}',
382 vzcreate: 'Create CT {0}',
aabf3add 383 vzrestore: 'Restore CT {0}',
acc0c13a
DM
384 vzdestroy: 'Destroy CT {0}',
385 vzstart: 'Start CT {0}',
386 vzstop: 'Stop CT {0}',
387 srvstart: 'Start service {0}',
388 srvstop: 'Stop service {0}',
389 srvrestart: 'Restart service {0}',
4a4051d8
DM
390 srvreload: 'Reload service {0}',
391 vzdump: 'Backup'
acc0c13a 392 },
d1233856 393
acc0c13a
DM
394 format_task_description: function(type, id) {
395 var format = PVE.Utils.task_desc_table[type];
396 if (format) {
397 return Ext.String.format(format, id);
d1233856 398 }
aff192e6
DM
399 return type;
400 },
401
aff192e6
DM
402 parse_task_upid: function(upid) {
403 var task = {};
404
55d19d9b 405 var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
aff192e6
DM
406 if (!res) {
407 throw "unable to parse upid '" + upid + "'";
408 }
409 task.node = res[1];
410 task.pid = parseInt(res[2], 16);
411 task.pstart = parseInt(res[3], 16);
412 task.starttime = parseInt(res[4], 16);
413 task.type = res[5];
414 task.id = res[6];
415 task.user = res[7];
416
417 task.desc = PVE.Utils.format_task_description(task.type, task.id);
418
419 return task;
420 },
421
422 format_size: function(size) {
a2f57991 423 /*jslint confusion: true */
aff192e6 424
a1d849df
DM
425 if (size < 1024) {
426 return size;
427 }
428
aff192e6
DM
429 var kb = size / 1024;
430
431 if (kb < 1024) {
432 return kb.toFixed(0) + "KB";
433 }
434
435 var mb = size / (1024*1024);
436
437 if (mb < 1024) {
438 return mb.toFixed(0) + "MB";
439 }
440
441 var gb = mb / 1024;
442
443 if (gb < 1024) {
444 return gb.toFixed(2) + "GB";
445 }
446
447 var tb = gb / 1024;
448
449 return tb.toFixed(2) + "TB";
450
451 },
452
453 format_html_bar: function(per, text) {
454
455 return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" +
456 "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" +
457 "</div></div>";
458
459 },
460
461 format_cpu_bar: function(per1, per2, text) {
462
463 return "<div class='pve-bar-border'>" +
464 "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" +
465 "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" +
466 "<div class='pve-bar-text'>" + text + "</div>" +
467 "</div>";
468 },
469
470 format_large_bar: function(per, text) {
471
472 if (!text) {
473 text = per.toFixed(1) + "%";
474 }
475
476 return "<div class='pve-largebar-border'>" +
477 "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" +
478 "<div class='pve-largebar-text'>" + text + "</div>" +
479 "</div>";
480 },
481
482 format_duration_long: function(ut) {
483
484 var days = Math.floor(ut / 86400);
485 ut -= days*86400;
486 var hours = Math.floor(ut / 3600);
487 ut -= hours*3600;
488 var mins = Math.floor(ut / 60);
489 ut -= mins*60;
490
491 var hours_str = '00' + hours.toString();
492 hours_str = hours_str.substr(hours_str.length - 2);
493 var mins_str = "00" + mins.toString();
494 mins_str = mins_str.substr(mins_str.length - 2);
495 var ut_str = "00" + ut.toString();
496 ut_str = ut_str.substr(ut_str.length - 2);
497
498 if (days) {
499 var ds = days > 1 ? 'days' : 'day';
500 return days.toString() + ' ' + ds + ' ' +
501 hours_str + ':' + mins_str + ':' + ut_str;
502 } else {
503 return hours_str + ':' + mins_str + ':' + ut_str;
504 }
505 },
506
507 format_duration_short: function(ut) {
508
509 if (ut < 60) {
510 return ut.toString() + 's';
511 }
512
513 if (ut < 3600) {
514 var mins = ut / 60;
515 return mins.toFixed(0) + 'm';
516 }
517
518 if (ut < 86400) {
519 var hours = ut / 3600;
520 return hours.toFixed(0) + 'h';
521 }
522
523 var days = ut / 86400;
524 return days.toFixed(0) + 'd';
525 },
526
527 format_storage_type: function(value) {
528 if (value === 'dir') {
529 return 'Directory';
530 } else if (value === 'nfs') {
531 return 'NFS';
532 } else if (value === 'lvm') {
533 return 'LVM';
534 } else if (value === 'iscsi') {
535 return 'iSCSI';
536 } else {
537 return 'unknown';
538 }
539 },
540
541 format_boolean_with_default: function(value) {
542 if (Ext.isDefined(value) && value !== '') {
543 return value ? 'Yes' : 'No';
544 }
545 return 'Default';
546 },
547
548 format_boolean: function(value) {
549 return value ? 'Yes' : 'No';
550 },
551
552 format_neg_boolean: function(value) {
553 return !value ? 'Yes' : 'No';
554 },
555
556 format_content_types: function(value) {
557 var cta = [];
558
559 Ext.each(value.split(','), function(ct) {
560 if (ct === 'images') {
561 cta.push('Images');
562 } else if (ct === 'backup') {
563 cta.push('Backups');
564 } else if (ct === 'vztmpl') {
565 cta.push('Templates');
566 } else if (ct === 'iso') {
567 cta.push('ISO');
9f767883
DM
568 } else if (ct === 'rootdir') {
569 cta.push('Containers');
aff192e6
DM
570 }
571 });
572
573 return cta.join(', ');
574 },
575
576 render_storage_content: function(value, metaData, record) {
577 var data = record.data;
578 if (Ext.isNumber(data.channel) &&
579 Ext.isNumber(data.id) &&
580 Ext.isNumber(data.lun)) {
581 return "CH " +
582 Ext.String.leftPad(data.channel,2, '0') +
583 " ID " + data.id + " LUN " + data.lun;
584 }
585 return data.volid.replace(/^.*:(.*\/)?/,'');
586 },
587
588 render_serverity: function (value) {
589 return PVE.Utils.log_severity_hash[value] || value;
590 },
591
592 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
593
594 var maxcpu = record.data.maxcpu;
595
596 if (!record.data.uptime) {
597 return '';
598 }
599
600 if (!(Ext.isNumeric(value) && Ext.isNumeric(maxcpu) && (maxcpu >= 1))) {
601 return '';
602 }
603
604 var per = (value * 100) / maxcpu;
605
606 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
607 },
608
609 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
a2f57991 610 /*jslint confusion: true */
aff192e6
DM
611
612 if (!Ext.isNumeric(value)) {
613 return '';
614 }
615
616 return PVE.Utils.format_size(value);
617 },
618
619 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
620 var servertime = new Date(value * 1000);
621 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
622 },
623
624 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
625
626 var mem = value;
627 var maxmem = record.data.maxmem;
628
629 if (!record.data.uptime) {
630 return '';
631 }
632
633 if (!(Ext.isNumeric(mem) && maxmem)) {
634 return '';
635 }
636
637 var per = (mem * 100) / maxmem;
638
639 return per.toFixed(1) + '%';
640 },
641
642 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
643
644 var disk = value;
645 var maxdisk = record.data.maxdisk;
646
647 if (!(Ext.isNumeric(disk) && maxdisk)) {
648 return '';
649 }
650
651 var per = (disk * 100) / maxdisk;
652
653 return per.toFixed(1) + '%';
654 },
655
656 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
657
658 var cls = 'pve-itype-icon-' + value;
659
660 if (record.data.running) {
661 metaData.tdCls = cls + "-running";
662 } else {
663 metaData.tdCls = cls;
664 }
665
666 return value;
667 },
668
669 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
670
671 var uptime = value;
672
673 if (uptime === undefined) {
674 return '';
675 }
676
677 if (uptime <= 0) {
678 return '-';
679 }
680
681 return PVE.Utils.format_duration_long(uptime);
682 },
683
684 render_upid: function(value, metaData, record) {
685 var type = record.data.type;
686 var id = record.data.id;
687
688 return PVE.Utils.format_task_description(type, id);
176eee4f
DM
689 },
690
691 openConoleWindow: function(vmtype, vmid, nodename) {
692 var url = Ext.urlEncode({
693 console: vmtype, // kvm or openvz
694 vmid: vmid,
695 node: nodename
696 });
697 var nw = window.open("?" + url, '_blank',
698 "innerWidth=745,innerheight=427");
699 nw.focus();
aff192e6 700 }
176eee4f 701
aff192e6
DM
702}});
703