1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]
local M = require('acf2.model')
local posix = require('posix')
local Host = M.new()
Host.address = M.net.IPAddress{addr='ipaddr'}
Host.canonical = M.String{ui_name='Canonical name'}
Host.alias = M.Set{
type=M.String,
addr='alias/#',
ui_name='Aliases',
ui_member='Alias',
detail=false
}
local Resolv = M.new()
Resolv.servers = M.List{
type=M.net.IPAddress, addr='nameserver/#', widget='inline'
}
Resolv.search_domains = M.List{
type=M.String, addr='search/domain/#', widget='inline'
}
local iface_aug_addr = '/augeas/etc/network/interfaces'
local iface_sys_dir = '/sys/class/net'
local vlan_pattern = '^(.+)%.(%d+)$'
for _, trigger in ipairs{
{phase='pre', action='stop'}, {phase='post', action='start'}
} do
M.trigger(
trigger.phase,
iface_aug_addr,
function() os.execute('rc-service networking '..trigger.action) end
)
end
M.defer(iface_aug_addr)
-- TODO allow multiple addresses of same family
local IPv4 = M.new()
IPv4.method = M.String{
required=true,
choice={
{'dhcp', 'DHCP'},
{'loopback', enabled=false},
{'unconfigured', be_value='manual'},
'static'
},
default='unconfigured'
}
IPv4.address = M.net.IPv4Address{
condition={method='static'}, required=true, cidr=true, mask_addr='netmask'
}
IPv4.gateway = M.net.IPv4Address{condition={method='static'}}
local IPv6 = M.new()
IPv6.method = M.String{
required=true,
choice={
{'loopback', enabled=false}, {'unconfigured', be_value='manual'}, 'static'
},
default='unconfigured'
}
IPv6.address = M.net.IPv6Address{
condition={method='static'}, required=true, cidr=true, mask_addr='netmask'
}
IPv6.gateway = M.net.IPv6Address{condition={method='static'}}
local Interface = M.new()
function Interface:validate()
if self.status == 'attached' then
for _, version in ipairs{4, 6} do
self['ipv'..version].method =
self.class == 'loopback' and 'loopback' or 'unconfigured'
end
end
end
function Interface:is_removable() return self.class == 'logical' end
function Interface:auto_set()
for _, set in M.node.ipairs(self:fetch('../../enabled-ifaces')) do
if M.node.contains(set, self) then return set end
end
end
function Interface:auto_vlan_tag()
local name = M.node.name(self)
local _, tag = name:match(vlan_pattern)
if tag then return tag end
return name:match('^vlan(%d+)$')
end
Interface.enabled = M.Boolean{
compute=function(self, iface) return iface:auto_set() and true or false end,
store=function(self, iface, value)
local set = iface:auto_set()
if value and not set then
M.node.insert(iface:fetch('../../enabled-ifaces/1'), iface)
elseif not value and set then set[iface] = nil end
end
}
Interface.class = M.String{
compute=function(self, iface, txn)
local name = M.node.name(iface)
if name == 'lo' then return 'loopback' end
local saddr = M.path.rawjoin('/files', iface_sys_dir, name, '.')
if not txn:get(saddr) then return 'logical' end
for _, addr in ipairs{
{saddr, 'bonding'},
{saddr, 'bridge'},
{'/files/proc/net/vlan', name}
} do
if txn:get(M.path.join(unpack(addr))) then
return 'logical'
end
end
return 'physical'
end,
choice={'loopback', 'logical', 'physical'}
}
Interface.type = M.String{
condition={class='logical'},
compute=function(self, iface)
if #iface.slaves > 0 then return 'bond' end
if #iface.ports > 0 then return 'bridge' end
if iface.vlan_tag then return 'vlan' end
end,
editable=function(self, iface) return not iface:auto_vlan_tag() end,
required=true,
choice={'bond', 'bridge', {'vlan', 'VLAN'}}
}
Interface.status = M.String{
visible=false,
compute=function(self, obj)
if obj.class == 'loopback' then return 'attached' end
for _, iface in M.node.pairs(M.node.parent(obj)) do
if (
iface.type == 'bond' and M.node.contains(iface.slaves, obj)
) or (
iface.type == 'bridge' and M.node.contains(iface.ports, obj)
) then
return 'attached'
end
if iface.type == 'vlan' and iface.trunk == obj then
return 'configured'
end
end
for _, version in ipairs{4, 6} do
if obj['ipv'..version].method ~= 'unconfigured' then
return 'configured'
end
end
return 'detached'
end
}
Interface.slaves = M.Set{
condition={type='bond'},
type=M.Reference{scope='../..', filter={status='detached'}},
required=true,
addr='@family/link/@method/none/bond-slaves'
}
Interface.ports = M.Set{
condition={type='bridge'},
type=M.Reference{scope='../..', filter={status='detached'}},
required=true,
addr='@family/link/@method/none/bridge-ports'
}
-- TODO do not allow VLAN creation for non-existent interfaces
Interface.trunk = M.Reference{
condition={type='vlan'},
compute=function(self, iface)
local trunk = M.node.name(iface):match(vlan_pattern)
if trunk and iface:fetch('..')[trunk] then return trunk end
end,
required=true,
scope='..',
filter={status={'detached', 'configured'}},
addr='@family/link/@method/none/vlan-raw-device'
}
-- TODO ensure that (trunk, tag) is unique
Interface.vlan_tag = M.Integer{
condition={type='vlan'},
compute='auto_vlan_tag',
required=true,
min=0,
max=4095,
addr='@family/link/@method/none/vlan-id',
ui_name='VLAN tag'
}
Interface.ipv4 = M.Model{
model=IPv4,
condition={status={'detached', 'configured'}},
create=true,
addr='@family/inet',
ui_name='IPv4 configuration',
widget='inline'
}
Interface.ipv6 = M.Model{
model=IPv6,
condition={status={'detached', 'configured'}},
create=true,
addr='@family/inet6',
ui_name='IPv6 configuration',
widget='inline'
}
Interface.stats = M.Collection{
type=M.Number{editable=false},
editable=false,
addr=function(path)
return M.path.join(
'/files/sys/class/net', M.path.name(path), 'statistics'
)
end,
ui_name='Statistics',
ui_member='',
widget='inline'
}
local Net = M.new()
Net.host_name = M.String{addr='/augeas/etc/hostname/hostname'}
Net.hosts = M.List{type=Host, addr='/augeas/etc/hosts'}
Net.resolver = M.Model{
model=Resolv, addr='/augeas/etc/resolv.conf', ui_name='DNS resolver'
}
Net.enabled_ifaces = M.List{
type=M.Set{type=M.Reference{scope='../../interfaces', on_delete='set-null'}},
visible=false,
addr=iface_aug_addr..'/auto/#'
}
Net.interfaces = M.Collection{
type=Interface, addr=iface_aug_addr..'/iface/@', widget='inline'
}
M.register('net', Net, {ui_name='Network'})
M.permission.defaults('/net')
return function(txn)
local ifaces = txn:fetch('/net/interfaces')
for _, name in ipairs(posix.dir(iface_sys_dir)) do
if not ifaces[name] and posix.stat(
M.path.join(iface_sys_dir, name), 'type'
) == 'link' then
ifaces[name] = {}
if ifaces[name].class == 'logical' then ifaces[name] = nil
else
for _, version in ipairs{4, 6} do
ifaces[name]['ipv'..version].method = 'unconfigured'
end
end
end
end
end
|