[pve-devel] [PATCH manager 5/8] add qemu IPConfigEdit for CloudInit

Dominik Csapak d.csapak at proxmox.com
Thu Mar 15 16:21:33 CET 2018


resembles the lxc IPConfigEdit, but is slightly different

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 www/manager6/Makefile             |   1 +
 www/manager6/Parser.js            |  57 +++++++++
 www/manager6/qemu/IPConfigEdit.js | 248 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 306 insertions(+)
 create mode 100644 www/manager6/qemu/IPConfigEdit.js

diff --git a/www/manager6/Makefile b/www/manager6/Makefile
index e0c46557..f2cfbdd2 100644
--- a/www/manager6/Makefile
+++ b/www/manager6/Makefile
@@ -129,6 +129,7 @@ JSSRC= 				                 	\
 	qemu/CreateWizard.js				\
 	qemu/USBEdit.js					\
 	qemu/AgentIPView.js				\
+	qemu/IPConfigEdit.js				\
 	lxc/Summary.js					\
 	lxc/Network.js					\
 	lxc/Resources.js				\
diff --git a/www/manager6/Parser.js b/www/manager6/Parser.js
index c796d35b..8253bd80 100644
--- a/www/manager6/Parser.js
+++ b/www/manager6/Parser.js
@@ -162,6 +162,63 @@ Ext.define('PVE.Parser', { statics: {
 	return drivestr;
     },
 
+    parseIPConfig: function(key, value) {
+	if (!(key && value)) {
+	    return;
+	}
+
+	var res = {};
+
+	var errors = false;
+	Ext.Array.each(value.split(','), function(p) {
+	    if (!p || p.match(/^\s*$/)) {
+		return; // continue
+	    }
+
+	    var match_res;
+	    if ((match_res = p.match(/^ip=(\S+)$/)) !== null) {
+		res.ip = match_res[1];
+	    } else if ((match_res = p.match(/^gw=(\S+)$/)) !== null) {
+		res.gw = match_res[1];
+	    } else if ((match_res = p.match(/^ip6=(\S+)$/)) !== null) {
+		res.ip6 = match_res[1];
+	    } else if ((match_res = p.match(/^gw6=(\S+)$/)) !== null) {
+		res.gw6 = match_res[1];
+	    } else {
+		errors = true;
+		return false; // break
+	    }
+	});
+
+	if (errors) {
+	    return;
+	}
+
+	return res;
+    },
+
+    printIPConfig: function(cfg) {
+	var c = "";
+	var str = "";
+	if (cfg.ip) {
+	    str += "ip=" + cfg.ip;
+	    c = ",";
+	}
+	if (cfg.gw) {
+	    str += c + "gw=" + cfg.gw;
+	    c = ",";
+	}
+	if (cfg.ip6) {
+	    str += c + "ip6=" + cfg.ip6;
+	    c = ",";
+	}
+	if (cfg.gw6) {
+	    str += c + "gw6=" + cfg.gw6;
+	    c = ",";
+	}
+	return str;
+    },
+
     parseOpenVZNetIf: function(value) {
 	if (!value) {
 	    return;
diff --git a/www/manager6/qemu/IPConfigEdit.js b/www/manager6/qemu/IPConfigEdit.js
new file mode 100644
index 00000000..e185f1a3
--- /dev/null
+++ b/www/manager6/qemu/IPConfigEdit.js
@@ -0,0 +1,248 @@
+Ext.define('PVE.qemu.IPConfigPanel', {
+    extend: 'Proxmox.panel.InputPanel',
+    xtype: 'pveIPConfigPanel',
+
+    insideWizard: false,
+
+    vmconfig: {},
+
+    onGetValues: function(values) {
+	var me = this;
+
+	if (values.ipv4mode !== 'static') {
+	    values.ip = values.ipv4mode;
+	}
+
+	if (values.ipv6mode !== 'static') {
+	    values.ip6 = values.ipv6mode;
+	}
+
+	var params = {};
+
+	var cfg = PVE.Parser.printIPConfig(values);
+	if (cfg === '') {
+	    params['delete'] = [me.confid];
+	} else {
+	    params[me.confid] = cfg;
+	}
+	return params;
+    },
+
+    setVMConfig: function(config) {
+	var me = this;
+	me.vmconfig = config;
+    },
+
+    setIPConfig: function(confid, data) {
+	var me = this;
+
+	me.confid = confid;
+
+	if (data.ip === 'dhcp') {
+	    data.ipv4mode = data.ip;
+	    data.ip = '';
+	} else {
+	    data.ipv4mode = 'static';
+	}
+	if (data.ip6 === 'dhcp' || data.ip6 === 'auto') {
+	    data.ipv6mode = data.ip6;
+	    data.ip6 = '';
+	} else {
+	    data.ipv6mode = 'static';
+	}
+
+	me.ipconfig = data;
+	me.setValues(me.ipconfig);
+    },
+
+    initComponent : function() {
+	var me = this;
+
+	me.ipconfig = {};
+
+	me.column1 = [
+	    {
+		xtype: 'displayfield',
+		fieldLabel: gettext('Network Device'),
+		value: me.netid
+	    },
+	    {
+		layout: {
+		    type: 'hbox',
+		    align: 'middle'
+		},
+		border: false,
+		margin: '0 0 5 0',
+		items: [
+		    {
+			xtype: 'label',
+			text: gettext('IPv4') + ':'
+		    },
+		    {
+			xtype: 'radiofield',
+			boxLabel: gettext('Static'),
+			name: 'ipv4mode',
+			inputValue: 'static',
+			checked: false,
+			margin: '0 0 0 10',
+			listeners: {
+			    change: function(cb, value) {
+				me.down('field[name=ip]').setDisabled(!value);
+				me.down('field[name=gw]').setDisabled(!value);
+			    }
+			}
+		    },
+		    {
+			xtype: 'radiofield',
+			boxLabel: gettext('DHCP'),
+			name: 'ipv4mode',
+			inputValue: 'dhcp',
+			checked: false,
+			margin: '0 0 0 10'
+		    }
+		]
+	    },
+	    {
+		xtype: 'textfield',
+		name: 'ip',
+		vtype: 'IPCIDRAddress',
+		value: '',
+		disabled: true,
+		fieldLabel: gettext('IPv4/CIDR')
+	    },
+	    {
+		xtype: 'textfield',
+		name: 'gw',
+		value: '',
+		vtype: 'IPAddress',
+		disabled: true,
+		fieldLabel: gettext('Gateway') + ' (' + gettext('IPv4') +')'
+	    }
+	];
+
+	me.column2 = [
+	    {
+		xtype: 'displayfield'
+	    },
+	    {
+		layout: {
+		    type: 'hbox',
+		    align: 'middle'
+		},
+		border: false,
+		margin: '0 0 5 0',
+		items: [
+		    {
+			xtype: 'label',
+			text: gettext('IPv6') + ':'
+		    },
+		    {
+			xtype: 'radiofield',
+			boxLabel: gettext('Static'),
+			name: 'ipv6mode',
+			inputValue: 'static',
+			checked: false,
+			margin: '0 0 0 10',
+			listeners: {
+			    change: function(cb, value) {
+				me.down('field[name=ip6]').setDisabled(!value);
+				me.down('field[name=gw6]').setDisabled(!value);
+			    }
+			}
+		    },
+		    {
+			xtype: 'radiofield',
+			boxLabel: gettext('DHCP'),
+			name: 'ipv6mode',
+			inputValue: 'dhcp',
+			checked: false,
+			margin: '0 0 0 10'
+		    },
+		    {
+			xtype: 'radiofield',
+			boxLabel: gettext('SLAAC'),
+			name: 'ipv6mode',
+			inputValue: 'auto',
+			checked: false,
+			margin: '0 0 0 10'
+		    }
+		]
+	    },
+	    {
+		xtype: 'textfield',
+		name: 'ip6',
+		value: '',
+		vtype: 'IP6CIDRAddress',
+		disabled: true,
+		fieldLabel: gettext('IPv6/CIDR')
+	    },
+	    {
+		xtype: 'textfield',
+		name: 'gw6',
+		vtype: 'IP6Address',
+		value: '',
+		disabled: true,
+		fieldLabel: gettext('Gateway') + ' (' + gettext('IPv6') +')'
+	    }
+	];
+
+	me.callParent();
+    }
+});
+
+Ext.define('PVE.qemu.IPConfigEdit', {
+    extend: 'Proxmox.window.Edit',
+
+    isAdd: true,
+
+    initComponent : function() {
+	/*jslint confusion: true */
+
+	var me = this;
+
+	// convert confid from netX to ipconfigX
+	var match = me.confid.match(/^net(\d+)$/);
+	if (match) {
+	    me.netid = me.confid;
+	    me.confid = 'ipconfig' + match[1];
+	}
+
+	var nodename = me.pveSelNode.data.node;
+	if (!nodename) {
+	    throw "no node name specified";
+	}
+
+	me.isCreate = me.confid ? false : true;
+
+	var ipanel = Ext.create('PVE.qemu.IPConfigPanel', {
+	    confid: me.confid,
+	    netid: me.netid,
+	    nodename: nodename
+	});
+
+	Ext.applyIf(me, {
+	    subject: gettext('Network Config'),
+	    items: ipanel
+	});
+
+	me.callParent();
+
+	me.load({
+	    success: function(response, options) {
+		me.vmconfig = response.result.data;
+		var ipconfig = {};
+		var value = me.vmconfig[me.confid];
+		if (value) {
+		    ipconfig = PVE.Parser.parseIPConfig(me.confid, value);
+		    if (!ipconfig) {
+			Ext.Msg.alert(gettext('Error'), gettext('Unable to parse network configuration'));
+			me.close();
+			return;
+		    }
+		}
+		ipanel.setIPConfig(me.confid, ipconfig);
+		ipanel.setVMConfig(me.vmconfig);
+	    }
+	});
+    }
+});
-- 
2.11.0





More information about the pve-devel mailing list