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