2 Ext
.ns('Proxmox.Setup');
4 if (!Ext
.isDefined(Proxmox
.Setup
.auth_cookie_name
)) {
5 throw "Proxmox library not initialized";
8 // avoid errors when running without development tools
9 if (!Ext
.isDefined(Ext
.global
.console
)) {
21 Ext
.global
.console
= console
;
24 Ext
.Ajax
.defaultHeaders
= {
25 'Accept': 'application/json',
28 Ext
.Ajax
.on('beforerequest', function(conn
, options
) {
29 if (Proxmox
.CSRFPreventionToken
) {
30 if (!options
.headers
) {
33 options
.headers
.CSRFPreventionToken
= Proxmox
.CSRFPreventionToken
;
35 let storedAuth
= Proxmox
.Utils
.getStoredAuth();
36 if (storedAuth
.token
) {
37 options
.headers
.Authorization
= storedAuth
.token
;
41 Ext
.define('Proxmox.Utils', { // a singleton
44 yesText
: gettext('Yes'),
45 noText
: gettext('No'),
46 enabledText
: gettext('Enabled'),
47 disabledText
: gettext('Disabled'),
48 noneText
: gettext('none'),
49 NoneText
: gettext('None'),
50 errorText
: gettext('Error'),
51 warningsText
: gettext('Warnings'),
52 unknownText
: gettext('Unknown'),
53 defaultText
: gettext('Default'),
54 daysText
: gettext('days'),
55 dayText
: gettext('day'),
56 runningText
: gettext('running'),
57 stoppedText
: gettext('stopped'),
58 neverText
: gettext('never'),
59 totalText
: gettext('Total'),
60 usedText
: gettext('Used'),
61 directoryText
: gettext('Directory'),
62 stateText
: gettext('State'),
63 groupText
: gettext('Group'),
65 language_map
: { //language map is sorted alphabetically by iso 639-1
66 ar
: `العربية - ${gettext("Arabic")}`,
67 ca
: `Català - ${gettext("Catalan")}`,
68 da
: `Dansk - ${gettext("Danish")}`,
69 de
: `Deutsch - ${gettext("German")}`,
70 en
: `English - ${gettext("English")}`,
71 es
: `Español - ${gettext("Spanish")}`,
72 eu
: `Euskera (Basque) - ${gettext("Euskera (Basque)")}`,
73 fa
: `فارسی - ${gettext("Persian (Farsi)")}`,
74 fr
: `Français - ${gettext("French")}`,
75 he
: `עברית - ${gettext("Hebrew")}`,
76 it
: `Italiano - ${gettext("Italian")}`,
77 ja
: `日本語 - ${gettext("Japanese")}`,
78 kr
: `한국어 - ${gettext("Korean")}`,
79 nb
: `Bokmål - ${gettext("Norwegian (Bokmal)")}`,
80 nl
: `Nederlands - ${gettext("Dutch")}`,
81 nn
: `Nynorsk - ${gettext("Norwegian (Nynorsk)")}`,
82 pl
: `Polski - ${gettext("Polish")}`,
83 pt_BR
: `Português Brasileiro - ${gettext("Portuguese (Brazil)")}`,
84 ru
: `Русский - ${gettext("Russian")}`,
85 sl
: `Slovenščina - ${gettext("Slovenian")}`,
86 sv
: `Svenska - ${gettext("Swedish")}`,
87 tr
: `Türkçe - ${gettext("Turkish")}`,
88 zh_CN
: `中文(简体)- ${gettext("Chinese (Simplified)")}`,
89 zh_TW
: `中文(繁體)- ${gettext("Chinese (Traditional)")}`,
92 render_language: function(value
) {
93 if (!value
|| value
=== '__default__') {
94 return Proxmox
.Utils
.defaultText
+ ' (English)';
96 let text
= Proxmox
.Utils
.language_map
[value
];
98 return text
+ ' (' + value
+ ')';
103 renderEnabledIcon
: enabled
=> `<i class="fa fa-${enabled ? 'check' : 'minus'}"></i>`,
105 language_array: function() {
106 let data
= [['__default__', Proxmox
.Utils
.render_language('')]];
107 Ext
.Object
.each(Proxmox
.Utils
.language_map
, function(key
, value
) {
108 data
.push([key
, Proxmox
.Utils
.render_language(value
)]);
115 crisp
: 'Light theme',
116 "proxmox-dark": 'Proxmox Dark',
119 render_theme: function(value
) {
120 if (!value
|| value
=== '__default__') {
121 return Proxmox
.Utils
.defaultText
+ ' (auto)';
123 let text
= Proxmox
.Utils
.theme_map
[value
];
130 theme_array: function() {
131 let data
= [['__default__', Proxmox
.Utils
.render_theme('')]];
132 Ext
.Object
.each(Proxmox
.Utils
.theme_map
, function(key
, value
) {
133 data
.push([key
, Proxmox
.Utils
.render_theme(value
)]);
139 bond_mode_gettext_map
: {
140 '802.3ad': 'LACP (802.3ad)',
141 'lacp-balance-slb': 'LACP (balance-slb)',
142 'lacp-balance-tcp': 'LACP (balance-tcp)',
145 render_bond_mode
: value
=> Proxmox
.Utils
.bond_mode_gettext_map
[value
] || value
|| '',
147 bond_mode_array: function(modes
) {
148 return modes
.map(mode
=> [mode
, Proxmox
.Utils
.render_bond_mode(mode
)]);
151 getNoSubKeyHtml: function(url
) {
152 // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
153 return Ext
.String
.format('You do not have a valid subscription for this server. Please visit <a target="_blank" href="{0}">www.proxmox.com</a> to get a list of available options.', url
|| 'https://www.proxmox.com');
156 format_boolean_with_default: function(value
) {
157 if (Ext
.isDefined(value
) && value
!== '__default__') {
158 return value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
160 return Proxmox
.Utils
.defaultText
;
163 format_boolean: function(value
) {
164 return value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
167 format_neg_boolean: function(value
) {
168 return !value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
171 format_enabled_toggle: function(value
) {
172 return value
? Proxmox
.Utils
.enabledText
: Proxmox
.Utils
.disabledText
;
175 format_expire: function(date
) {
177 return Proxmox
.Utils
.neverText
;
179 return Ext
.Date
.format(date
, "Y-m-d");
182 // somewhat like a human would tell durations, omit zero values and do not
183 // give seconds precision if we talk days already
184 format_duration_human: function(ut
) {
185 let seconds
= 0, minutes
= 0, hours
= 0, days
= 0, years
= 0;
192 seconds
= Number((remaining
% 60).toFixed(1));
193 remaining
= Math
.trunc(remaining
/ 60);
195 minutes
= remaining
% 60;
196 remaining
= Math
.trunc(remaining
/ 60);
198 hours
= remaining
% 24;
199 remaining
= Math
.trunc(remaining
/ 24);
201 days
= remaining
% 365;
202 remaining
= Math
.trunc(remaining
/ 365); // yea, just lets ignore leap years...
211 let add
= (t
, unit
) => {
212 if (t
> 0) res
.push(t
+ unit
);
216 let addMinutes
= !add(years
, 'y');
217 let addSeconds
= !add(days
, 'd');
225 return res
.join(' ');
228 format_duration_long: function(ut
) {
229 let days
= Math
.floor(ut
/ 86400);
231 let hours
= Math
.floor(ut
/ 3600);
233 let mins
= Math
.floor(ut
/ 60);
236 let hours_str
= '00' + hours
.toString();
237 hours_str
= hours_str
.substr(hours_str
.length
- 2);
238 let mins_str
= "00" + mins
.toString();
239 mins_str
= mins_str
.substr(mins_str
.length
- 2);
240 let ut_str
= "00" + ut
.toString();
241 ut_str
= ut_str
.substr(ut_str
.length
- 2);
244 let ds
= days
> 1 ? Proxmox
.Utils
.daysText
: Proxmox
.Utils
.dayText
;
245 return days
.toString() + ' ' + ds
+ ' ' +
246 hours_str
+ ':' + mins_str
+ ':' + ut_str
;
248 return hours_str
+ ':' + mins_str
+ ':' + ut_str
;
252 format_subscription_level: function(level
) {
255 } else if (level
=== 'b') {
257 } else if (level
=== 's') {
259 } else if (level
=== 'p') {
262 return Proxmox
.Utils
.noneText
;
266 compute_min_label_width: function(text
, width
) {
267 if (width
=== undefined) { width
= 100; }
269 let tm
= new Ext
.util
.TextMetrics();
270 let min
= tm
.getWidth(text
+ ':');
272 return min
< width
? width
: min
;
275 // returns username + realm
276 parse_userid: function(userid
) {
277 if (!Ext
.isString(userid
)) {
278 return [undefined, undefined];
281 let match
= userid
.match(/^(.+)@([^@]+)$/);
282 if (match
!== null) {
283 return [match
[1], match
[2]];
286 return [undefined, undefined];
289 render_username: function(userid
) {
290 let username
= Proxmox
.Utils
.parse_userid(userid
)[0] || "";
291 return Ext
.htmlEncode(username
);
294 render_realm: function(userid
) {
295 let username
= Proxmox
.Utils
.parse_userid(userid
)[1] || "";
296 return Ext
.htmlEncode(username
);
299 getStoredAuth: function() {
300 let storedAuth
= JSON
.parse(window
.localStorage
.getItem('ProxmoxUser'));
301 return storedAuth
|| {};
304 setAuthData: function(data
) {
305 Proxmox
.UserName
= data
.username
;
306 Proxmox
.LoggedOut
= data
.LoggedOut
;
307 // creates a session cookie (expire = null)
308 // that way the cookie gets deleted after the browser window is closed
310 Proxmox
.CSRFPreventionToken
= data
.CSRFPreventionToken
;
311 Ext
.util
.Cookies
.set(Proxmox
.Setup
.auth_cookie_name
, data
.ticket
, null, '/', null, true);
315 window
.localStorage
.setItem('ProxmoxUser', JSON
.stringify(data
));
320 if (Proxmox
.LoggedOut
) {
323 let storedAuth
= Proxmox
.Utils
.getStoredAuth();
324 let cookie
= Ext
.util
.Cookies
.get(Proxmox
.Setup
.auth_cookie_name
);
325 if ((Proxmox
.UserName
!== '' && cookie
&& !cookie
.startsWith("PVE:tfa!")) || storedAuth
.token
) {
326 return cookie
|| storedAuth
.token
;
332 authClear: function() {
333 if (Proxmox
.LoggedOut
) {
336 // ExtJS clear is basically the same, but browser may complain if any cookie isn't "secure"
337 Ext
.util
.Cookies
.set(Proxmox
.Setup
.auth_cookie_name
, "", new Date(0), null, null, true);
338 window
.localStorage
.removeItem("ProxmoxUser");
341 // The End-User gets redirected back here after login on the OpenID auth. portal, and in the
342 // redirection URL the state and auth.code are passed as URL GET params, this helper parses those
343 getOpenIDRedirectionAuthorization: function() {
344 const auth
= Ext
.Object
.fromQueryString(window
.location
.search
);
345 if (auth
.state
!== undefined && auth
.code
!== undefined) {
351 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
352 // use el.mask() instead
353 setErrorMask: function(comp
, msg
) {
360 } else if (msg
=== true) {
361 el
.mask(gettext("Loading..."));
367 getResponseErrorMessage
: (err
) => {
368 if (!err
.statusText
) {
369 return gettext('Connection error');
371 let msg
= [`${err.statusText} (${err.status})`];
372 if (err
.response
&& err
.response
.responseText
) {
373 let txt
= err
.response
.responseText
;
375 let res
= JSON
.parse(txt
);
376 if (res
.errors
&& typeof res
.errors
=== 'object') {
377 for (let [key
, value
] of Object
.entries(res
.errors
)) {
378 msg
.push(Ext
.String
.htmlEncode(`${key}: ${value}`));
382 // fallback to string
383 msg
.push(Ext
.String
.htmlEncode(txt
));
386 return msg
.join('<br>');
389 monStoreErrors: function(component
, store
, clearMaskBeforeLoad
, errorCallback
) {
390 if (clearMaskBeforeLoad
) {
391 component
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
392 Proxmox
.Utils
.setErrorMask(component
, false);
395 component
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
396 if (!component
.loadCount
) {
397 component
.loadCount
= 0; // make sure it is nucomponent.ic
398 Proxmox
.Utils
.setErrorMask(component
, true);
403 // only works with 'proxmox' proxy
404 component
.mon(store
.proxy
, 'afterload', function(proxy
, request
, success
) {
405 component
.loadCount
++;
408 Proxmox
.Utils
.setErrorMask(component
, false);
412 let error
= request
._operation
.getError();
413 let msg
= Proxmox
.Utils
.getResponseErrorMessage(error
);
414 if (!errorCallback
|| !errorCallback(error
, msg
)) {
415 Proxmox
.Utils
.setErrorMask(component
, msg
);
420 extractRequestError: function(result
, verbose
) {
421 let msg
= gettext('Successful');
423 if (!result
.success
) {
424 msg
= gettext("Unknown error");
425 if (result
.message
) {
426 msg
= Ext
.htmlEncode(result
.message
);
428 msg
+= ` (${result.status})`;
431 if (verbose
&& Ext
.isObject(result
.errors
)) {
433 Ext
.Object
.each(result
.errors
, (prop
, desc
) => {
434 msg
+= `<br><b>${Ext.htmlEncode(prop)}</b>: ${Ext.htmlEncode(desc)}`;
443 API2Request: function(reqOpts
) {
444 let newopts
= Ext
.apply({
445 waitMsg
: gettext('Please wait...'),
448 // default to enable if user isn't handling the failure already explicitly
449 let autoErrorAlert
= reqOpts
.autoErrorAlert
??
450 (typeof reqOpts
.failure
!== 'function' && typeof reqOpts
.callback
!== 'function');
452 if (!newopts
.url
.match(/^\/api2/)) {
453 newopts
.url
= '/api2/extjs' + newopts
.url
;
455 delete newopts
.callback
;
457 let createWrapper = function(successFn
, callbackFn
, failureFn
) {
459 success: function(response
, options
) {
460 if (options
.waitMsgTarget
) {
461 if (Proxmox
.Utils
.toolkit
=== 'touch') {
462 options
.waitMsgTarget
.setMasked(false);
464 options
.waitMsgTarget
.setLoading(false);
467 let result
= Ext
.decode(response
.responseText
);
468 response
.result
= result
;
469 if (!result
.success
) {
470 response
.htmlStatus
= Proxmox
.Utils
.extractRequestError(result
, true);
471 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
472 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
473 if (autoErrorAlert
) {
474 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
);
478 Ext
.callback(callbackFn
, options
.scope
, [options
, true, response
]);
479 Ext
.callback(successFn
, options
.scope
, [response
, options
]);
481 failure: function(response
, options
) {
482 if (options
.waitMsgTarget
) {
483 if (Proxmox
.Utils
.toolkit
=== 'touch') {
484 options
.waitMsgTarget
.setMasked(false);
486 options
.waitMsgTarget
.setLoading(false);
489 response
.result
= {};
491 response
.result
= Ext
.decode(response
.responseText
);
495 let msg
= gettext('Connection error') + ' - server offline?';
496 if (response
.aborted
) {
497 msg
= gettext('Connection error') + ' - aborted.';
498 } else if (response
.timedout
) {
499 msg
= gettext('Connection error') + ' - Timeout.';
500 } else if (response
.status
&& response
.statusText
) {
501 msg
= gettext('Connection error') + ' ' + response
.status
+ ': ' + response
.statusText
;
503 response
.htmlStatus
= msg
;
504 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
505 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
510 createWrapper(reqOpts
.success
, reqOpts
.callback
, reqOpts
.failure
);
512 let target
= newopts
.waitMsgTarget
;
514 if (Proxmox
.Utils
.toolkit
=== 'touch') {
515 target
.setMasked({ xtype
: 'loadmask', message
: newopts
.waitMsg
});
517 // Note: ExtJS bug - this does not work when component is not rendered
518 target
.setLoading(newopts
.waitMsg
);
521 Ext
.Ajax
.request(newopts
);
524 // can be useful for catching displaying errors from the API, e.g.:
525 // Proxmox.Async.api2({
527 // }).catch(Proxmox.Utils.alertResponseFailure);
528 alertResponseFailure
: (response
) => {
531 response
.htmlStatus
|| response
.result
.message
,
535 checked_command: function(orig_cmd
) {
536 Proxmox
.Utils
.API2Request(
538 url
: '/nodes/localhost/subscription',
540 failure: function(response
, opts
) {
541 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
);
543 success: function(response
, opts
) {
544 let res
= response
.result
;
545 if (res
=== null || res
=== undefined || !res
|| res
546 .data
.status
.toLowerCase() !== 'active') {
548 title
: gettext('No valid subscription'),
549 icon
: Ext
.Msg
.WARNING
,
550 message
: Proxmox
.Utils
.getNoSubKeyHtml(res
.data
.url
),
552 callback: function(btn
) {
567 assemble_field_data: function(values
, data
) {
568 if (!Ext
.isObject(data
)) {
571 Ext
.Object
.each(data
, function(name
, val
) {
572 if (Object
.prototype.hasOwnProperty
.call(values
, name
)) {
573 let bucket
= values
[name
];
574 if (!Ext
.isArray(bucket
)) {
575 bucket
= values
[name
] = [bucket
];
577 if (Ext
.isArray(val
)) {
578 values
[name
] = bucket
.concat(val
);
588 updateColumnWidth: function(container
, thresholdWidth
) {
589 let mode
= Ext
.state
.Manager
.get('summarycolumns') || 'auto';
591 if (mode
!== 'auto') {
592 factor
= parseInt(mode
, 10);
593 if (Number
.isNaN(factor
)) {
597 thresholdWidth
= (thresholdWidth
|| 1400) + 1;
598 factor
= Math
.ceil(container
.getSize().width
/ thresholdWidth
);
601 if (container
.oldFactor
=== factor
) {
605 let items
= container
.query('>'); // direct children
606 factor
= Math
.min(factor
, items
.length
);
607 container
.oldFactor
= factor
;
609 items
.forEach((item
) => {
610 item
.columnWidth
= 1 / factor
;
613 // we have to update the layout twice, since the first layout change
614 // can trigger the scrollbar which reduces the amount of space left
615 container
.updateLayout();
616 container
.updateLayout();
619 // NOTE: depreacated, use updateColumnWidth
620 updateColumns
: container
=> Proxmox
.Utils
.updateColumnWidth(container
),
622 dialog_title: function(subject
, create
, isAdd
) {
625 return gettext('Add') + ': ' + subject
;
627 return gettext('Create') + ': ' + subject
;
630 return gettext('Edit') + ': ' + subject
;
634 network_iface_types
: {
635 eth
: gettext("Network Device"),
636 bridge
: 'Linux Bridge',
639 OVSBridge
: 'OVS Bridge',
642 OVSIntPort
: 'OVS IntPort',
645 render_network_iface_type: function(value
) {
646 return Proxmox
.Utils
.network_iface_types
[value
] ||
647 Proxmox
.Utils
.unknownText
;
650 // NOTE: only add general, product agnostic, ones here! Else use override helper in product repos
652 aptupdate
: ['', gettext('Update package database')],
653 diskinit
: ['Disk', gettext('Initialize Disk with GPT')],
654 spiceshell
: ['', gettext('Shell') + ' (Spice)'],
655 srvreload
: ['SRV', gettext('Reload')],
656 srvrestart
: ['SRV', gettext('Restart')],
657 srvstart
: ['SRV', gettext('Start')],
658 srvstop
: ['SRV', gettext('Stop')],
659 termproxy
: ['', gettext('Console') + ' (xterm.js)'],
660 vncshell
: ['', gettext('Shell')],
663 // to add or change existing for product specific ones
664 override_task_descriptions: function(extra
) {
665 for (const [key
, value
] of Object
.entries(extra
)) {
666 Proxmox
.Utils
.task_desc_table
[key
] = value
;
670 format_task_description: function(type
, id
) {
671 let farray
= Proxmox
.Utils
.task_desc_table
[type
];
679 } else if (Ext
.isFunction(farray
)) {
680 return farray(type
, id
);
682 let prefix
= farray
[0];
684 if (prefix
&& id
!== undefined) {
685 return prefix
+ ' ' + id
+ ' - ' + text
;
690 format_size: function(size
, useSI
) {
691 let unitsSI
= [gettext('B'), gettext('KB'), gettext('MB'), gettext('GB'),
692 gettext('TB'), gettext('PB'), gettext('EB'), gettext('ZB'), gettext('YB')];
693 let unitsIEC
= [gettext('B'), gettext('KiB'), gettext('MiB'), gettext('GiB'),
694 gettext('TiB'), gettext('PiB'), gettext('EiB'), gettext('ZiB'), gettext('YiB')];
697 const baseValue
= useSI
? 1000 : 1024;
698 while (size
>= baseValue
&& order
< unitsSI
.length
) {
699 size
= size
/ baseValue
;
703 let unit
= useSI
? unitsSI
[order
] : unitsIEC
[order
];
707 return `${size.toFixed(commaDigits)} ${unit}`;
715 'GiB': 1024*1024*1024,
716 'TiB': 1024*1024*1024*1024,
717 'PiB': 1024*1024*1024*1024*1024,
721 'GB': 1000*1000*1000,
722 'TB': 1000*1000*1000*1000,
723 'PB': 1000*1000*1000*1000*1000,
726 parse_size_unit: function(val
) {
727 //let m = val.match(/([.\d])+\s?([KMGTP]?)(i?)B?\s*$/i);
728 let m
= val
.match(/(\d+(?:\.\d+)?)\s?([KMGTP]?)(i?)B?\s*$/i);
729 let size
= parseFloat(m
[1]);
730 let scale
= m
[2].toUpperCase();
731 let binary
= m
[3].toLowerCase();
733 let unit
= `${scale}${binary}B`;
734 let factor
= Proxmox
.Utils
.SizeUnits
[unit
];
736 return { size
, factor
, unit
, binary
}; // for convenience return all we got
739 size_unit_to_bytes: function(val
) {
740 let { size
, factor
} = Proxmox
.Utils
.parse_size_unit(val
);
741 return size
* factor
;
744 autoscale_size_unit: function(val
) {
745 let { size
, factor
, binary
} = Proxmox
.Utils
.parse_size_unit(val
);
746 return Proxmox
.Utils
.format_size(size
* factor
, binary
!== "i");
749 size_unit_ratios: function(a
, b
) {
750 a
= typeof a
!== "undefined" ? a
: 0;
751 b
= typeof b
!== "undefined" ? b
: Infinity
;
752 let aBytes
= typeof a
=== "number" ? a
: Proxmox
.Utils
.size_unit_to_bytes(a
);
753 let bBytes
= typeof b
=== "number" ? b
: Proxmox
.Utils
.size_unit_to_bytes(b
);
754 return aBytes
/ (bBytes
|| Infinity
); // avoid division by zero
757 render_upid: function(value
, metaData
, record
) {
758 let task
= record
.data
;
759 let type
= task
.type
|| task
.worker_type
;
760 let id
= task
.id
|| task
.worker_id
;
762 return Proxmox
.Utils
.format_task_description(type
, id
);
765 render_uptime: function(value
) {
768 if (uptime
=== undefined) {
776 return Proxmox
.Utils
.format_duration_long(uptime
);
779 systemd_unescape: function(string_value
) {
780 const charcode_0
= '0'.charCodeAt(0);
781 const charcode_9
= '9'.charCodeAt(0);
782 const charcode_A
= 'A'.charCodeAt(0);
783 const charcode_F
= 'F'.charCodeAt(0);
784 const charcode_a
= 'a'.charCodeAt(0);
785 const charcode_f
= 'f'.charCodeAt(0);
786 const charcode_x
= 'x'.charCodeAt(0);
787 const charcode_minus
= '-'.charCodeAt(0);
788 const charcode_slash
= '/'.charCodeAt(0);
789 const charcode_backslash
= '\\'.charCodeAt(0);
791 let parse_hex_digit = function(d
) {
792 if (d
>= charcode_0
&& d
<= charcode_9
) {
793 return d
- charcode_0
;
795 if (d
>= charcode_A
&& d
<= charcode_F
) {
796 return d
- charcode_A
+ 10;
798 if (d
>= charcode_a
&& d
<= charcode_f
) {
799 return d
- charcode_a
+ 10;
801 throw "got invalid hex digit";
804 let value
= new TextEncoder().encode(string_value
);
805 let result
= new Uint8Array(value
.length
);
810 while (i
< value
.length
) {
812 if (c0
=== charcode_minus
) {
813 result
.set([charcode_slash
], result_len
);
818 if ((i
+ 4) < value
.length
) {
820 if (c0
=== charcode_backslash
&& c1
=== charcode_x
) {
821 let h1
= parse_hex_digit(value
[i
+2]);
822 let h0
= parse_hex_digit(value
[i
+3]);
824 result
.set([ord
], result_len
);
830 result
.set([c0
], result_len
);
835 return new TextDecoder().decode(result
.slice(0, result
.len
));
838 parse_task_upid: function(upid
) {
841 let res
= upid
.match(/^UPID:([^\s:]+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):(([0-9A-Fa-f]{8,16}):)?([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
843 throw "unable to parse upid '" + upid
+ "'";
846 task
.pid
= parseInt(res
[2], 16);
847 task
.pstart
= parseInt(res
[3], 16);
848 if (res
[5] !== undefined) {
849 task
.task_id
= parseInt(res
[5], 16);
851 task
.starttime
= parseInt(res
[6], 16);
853 task
.id
= Proxmox
.Utils
.systemd_unescape(res
[8]);
856 task
.desc
= Proxmox
.Utils
.format_task_description(task
.type
, task
.id
);
861 parse_task_status: function(status
) {
862 if (status
=== 'OK') {
866 if (status
=== 'unknown') {
870 let match
= status
.match(/^WARNINGS: (.*)$/);
878 format_task_status: function(status
) {
879 let parsed
= Proxmox
.Utils
.parse_task_status(status
);
881 case 'unknown': return Proxmox
.Utils
.unknownText
;
882 case 'error': return Proxmox
.Utils
.errorText
+ ': ' + status
;
883 case 'warning': return status
.replace('WARNINGS', Proxmox
.Utils
.warningsText
);
884 case 'ok': // fall-through
885 default: return status
;
889 render_duration: function(value
) {
890 if (value
=== undefined) {
893 return Proxmox
.Utils
.format_duration_human(value
);
896 render_timestamp: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
897 let servertime
= new Date(value
* 1000);
898 return Ext
.Date
.format(servertime
, 'Y-m-d H:i:s');
901 render_zfs_health: function(value
) {
902 if (typeof value
=== 'undefined') {
905 var iconCls
= 'question-circle';
909 iconCls
= 'check-circle good';
913 iconCls
= 'exclamation-circle warning';
918 iconCls
= 'times-circle critical';
923 return '<i class="fa fa-' + iconCls
+ '"></i> ' + value
;
926 get_help_info: function(section
) {
928 if (typeof proxmoxOnlineHelpInfo
!== 'undefined') {
929 helpMap
= proxmoxOnlineHelpInfo
; // eslint-disable-line no-undef
930 } else if (typeof pveOnlineHelpInfo
!== 'undefined') {
931 // be backward compatible with older pve-doc-generators
932 helpMap
= pveOnlineHelpInfo
; // eslint-disable-line no-undef
934 throw "no global OnlineHelpInfo map declared";
937 if (helpMap
[section
]) {
938 return helpMap
[section
];
940 // try to normalize - and _ separators, to support asciidoc and sphinx
941 // references at the same time.
942 let section_minus_normalized
= section
.replace(/_
/g
, '-');
943 if (helpMap
[section_minus_normalized
]) {
944 return helpMap
[section_minus_normalized
];
946 let section_underscore_normalized
= section
.replace(/-/g
, '_');
947 return helpMap
[section_underscore_normalized
];
950 get_help_link: function(section
) {
951 let info
= Proxmox
.Utils
.get_help_info(section
);
955 return window
.location
.origin
+ info
.link
;
958 openXtermJsViewer: function(vmtype
, vmid
, nodename
, vmname
, cmd
) {
959 let url
= Ext
.Object
.toQueryString({
960 console
: vmtype
, // kvm, lxc, upgrade or shell
968 let nw
= window
.open("?" + url
, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420');
974 render_optional_url: function(value
) {
975 if (value
&& value
.match(/^https?:\/\//) !== null) {
976 return '<a target="_blank" href="' + value
+ '">' + value
+ '</a>';
981 render_san: function(value
) {
983 if (Ext
.isArray(value
)) {
984 value
.forEach(function(val
) {
985 if (!Ext
.isNumber(val
)) {
989 return names
.join('<br>');
994 render_usage
: val
=> (val
* 100).toFixed(2) + '%',
996 render_cpu_usage: function(val
, max
) {
997 return Ext
.String
.format(
998 `${gettext('{0}% of {1}')} ${gettext('CPU(s)')}`,
999 (val
*100).toFixed(2),
1004 render_size_usage: function(val
, max
, useSI
) {
1006 return gettext('N/A');
1008 let fmt
= v
=> Proxmox
.Utils
.format_size(v
, useSI
);
1009 let ratio
= (val
* 100 / max
).toFixed(2);
1010 return ratio
+ '% (' + Ext
.String
.format(gettext('{0} of {1}'), fmt(val
), fmt(max
)) + ')';
1013 render_cpu: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
1014 if (!(record
.data
.uptime
&& Ext
.isNumeric(value
))) {
1018 let maxcpu
= record
.data
.maxcpu
|| 1;
1019 if (!Ext
.isNumeric(maxcpu
) || maxcpu
< 1) {
1022 let cpuText
= maxcpu
> 1 ? 'CPUs' : 'CPU';
1023 let ratio
= (value
* 100).toFixed(1);
1024 return `${ratio}% of ${maxcpu.toString()} ${cpuText}`;
1027 render_size: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
1028 if (!Ext
.isNumeric(value
)) {
1031 return Proxmox
.Utils
.format_size(value
);
1034 render_cpu_model: function(cpu
) {
1035 let socketText
= cpu
.sockets
> 1 ? gettext('Sockets') : gettext('Socket');
1036 return `${cpu.cpus} x ${cpu.model} (${cpu.sockets.toString()} ${socketText})`;
1039 /* this is different for nodes */
1040 render_node_cpu_usage: function(value
, record
) {
1041 return Proxmox
.Utils
.render_cpu_usage(value
, record
.cpus
);
1044 render_node_size_usage: function(record
) {
1045 return Proxmox
.Utils
.render_size_usage(record
.used
, record
.total
);
1048 loadTextFromFile: function(file
, callback
, maxBytes
) {
1049 let maxSize
= maxBytes
|| 8192;
1050 if (file
.size
> maxSize
) {
1051 Ext
.Msg
.alert(gettext('Error'), gettext("Invalid file size: ") + file
.size
);
1054 let reader
= new FileReader();
1055 reader
.onload
= evt
=> callback(evt
.target
.result
);
1056 reader
.readAsText(file
);
1059 parsePropertyString: function(value
, defaultKey
) {
1063 if (typeof value
!== 'string' || value
=== '') {
1067 Ext
.Array
.each(value
.split(','), function(p
) {
1068 var kv
= p
.split('=', 2);
1069 if (Ext
.isDefined(kv
[1])) {
1071 } else if (Ext
.isDefined(defaultKey
)) {
1072 if (Ext
.isDefined(res
[defaultKey
])) {
1073 error
= 'defaultKey may be only defined once in propertyString';
1074 return false; // break
1076 res
[defaultKey
] = kv
[0];
1078 error
= 'invalid propertyString, not a key=value pair and no defaultKey defined';
1079 return false; // break
1084 if (error
!== undefined) {
1085 console
.error(error
);
1092 printPropertyString: function(data
, defaultKey
) {
1093 var stringparts
= [],
1094 gotDefaultKeyVal
= false,
1097 Ext
.Object
.each(data
, function(key
, value
) {
1098 if (defaultKey
!== undefined && key
=== defaultKey
) {
1099 gotDefaultKeyVal
= true;
1100 defaultKeyVal
= value
;
1101 } else if (Ext
.isArray(value
)) {
1102 stringparts
.push(key
+ '=' + value
.join(';'));
1103 } else if (value
!== '') {
1104 stringparts
.push(key
+ '=' + value
);
1108 stringparts
= stringparts
.sort();
1109 if (gotDefaultKeyVal
) {
1110 stringparts
.unshift(defaultKeyVal
);
1113 return stringparts
.join(',');
1116 acmedomain_count
: 5,
1118 parseACMEPluginData: function(data
) {
1121 data
.split('\n').forEach((line
) => {
1122 // capture everything after the first = as value
1123 let [key
, value
] = line
.split('=');
1124 if (value
!== undefined) {
1127 extradata
.push(line
);
1130 return [res
, extradata
];
1133 delete_if_default: function(values
, fieldname
, default_val
, create
) {
1134 if (values
[fieldname
] === '' || values
[fieldname
] === default_val
) {
1136 if (values
.delete) {
1137 if (Ext
.isArray(values
.delete)) {
1138 values
.delete.push(fieldname
);
1140 values
.delete += ',' + fieldname
;
1143 values
.delete = fieldname
;
1147 delete values
[fieldname
];
1151 printACME: function(value
) {
1152 if (Ext
.isArray(value
.domains
)) {
1153 value
.domains
= value
.domains
.join(';');
1155 return Proxmox
.Utils
.printPropertyString(value
);
1158 parseACME: function(value
) {
1166 Ext
.Array
.each(value
.split(','), function(p
) {
1167 var kv
= p
.split('=', 2);
1168 if (Ext
.isDefined(kv
[1])) {
1171 error
= 'Failed to parse key-value pair: '+p
;
1177 if (error
!== undefined) {
1178 console
.error(error
);
1182 if (res
.domains
!== undefined) {
1183 res
.domains
= res
.domains
.split(/;/);
1189 add_domain_to_acme: function(acme
, domain
) {
1190 if (acme
.domains
=== undefined) {
1191 acme
.domains
= [domain
];
1193 acme
.domains
.push(domain
);
1194 acme
.domains
= acme
.domains
.filter((value
, index
, self
) => self
.indexOf(value
) === index
);
1199 remove_domain_from_acme: function(acme
, domain
) {
1200 if (acme
.domains
!== undefined) {
1201 acme
.domains
= acme
.domains
.filter(
1202 (value
, index
, self
) => self
.indexOf(value
) === index
&& value
!== domain
,
1208 get_health_icon: function(state
, circle
) {
1209 if (circle
=== undefined) {
1213 if (state
=== undefined) {
1217 var icon
= 'faded fa-question';
1220 icon
= 'good fa-check';
1223 icon
= 'warning fa-upload';
1226 icon
= 'warning fa-refresh';
1229 icon
= 'warning fa-exclamation';
1232 icon
= 'critical fa-times';
1244 formatNodeRepoStatus: function(status
, product
) {
1245 let fmt
= (txt
, cls
) => `<i class="fa fa-fw fa-lg fa-${cls}"></i>${txt}`;
1247 let getUpdates
= Ext
.String
.format(gettext('{0} updates'), product
);
1248 let noRepo
= Ext
.String
.format(gettext('No {0} repository enabled!'), product
);
1250 if (status
=== 'ok') {
1251 return fmt(getUpdates
, 'check-circle good') + ' ' +
1252 fmt(gettext('Production-ready Enterprise repository enabled'), 'check-circle good');
1253 } else if (status
=== 'no-sub') {
1254 return fmt(gettext('Production-ready Enterprise repository enabled'), 'check-circle good') + ' ' +
1255 fmt(gettext('Enterprise repository needs valid subscription'), 'exclamation-circle warning');
1256 } else if (status
=== 'non-production') {
1257 return fmt(getUpdates
, 'check-circle good') + ' ' +
1258 fmt(gettext('Non production-ready repository enabled!'), 'exclamation-circle warning');
1259 } else if (status
=== 'no-repo') {
1260 return fmt(noRepo
, 'exclamation-circle critical');
1263 return Proxmox
.Utils
.unknownText
;
1266 render_u2f_error: function(error
) {
1268 '1': gettext('Other Error'),
1269 '2': gettext('Bad Request'),
1270 '3': gettext('Configuration Unsupported'),
1271 '4': gettext('Device Ineligible'),
1272 '5': gettext('Timeout'),
1274 return "U2F Error: " + ErrorNames
[error
] || Proxmox
.Utils
.unknownText
;
1277 // Convert an ArrayBuffer to a base64url encoded string.
1278 // A `null` value will be preserved for convenience.
1279 bytes_to_base64url: function(bytes
) {
1280 if (bytes
=== null) {
1285 .from(new Uint8Array(bytes
))
1286 .map(val
=> String
.fromCharCode(val
))
1289 .replace(/\+/g, '-')
1290 .replace(/\//g, '_')
1291 .replace(/[=]/g, '');
1294 // Convert an a base64url string to an ArrayBuffer.
1295 // A `null` value will be preserved for convenience.
1296 base64url_to_bytes: function(b64u
) {
1297 if (b64u
=== null) {
1301 return new Uint8Array(
1304 .replace(/_/g, '/'),
1307 .map(val => val.charCodeAt(0)),
1311 stringToRGB: function(string) {
1316 string += 'prox
'; // give short strings more variance
1317 for (let i = 0; i < string.length; i++) {
1318 hash = string.charCodeAt(i) + ((hash << 5) - hash);
1319 hash = hash & hash; // to int
1322 let alpha = 0.7; // make the color a bit brighter
1323 let bg = 255; // assume white background
1326 (hash & 255) * alpha + bg * (1 - alpha),
1327 ((hash >> 8) & 255) * alpha + bg * (1 - alpha),
1328 ((hash >> 16) & 255) * alpha + bg * (1 - alpha),
1332 rgbToCss: function(rgb) {
1333 return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
1336 rgbToHex: function(rgb) {
1337 let r = Math.round(rgb[0]).toString(16);
1338 let g = Math.round(rgb[1]).toString(16);
1339 let b = Math.round(rgb[2]).toString(16);
1340 return `${r}${g}${b}`;
1343 hexToRGB: function(hex) {
1347 if (hex.length === 7) {
1350 let r = parseInt(hex.slice(0, 2), 16);
1351 let g = parseInt(hex.slice(2, 4), 16);
1352 let b = parseInt(hex.slice(4, 6), 16);
1356 // optimized & simplified SAPC function
1357 // https://github.com/Myndex/SAPC-APCA
1358 getTextContrastClass: function(rgb) {
1359 const blkThrs = 0.022;
1360 const blkClmp = 1.414;
1362 // linearize & gamma correction
1363 let r = (rgb[0] / 255) ** 2.4;
1364 let g = (rgb[1] / 255) ** 2.4;
1365 let b = (rgb[2] / 255) ** 2.4;
1367 // relative luminance sRGB
1368 let bg = r * 0.2126729 + g * 0.7151522 + b * 0.0721750;
1371 bg = bg > blkThrs ? bg : bg + (blkThrs - bg) ** blkClmp;
1373 // SAPC with white text
1374 let contrastLight = bg ** 0.65 - 1;
1375 // SAPC with black text
1376 let contrastDark = bg ** 0.56 - 0.046134502;
1378 if (Math.abs(contrastLight) >= Math.abs(contrastDark)) {
1385 getTagElement: function(string, color_overrides) {
1386 let rgb = color_overrides?.[string] || Proxmox.Utils.stringToRGB(string);
1387 let style = `background-color: ${Proxmox.Utils.rgbToCss(rgb)};`;
1389 if (rgb.length > 3) {
1390 style += `color: ${Proxmox.Utils.rgbToCss([rgb[3], rgb[4], rgb[5]])}`;
1391 cls = "proxmox-tag-dark";
1393 let txtCls = Proxmox.Utils.getTextContrastClass(rgb);
1394 cls = `proxmox-tag-${txtCls}`;
1396 return `<span class="${cls}" style="${style}">${string}</span>`;
1399 // Setting filename here when downloading from a remote url sometimes fails in chromium browsers
1400 // because of a bug when using attribute download in conjunction with a self signed certificate.
1401 // For more info see https://bugs.chromium.org/p/chromium/issues/detail?id=993362
1402 downloadAsFile: function(source, fileName) {
1403 let hiddenElement = document.createElement('a
');
1404 hiddenElement.href = source;
1405 hiddenElement.target = '_blank
';
1407 hiddenElement.download = fileName;
1409 hiddenElement.click();
1414 constructor: function() {
1416 Ext.apply(me, me.utilities);
1418 let IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
1419 let IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
1420 let IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
1421 let IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
1422 let IPV4_CIDR_MASK = "([0-9]{1,2})";
1423 let IPV6_CIDR_MASK = "([0-9]{1,3})";
1426 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
1427 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")/" + IPV4_CIDR_MASK + "$");
1429 /* eslint-disable no-useless-concat,no-multi-spaces */
1430 let IPV6_REGEXP = "(?:" +
1431 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
1432 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
1433 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
1434 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
1435 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
1436 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
1437 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
1438 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
1439 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
1441 /* eslint-enable no-useless-concat,no-multi-spaces */
1443 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
1444 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")/" + IPV6_CIDR_MASK + "$");
1445 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
1447 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
1448 me.IP64_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + "/" + IPV6_CIDR_MASK + ")|(?:" + IPV4_REGEXP + "/" + IPV4_CIDR_MASK + ")$");
1450 let 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])?))";
1451 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
1452 me.DnsName_or_Wildcard_match = new RegExp("^(?:\\*\\.)?" + DnsName_REGEXP + "$");
1454 me.CpuSet_match = /^[0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*$/;
1456 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(?::(\\d+))?$");
1457 me.HostPortBrackets_match = new RegExp("^\\[(" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](?::(\\d+))?$");
1458 me.IP6_dotnotation_match = new RegExp("^(" + IPV6_REGEXP + ")(?:\\.(\\d+))?$");
1459 me.Vlan_match = /^vlan(\d+)/;
1460 me.VlanInterface_match = /(\w+)\.(\d+)/;
1464 Ext.define('Proxmox
.Async
', {
1467 // Returns a Promise resolving to the result of an `API2Request` or rejecting to the error
1468 // response on failure
1469 api2: function(reqOpts) {
1470 return new Promise((resolve, reject) => {
1471 delete reqOpts.callback; // not allowed in this api
1472 reqOpts.success = response => resolve(response);
1473 reqOpts.failure = response => reject(response);
1474 Proxmox.Utils.API2Request(reqOpts);
1478 // Delay for a number of milliseconds.
1479 sleep: function(millis) {
1480 return new Promise((resolve, _reject) => setTimeout(resolve, millis));
1484 Ext.override(Ext.data.Store, {
1485 // If the store's proxy is changed
while it is waiting
for an AJAX
1486 // response, `onProxyLoad` will still be called for the outdated response.
1487 // To avoid displaying inconsistent information, only process responses
1488 // belonging to the current proxy. However, do not apply this workaround
1489 // to the mobile UI, as Sencha Touch has an incompatible internal API.
1490 onProxyLoad: function(operation
) {
1492 if (Proxmox
.Utils
.toolkit
=== 'touch' || operation
.getProxy() === me
.getProxy()) {
1493 me
.callParent(arguments
);
1495 console
.log(`ignored outdated response: ${operation.getRequest().getUrl()}`);