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