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