]>
Commit | Line | Data |
---|---|---|
0381b693 DM |
1 | // Some configuration values are complex strings - |
2 | // so we need parsers/generators for them. | |
aff192e6 DM |
3 | |
4 | Ext.define('PVE.Parser', { statics: { | |
5 | ||
6 | // this class only contains static functions | |
7 | ||
8 | parseQemuNetwork: function(key, value) { | |
9 | if (!(key && value)) { | |
10 | return; | |
11 | } | |
12 | ||
13 | var res = {}; | |
14 | ||
15 | var errors = false; | |
16 | Ext.Array.each(value.split(','), function(p) { | |
17 | if (!p || p.match(/^\s*$/)) { | |
18 | return; // continue | |
19 | } | |
20 | ||
21 | var match_res; | |
22 | ||
e3acafc9 | 23 | if ((match_res = p.match(/^(ne2k_pci|e1000|vmxnet3|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i)) !== null) { |
aff192e6 DM |
24 | res.model = match_res[1].toLowerCase(); |
25 | if (match_res[3]) { | |
26 | res.macaddr = match_res[3]; | |
27 | } | |
28 | } else if ((match_res = p.match(/^bridge=(\S+)$/)) !== null) { | |
29 | res.bridge = match_res[1]; | |
30 | } else if ((match_res = p.match(/^rate=(\d+(\.\d+)?)$/)) !== null) { | |
31 | res.rate = match_res[1]; | |
cd265ba2 | 32 | } else if ((match_res = p.match(/^tag=(\d+(\.\d+)?)$/)) !== null) { |
33 | res.tag = match_res[1]; | |
a61b1ac9 AD |
34 | } else if ((match_res = p.match(/^firewall=(\d+)$/)) !== null) { |
35 | res.firewall = match_res[1]; | |
aff192e6 DM |
36 | } else { |
37 | errors = true; | |
38 | return false; // break | |
39 | } | |
40 | }); | |
41 | ||
42 | if (errors || !res.model) { | |
43 | return; | |
44 | } | |
45 | ||
46 | return res; | |
47 | }, | |
48 | ||
49 | printQemuNetwork: function(net) { | |
50 | ||
51 | var netstr = net.model; | |
52 | if (net.macaddr) { | |
53 | netstr += "=" + net.macaddr; | |
54 | } | |
55 | if (net.bridge) { | |
56 | netstr += ",bridge=" + net.bridge; | |
2cc2eaf2 DM |
57 | if (net.tag) { |
58 | netstr += ",tag=" + net.tag; | |
59 | } | |
a61b1ac9 AD |
60 | if (net.firewall) { |
61 | netstr += ",firewall=" + net.firewall; | |
62 | } | |
aff192e6 DM |
63 | } |
64 | if (net.rate) { | |
65 | netstr += ",rate=" + net.rate; | |
66 | } | |
67 | return netstr; | |
68 | }, | |
69 | ||
70 | parseQemuDrive: function(key, value) { | |
71 | if (!(key && value)) { | |
72 | return; | |
73 | } | |
74 | ||
75 | var res = {}; | |
76 | ||
77 | var match_res = key.match(/^([a-z]+)(\d+)$/); | |
78 | if (!match_res) { | |
79 | return; | |
80 | } | |
81 | res['interface'] = match_res[1]; | |
82 | res.index = match_res[2]; | |
83 | ||
84 | var errors = false; | |
85 | Ext.Array.each(value.split(','), function(p) { | |
86 | if (!p || p.match(/^\s*$/)) { | |
87 | return; // continue | |
88 | } | |
ed4ea4ce | 89 | var match_res = p.match(/^([a-z_]+)=(\S+)$/); |
aff192e6 DM |
90 | if (!match_res) { |
91 | if (!p.match(/\=/)) { | |
92 | res.file = p; | |
93 | return; // continue | |
94 | } | |
95 | errors = true; | |
96 | return false; // break | |
97 | } | |
98 | var k = match_res[1]; | |
99 | if (k === 'volume') { | |
100 | k = 'file'; | |
101 | } | |
102 | ||
103 | if (Ext.isDefined(res[k])) { | |
104 | errors = true; | |
105 | return false; // break | |
106 | } | |
107 | ||
108 | var v = match_res[2]; | |
0381b693 | 109 | |
aff192e6 DM |
110 | if (k === 'cache' && v === 'off') { |
111 | v = 'none'; | |
112 | } | |
113 | ||
114 | res[k] = v; | |
115 | }); | |
116 | ||
117 | if (errors || !res.file) { | |
118 | return; | |
119 | } | |
120 | ||
121 | return res; | |
122 | }, | |
123 | ||
124 | printQemuDrive: function(drive) { | |
125 | ||
126 | var drivestr = drive.file; | |
127 | ||
128 | Ext.Object.each(drive, function(key, value) { | |
0381b693 | 129 | if (!Ext.isDefined(value) || key === 'file' || |
aff192e6 DM |
130 | key === 'index' || key === 'interface') { |
131 | return; // continue | |
132 | } | |
133 | drivestr += ',' + key + '=' + value; | |
134 | }); | |
135 | ||
136 | return drivestr; | |
9021b40c DM |
137 | }, |
138 | ||
139 | parseOpenVZNetIf: function(value) { | |
140 | if (!value) { | |
141 | return; | |
142 | } | |
143 | ||
144 | var res = {}; | |
145 | ||
146 | var errors = false; | |
147 | Ext.Array.each(value.split(';'), function(item) { | |
148 | if (!item || item.match(/^\s*$/)) { | |
149 | return; // continue | |
150 | } | |
151 | ||
152 | var data = {}; | |
153 | Ext.Array.each(item.split(','), function(p) { | |
154 | if (!p || p.match(/^\s*$/)) { | |
155 | return; // continue | |
156 | } | |
4bfdc218 | 157 | var match_res = p.match(/^(ifname|mac|bridge|host_ifname|host_mac|mac_filter)=(\S+)$/); |
9021b40c DM |
158 | if (!match_res) { |
159 | errors = true; | |
160 | return false; // break | |
161 | } | |
0381b693 | 162 | if (match_res[1] === 'bridge'){ |
85c2c7b9 | 163 | var bridgevlanf = match_res[2]; |
0381b693 | 164 | var bridge_res = bridgevlanf.match(/^(vmbr(\d+))(v(\d+))?(f)?$/); |
85c2c7b9 AD |
165 | if (!bridge_res) { |
166 | errors = true; | |
167 | return false; // break | |
168 | } | |
169 | data['bridge'] = bridge_res[1]; | |
170 | data['tag'] = bridge_res[4]; | |
0381b693 DM |
171 | data['firewall'] = bridge_res[5] ? 1 : 0; |
172 | } else { | |
85c2c7b9 AD |
173 | data[match_res[1]] = match_res[2]; |
174 | } | |
9021b40c | 175 | }); |
aff192e6 | 176 | |
9021b40c DM |
177 | if (errors || !data.ifname) { |
178 | errors = true; | |
179 | return false; // break | |
180 | } | |
181 | ||
182 | data.raw = item; | |
183 | ||
184 | res[data.ifname] = data; | |
185 | }); | |
186 | ||
187 | return errors ? undefined: res; | |
188 | }, | |
189 | ||
190 | printOpenVZNetIf: function(netif) { | |
191 | var netarray = []; | |
192 | ||
193 | Ext.Object.each(netif, function(iface, data) { | |
194 | var tmparray = []; | |
85c2c7b9 | 195 | Ext.Array.each(['ifname', 'mac', 'bridge', 'host_ifname' , 'host_mac', 'mac_filter', 'tag', 'firewall'], function(key) { |
9021b40c | 196 | var value = data[key]; |
0381b693 | 197 | if (key === 'bridge'){ |
85c2c7b9 AD |
198 | if(data['tag']){ |
199 | value = value + 'v' + data['tag']; | |
200 | } | |
0381b693 | 201 | if (data['firewall']){ |
85c2c7b9 AD |
202 | value = value + 'f'; |
203 | } | |
85c2c7b9 | 204 | } |
9021b40c DM |
205 | if (value) { |
206 | tmparray.push(key + '=' + value); | |
207 | } | |
85c2c7b9 | 208 | |
9021b40c DM |
209 | }); |
210 | netarray.push(tmparray.join(',')); | |
211 | }); | |
212 | ||
213 | return netarray.join(';'); | |
640f8f14 DM |
214 | }, |
215 | ||
216 | parseStartup: function(value) { | |
217 | if (value === undefined) { | |
218 | return; | |
219 | } | |
220 | ||
221 | var res = {}; | |
222 | ||
223 | var errors = false; | |
224 | Ext.Array.each(value.split(','), function(p) { | |
225 | if (!p || p.match(/^\s*$/)) { | |
226 | return; // continue | |
227 | } | |
228 | ||
229 | var match_res; | |
230 | ||
231 | if ((match_res = p.match(/^(order)?=(\d+)$/)) !== null) { | |
232 | res.order = match_res[2]; | |
233 | } else if ((match_res = p.match(/^up=(\d+)$/)) !== null) { | |
234 | res.up = match_res[1]; | |
235 | } else if ((match_res = p.match(/^down=(\d+)$/)) !== null) { | |
236 | res.down = match_res[1]; | |
237 | } else { | |
238 | errors = true; | |
239 | return false; // break | |
240 | } | |
241 | }); | |
242 | ||
243 | if (errors) { | |
244 | return; | |
245 | } | |
246 | ||
247 | return res; | |
248 | }, | |
0381b693 | 249 | |
640f8f14 DM |
250 | printStartup: function(startup) { |
251 | var arr = []; | |
252 | if (startup.order !== undefined && startup.order !== '') { | |
253 | arr.push('order=' + startup.order); | |
254 | } | |
255 | if (startup.up !== undefined && startup.up !== '') { | |
256 | arr.push('up=' + startup.up); | |
257 | } | |
258 | if (startup.down !== undefined && startup.down !== '') { | |
259 | arr.push('down=' + startup.down); | |
260 | } | |
261 | ||
262 | return arr.join(','); | |
71b05bd6 DM |
263 | }, |
264 | ||
265 | parseQemuSmbios1: function(value) { | |
266 | var res = {}; | |
267 | ||
268 | Ext.Array.each(value.split(','), function(p) { | |
269 | var kva = p.split(/=/, 2); | |
270 | res[kva[0]] = kva[1]; | |
271 | }); | |
272 | ||
273 | return res; | |
274 | }, | |
275 | ||
276 | printQemuSmbios1: function(data) { | |
277 | ||
278 | var datastr = ''; | |
279 | ||
280 | Ext.Object.each(data, function(key, value) { | |
281 | if (value === '') return; | |
282 | datastr += (datastr !== '' ? ',' : '') + key + '=' + value; | |
283 | }); | |
284 | ||
285 | return datastr; | |
286 | }, | |
287 | ||
c5ebd41f DM |
288 | parseTfaConfig: function(value) { |
289 | var res = {}; | |
290 | ||
291 | Ext.Array.each(value.split(','), function(p) { | |
292 | var kva = p.split(/=/, 2); | |
293 | res[kva[0]] = kva[1]; | |
294 | }); | |
295 | ||
296 | return res; | |
297 | } | |
640f8f14 | 298 | |
aff192e6 | 299 | }}); |