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