[pve-devel] [RFC v2 cloudinit pve-manager 2/2] cloudinit: replace add/delete/remove with enabled

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Aug 19 12:56:38 CEST 2015


And a commit button to commit the current config by ejecting
the disk and replacing it with a new one.
---
 www/manager/Parser.js         |  28 ++++++++
 www/manager/qemu/CloudInit.js | 157 ++++++++++++++++++++++++------------------
 2 files changed, 119 insertions(+), 66 deletions(-)

diff --git a/www/manager/Parser.js b/www/manager/Parser.js
index fe2cc41..018cbef 100644
--- a/www/manager/Parser.js
+++ b/www/manager/Parser.js
@@ -146,6 +146,34 @@ Ext.define('PVE.Parser', { statics: {
 	return drivestr;
     },
 
+    parseVolume: function(key, value) {
+	if (!(key && value)) {
+	    return;
+	}
+
+	var res = {};
+	var match_res = key.match(/^([a-z]+)(\d+)$/);
+	if (!match_res)
+	    return;
+	res['bus'] = match_res[1];
+	res['index'] = match_res[2];
+
+	match_res = value.match(/^([^:]+):([^,]+)(?:,(.*))?$/);
+	if (!match_res)
+	    return;
+	res['storage'] = match_res[1];
+	res['file'] = match_res[2];
+	res['args'] = match_res[3];
+	return res;
+    },
+
+    printVolume: function(volume) {
+	var volid = volume.storage + ':' + volume.file;
+	if (volume.args.length)
+	    volid += ',' + volume.args;
+	return volid;
+    },
+
     parseIPConfig: function(key, value) {
 	if (!(key && value)) {
 	    return;
diff --git a/www/manager/qemu/CloudInit.js b/www/manager/qemu/CloudInit.js
index 1a4b880..4cc384b 100644
--- a/www/manager/qemu/CloudInit.js
+++ b/www/manager/qemu/CloudInit.js
@@ -8,6 +8,8 @@ Ext.define('PVE.qemu.CloudInit', {
 	var i, confid;
 
 	me.pveBusId = undefined;
+	me.pveDiskInfo = undefined;
+	me.pveDiskDeleted = 0;
 
 	var nodename = me.pveSelNode.data.node;
 	if (!nodename) {
@@ -21,7 +23,23 @@ Ext.define('PVE.qemu.CloudInit', {
 
 	var caps = Ext.state.Manager.get('GuiCap');
 
-	var rows = {};
+	var rows = {
+	    searchdomain: {
+		editor: caps.vms['VM.Config.Network'] ? 'PVE.qemu.SearchdomainEdit' : undefined,
+		never_delete: caps.vms['VM.Config.Network'] ? false : true,
+		header: gettext('Searchdomain'),
+	    },
+	    nameserver: {
+		editor: caps.vms['VM.Config.Network'] ? 'PVE.qemu.NameserverEdit' : undefined,
+		never_delete: caps.vms['VM.Config.Network'] ? false : true,
+		header: gettext('Nameserver'),
+	    },
+	    sshkey: {
+		editor: caps.vms['VM.Config.Network'] ? 'PVE.qemu.SSHKeyEdit' : undefined,
+		never_delete: caps.vms['VM.Config.Network'] ? false : true,
+		header: gettext('SSH Key'),
+	    }
+	};
 
 	for (i = 0; i < 32; i++) {
 	    var confid = "ipconfig" + i;
@@ -37,8 +55,7 @@ Ext.define('PVE.qemu.CloudInit', {
 	    };
 	}
 
-	// we also need to know whether there's a cloudinit image already
-	// available
+	// we also need to know whether there's a cloudinit image already available
 	PVE.Utils.forEachBus(undefined, function(type, id) {
 	    rows[type + id] = { visible: false };
 	});
@@ -85,10 +102,8 @@ Ext.define('PVE.qemu.CloudInit', {
 	    win.on('destroy', reload);
 	};
 
-	var add_btn = new PVE.button.Button({
-	    text: gettext('Generate'),
-	    disabled: true,
-	    handler: function() {
+	var set_cloudinit_status = function(box, enabled) {
+	    if (enabled) {
 		var win = Ext.create('PVE.qemu.CloudInitCreator', {
 		    url: '/api2/extjs/' + baseurl,
 		    pveSelNode: me.pveSelNode,
@@ -96,22 +111,7 @@ Ext.define('PVE.qemu.CloudInit', {
 		});
 		win.on('destroy', reload);
 		win.show();
-	    }
-        });
-
-	var delete_btn = new PVE.button.Button({
-	    text: gettext('Delete'),
-	    disabled: true,
-	    dangerous: true,
-	    confirmMsg: function() {
-		var msg = gettext('Are you sure you want to remove the CloudInit Image?');
-		if (me.pveBusId.match(/^unused\d+$/)) {
-		    msg += " " + gettext('This will permanently erase all image data.');
-		}
-
-		return msg;
-	    },
-	    handler: function(b, e) {
+	    } else {
 		PVE.Utils.API2Request({
 		    url: '/api2/extjs/' + baseurl,
 		    waitMsgTarget: me,
@@ -125,9 +125,66 @@ Ext.define('PVE.qemu.CloudInit', {
 		    },
 		    failure: function (response, opts) {
 			Ext.Msg.alert('Error', response.htmlStatus);
+			box.setValue(true);
 		    }
 		});
 	    }
+	};
+
+	var run_commit = function() {
+	    var confid = me.pveBusId;
+	    if (!Ext.isDefined(confid)) {
+		Ext.Msg.alert('Error', "internal error: cannot commit");
+		return;
+	    }
+
+	    var drive = PVE.Parser.parseVolume(confid, me.pveDiskInfo);
+	    if (!drive) {
+		Ext.Msg.alert('Error', "bad volume string: " + me.pveDiskInfo);
+		return;
+	    }
+
+	    var eject_params = {};
+	    var insert_params = {};
+	    eject_params[confid] = 'none,media=cdrom';
+	    insert_params[confid] = drive.storage + ':cloudinit';
+
+	    var failure = function(response, opts) {
+		Ext.Msg.alert('Error', response.htmlStatus);
+	    };
+
+	    PVE.Utils.API2Request({
+		url: '/api2/extjs/' + baseurl,
+		waitMsgTarget: me,
+		method: 'PUT',
+		params: eject_params,
+		failure: failure,
+		callback: function() {
+		    PVE.Utils.API2Request({
+			url: '/api2/extjs/' + baseurl,
+			waitMsgTarget: me,
+			method: 'PUT',
+			params: insert_params,
+			failure: failure,
+			callback: reload
+		    });
+		},
+	    });
+	};
+
+	var enable_cb = new PVE.form.Checkbox({
+	    boxLabel: gettext('Enable'),
+	    checked: false,
+	    height: 22, // hack: set same height as text fields
+	    listeners: {
+		change: set_cloudinit_status
+	    }
+	});
+
+	var commit_btn = new PVE.button.Button({
+	    text: gettext('Commit Config'),
+	    disabled: true,
+	    handler: run_commit
 	});
 
 	var edit_btn = new PVE.button.Button({
@@ -137,34 +194,6 @@ Ext.define('PVE.qemu.CloudInit', {
 	    handler: run_editor
         });
 
-	var remove_btn = new PVE.button.Button({
-	    text: gettext('Remove'),
-	    selModel: sm,
-	    disabled: true,
-	    dangerous: true,
-	    confirmMsg: function(rec) {
-		var msg = Ext.String.format(gettext('Are you sure you want to remove entry {0}'),
-					    "'" + me.renderKey(rec.data.key, {}, rec) + "'");
-		return msg;
-	    },
-	    handler: function(b, e, rec) {
-		PVE.Utils.API2Request({
-		    url: '/api2/extjs/' + baseurl,
-		    waitMsgTarget: me,
-		    method: 'PUT',
-		    params: {
-			'delete': rec.data.key
-		    },
-		    callback: function() {
-			reload();
-		    },
-		    failure: function (response, opts) {
-			Ext.Msg.alert('Error', response.htmlStatus);
-		    }
-		});
-	    }
-	});
-
 	var revert_btn = new PVE.button.Button({
 	    text: gettext('Revert'),
 	    selModel: sm,
@@ -195,7 +224,6 @@ Ext.define('PVE.qemu.CloudInit', {
 	    var rec = sm.getSelection()[0];
 
 	    if (!rec) {
-		remove_btn.disable();
 		edit_btn.disable();
 		revert_btn.disable();
 		return;
@@ -206,8 +234,6 @@ Ext.define('PVE.qemu.CloudInit', {
 
 	    var pending = rec.data['delete'] || me.hasPendingChanges(key);
 
-	    remove_btn.setDisabled(rec.data['delete'] || (rowdef.never_delete === true));
-
 	    edit_btn.setDisabled(rec.data['delete'] || !rowdef.editor);
 
 	    revert_btn.setDisabled(!pending);
@@ -216,6 +242,8 @@ Ext.define('PVE.qemu.CloudInit', {
 	var update_data = function() {
 	    var i;
 	    me.pveBusId = undefined;
+	    me.pveDiskInfo = undefined;
+	    me.pveDiskDeleted = 0;
 	    PVE.Utils.forEachBus(undefined, function(type, id) {
 		var confid = type + id;
 		var entry = me.rstore.getById(confid);
@@ -223,18 +251,16 @@ Ext.define('PVE.qemu.CloudInit', {
 		    return; // continue
 		if (entry.data.value.match(/vm-\d+-cloudinit/)) {
 		    me.pveBusId = confid;
+		    me.pveDiskInfo = entry.data.value;
+		    me.pveDiskDeleted = entry.data['delete'];
 		    return false; // break
 		}
 	    });
-	    if (Ext.isDefined(me.pveBusId)) {
-		add_btn.setText(gettext('Regenerate'));
-		add_btn.enable();
-		delete_btn.enable();
-	    } else {
-		add_btn.setText(gettext('Generate'));
-		add_btn.enable();
-		delete_btn.disable();
-	    }
+	    enable_cb.suspendEvents();
+	    var enabled = Ext.isDefined(me.pveBusId) && !me.pveDiskDeleted;
+	    enable_cb.setValue(enabled);
+	    commit_btn.setDisabled(!enabled);
+	    enable_cb.resumeEvents(false);
 
 	    // add/remove arrays because .add/.remove recurses into the
 	    // 'datachange' signal
@@ -268,9 +294,8 @@ Ext.define('PVE.qemu.CloudInit', {
 	    selModel: sm,
 	    cwidth1: 170,
 	    tbar: [ 
-		add_btn,
-		delete_btn,
-		remove_btn,
+		enable_cb,
+		commit_btn,
 		edit_btn,
 		revert_btn
 	    ],
-- 
2.1.4





More information about the pve-devel mailing list