-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.eselect
307 lines (226 loc) · 6.33 KB
/
net.eselect
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# -*-eselect-*- vim: ft=eselect
# This software is public domain
DESCRIPTION="Manage openrc networking service configuration"
MAINTAINER="ameretat.reith@gmail.com"
VERSION="0.1.0"
E_PATH="${EROOT%/}/etc/eselect/net"
E_CONF="$E_PATH/conf.d"
E_DEVS=$(canonicalise "$E_PATH/devs")
E_CONFDB="$E_PATH/db"
inherit config
find_configurations() {
find -L "$E_CONF" -type f -printf '%P\n'
}
checksum() {
[[ -r $1 ]] || die "Cannot access file $1"
md5sum -- "$1" | cut -f1 -d' '
}
file_ifacename() {
local iface=$(basename "$1")
echo "${iface#net\.}"
}
interface_config_file() {
[[ -z "$1" ]] && die "no interface provided"
local devname=${1#net\.}
local devrc="net.$devname"
local devconf="$EROOT/etc/conf.d/$devrc"
[[ ! -e $EROOT/etc/init.d/$devrc ]] && die "open-rc service $devrc does not exist"
if [[ -L $devconf ]]; then
if [[ $(dirname "$(canonicalise "$devconf")" ) != $E_DEVS ]]; then
echo "external_link"
else
echo "known,$(load_config "$E_CONFDB" "conf_$devname")"
fi
elif [[ -e $devconf ]]; then
echo "external"
else
echo "not_exist"
fi
}
delete_config() {
[[ -z "$1" ]] && die "No DB file provided"
[[ -z "$2" ]] && die "No key provided"
sed -i -e "/^$2=/d" $1
}
### show action ###
describe_show() {
echo "Show current interface configurations"
}
describe_show_parameters() {
echo "[iface]"
}
do_show() {
if [[ $# -eq 0 ]]; then
show_current_configs
elif [[ $# -eq 1 ]]; then
show_interface_config "$1" --brief
else
die -q "Too many parameters"
fi
}
show_current_configs() {
local iface
write_list_start "Network devices configurations:"
for f in "${EROOT}"/etc/init.d/net.*; do
iface=$(basename "$f")
show_interface_config ${iface#net\.}
done
}
show_interface_config() {
local iface_conf iface brief msg
iface=$1
[[ $2 == "--brief" ]] && brief=1
iface_conf=$(interface_config_file $iface)
case $iface_conf in
not_exist)
msg="not configured";;
external*)
msg="not configured by eselect";;
known,*)
msg="${iface_conf#known,}";;
*)
die "unknown interface_conf $iface_conf"
esac
[[ -n $brief ]] && echo "$msg" || write_kv_list_entry $iface "$msg"
}
### list action ###
describe_list() {
echo "List available configrations"
}
do_list() {
[[ -z "${@}" ]] || die -q "Too many parameters"
local i targets
targets=( $(find_configurations) )
for (( i = 0; i < ${#targets[@]}; i++ )); do
[[ ${targets[i]} = \
$(basename "$(canonicalise "${EROOT}/etc/eselect/net/conf.d")") ]] \
&& targets[i]=$(highlight_marker "${targets[i]}")
done
write_list_start "Available network configurations:"
write_numbered_list -m "(none found)" "${targets[@]}"
}
### make action ###
describe_make() {
echo "Make new configuration profile"
}
describe_make_parameters() {
echo "<name>"
}
do_make() {
[[ -z "$1" ]] && die -q "No name provided"
[[ -n "$2" ]] && die -q "Too many parameters"
[[ -e $E_CONF/$1 ]] && die -q "$E_CONF/$1 already exists"
cat >"$E_CONF/$1" <<EOF
# -*- gentoo-conf-d -*- vim: ft=gentoo-conf-d
#
# This is a netifrc network configuration. It will be processed to make config
# file. _iface_ in labels is a placeholder for interface, so modules__iface_
# will be modules_eth0 in produced config file.
EOF
}
### set action ###
describe_set() {
echo "Configure an interface to saved configuration"
}
describe_set_parameters() {
echo "<iface> <config>"
}
do_set() {
if [[ -z "${1}" ]]; then
die -q "No interface provided"
elif [[ -z "${2}" ]]; then
die -q "No configuration provided"
elif [[ -n "${3}" ]]; then
die -q "Too many parameters"
elif [[ ! -e "${EROOT}/etc/init.d/net.${1}" ]] ; then
die -q "There is no service for interface $1"
fi
local configs=( $(find_configurations) )
local config_file config devconf
[[ -n $config ]] && die -q "Invalid config $2"
if is_number $2; then
[[ $2 -ge 1 && $2 -le ${#configs[@]} ]] || die -q "Invalid number $2"
config=${configs[$2-1]}
[[ -z $config ]] && die -q "Invalid config choise $2"
else
config="$2"
fi
config_file="${E_CONF}/$config"
if [[ ! -e $config_file ]]; then
die -q "Configuration file does not exist"
fi
devconf=${EROOT}/etc/conf.d/net.${1}
if [[ -e $devconf ]]; then
if [[ ! -L $devconf || \
$(dirname "$(canonicalise "$devconf")" ) != $E_DEVS ]]; then
die -q "Configuration file for interface $1 exists but" \
"It's not created by eselect-net. Refused to do anything."
fi
fi
sed -e "s#^\(.*\)_iface_\(.*\)=#\1$1\2=#g" -- "$config_file" >"$E_DEVS/net.$1"
ln -sf -- "$E_DEVS/net.$1" "$devconf"
store_config "$E_CONFDB" "conf_$1" "$config"
store_config "$E_CONFDB" "checksum_$1" "$(checksum "$config_file")"
}
### unset action ###
describe_unset() {
echo "Remove configuration link"
}
describe_unset_parameters() {
echo "<iface>"
}
do_unset() {
[[ -z "${1}" ]] && die -q "No interface provided"
[[ -n "${2}" ]] && die -q "Too many parameters"
local devconf=${EROOT}/etc/conf.d/net.$1
if [[ -L $devconf &&
$(dirname "$(canonicalise "$devconf")" ) == $E_DEVS ]]; then
rm -vf -- "$(canonicalise $devconf)"
rm -v -- "$devconf"
delete_config "$E_CONFDB" "conf_$1"
delete_config "$E_CONFDB" "checksum_$1"
elif [[ -e $devconf ]]; then
die -q "$1 is not configured by eselect. Refused to do anything."
else
die -q "$1 is not configured."
fi
}
### check action ###
describe_check() {
echo "Check for interfaces using outdated configurations"
}
do_check() {
[[ $# -eq 0 ]] || die -q "Too many parameters"
write_list_start "Network configurations status:"
local devconf devname mainconf iface confchecksum savedchecksum
for devconf in "$E_DEVS"/net.*; do
iface=$(file_ifacename "$devconf")
mainconf=$(load_config "$E_CONFDB" "conf_$iface")
savedchecksum=$(load_config "$E_CONFDB" "checksum_$iface")
if [[ -z $mainconf ]]; then
die "Configuration template for $iface is not known"
elif [[ -z $savedchecksum ]]; then
die "Checksum of saved template for $iface is not known"
fi
confchecksum=$(checksum "$E_CONF/$mainconf")
if [[ $confchecksum == $savedchecksum ]]; then
write_kv_list_entry "$iface" "ok"
else
write_kv_list_entry "$iface" "outdated"
fi
done
}
### update action ###
describe_update() {
echo "Update interface configuration"
}
describe_update_parameters() {
echo "<iface>"
}
do_update() {
local config
if [[ -z "$1" ]]; then
die -q "No interface provided"
fi
do_set "$1" $(show_interface_config "$1" --brief)
}