]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/qemu/Config.js
ui: qemu: set correct disabled state for start button
[pve-manager.git] / www / manager6 / qemu / Config.js
1 Ext.define('PVE.qemu.Config', {
2 extend: 'PVE.panel.Config',
3 alias: 'widget.PVE.qemu.Config',
4
5 onlineHelp: 'chapter_virtual_machines',
6
7 initComponent: function() {
8 var me = this;
9 var vm = me.pveSelNode.data;
10
11 var nodename = vm.node;
12 if (!nodename) {
13 throw "no node name specified";
14 }
15
16 var vmid = vm.vmid;
17 if (!vmid) {
18 throw "no VM ID specified";
19 }
20
21 var template = !!vm.template;
22
23 var running = !!vm.uptime;
24
25 var caps = Ext.state.Manager.get('GuiCap');
26
27 var base_url = '/nodes/' + nodename + "/qemu/" + vmid;
28
29 me.statusStore = Ext.create('Proxmox.data.ObjectStore', {
30 url: '/api2/json' + base_url + '/status/current',
31 interval: 1000
32 });
33
34 var vm_command = function(cmd, params) {
35 Proxmox.Utils.API2Request({
36 params: params,
37 url: base_url + '/status/' + cmd,
38 waitMsgTarget: me,
39 method: 'POST',
40 failure: function(response, opts) {
41 Ext.Msg.alert('Error', response.htmlStatus);
42 }
43 });
44 };
45
46 var resumeBtn = Ext.create('Ext.Button', {
47 text: gettext('Resume'),
48 disabled: !caps.vms['VM.PowerMgmt'],
49 hidden: true,
50 handler: function() {
51 vm_command('resume');
52 },
53 iconCls: 'fa fa-play'
54 });
55
56 var startBtn = Ext.create('Ext.Button', {
57 text: gettext('Start'),
58 disabled: !caps.vms['VM.PowerMgmt'] || running,
59 hidden: template,
60 handler: function() {
61 vm_command('start');
62 },
63 iconCls: 'fa fa-play'
64 });
65
66 var migrateBtn = Ext.create('Ext.Button', {
67 text: gettext('Migrate'),
68 disabled: !caps.vms['VM.Migrate'],
69 hidden: PVE.data.ResourceStore.getNodes().length < 2,
70 handler: function() {
71 var win = Ext.create('PVE.window.Migrate', {
72 vmtype: 'qemu',
73 nodename: nodename,
74 vmid: vmid
75 });
76 win.show();
77 },
78 iconCls: 'fa fa-send-o'
79 });
80
81 var moreBtn = Ext.create('Proxmox.button.Button', {
82 text: gettext('More'),
83 menu: { items: [
84 {
85 text: gettext('Clone'),
86 iconCls: 'fa fa-fw fa-clone',
87 hidden: caps.vms['VM.Clone'] ? false : true,
88 handler: function() {
89 PVE.window.Clone.wrap(nodename, vmid, template, 'qemu');
90 }
91 },
92 {
93 text: gettext('Convert to template'),
94 disabled: template,
95 xtype: 'pveMenuItem',
96 iconCls: 'fa fa-fw fa-file-o',
97 hidden: caps.vms['VM.Allocate'] ? false : true,
98 confirmMsg: Proxmox.Utils.format_task_description('qmtemplate', vmid),
99 handler: function() {
100 Proxmox.Utils.API2Request({
101 url: base_url + '/template',
102 waitMsgTarget: me,
103 method: 'POST',
104 failure: function(response, opts) {
105 Ext.Msg.alert('Error', response.htmlStatus);
106 }
107 });
108 }
109 },
110 {
111 iconCls: 'fa fa-heartbeat ',
112 hidden: !caps.nodes['Sys.Console'],
113 text: gettext('Manage HA'),
114 handler: function() {
115 var ha = vm.hastate;
116 Ext.create('PVE.ha.VMResourceEdit', {
117 vmid: vmid,
118 isCreate: (!ha || ha === 'unmanaged')
119 }).show();
120 }
121 },
122 {
123 text: gettext('Remove'),
124 itemId: 'removeBtn',
125 disabled: !caps.vms['VM.Allocate'],
126 handler: function() {
127 Ext.create('PVE.window.SafeDestroy', {
128 url: base_url,
129 item: { type: 'VM', id: vmid }
130 }).show();
131 },
132 iconCls: 'fa fa-trash-o'
133 }
134 ]}
135 });
136
137 var shutdownBtn = Ext.create('PVE.button.Split', {
138 text: gettext('Shutdown'),
139 disabled: !caps.vms['VM.PowerMgmt'] || !running,
140 hidden: template,
141 confirmMsg: Proxmox.Utils.format_task_description('qmshutdown', vmid),
142 handler: function() {
143 vm_command('shutdown');
144 },
145 menu: {
146 items: [{
147 text: gettext('Reboot'),
148 disabled: !caps.vms['VM.PowerMgmt'],
149 tooltip: Ext.String.format(gettext('Shutdown, apply pending changes and reboot {0}'), 'VM'),
150 confirmMsg: Proxmox.Utils.format_task_description('qmreboot', vmid),
151 handler: function() {
152 vm_command("reboot");
153 },
154 iconCls: 'fa fa-refresh'
155 },{
156 text: gettext('Pause'),
157 disabled: !caps.vms['VM.PowerMgmt'],
158 confirmMsg: Proxmox.Utils.format_task_description('qmpause', vmid),
159 handler: function() {
160 vm_command("suspend");
161 },
162 iconCls: 'fa fa-pause'
163 },{
164 text: gettext('Hibernate'),
165 disabled: !caps.vms['VM.PowerMgmt'],
166 confirmMsg: Proxmox.Utils.format_task_description('qmsuspend', vmid),
167 tooltip: gettext('Suspend to disk'),
168 handler: function() {
169 vm_command("suspend", { todisk: 1 });
170 },
171 iconCls: 'fa fa-download'
172 },{
173 text: gettext('Stop'),
174 disabled: !caps.vms['VM.PowerMgmt'],
175 dangerous: true,
176 tooltip: Ext.String.format(gettext('Stop {0} immediately'), 'VM'),
177 confirmMsg: Proxmox.Utils.format_task_description('qmstop', vmid),
178 handler: function() {
179 vm_command("stop", { timeout: 30 });
180 },
181 iconCls: 'fa fa-stop'
182 },{
183 text: gettext('Reset'),
184 disabled: !caps.vms['VM.PowerMgmt'],
185 tooltip: Ext.String.format(gettext('Reset {0} immediately'), 'VM'),
186 confirmMsg: Proxmox.Utils.format_task_description('qmreset', vmid),
187 handler: function() {
188 vm_command("reset");
189 },
190 iconCls: 'fa fa-bolt'
191 }]
192 },
193 iconCls: 'fa fa-power-off'
194 });
195
196 var consoleBtn = Ext.create('PVE.button.ConsoleButton', {
197 disabled: !caps.vms['VM.Console'],
198 hidden: template,
199 consoleType: 'kvm',
200 consoleName: vm.name,
201 nodename: nodename,
202 vmid: vmid
203 });
204
205 var statusTxt = Ext.create('Ext.toolbar.TextItem', {
206 data: {
207 lock: undefined
208 },
209 tpl: [
210 '<tpl if="lock">',
211 '<i class="fa fa-lg fa-lock"></i> ({lock})',
212 '</tpl>'
213 ]
214 });
215
216 Ext.apply(me, {
217 title: Ext.String.format(gettext("Virtual Machine {0} on node '{1}'"), vm.text, nodename),
218 hstateid: 'kvmtab',
219 tbarSpacing: false,
220 tbar: [ statusTxt, '->', resumeBtn, startBtn, shutdownBtn, migrateBtn, consoleBtn, moreBtn ],
221 defaults: { statusStore: me.statusStore },
222 items: [
223 {
224 title: gettext('Summary'),
225 xtype: 'pveGuestSummary',
226 iconCls: 'fa fa-book',
227 itemId: 'summary'
228 }
229 ]
230 });
231
232 if (caps.vms['VM.Console'] && !template) {
233 me.items.push({
234 title: gettext('Console'),
235 itemId: 'console',
236 iconCls: 'fa fa-terminal',
237 xtype: 'pveNoVncConsole',
238 vmid: vmid,
239 consoleType: 'kvm',
240 nodename: nodename
241 });
242 }
243
244 me.items.push(
245 {
246 title: gettext('Hardware'),
247 itemId: 'hardware',
248 iconCls: 'fa fa-desktop',
249 xtype: 'PVE.qemu.HardwareView'
250 },
251 {
252 title: 'Cloud-Init',
253 itemId: 'cloudinit',
254 iconCls: 'fa fa-cloud',
255 xtype: 'pveCiPanel'
256 },
257 {
258 title: gettext('Options'),
259 iconCls: 'fa fa-gear',
260 itemId: 'options',
261 xtype: 'PVE.qemu.Options'
262 },
263 {
264 title: gettext('Task History'),
265 itemId: 'tasks',
266 xtype: 'proxmoxNodeTasks',
267 iconCls: 'fa fa-list',
268 nodename: nodename,
269 vmidFilter: vmid
270 }
271 );
272
273 if (caps.vms['VM.Monitor'] && !template) {
274 me.items.push({
275 title: gettext('Monitor'),
276 iconCls: 'fa fa-eye',
277 itemId: 'monitor',
278 xtype: 'pveQemuMonitor'
279 });
280 }
281
282 if (caps.vms['VM.Backup']) {
283 me.items.push({
284 title: gettext('Backup'),
285 iconCls: 'fa fa-floppy-o',
286 xtype: 'pveBackupView',
287 itemId: 'backup'
288 },
289 {
290 title: gettext('Replication'),
291 iconCls: 'fa fa-retweet',
292 xtype: 'pveReplicaView',
293 itemId: 'replication'
294 });
295 }
296
297 if ((caps.vms['VM.Snapshot'] || caps.vms['VM.Snapshot.Rollback'] ||
298 caps.vms['VM.Audit']) && !template) {
299 me.items.push({
300 title: gettext('Snapshots'),
301 iconCls: 'fa fa-history',
302 type: 'qemu',
303 xtype: 'pveGuestSnapshotTree',
304 itemId: 'snapshot'
305 });
306 }
307
308 if (caps.vms['VM.Console']) {
309 me.items.push(
310 {
311 xtype: 'pveFirewallRules',
312 title: gettext('Firewall'),
313 iconCls: 'fa fa-shield',
314 allow_iface: true,
315 base_url: base_url + '/firewall/rules',
316 list_refs_url: base_url + '/firewall/refs',
317 itemId: 'firewall'
318 },
319 {
320 xtype: 'pveFirewallOptions',
321 groups: ['firewall'],
322 iconCls: 'fa fa-gear',
323 onlineHelp: 'pve_firewall_vm_container_configuration',
324 title: gettext('Options'),
325 base_url: base_url + '/firewall/options',
326 fwtype: 'vm',
327 itemId: 'firewall-options'
328 },
329 {
330 xtype: 'pveFirewallAliases',
331 title: gettext('Alias'),
332 groups: ['firewall'],
333 iconCls: 'fa fa-external-link',
334 base_url: base_url + '/firewall/aliases',
335 itemId: 'firewall-aliases'
336 },
337 {
338 xtype: 'pveIPSet',
339 title: gettext('IPSet'),
340 groups: ['firewall'],
341 iconCls: 'fa fa-list-ol',
342 base_url: base_url + '/firewall/ipset',
343 list_refs_url: base_url + '/firewall/refs',
344 itemId: 'firewall-ipset'
345 },
346 {
347 title: gettext('Log'),
348 groups: ['firewall'],
349 iconCls: 'fa fa-list',
350 onlineHelp: 'chapter_pve_firewall',
351 itemId: 'firewall-fwlog',
352 xtype: 'proxmoxLogView',
353 url: '/api2/extjs' + base_url + '/firewall/log'
354 }
355 );
356 }
357
358 if (caps.vms['Permissions.Modify']) {
359 me.items.push({
360 xtype: 'pveACLView',
361 title: gettext('Permissions'),
362 iconCls: 'fa fa-unlock',
363 itemId: 'permissions',
364 path: '/vms/' + vmid
365 });
366 }
367
368 me.callParent();
369
370 var prevQMPStatus = 'unknown';
371 me.mon(me.statusStore, 'load', function(s, records, success) {
372 var status;
373 var qmpstatus;
374 var spice = false;
375 var xtermjs = false;
376 var lock;
377
378 if (!success) {
379 status = qmpstatus = 'unknown';
380 } else {
381 var rec = s.data.get('status');
382 status = rec ? rec.data.value : 'unknown';
383 rec = s.data.get('qmpstatus');
384 qmpstatus = rec ? rec.data.value : 'unknown';
385 rec = s.data.get('template');
386 template = rec.data.value || false;
387 rec = s.data.get('lock');
388 lock = rec ? rec.data.value : undefined;
389
390 spice = s.data.get('spice') ? true : false;
391 xtermjs = s.data.get('serial') ? true : false;
392
393 }
394
395 if (template) {
396 return;
397 }
398
399 var resume = (['prelaunch', 'paused', 'suspended'].indexOf(qmpstatus) !== -1);
400
401 if (resume || lock === 'suspended') {
402 startBtn.setVisible(false);
403 resumeBtn.setVisible(true);
404 } else {
405 startBtn.setVisible(true);
406 resumeBtn.setVisible(false);
407 }
408
409 consoleBtn.setEnableSpice(spice);
410 consoleBtn.setEnableXtermJS(xtermjs);
411
412 statusTxt.update({ lock: lock });
413
414 let guest_running = status === 'running' &&
415 !(qmpstatus === "shutdown" || qmpstatus === "prelaunch");
416 startBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || template || guest_running);
417
418 shutdownBtn.setDisabled(!caps.vms['VM.PowerMgmt'] || status !== 'running');
419 me.down('#removeBtn').setDisabled(!caps.vms['VM.Allocate'] || status !== 'stopped');
420 consoleBtn.setDisabled(template);
421
422 let wasStopped = ['prelaunch', 'stopped', 'suspended'].indexOf(prevQMPStatus) !== -1;
423 if (wasStopped && qmpstatus === 'running') {
424 let con = me.down('#console');
425 if (con) {
426 con.reload();
427 }
428 }
429
430 prevQMPStatus = qmpstatus;
431 });
432
433 me.on('afterrender', function() {
434 me.statusStore.startUpdate();
435 });
436
437 me.on('destroy', function() {
438 me.statusStore.stopUpdate();
439 });
440 }
441 });