]> git.proxmox.com Git - pve-manager.git/blame - www/manager/Utils.js
allow to add ipv6 address to OpenVZ containers
[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
035cde2b
DM
28var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
29var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
30var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
31var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
32
33
34var IP4_match = new RegExp("^(" + IPV4_REGEXP + ")$");
35
36var IPV6_REGEXP = "(?:" +
37 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
38 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
39 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
40 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
41 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
42 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
43 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
44 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
45 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
46 ")";
47
48var IP64_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
49
10b4a988 50// custom Vtypes
aff192e6
DM
51Ext.apply(Ext.form.field.VTypes, {
52 IPAddress: function(v) {
035cde2b 53 return IP64_match.test(v);
aff192e6 54 },
a2dca26b 55 IPAddressText: gettext('Example') + ': 192.168.1.1',
aff192e6
DM
56 IPAddressMask: /[\d\.]/i,
57
035cde2b
DM
58 IP64Address: function(v) {
59 return IP64_match.test(v);
60 },
61 IP64AddressText: gettext('Example') + ': 192.168.1.1 2001:DB8::42',
62 IP64AddressMask: /[A-Fa-f0-9\.:]/,
63
aff192e6
DM
64 MacAddress: function(v) {
65 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v);
66 },
67 MacAddressMask: /[a-fA-F0-9:]/,
a2dca26b 68 MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab',
aff192e6
DM
69
70 BridgeName: function(v) {
71 return (/^vmbr\d{1,4}$/).test(v);
72 },
a2dca26b 73 BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
aff192e6
DM
74
75 BondName: function(v) {
76 return (/^bond\d{1,4}$/).test(v);
77 },
a2dca26b 78 BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
aff192e6
DM
79
80 QemuStartDate: function(v) {
81 return (/^(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)$/).test(v);
82 },
a2dca26b 83 QemuStartDateText: gettext('Format') + ': "now" or "2006-06-17T16:01:21" or "2006-06-17"',
aff192e6
DM
84
85 StorageId: function(v) {
86 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
87 },
a2dca26b 88 StorageIdText: gettext("Allowed characters") + ": 'a-z', '0-9', '-', '_', '.'",
10b4a988
DM
89
90 HttpProxy: function(v) {
91 return (/^http:\/\/.*$/).test(v);
92 },
667b2be9
DM
93 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
94
95 DnsName: function(v) {
0d201dfe 96 return (/^(([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)\.)*([A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?)$/).test(v);
667b2be9
DM
97 },
98 DnsNameText: gettext('This is not a valid DNS name')
aff192e6
DM
99});
100
101// we dont want that a displayfield set the form dirty flag!
102Ext.override(Ext.form.field.Display, {
103 isDirty: function() { return false; }
104});
105
106// hack: ExtJS does not display the correct value if we
107// call setValue while the store is loading, so we need
108// to call it again after loading
109Ext.override(Ext.form.field.ComboBox, {
110 onLoad: function() {
111 this.setValue(this.value, false);
112 this.callOverridden(arguments);
113 }
114});
115
116Ext.define('PVE.Utils', { statics: {
117
118 // this class only contains static functions
119
120 log_severity_hash: {
121 0: "panic",
122 1: "alert",
123 2: "critical",
124 3: "error",
125 4: "warning",
126 5: "notice",
127 6: "info",
128 7: "debug"
129 },
130
16b69b6c
DM
131 support_level_hash: {
132 'c': gettext('Community'),
133 'b': gettext('Basic'),
134 's': gettext('Standard'),
135 'p': gettext('Premium')
136 },
137
9baa9680
DM
138 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.',
139
aff192e6 140 kvm_ostypes: {
2ed04b3c 141 other: gettext('Other OS types'),
aff192e6
DM
142 wxp: 'Microsoft Windows XP/2003',
143 w2k: 'Microsoft Windows 2000',
144 w2k8: 'Microsoft Windows Vista/2008',
145 win7: 'Microsoft Windows 7/2008r2',
1d7334d2 146 win8: 'Microsoft Windows 8/2012',
aff192e6 147 l24: 'Linux 2.4 Kernel',
51770186
AD
148 l26: 'Linux 3.X/2.6 Kernel',
149 solaris: 'Solaris Kernel'
aff192e6
DM
150 },
151
152 render_kvm_ostype: function (value) {
153 if (!value) {
2ed04b3c 154 return gettext('Other OS types');
aff192e6
DM
155 }
156 var text = PVE.Utils.kvm_ostypes[value];
157 if (text) {
158 return text + ' (' + value + ')';
159 }
160 return value;
161 },
162
74184048
DM
163 render_scsihw: function(value) {
164 if (!value) {
165 return PVE.Utils.defaultText + ' (lsi)';
166 } else if (value === 'lsi') {
167 return 'LSI 53C895A';
168 } else if (value === 'megasas') {
169 return 'MegaRAID SAS 8708EM2';
170 } else if (value === 'virtio-scsi-pci') {
171 return 'VIRTIO';
172 } else {
173 return value;
174 }
175 },
176
aff192e6
DM
177 // fixme: auto-generate this
178 // for now, please keep in sync with PVE::Tools::kvmkeymaps
179 kvm_keymaps: {
180 //ar: 'Arabic',
5fe5283f 181 da: 'Danish',
aff192e6
DM
182 de: 'German',
183 'de-ch': 'German (Swiss)',
184 'en-gb': 'English (UK)',
185 'en-us': 'English (USA',
186 es: 'Spanish',
187 //et: 'Estonia',
188 fi: 'Finnish',
189 //fo: 'Faroe Islands',
190 fr: 'French',
191 'fr-be': 'French (Belgium)',
192 'fr-ca': 'French (Canada)',
193 'fr-ch': 'French (Swiss)',
194 //hr: 'Croatia',
195 hu: 'Hungarian',
196 is: 'Icelandic',
197 it: 'Italian',
198 ja: 'Japanese',
199 lt: 'Lithuanian',
200 //lv: 'Latvian',
201 mk: 'Macedonian',
202 nl: 'Dutch',
203 //'nl-be': 'Dutch (Belgium)',
204 no: 'Norwegian',
205 pl: 'Polish',
206 pt: 'Portuguese',
207 'pt-br': 'Portuguese (Brazil)',
208 //ru: 'Russian',
b4e7aa2c 209 sl: 'Slovenian',
1133bd83 210 sv: 'Swedish',
aff192e6 211 //th: 'Thai',
1133bd83 212 tr: 'Turkish'
aff192e6
DM
213 },
214
215 kvm_vga_drivers: {
0070ee37
DP
216 std: gettext('Standard VGA'),
217 vmware: gettext('VMWare compatible'),
0c47a682 218 cirrus: 'Cirrus Logic GD5446',
76d0de20 219 qxl: 'SPICE',
caf64d77
DM
220 qxl2: 'SPICE dual monitor',
221 qxl3: 'SPICE three monitors',
222 qxl4: 'SPICE four monitors',
0070ee37
DP
223 serial0: gettext('Serial terminal') + ' 0',
224 serial1: gettext('Serial terminal') + ' 1',
225 serial2: gettext('Serial terminal') + ' 2',
226 serial3: gettext('Serial terminal') + ' 3'
aff192e6
DM
227 },
228
229 render_kvm_language: function (value) {
230 if (!value) {
2ed04b3c 231 return PVE.Utils.defaultText;
aff192e6
DM
232 }
233 var text = PVE.Utils.kvm_keymaps[value];
234 if (text) {
235 return text + ' (' + value + ')';
236 }
237 return value;
238 },
239
240 kvm_keymap_array: function() {
241 var data = [['', PVE.Utils.render_kvm_language('')]];
242 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
243 data.push([key, PVE.Utils.render_kvm_language(value)]);
244 });
245
246 return data;
247 },
248
19fa0647 249 language_map: {
d0525640 250 zh_CN: 'Chinese',
226edf6d 251 ca: 'Catalan',
22217294 252 ja: 'Japanese',
19fa0647 253 en: 'English',
18fb1962 254 da: 'Danish',
2198a479 255 de: 'German',
8009570d 256 es: 'Spanish',
4a3a5520 257 fr: 'French',
72da7de7 258 it: 'Italian',
2d32003f
DM
259 nb: 'Norwegian (Bokmal)',
260 nn: 'Norwegian (Nynorsk)',
a4326a6c 261 ru: 'Russian',
b4e7aa2c 262 sl: 'Slovenian',
05e3e996 263 sv: 'Swedish',
fb1cf1bf 264 pl: 'Polish',
d97c5983 265 pt_BR: 'Portuguese (Brazil)',
05e3e996 266 tr: 'Turkish'
19fa0647
DM
267 },
268
269 render_language: function (value) {
270 if (!value) {
7e6b14a8 271 return PVE.Utils.defaultText + ' (English)';
19fa0647
DM
272 }
273 var text = PVE.Utils.language_map[value];
274 if (text) {
275 return text + ' (' + value + ')';
276 }
277 return value;
278 },
279
280 language_array: function() {
281 var data = [['', PVE.Utils.render_language('')]];
282 Ext.Object.each(PVE.Utils.language_map, function(key, value) {
283 data.push([key, PVE.Utils.render_language(value)]);
284 });
285
286 return data;
287 },
288
aff192e6
DM
289 render_kvm_vga_driver: function (value) {
290 if (!value) {
2ed04b3c 291 return PVE.Utils.defaultText;
aff192e6
DM
292 }
293 var text = PVE.Utils.kvm_vga_drivers[value];
294 if (text) {
295 return text + ' (' + value + ')';
296 }
297 return value;
298 },
299
300 kvm_vga_driver_array: function() {
301 var data = [['', PVE.Utils.render_kvm_vga_driver('')]];
302 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
303 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
304 });
305
306 return data;
307 },
308
640f8f14
DM
309 render_kvm_startup: function(value) {
310 var startup = PVE.Parser.parseStartup(value);
311
312 var res = 'order=';
313 if (startup.order === undefined) {
314 res += 'any';
315 } else {
316 res += startup.order;
317 }
318 if (startup.up !== undefined) {
319 res += ',up=' + startup.up;
320 }
321 if (startup.down !== undefined) {
322 res += ',down=' + startup.down;
323 }
324
325 return res;
326 },
327
aff192e6
DM
328 authOK: function() {
329 return Ext.util.Cookies.get('PVEAuthCookie');
330 },
331
332 authClear: function() {
333 Ext.util.Cookies.clear("PVEAuthCookie");
334 },
335
336 // fixme: remove - not needed?
337 gridLineHeigh: function() {
338 return 21;
339
340 //if (Ext.isGecko)
341 //return 23;
342 //return 21;
343 },
344
345 extractRequestError: function(result, verbose) {
a2dca26b 346 var msg = gettext('Successful');
aff192e6
DM
347
348 if (!result.success) {
a2dca26b 349 msg = gettext("Unknown error");
aff192e6
DM
350 if (result.message) {
351 msg = result.message;
352 if (result.status) {
353 msg += ' (' + result.status + ')';
354 }
355 }
356 if (verbose && Ext.isObject(result.errors)) {
357 msg += "<br>";
358 Ext.Object.each(result.errors, function(prop, desc) {
359 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
360 Ext.htmlEncode(desc);
361 });
362 }
363 }
364
365 return msg;
366 },
367
368 extractFormActionError: function(action) {
369 var msg;
370 switch (action.failureType) {
371 case Ext.form.action.Action.CLIENT_INVALID:
a2dca26b 372 msg = gettext('Form fields may not be submitted with invalid values');
aff192e6
DM
373 break;
374 case Ext.form.action.Action.CONNECT_FAILURE:
a2dca26b 375 msg = gettext('Connection error');
aff192e6
DM
376 var resp = action.response;
377 if (resp.status && resp.statusText) {
378 msg += " " + resp.status + ": " + resp.statusText;
379 }
380 break;
381 case Ext.form.action.Action.LOAD_FAILURE:
382 case Ext.form.action.Action.SERVER_INVALID:
383 msg = PVE.Utils.extractRequestError(action.result, true);
384 break;
385 }
386 return msg;
387 },
388
389 // Ext.Ajax.request
390 API2Request: function(reqOpts) {
391
392 var newopts = Ext.apply({
a2dca26b 393 waitMsg: gettext('Please wait...')
aff192e6
DM
394 }, reqOpts);
395
396 if (!newopts.url.match(/^\/api2/)) {
397 newopts.url = '/api2/extjs' + newopts.url;
398 }
399 delete newopts.callback;
400
401 var createWrapper = function(successFn, callbackFn, failureFn) {
402 Ext.apply(newopts, {
403 success: function(response, options) {
404 if (options.waitMsgTarget) {
405 options.waitMsgTarget.setLoading(false);
406 }
407 var result = Ext.decode(response.responseText);
408 response.result = result;
409 if (!result.success) {
410 response.htmlStatus = PVE.Utils.extractRequestError(result, true);
411 Ext.callback(callbackFn, options.scope, [options, false, response]);
412 Ext.callback(failureFn, options.scope, [response, options]);
413 return;
414 }
415 Ext.callback(callbackFn, options.scope, [options, true, response]);
416 Ext.callback(successFn, options.scope, [response, options]);
417 },
418 failure: function(response, options) {
419 if (options.waitMsgTarget) {
420 options.waitMsgTarget.setLoading(false);
421 }
52406c42
DM
422 response.result = {};
423 try {
424 response.result = Ext.decode(response.responseText);
db16032c 425 } catch(e) {}
a2dca26b 426 var msg = gettext('Connection error') + ' - server offline?';
aff192e6 427 if (response.aborted) {
a2dca26b 428 msg = gettext('Connection error') + ' - aborted.';
aff192e6 429 } else if (response.timedout) {
a2dca26b 430 msg = gettext('Connection error') + ' - Timeout.';
aff192e6 431 } else if (response.status && response.statusText) {
a2dca26b 432 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
aff192e6
DM
433 }
434 response.htmlStatus = msg;
435 Ext.callback(callbackFn, options.scope, [options, false, response]);
436 Ext.callback(failureFn, options.scope, [response, options]);
437 }
438 });
439 };
440
441 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
442
443 var target = newopts.waitMsgTarget;
444 if (target) {
445 // Note: ExtJS bug - this does not work when component is not rendered
2c535170 446 target.setLoading(newopts.waitMsg);
aff192e6
DM
447 }
448 Ext.Ajax.request(newopts);
449 },
450
451 assemble_field_data: function(values, data) {
452 if (Ext.isObject(data)) {
453 Ext.Object.each(data, function(name, val) {
454 if (values.hasOwnProperty(name)) {
455 var bucket = values[name];
456 if (!Ext.isArray(bucket)) {
457 bucket = values[name] = [bucket];
458 }
459 if (Ext.isArray(val)) {
460 values[name] = bucket.concat(val);
461 } else {
462 bucket.push(val);
463 }
464 } else {
465 values[name] = val;
466 }
467 });
468 }
469 },
470
9baa9680
DM
471 checked_command: function(orig_cmd) {
472 PVE.Utils.API2Request({
473 url: '/nodes/localhost/subscription',
474 method: 'GET',
475 //waitMsgTarget: me,
476 failure: function(response, opts) {
0070ee37 477 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
9baa9680
DM
478 },
479 success: function(response, opts) {
480 var data = response.result.data;
481
482 if (data.status !== 'Active') {
483 Ext.Msg.show({
0070ee37 484 title: gettext('No valid subscription'),
9baa9680
DM
485 icon: Ext.Msg.WARNING,
486 msg: PVE.Utils.noSubKeyHtml,
487 buttons: Ext.Msg.OK,
488 callback: function(btn) {
489 if (btn !== 'ok') {
490 return;
491 }
492 orig_cmd();
493 }
494 });
495 } else {
496 orig_cmd();
497 }
498 }
499 });
500 },
501
acc0c13a 502 task_desc_table: {
c25f9d30 503 vncproxy: [ 'VM/CT', gettext('Console') ],
4af2c66c 504 spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ],
2ed04b3c 505 vncshell: [ '', gettext('Shell') ],
93d4a58a
DM
506 qmsnapshot: [ 'VM', gettext('Snapshot') ],
507 qmrollback: [ 'VM', gettext('Rollback') ],
508 qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ],
c25f9d30
DM
509 qmcreate: [ 'VM', gettext('Create') ],
510 qmrestore: [ 'VM', gettext('Restore') ],
511 qmdestroy: [ 'VM', gettext('Destroy') ],
512 qmigrate: [ 'VM', gettext('Migrate') ],
f16369ee 513 qmclone: [ 'VM', gettext('Clone') ],
772a8ffb 514 qmmove: [ 'VM', gettext('Move disk') ],
dd271a32 515 qmtemplate: [ 'VM', gettext('Convert to template') ],
c25f9d30
DM
516 qmstart: [ 'VM', gettext('Start') ],
517 qmstop: [ 'VM', gettext('Stop') ],
518 qmreset: [ 'VM', gettext('Reset') ],
519 qmshutdown: [ 'VM', gettext('Shutdown') ],
520 qmsuspend: [ 'VM', gettext('Suspend') ],
521 qmresume: [ 'VM', gettext('Resume') ],
9fb30485 522 qmconfig: [ 'VM', gettext('Configure') ],
c25f9d30
DM
523 vzcreate: ['CT', gettext('Create') ],
524 vzrestore: ['CT', gettext('Restore') ],
525 vzdestroy: ['CT', gettext('Destroy') ],
526 vzmigrate: [ 'CT', gettext('Migrate') ],
527 vzstart: ['CT', gettext('Start') ],
528 vzstop: ['CT', gettext('Stop') ],
8710f280
DM
529 vzmount: ['CT', gettext('Mount') ],
530 vzumount: ['CT', gettext('Unmount') ],
51ed1415 531 vzshutdown: ['CT', gettext('Shutdown') ],
3f76170f
DM
532 hamigrate: [ 'HA', gettext('Migrate') ],
533 hastart: [ 'HA', gettext('Start') ],
534 hastop: [ 'HA', gettext('Stop') ],
c25f9d30
DM
535 srvstart: ['SRV', gettext('Start') ],
536 srvstop: ['SRV', gettext('Stop') ],
537 srvrestart: ['SRV', gettext('Restart') ],
538 srvreload: ['SRV', gettext('Reload') ],
c9164975 539 imgcopy: ['', gettext('Copy data') ],
4a3a5520 540 imgdel: ['', gettext('Erase data') ],
c9164975 541 download: ['', gettext('Download') ],
640f8f14 542 vzdump: ['', gettext('Backup') ],
b9065f4d 543 aptupdate: ['', gettext('Update package database') ],
640f8f14
DM
544 startall: [ '', gettext('Start all VMs and Containers') ],
545 stopall: [ '', gettext('Stop all VMs and Containers') ]
acc0c13a 546 },
d1233856 547
acc0c13a 548 format_task_description: function(type, id) {
2ed04b3c
DM
549 var farray = PVE.Utils.task_desc_table[type];
550 if (!farray) {
551 return type;
d1233856 552 }
2ed04b3c 553 var prefix = farray[0];
c25f9d30 554 var text = farray[1];
2ed04b3c 555 if (prefix) {
c25f9d30 556 return prefix + ' ' + id + ' - ' + text;
2ed04b3c 557 }
81e94b6d 558 return text;
aff192e6
DM
559 },
560
aff192e6
DM
561 parse_task_upid: function(upid) {
562 var task = {};
563
55d19d9b 564 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
565 if (!res) {
566 throw "unable to parse upid '" + upid + "'";
567 }
568 task.node = res[1];
569 task.pid = parseInt(res[2], 16);
570 task.pstart = parseInt(res[3], 16);
571 task.starttime = parseInt(res[4], 16);
572 task.type = res[5];
573 task.id = res[6];
574 task.user = res[7];
575
576 task.desc = PVE.Utils.format_task_description(task.type, task.id);
577
578 return task;
579 },
580
581 format_size: function(size) {
a2f57991 582 /*jslint confusion: true */
aff192e6 583
a1d849df
DM
584 if (size < 1024) {
585 return size;
586 }
587
aff192e6
DM
588 var kb = size / 1024;
589
590 if (kb < 1024) {
591 return kb.toFixed(0) + "KB";
592 }
593
594 var mb = size / (1024*1024);
595
596 if (mb < 1024) {
597 return mb.toFixed(0) + "MB";
598 }
599
600 var gb = mb / 1024;
601
602 if (gb < 1024) {
603 return gb.toFixed(2) + "GB";
604 }
605
606 var tb = gb / 1024;
607
608 return tb.toFixed(2) + "TB";
609
610 },
611
612 format_html_bar: function(per, text) {
613
614 return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" +
615 "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" +
616 "</div></div>";
617
618 },
619
620 format_cpu_bar: function(per1, per2, text) {
621
622 return "<div class='pve-bar-border'>" +
623 "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" +
624 "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" +
625 "<div class='pve-bar-text'>" + text + "</div>" +
626 "</div>";
627 },
628
629 format_large_bar: function(per, text) {
630
631 if (!text) {
632 text = per.toFixed(1) + "%";
633 }
634
635 return "<div class='pve-largebar-border'>" +
636 "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" +
637 "<div class='pve-largebar-text'>" + text + "</div>" +
638 "</div>";
639 },
640
641 format_duration_long: function(ut) {
642
643 var days = Math.floor(ut / 86400);
644 ut -= days*86400;
645 var hours = Math.floor(ut / 3600);
646 ut -= hours*3600;
647 var mins = Math.floor(ut / 60);
648 ut -= mins*60;
649
650 var hours_str = '00' + hours.toString();
651 hours_str = hours_str.substr(hours_str.length - 2);
652 var mins_str = "00" + mins.toString();
653 mins_str = mins_str.substr(mins_str.length - 2);
654 var ut_str = "00" + ut.toString();
655 ut_str = ut_str.substr(ut_str.length - 2);
656
657 if (days) {
7e6b14a8 658 var ds = days > 1 ? PVE.Utils.daysText : PVE.Utils.dayText;
aff192e6
DM
659 return days.toString() + ' ' + ds + ' ' +
660 hours_str + ':' + mins_str + ':' + ut_str;
661 } else {
662 return hours_str + ':' + mins_str + ':' + ut_str;
663 }
664 },
665
666 format_duration_short: function(ut) {
667
668 if (ut < 60) {
669 return ut.toString() + 's';
670 }
671
672 if (ut < 3600) {
673 var mins = ut / 60;
674 return mins.toFixed(0) + 'm';
675 }
676
677 if (ut < 86400) {
678 var hours = ut / 3600;
679 return hours.toFixed(0) + 'h';
680 }
681
682 var days = ut / 86400;
683 return days.toFixed(0) + 'd';
684 },
685
7e6b14a8
DM
686 yesText: gettext('Yes'),
687 noText: gettext('No'),
32314691 688 errorText: gettext('Error'),
7e6b14a8
DM
689 unknownText: gettext('Unknown'),
690 defaultText: gettext('Default'),
691 daysText: gettext('days'),
692 dayText: gettext('day'),
32314691
DM
693 runningText: gettext('running'),
694 stoppedText: gettext('stopped'),
b62861ce 695 neverText: gettext('never'),
bc3e73be
DM
696 totalText: gettext('Total'),
697 usedText: gettext('Used'),
b62861ce
DM
698
699 format_expire: function(date) {
700 if (!date) {
701 return PVE.Utils.neverText;
702 }
703 return Ext.Date.format(date, "Y-m-d");
704 },
7e6b14a8 705
aff192e6
DM
706 format_storage_type: function(value) {
707 if (value === 'dir') {
708 return 'Directory';
709 } else if (value === 'nfs') {
710 return 'NFS';
001b7f76
DM
711 } else if (value === 'glusterfs') {
712 return 'GlusterFS';
aff192e6
DM
713 } else if (value === 'lvm') {
714 return 'LVM';
715 } else if (value === 'iscsi') {
716 return 'iSCSI';
56fd4763
AD
717 } else if (value === 'rbd') {
718 return 'RBD';
719 } else if (value === 'sheepdog') {
720 return 'Sheepdog';
7995b6eb
MR
721 } else if (value === 'zfs') {
722 return 'ZFS';
56fd4763
AD
723 } else if (value === 'iscsidirect') {
724 return 'iSCSIDirect';
aff192e6 725 } else {
7e6b14a8 726 return PVE.Utils.unknownText;
aff192e6
DM
727 }
728 },
729
730 format_boolean_with_default: function(value) {
731 if (Ext.isDefined(value) && value !== '') {
a2dca26b 732 return value ? PVE.Utils.yesText : PVE.Utils.noText;
aff192e6 733 }
a2dca26b 734 return PVE.Utils.defaultText;
aff192e6
DM
735 },
736
737 format_boolean: function(value) {
a2dca26b 738 return value ? PVE.Utils.yesText : PVE.Utils.noText;
aff192e6
DM
739 },
740
741 format_neg_boolean: function(value) {
a2dca26b 742 return !value ? PVE.Utils.yesText : PVE.Utils.noText;
aff192e6
DM
743 },
744
745 format_content_types: function(value) {
746 var cta = [];
747
f29297db 748 Ext.each(value.split(',').sort(), function(ct) {
aff192e6
DM
749 if (ct === 'images') {
750 cta.push('Images');
751 } else if (ct === 'backup') {
752 cta.push('Backups');
753 } else if (ct === 'vztmpl') {
754 cta.push('Templates');
755 } else if (ct === 'iso') {
756 cta.push('ISO');
9f767883
DM
757 } else if (ct === 'rootdir') {
758 cta.push('Containers');
aff192e6
DM
759 }
760 });
761
762 return cta.join(', ');
763 },
764
765 render_storage_content: function(value, metaData, record) {
766 var data = record.data;
767 if (Ext.isNumber(data.channel) &&
768 Ext.isNumber(data.id) &&
769 Ext.isNumber(data.lun)) {
770 return "CH " +
771 Ext.String.leftPad(data.channel,2, '0') +
772 " ID " + data.id + " LUN " + data.lun;
773 }
774 return data.volid.replace(/^.*:(.*\/)?/,'');
775 },
776
777 render_serverity: function (value) {
778 return PVE.Utils.log_severity_hash[value] || value;
779 },
780
781 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
782
105270d3 783 if (!(record.data.uptime && Ext.isNumeric(value))) {
aff192e6
DM
784 return '';
785 }
786
105270d3
DM
787 var maxcpu = record.data.maxcpu || 1;
788
789 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
aff192e6
DM
790 return '';
791 }
105270d3
DM
792
793 var per = value * 100;
aff192e6
DM
794
795 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
796 },
797
798 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
a2f57991 799 /*jslint confusion: true */
aff192e6
DM
800
801 if (!Ext.isNumeric(value)) {
802 return '';
803 }
804
805 return PVE.Utils.format_size(value);
806 },
807
808 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
809 var servertime = new Date(value * 1000);
810 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
811 },
812
813 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
814
815 var mem = value;
816 var maxmem = record.data.maxmem;
817
818 if (!record.data.uptime) {
819 return '';
820 }
821
822 if (!(Ext.isNumeric(mem) && maxmem)) {
823 return '';
824 }
825
826 var per = (mem * 100) / maxmem;
827
828 return per.toFixed(1) + '%';
829 },
830
831 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
832
833 var disk = value;
834 var maxdisk = record.data.maxdisk;
835
836 if (!(Ext.isNumeric(disk) && maxdisk)) {
837 return '';
838 }
839
840 var per = (disk * 100) / maxdisk;
841
842 return per.toFixed(1) + '%';
843 },
844
845 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
846
847 var cls = 'pve-itype-icon-' + value;
848
849 if (record.data.running) {
850 metaData.tdCls = cls + "-running";
5f28da0a
DM
851 } else if (record.data.template) {
852 metaData.tdCls = cls + "-template";
aff192e6
DM
853 } else {
854 metaData.tdCls = cls;
855 }
856
857 return value;
858 },
859
860 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
861
862 var uptime = value;
863
864 if (uptime === undefined) {
865 return '';
866 }
867
868 if (uptime <= 0) {
869 return '-';
870 }
871
872 return PVE.Utils.format_duration_long(uptime);
873 },
874
16b69b6c
DM
875 render_support_level: function(value, metaData, record) {
876 return PVE.Utils.support_level_hash[value] || '-';
877 },
878
aff192e6
DM
879 render_upid: function(value, metaData, record) {
880 var type = record.data.type;
881 var id = record.data.id;
882
883 return PVE.Utils.format_task_description(type, id);
176eee4f
DM
884 },
885
58eb5905
DM
886 dialog_title: function(subject, create, isAdd) {
887 if (create) {
888 if (isAdd) {
889 return gettext('Add') + ': ' + subject;
890 } else {
891 return gettext('Create') + ': ' + subject;
892 }
893 } else {
894 return gettext('Edit') + ': ' + subject;
895 }
896 },
897
fb3feb55 898 openConoleWindow: function(vmtype, vmid, nodename, vmname) {
176eee4f 899 var url = Ext.urlEncode({
fb3feb55 900 console: vmtype, // kvm, openvz or shell
176eee4f 901 vmid: vmid,
fb3feb55 902 vmname: vmname,
176eee4f
DM
903 node: nodename
904 });
905 var nw = window.open("?" + url, '_blank',
906 "innerWidth=745,innerheight=427");
907 nw.focus();
5f663f5f
DM
908 },
909
910 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
911 // use el.mask() instead
912 setErrorMask: function(comp, msg) {
913 var el = comp.el;
914 if (!el) {
915 return;
916 }
917 if (!msg) {
918 el.unmask();
919 } else {
920 if (msg === true) {
921 el.mask(gettext("Loading..."));
922 } else {
923 el.mask(msg);
924 }
925 }
926 },
927
928 monStoreErrors: function(me, store) {
929 me.mon(store, 'beforeload', function(s, operation, eOpts) {
930 if (!me.loadCount) {
931 me.loadCount = 0; // make sure it is numeric
932 PVE.Utils.setErrorMask(me, true);
933 }
934 });
935
936 // only works with 'pve' proxy
937 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
938 me.loadCount++;
939
940 if (success) {
941 PVE.Utils.setErrorMask(me, false);
942 return;
943 }
944
945 var msg;
946 var operation = request.operation;
947 var error = operation.getError();
948 if (error.statusText) {
949 msg = error.statusText + ' (' + error.status + ')';
950 } else {
951 msg = gettext('Connection error');
952 }
953 PVE.Utils.setErrorMask(me, msg);
954 });
aff192e6 955 }
176eee4f 956
aff192e6
DM
957}});
958