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