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