]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/qemu/AgentIPView.js
correctly update qemu ip information
[pve-manager.git] / www / manager6 / qemu / AgentIPView.js
1 Ext.define('PVE.window.IPInfo', {
2 extend: 'Ext.window.Window',
3 width: 600,
4 title: gettext('Guest Agent Network Information'),
5 height: 300,
6 layout: {
7 type: 'fit'
8 },
9 modal: true,
10 items: [
11 {
12 xtype: 'grid',
13 emptyText: gettext('No network information'),
14 columns: [
15 {
16 dataIndex: 'name',
17 text: gettext('Name'),
18 flex: 3
19 },
20 {
21 dataIndex: 'hardware-address',
22 text: gettext('MAC address'),
23 width: 140
24 },
25 {
26 dataIndex: 'ip-addresses',
27 text: gettext('IP address'),
28 align: 'right',
29 flex: 4,
30 renderer: function(val) {
31 if (!Ext.isArray(val)) {
32 return '';
33 }
34 var ips = [];
35 val.forEach(function(ip) {
36 var addr = ip['ip-address'];
37 var pref = ip.prefix;
38 if (addr && pref) {
39 ips.push(addr + '/' + pref);
40 }
41 });
42 return ips.join('<br>');
43 }
44 }
45 ]
46 }
47 ]
48 });
49
50 Ext.define('PVE.qemu.AgentIPView', {
51 extend: 'Ext.container.Container',
52 xtype: 'pveAgentIPView',
53
54 layout: {
55 type: 'hbox',
56 align: 'top'
57 },
58
59 nics: [],
60
61 items: [
62 {
63 xtype: 'box',
64 html: '<i class="fa fa-exchange"></i> IPs'
65 },
66 {
67 xtype: 'container',
68 flex: 1,
69 layout: {
70 type: 'vbox',
71 align: 'right',
72 pack: 'end'
73 },
74 items: [
75 {
76 xtype: 'label',
77 flex: 1,
78 itemId: 'ipBox',
79 style: {
80 'text-align': 'right'
81 }
82 },
83 {
84 xtype: 'button',
85 itemId: 'moreBtn',
86 hidden: true,
87 ui: 'default-toolbar',
88 handler: function(btn) {
89 var me = this.up('pveAgentIPView');
90
91 var win = Ext.create('PVE.window.IPInfo');
92 win.down('grid').getStore().setData(me.nics);
93 win.show();
94 },
95 text: gettext('More')
96 }
97 ]
98 }
99 ],
100
101 getDefaultIps: function(nics) {
102 var me = this;
103 var ips = [];
104 nics.forEach(function(nic) {
105 if (nic['hardware-address'] &&
106 nic['hardware-address'] != '00:00:00:00:00:00') {
107
108 var nic_ips = nic['ip-addresses'] || [];
109 nic_ips.forEach(function(ip) {
110 var p = ip['ip-address'];
111 // show 2 ips at maximum
112 if (ips.length < 2) {
113 ips.push(p);
114 }
115 });
116 }
117 });
118
119 return ips;
120 },
121
122 startIPStore: function(store, records, success) {
123 var me = this;
124 var agentRec = store.getById('agent');
125 /*jslint confusion: true*/
126 /* value is number and string */
127 me.agent = (agentRec && agentRec.data.value === 1);
128 me.running = (store.getById('status').data.value === 'running');
129 /*jslint confusion: false*/
130
131 var caps = Ext.state.Manager.get('GuiCap');
132
133 if (!caps.vms['VM.Monitor']) {
134 var errorText = gettext("Requires '{0}' Privileges");
135 me.updateStatus(false, Ext.String.format(errorText, 'VM.Monitor'));
136 return;
137 }
138
139 if (me.agent && me.running && me.ipStore.isStopped) {
140 me.ipStore.startUpdate();
141 } else if (me.ipStore.isStopped) {
142 me.updateStatus();
143 }
144 },
145
146 updateStatus: function(unsuccessful, defaulttext) {
147 var me = this;
148 var text = defaulttext || gettext('No network information');
149 var more = false;
150 if (unsuccessful) {
151 text = gettext('Guest Agent not running');
152 } else if (me.agent && me.running) {
153 if (Ext.isArray(me.nics) && me.nics.length) {
154 more = true;
155 var ips = me.getDefaultIps(me.nics);
156 if (ips.length !== 0) {
157 text = ips.join('<br>');
158 }
159 } else if (me.nics && me.nics.error) {
160 var msg = gettext('Cannot get info from Guest Agent<br>Error: {0}');
161 text = Ext.String.format(text, me.nics.error.desc);
162 }
163 } else if (me.agent) {
164 text = gettext('Guest Agent not running');
165 } else {
166 text = gettext('No Guest Agent configured');
167 }
168
169 var ipBox = me.down('#ipBox');
170 ipBox.update(text);
171
172 var moreBtn = me.down('#moreBtn');
173 moreBtn.setVisible(more);
174 },
175
176 initComponent: function() {
177 var me = this;
178
179 if (!me.rstore) {
180 throw 'rstore not given';
181 }
182
183 if (!me.pveSelNode) {
184 throw 'pveSelNode not given';
185 }
186
187 var nodename = me.pveSelNode.data.node;
188 var vmid = me.pveSelNode.data.vmid;
189
190 me.ipStore = Ext.create('Proxmox.data.UpdateStore', {
191 interval: 10000,
192 storeid: 'pve-qemu-agent-' + vmid,
193 method: 'POST',
194 proxy: {
195 type: 'proxmox',
196 url: '/api2/json/nodes/' + nodename + '/qemu/' + vmid + '/agent/network-get-interfaces'
197 }
198 });
199
200 me.callParent();
201
202 me.mon(me.ipStore, 'load', function(store, records, success) {
203 if (records && records.length) {
204 me.nics = records[0].data.result;
205 } else {
206 me.nics = undefined;
207 }
208 me.updateStatus(!success);
209 });
210
211 me.on('destroy', me.ipStore.stopUpdate);
212
213 // if we already have info about the vm, use it immediately
214 if (me.rstore.getCount()) {
215 me.startIPStore(me.rstore, me.rstore.getData(), false);
216 }
217
218 // check if the guest agent is there on every statusstore load
219 me.mon(me.rstore, 'load', me.startIPStore, me);
220 }
221 });