]> git.proxmox.com Git - pve-manager.git/blobdiff - www/manager6/Utils.js
mobile: implement login with OTP based TFA enabled
[pve-manager.git] / www / manager6 / Utils.js
index 6da4c178be7416b5ad08e102d7f0eb14eda9b182..6a489e7ecd4bd9a98a86967316a5e50432e7f067 100644 (file)
@@ -19,6 +19,7 @@ Ext.Ajax.defaultHeaders = {
     'Accept': 'application/json'
 };
 
+/*jslint confusion: true */
 Ext.define('PVE.Utils', { utilities: {
 
     // this singleton contains miscellaneous utilities
@@ -45,15 +46,15 @@ Ext.define('PVE.Utils', { utilities: {
        'p': gettext('Premium')
     },
 
-    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.',
+    noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="https://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
 
     kvm_ostypes: {
        'Linux': [
-           { desc: '4.X/3.X/2.6 Kernel', val: 'l26' },
+           { desc: '5.x - 2.6 Kernel', val: 'l26' },
            { desc: '2.4 Kernel', val: 'l24' }
        ],
        'Microsoft Windows': [
-           { desc: '10/2016', val: 'win10' },
+           { desc: '10/2016/2019', val: 'win10' },
            { desc: '8.x/2012/2012r2', val: 'win8' },
            { desc: '7/2008r2', val: 'win7' },
            { desc: 'Vista/2008', val: 'w2k8' },
@@ -82,6 +83,12 @@ Ext.define('PVE.Utils', { utilities: {
            case 'good':
                icon = 'good fa-check';
                break;
+           case 'upgrade':
+               icon = 'warning fa-upload';
+               break;
+           case 'old':
+               icon = 'warning fa-refresh';
+               break;
            case 'warning':
                icon = 'warning fa-exclamation';
                break;
@@ -98,8 +105,60 @@ Ext.define('PVE.Utils', { utilities: {
        return icon;
     },
 
+    parse_ceph_version: function(service) {
+       if (service.ceph_version_short) {
+           return service.ceph_version_short;
+       }
+
+       if (service.ceph_version) {
+           var match = service.ceph_version.match(/version (\d+(\.\d+)*)/);
+           if (match) {
+               return match[1];
+           }
+       }
+
+       return undefined;
+    },
+
+    compare_ceph_versions: function(a, b) {
+       if (a === b) {
+           return 0;
+       }
+       let avers = a.toString().split('.');
+       let bvers = b.toString().split('.');
+
+       while (true) {
+           let av = avers.shift();
+           let bv = bvers.shift();
+
+           if (av === undefined && bv === undefined) {
+               return 0;
+           } else if (av === undefined)  {
+               return -1;
+           } else if (bv === undefined) {
+               return 1;
+           } else {
+               let diff = parseInt(av, 10) - parseInt(bv, 10);
+               if (diff != 0) return diff;
+               // else we need to look at the next parts
+           }
+       }
+
+    },
+
+    get_ceph_icon_html: function(health, fw) {
+       var state = PVE.Utils.map_ceph_health[health];
+       var cls = PVE.Utils.get_health_icon(state);
+       if (fw) {
+           cls += ' fa-fw';
+       }
+       return "<i class='fa " + cls + "'></i> ";
+    },
+
     map_ceph_health: {
        'HEALTH_OK':'good',
+       'HEALTH_UPGRADE':'upgrade',
+       'HEALTH_OLD':'old',
        'HEALTH_WARN':'warning',
        'HEALTH_ERR':'critical'
     },
@@ -122,6 +181,32 @@ Ext.define('PVE.Utils', { utilities: {
        return state;
     },
 
+    render_zfs_health: function(value) {
+       if (typeof value == 'undefined'){
+           return "";
+       }
+       var iconCls = 'question-circle';
+       switch (value) {
+           case 'AVAIL':
+           case 'ONLINE':
+               iconCls = 'check-circle good';
+               break;
+           case 'REMOVED':
+           case 'DEGRADED':
+               iconCls = 'exclamation-circle warning';
+               break;
+           case 'UNAVAIL':
+           case 'FAULTED':
+           case 'OFFLINE':
+               iconCls = 'times-circle critical';
+               break;
+           default: //unknown
+       }
+
+       return '<i class="fa fa-' + iconCls + '"></i> ' + value;
+
+    },
+
     get_kvm_osinfo: function(value) {
        var info = { base: 'Other' }; // default
        if (value) {
@@ -175,6 +260,36 @@ Ext.define('PVE.Utils', { utilities: {
        return fa.join(', ');
     },
 
+    render_qga_features: function(value) {
+       if (!value) {
+           return Proxmox.Utils.defaultText + ' (' + Proxmox.Utils.disabledText  + ')';
+       }
+       var props = PVE.Parser.parsePropertyString(value, 'enabled');
+       if (!PVE.Parser.parseBoolean(props.enabled)) {
+           return Proxmox.Utils.disabledText;
+       }
+
+       delete props.enabled;
+       var agentstring = Proxmox.Utils.enabledText;
+
+       Ext.Object.each(props, function(key, value) {
+           var keystring = '' ;
+           agentstring += ', ' + key + ': ';
+
+           if (PVE.Parser.parseBoolean(value)) {
+               agentstring += Proxmox.Utils.enabledText;
+           } else {
+               agentstring += Proxmox.Utils.disabledText;
+           }
+       });
+
+       return agentstring;
+    },
+
+    render_qemu_machine: function(value) {
+       return value || (Proxmox.Utils.defaultText + ' (i440fx)');
+    },
+
     render_qemu_bios: function(value) {
        if (!value) {
            return Proxmox.Utils.defaultText + ' (SeaBIOS)';
@@ -187,6 +302,18 @@ Ext.define('PVE.Utils', { utilities: {
        }
     },
 
+    render_dc_ha_opts: function(value) {
+       if (!value) {
+           return Proxmox.Utils.defaultText;
+       } else {
+           return PVE.Parser.printPropertyString(value);
+       }
+    },
+    render_as_property_string: function(value) {
+       return (!value) ? Proxmox.Utils.defaultText
+           : PVE.Parser.printPropertyString(value);
+    },
+
     render_scsihw: function(value) {
        if (!value) {
            return Proxmox.Utils.defaultText + ' (LSI 53C895A)';
@@ -255,7 +382,9 @@ Ext.define('PVE.Utils', { utilities: {
        serial0: gettext('Serial terminal') + ' 0',
        serial1: gettext('Serial terminal') + ' 1',
        serial2: gettext('Serial terminal') + ' 2',
-       serial3: gettext('Serial terminal') + ' 3'
+       serial3: gettext('Serial terminal') + ' 3',
+       virtio: 'VirtIO-GPU',
+       none: Proxmox.Utils.noneText
     },
 
     render_kvm_language: function (value) {
@@ -278,20 +407,23 @@ Ext.define('PVE.Utils', { utilities: {
        return data;
     },
 
+    console_map: {
+       '__default__': Proxmox.Utils.defaultText + ' (xterm.js)',
+       'vv': 'SPICE (remote-viewer)',
+       'html5': 'HTML5 (noVNC)',
+       'xtermjs': 'xterm.js'
+    },
+
     render_console_viewer: function(value) {
-       if (!value || value === '__default__') {
-           return Proxmox.Utils.defaultText + ' (HTML5)';
-       } else if (value === 'vv') {
-           return  'SPICE (remote-viewer)';
-       } else if (value === 'html5') {
-           return  'HTML5 (noVNC)';
-       } else {
-           return value;
+       value = value || '__default__';
+       if (PVE.Utils.console_map[value]) {
+           return PVE.Utils.console_map[value];
        }
+       return value;
     },
 
     console_viewer_array: function() {
-       return Ext.Array.map(['__default__','vv', 'html5'], function(v) {
+       return Ext.Array.map(Object.keys(PVE.Utils.console_map), function(v) {
            return [v, PVE.Utils.render_console_viewer(v)];
        });
     },
@@ -300,7 +432,11 @@ Ext.define('PVE.Utils', { utilities: {
        if (!value) {
            return Proxmox.Utils.defaultText;
        }
-       var text = PVE.Utils.kvm_vga_drivers[value];
+       var vga = PVE.Parser.parsePropertyString(value, 'type');
+       var text = PVE.Utils.kvm_vga_drivers[vga.type];
+       if (!vga.type) {
+           text = Proxmox.Utils.defaultText;
+       }
        if (text) {
            return text + ' (' + value + ')';
        }
@@ -376,11 +512,14 @@ Ext.define('PVE.Utils', { utilities: {
        return days.toFixed(1) + 'd';
     },
 
-    imagesText: gettext('Disk image'),
-    backupFileText: gettext('VZDump backup file'),
-    vztmplText: gettext('Container template'),
-    isoImageText: gettext('ISO image'),
-    containersText: gettext('Container'),
+    contentTypes: {
+       'images': gettext('Disk image'),
+       'backup': gettext('VZDump backup file'),
+       'vztmpl': gettext('Container template'),
+       'iso': gettext('ISO image'),
+       'rootdir': gettext('Container'),
+       'snippets': gettext('Snippets')
+    },
 
     storageSchema: {
        dir: {
@@ -418,26 +557,26 @@ Ext.define('PVE.Utils', { utilities: {
            ipanel: 'IScsiInputPanel',
            faIcon: 'building'
        },
-       sheepdog: {
-           name: 'Sheepdog',
-           ipanel: 'SheepdogInputPanel',
-           hideAdd: true,
+       cephfs: {
+           name: 'CephFS',
+           ipanel: 'CephFSInputPanel',
            faIcon: 'building'
        },
-       rbd: {
-           name: 'RBD',
-           ipanel: 'RBDInputPanel',
+       pvecephfs: {
+           name: 'CephFS (PVE)',
+           ipanel: 'CephFSInputPanel',
            hideAdd: true,
            faIcon: 'building'
        },
-       rbd_ext: {
-           name: 'RBD (external)',
+       rbd: {
+           name: 'RBD',
            ipanel: 'RBDInputPanel',
            faIcon: 'building'
        },
        pveceph: {
            name: 'RBD (PVE)',
-           ipanel: 'PVERBDInputPanel',
+           ipanel: 'RBDInputPanel',
+           hideAdd: true,
            faIcon: 'building'
        },
        zfs: {
@@ -457,8 +596,10 @@ Ext.define('PVE.Utils', { utilities: {
     },
 
     format_storage_type: function(value, md, record) {
-       if (value === 'rbd' && record) {
-           value = (record.get('monhost')?'rbd_ext':'pveceph');
+       if (value === 'rbd') {
+           value = (!record || record.get('monhost') ? 'rbd' : 'pveceph');
+       } else if (value === 'cephfs') {
+           value = (!record || record.get('monhost') ? 'cephfs' : 'pvecephfs');
        }
 
        var schema = PVE.Utils.storageSchema[value];
@@ -482,23 +623,9 @@ Ext.define('PVE.Utils', { utilities: {
     },
 
     format_content_types: function(value) {
-       var cta = [];
-
-       Ext.each(value.split(',').sort(), function(ct) {
-           if (ct === 'images') {
-               cta.push(PVE.Utils.imagesText);
-           } else if (ct === 'backup') {
-               cta.push(PVE.Utils.backupFileText);
-           } else if (ct === 'vztmpl') {
-               cta.push(PVE.Utils.vztmplText);
-           } else if (ct === 'iso') {
-               cta.push(PVE.Utils.isoImageText);
-           } else if (ct === 'rootdir') {
-               cta.push(PVE.Utils.containersText);
-           }
-       });
-
-       return cta.join(', ');
+       return value.split(',').sort().map(function(ct) {
+           return PVE.Utils.contentTypes[ct] || ct;
+       }).join(', ');
     },
 
     render_storage_content: function(value, metaData, record) {
@@ -660,6 +787,10 @@ Ext.define('PVE.Utils', { utilities: {
            status = record.status + ' ha-' + record.hastate;
        }
 
+       if (record.lock) {
+           status += ' locked lock-' + record.lock;
+       }
+
        var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
        if (defaults && defaults.iconCls) {
            var retVal = defaults.iconCls + ' ' + status;
@@ -718,17 +849,55 @@ Ext.define('PVE.Utils', { utilities: {
        return PVE.Utils.render_size_usage(record.used, record.total);
     },
 
+    render_optional_url: function(value) {
+       var match;
+       if (value && (match = value.match(/^https?:\/\//)) !== null) {
+           return '<a target="_blank" href="' + value + '">' + value + '</a>';
+       }
+       return value;
+    },
+
+    render_san: function(value) {
+       var names = [];
+       if (Ext.isArray(value)) {
+           value.forEach(function(val) {
+               if (!Ext.isNumber(val)) {
+                   names.push(val);
+               }
+           });
+           return names.join('<br>');
+       }
+       return value;
+    },
+
+    render_full_name: function(firstname, metaData, record) {
+       var first = firstname || '';
+       var last = record.data.lastname || '';
+       return Ext.htmlEncode(first + " " + last);
+    },
+
+    render_u2f_error: function(error) {
+       var ErrorNames = {
+           '1': gettext('Other Error'),
+           '2': gettext('Bad Request'),
+           '3': gettext('Configuration Unsupported'),
+           '4': gettext('Device Ineligible'),
+           '5': gettext('Timeout')
+       };
+       return "U2F Error: "  + ErrorNames[error] || Proxmox.Utils.unknownText;
+    },
+
     windowHostname: function() {
        return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
             function(m, addr, offset, original) { return addr; });
     },
 
-    openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
-       var dv = PVE.Utils.defaultViewer(allowSpice);
-       PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
+    openDefaultConsoleWindow: function(consoles, vmtype, vmid, nodename, vmname, cmd) {
+       var dv = PVE.Utils.defaultViewer(consoles);
+       PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname, cmd);
     },
 
-    openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
+    openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname, cmd) {
        // kvm, lxc, shell, upgrade
 
        if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
@@ -740,9 +909,9 @@ Ext.define('PVE.Utils', { utilities: {
        }
 
        if (viewer === 'html5') {
-           PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
+           PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname, cmd);
        } else if (viewer === 'xtermjs') {
-           Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
+           Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname, cmd);
        } else if (viewer === 'vv') {
            var url;
            var params = { proxy: PVE.Utils.windowHostname() };
@@ -759,32 +928,52 @@ Ext.define('PVE.Utils', { utilities: {
                url = '/nodes/' + nodename + '/spiceshell';
                params.upgrade = 1;
                PVE.Utils.openSpiceViewer(url, params);
+           } else if (vmtype === 'cmd') {
+               url = '/nodes/' + nodename + '/spiceshell';
+               params.cmd = cmd;
+               PVE.Utils.openSpiceViewer(url, params);
            }
        } else {
            throw "unknown viewer type";
        }
     },
 
-    defaultViewer: function(allowSpice) {
-       var vncdefault = 'html5';
-       var dv = PVE.VersionInfo.console || vncdefault;
+    defaultViewer: function(consoles) {
+
+       var allowSpice, allowXtermjs;
+
+       if (consoles === true) {
+           allowSpice = true;
+           allowXtermjs = true;
+       } else if (typeof consoles === 'object') {
+           allowSpice = consoles.spice;
+           allowXtermjs = !!consoles.xtermjs;
+       }
+       var dv = PVE.VersionInfo.console || 'xtermjs';
        if (dv === 'vv' && !allowSpice) {
-           dv = vncdefault;
+           dv = (allowXtermjs) ? 'xtermjs' : 'html5';
+       } else if (dv === 'xtermjs' && !allowXtermjs) {
+           dv = (allowSpice) ? 'vv' : 'html5';
        }
 
        return dv;
     },
 
-    openVNCViewer: function(vmtype, vmid, nodename, vmname) {
-       var url = Ext.urlEncode({
+    openVNCViewer: function(vmtype, vmid, nodename, vmname, cmd) {
+       var sp = Ext.state.Manager.getProvider();
+       var url = Ext.Object.toQueryString({
            console: vmtype, // kvm, lxc, upgrade or shell
            novnc: 1,
            vmid: vmid,
            vmname: vmname,
-           node: nodename
+           node: nodename,
+           resize: sp.get('novnc-scaling', 'off'),
+           cmd: cmd
        });
        var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
-       nw.focus();
+       if (nw) {
+           nw.focus();
+       }
     },
 
     openSpiceViewer: function(url, params){
@@ -845,8 +1034,12 @@ Ext.define('PVE.Utils', { utilities: {
                    Ext.Msg.alert('Error', response.htmlStatus);
                },
                success: function(response, opts) {
-                   var allowSpice = response.result.data.spice;
-                   PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
+                   let conf = response.result.data;
+                   var consoles = {
+                       spice: !!conf.spice,
+                       xtermjs: !!conf.serial,
+                   };
+                   PVE.Utils.openDefaultConsoleWindow(consoles, 'kvm', vmid, nodename, vmname);
                }
            });
        } else if (record.data.type === 'lxc' && !record.data.template) {
@@ -898,6 +1091,7 @@ Ext.define('PVE.Utils', { utilities: {
        }
 
        menu.showAt(event.getXY());
+       return menu;
     },
 
     // helper for deleting field which are set to there default values
@@ -963,6 +1157,89 @@ Ext.define('PVE.Utils', { utilities: {
                }
            }
        }
+    },
+
+    mp_counts: { mps: 256, unused: 256 },
+
+    forEachMP: function(func, includeUnused) {
+       var i, cont;
+       for (i = 0; i < PVE.Utils.mp_counts.mps; i++) {
+           cont = func('mp', i);
+           if (!cont && cont !== undefined) {
+               return;
+           }
+       }
+
+       if (!includeUnused) {
+           return;
+       }
+
+       for (i = 0; i < PVE.Utils.mp_counts.unused; i++) {
+           cont = func('unused', i);
+           if (!cont && cont !== undefined) {
+               return;
+           }
+       }
+    },
+
+    cleanEmptyObjectKeys: function (obj) {
+       var propName;
+       for (propName in obj) {
+           if (obj.hasOwnProperty(propName)) {
+               if (obj[propName] === null || obj[propName] === undefined) {
+                   delete obj[propName];
+               }
+           }
+       }
+    },
+
+    handleStoreErrorOrMask: function(me, store, regex, callback) {
+
+       me.mon(store, 'load', function (proxy, response, success, operation) {
+
+           if (success) {
+               Proxmox.Utils.setErrorMask(me, false);
+               return;
+           }
+           var msg;
+
+           if (operation.error.statusText) {
+               if (operation.error.statusText.match(regex)) {
+                   callback(me, operation.error);
+                   return;
+               } else {
+                   msg = operation.error.statusText + ' (' + operation.error.status + ')';
+               }
+           } else {
+               msg = gettext('Connection error');
+           }
+           Proxmox.Utils.setErrorMask(me, msg);
+       });
+    },
+
+    showCephInstallOrMask: function(container, msg, nodename, callback){
+       var regex = new RegExp("not (installed|initialized)", "i");
+       if (msg.match(regex)) {
+           if (Proxmox.UserName === 'root@pam') {
+               container.el.mask();
+               if (!container.down('pveCephInstallWindow')){
+                   var isInstalled = msg.match(/not initialized/i) ? true : false;
+                   var win = Ext.create('PVE.ceph.Install', {
+                       nodename: nodename
+                   });
+                   win.getViewModel().set('isInstalled', isInstalled);
+                   container.add(win);
+                   win.show();
+                   callback(win);
+               }
+           } else {
+               container.mask(Ext.String.format(gettext('{0} not installed.') +
+                   ' ' + gettext('Log in as root to install.'), 'Ceph'), ['pve-static-mask']);
+           }
+           return true;
+       } else {
+           return false;
+       }
     }
 },