Config.js 6.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Ext.define('PVE.qemu.Config', {
    extend: 'PVE.panel.Config',
    alias: 'widget.PVE.qemu.Config',

    initComponent: function() {
        var me = this;

	var nodename = me.pveSelNode.data.node;
	if (!nodename) {
	    throw "no node name specified";
	}

	var vmid = me.pveSelNode.data.vmid;
	if (!vmid) {
	    throw "no VM ID specified";
	}

18 19
	var caps = Ext.state.Manager.get('GuiCap');

Dietmar Maurer's avatar
Dietmar Maurer committed
20 21
	var base_url = '/nodes/' + nodename + "/qemu/" + vmid;
 
Dietmar Maurer's avatar
Dietmar Maurer committed
22
	me.statusStore = Ext.create('PVE.data.ObjectStore', {
Dietmar Maurer's avatar
Dietmar Maurer committed
23
	    url: '/api2/json' + base_url + '/status/current',
Dietmar Maurer's avatar
Dietmar Maurer committed
24 25 26 27 28 29
	    interval: 1000
	});

	var vm_command = function(cmd, params) {
	    PVE.Utils.API2Request({
		params: params,
Dietmar Maurer's avatar
Dietmar Maurer committed
30
		url: base_url + '/status/' + cmd,
Dietmar Maurer's avatar
Dietmar Maurer committed
31 32 33 34 35 36 37 38
		waitMsgTarget: me,
		method: 'POST',
		failure: function(response, opts) {
		    Ext.Msg.alert('Error', response.htmlStatus);
		}
	    });
	};

39 40 41 42 43 44 45 46 47
	var resumeBtn = Ext.create('Ext.Button', { 
	    text: gettext('Resume'),
	    disabled: !caps.vms['VM.PowerMgmt'],
	    visible: false,
	    handler: function() {
		vm_command('resume');
	    }			    
	}); 

Dietmar Maurer's avatar
Dietmar Maurer committed
48
	var startBtn = Ext.create('Ext.Button', { 
49
	    text: gettext('Start'),
50
	    disabled: !caps.vms['VM.PowerMgmt'],
Dietmar Maurer's avatar
Dietmar Maurer committed
51 52 53 54 55 56
	    handler: function() {
		vm_command('start');
	    }			    
	}); 
 
	var stopBtn = Ext.create('PVE.button.Button', {
57
	    text: gettext('Stop'),
58
	    disabled: !caps.vms['VM.PowerMgmt'],
59
	    confirmMsg: Ext.String.format(gettext("Do you really want to stop VM {0}?"), vmid),
Dietmar Maurer's avatar
Dietmar Maurer committed
60 61 62 63 64 65
	    handler: function() {
		vm_command("stop", { timeout: 30 });
	    }
	});

	var migrateBtn = Ext.create('Ext.Button', { 
66
	    text: gettext('Migrate'),
67
	    disabled: !caps.vms['VM.Migrate'],
Dietmar Maurer's avatar
Dietmar Maurer committed
68
	    handler: function() {
69 70 71 72
		var win = Ext.create('PVE.window.Migrate', {
		    vmtype: 'qemu',
		    nodename: nodename,
		    vmid: vmid
Dietmar Maurer's avatar
Dietmar Maurer committed
73 74 75 76 77 78
		});
		win.show();
	    }    
	});
 
	var resetBtn = Ext.create('PVE.button.Button', {
79
	    text: gettext('Reset'),
80
	    disabled: !caps.vms['VM.PowerMgmt'],
81
	    confirmMsg: Ext.String.format(gettext("Do you really want to reset VM {0}?"), vmid),
Dietmar Maurer's avatar
Dietmar Maurer committed
82 83 84 85 86 87
	    handler: function() { 
		vm_command("reset");
	    }
	});

	var shutdownBtn = Ext.create('PVE.button.Button', {
Dietmar Maurer's avatar
Dietmar Maurer committed
88
	    text: gettext('Shutdown'),
89
	    disabled: !caps.vms['VM.PowerMgmt'],
90
	    confirmMsg: Ext.String.format(gettext("Do you really want to shutdown VM {0}?"), vmid),
Dietmar Maurer's avatar
Dietmar Maurer committed
91
	    handler: function() {
92
		vm_command('shutdown');
Dietmar Maurer's avatar
Dietmar Maurer committed
93 94 95 96
	    }			    
	});

	var removeBtn = Ext.create('PVE.button.Button', {
97
	    text: gettext('Remove'),
98
	    disabled: !caps.vms['VM.Allocate'],
99
	    dangerous: true,
100
	    confirmMsg: Ext.String.format(gettext('Are you sure you want to remove VM {0}? This will permanently erase all VM data.'), vmid),
Dietmar Maurer's avatar
Dietmar Maurer committed
101 102
	    handler: function() {
		PVE.Utils.API2Request({
Dietmar Maurer's avatar
Dietmar Maurer committed
103
		    url: base_url,
Dietmar Maurer's avatar
Dietmar Maurer committed
104 105 106 107 108 109 110 111 112
		    method: 'DELETE',
		    waitMsgTarget: me,
		    failure: function(response, opts) {
			Ext.Msg.alert('Error', response.htmlStatus);
		    }
		});
	    } 
	});

113 114
	var vmname = me.pveSelNode.data.name;

Dietmar Maurer's avatar
Dietmar Maurer committed
115
	var consoleBtn = Ext.create('PVE.button.ConsoleButton', {
116
	    disabled: !caps.vms['VM.Console'],
Dietmar Maurer's avatar
Dietmar Maurer committed
117 118 119 120
	    consoleType: 'kvm',
	    consoleName: vmname,
	    nodename: nodename,
	    vmid: vmid
121 122
	});

123 124
	var descr = vmid + " (" + (vmname ? "'" + vmname + "' " : "'VM " + vmid + "'") + ")";

125
	Ext.apply(me, {
126
	    title: Ext.String.format(gettext("Virtual Machine {0} on node {1}"), descr, "'" + nodename + "'"),
127
	    hstateid: 'kvmtab',
128
	    tbar: [ resumeBtn, startBtn, shutdownBtn, stopBtn, resetBtn, 
129
		    removeBtn, migrateBtn, consoleBtn],
Dietmar Maurer's avatar
Dietmar Maurer committed
130
	    defaults: { statusStore: me.statusStore },
131 132
	    items: [
		{
133
		    title: gettext('Summary'),
134 135 136 137
		    xtype: 'pveQemuSummary',
		    itemId: 'summary'
		},
		{
138
		    title: gettext('Hardware'),
139 140 141 142
		    itemId: 'hardware',
		    xtype: 'PVE.qemu.HardwareView'
		},
		{
143
		    title: gettext('Options'),
144 145
		    itemId: 'options',
		    xtype: 'PVE.qemu.Options'
146 147
		},
		{
Damien PIQUET's avatar
Damien PIQUET committed
148
		    title: gettext('Task History'),
149 150 151
		    itemId: 'tasks',
		    xtype: 'pveNodeTasks',
		    vmidFilter: vmid
152 153 154 155
		}
	    ]
	});

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	if (caps.vms['VM.Monitor']) {
	    me.items.push({
		title: gettext('Monitor'),
		itemId: 'monitor',
		xtype: 'pveQemuMonitor'
	    });
	}

	if (caps.vms['VM.Backup']) {
	    me.items.push({
		title: gettext('Backup'),
		xtype: 'pveBackupView',
		itemId: 'backup'
	    });
	}

172 173 174 175 176 177 178 179
	if (caps.vms['VM.Snapshot']) {
	    me.items.push({
		title: gettext('Snapshots'),
		xtype: 'pveQemuSnapshotTree',
		itemId: 'snapshot'
	    });
	}

Dietmar Maurer's avatar
Dietmar Maurer committed
180 181 182 183 184 185 186 187 188 189 190 191 192
	if (caps.vms['VM.Console']) {
	    me.items.push([
		{
		    xtype: 'pveFirewallPanel',
		    title: gettext('Firewall'),
		    base_url: base_url + '/firewall',
		    fwtype: 'vm',
		    phstateid: me.hstateid,
		    itemId: 'firewall'
		}
	    ]);
	}

193 194 195 196 197 198 199 200 201
	if (caps.vms['Permissions.Modify']) {
	    me.items.push({
		xtype: 'pveACLView',
		title: gettext('Permissions'),
		itemId: 'permissions',
		path: '/vms/' + vmid
	    });
	}

202
	me.callParent();
Dietmar Maurer's avatar
Dietmar Maurer committed
203 204 205

        me.statusStore.on('load', function(s, records, success) {
	    var status;
206
	    var qmpstatus;
207
	    var template;
Dietmar Maurer's avatar
Dietmar Maurer committed
208
	    var spice = false;
209

Dietmar Maurer's avatar
Dietmar Maurer committed
210 211
	    if (!success) {
		me.workspace.checkVmMigration(me.pveSelNode);
212
		status = qmpstatus = 'unknown';
Dietmar Maurer's avatar
Dietmar Maurer committed
213 214 215
	    } else {
		var rec = s.data.get('status');
		status = rec ? rec.data.value : 'unknown';
216 217
		rec = s.data.get('qmpstatus');
		qmpstatus = rec ? rec.data.value : 'unknown';
218 219 220 221
		rec = s.data.get('template');
		if(rec.data.value){
		    template = rec.data.value;
		}
222 223
		spice = s.data.get('spice') ? true : false;

224 225 226 227
	    }

	    if (qmpstatus === 'prelaunch' || qmpstatus === 'paused') {
		startBtn.setVisible(false);
Dietmar Maurer's avatar
Dietmar Maurer committed
228
		resumeBtn.setVisible(true);
229 230
	    } else {
		startBtn.setVisible(true);
Dietmar Maurer's avatar
Dietmar Maurer committed
231
		resumeBtn.setVisible(false);
Dietmar Maurer's avatar
Dietmar Maurer committed
232
	    }
Dietmar Maurer's avatar
Dietmar Maurer committed
233 234

	    consoleBtn.setEnableSpice(spice);
Dietmar Maurer's avatar
Dietmar Maurer committed
235

236 237
	    startBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || status === 'running' || template);
	    resetBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || status !== 'running' || template);
238 239 240
	    shutdownBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || status !== 'running');
	    stopBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || status === 'stopped');
	    removeBtn.setDisabled(!caps.vms['VM.Allocate'] || status !== 'stopped');
Dietmar Maurer's avatar
Dietmar Maurer committed
241 242 243 244 245 246 247 248 249
	});

	me.on('afterrender', function() {
	    me.statusStore.startUpdate();
	});

	me.on('destroy', function() {
	    me.statusStore.stopUpdate();
	});
250 251
   }
});