source: hybrid/branches/releng-9.0/nanobsd/files/tools/wl-config@ 10718

Last change on this file since 10718 was 10704, checked in by rick, 13 years ago

Let the redirected be controlled by gformat (yaml).

Related-To: nodefactory#152

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
File size: 7.0 KB
Line 
1#!/bin/sh
2# Wireless Leiden config-update script for FreeBSD 8.0 (nanobsd)
3# Based on the 'API' of Jasper
4# Rick van der Zwet
5# XXX: TODO, some proper error checking for fetch
6
7
8# Slow connection = no connection
9export HTTP_TIMEOUT=5
10
11
12check_access() {
13 # Direct Access - Internal IP
14 BASEURL="http://172.16.4.46/wleiden/config/"
15 # Connectivity check
16 fetch -o /dev/null -q $BASEURL > /dev/null && return
17 echo "# WARN: Fetch via internal $BASEURL failed"
18
19 # Direct Access - External DNS
20 BASEURL="http://132.229.112.21/wleiden/config/"
21 fetch -o /dev/null -q $BASEURL > /dev/null && return
22 echo "# CRIT: Fetch via external $BASEURL failed"
23
24 exit 1
25}
26check_access
27
28
29# Default config to fetch
30CONFIG=`hostname -s`
31
32# Determine it's statup and running location and some other hints
33# Skip named.conf as it not planned in current release
34FILES="authorized_keys dhcpd.conf dnsmasq.conf motd rc.conf.local resolv.conf pf.hybrid.conf.local wleiden.yaml"
35file_details() {
36 POST_CMD=""
37 FILE_HINT=""
38
39 case "$1" in
40 'authorized_keys')
41 STARTUP_LOC="/cfg/ssh/${FILE}"
42 RUNNING_LOC="/etc/ssh/${FILE}"
43 ;;
44 'dhcpd.conf')
45 STARTUP_LOC="/cfg/local/${FILE}"
46 RUNNING_LOC="/etc/local/${FILE}"
47 FILE_HINT="service isc-dhcpd restart"
48 ;;
49 'dnsmasq.conf')
50 STARTUP_LOC="/cfg/local/${FILE}"
51 RUNNING_LOC="/etc/local/${FILE}"
52 FILE_HINT="service dnsmasq restart"
53 ;;
54 'motd')
55 STARTUP_LOC="/cfg/${FILE}"
56 RUNNING_LOC="/etc/${FILE}"
57 POST_CMD="/etc/rc.d/motd onestart"
58 ;;
59 'named.conf')
60 STARTUP_LOC="/cfg/namedb/${FILE}"
61 RUNNING_LOC="/etc/namedb/${FILE}"
62 FILE_HINT="service named restart"
63 ;;
64 'rc.conf.local')
65 STARTUP_LOC="/cfg/${FILE}"
66 RUNNING_LOC="/etc/${FILE}"
67 FILE_HINT="service netif restart"
68 ;;
69 'resolv.conf')
70 STARTUP_LOC="/cfg/${FILE}"
71 RUNNING_LOC="/etc/${FILE}"
72 ;;
73 'pf.hybrid.conf.local')
74 STARTUP_LOC="/cfg/${FILE}"
75 RUNNING_LOC="/etc/${FILE}"
76 POST_CMD="service pf reload"
77 ;;
78 'wleiden.yaml')
79 STARTUP_LOC="/cfg/local/${FILE}"
80 RUNNING_LOC="/etc/local/${FILE}"
81 ;;
82 esac
83}
84
85usage() {
86 (
87 echo "Usage: $0 [-bn] [-c <config>] [-m <all|startup|testing|running>]"
88 echo " -b = batch mode, no user input"
89 echo " -c <config> = default configuration to fetch"
90 echo " -n = do not mount config partition"
91 echo " -m all = copy config files to running & config partition [default]"
92 echo " -m startup = copy config files to config partition"
93 echo " -m testing = do not copy config files"
94 echo " -m running = copy config files to running partition"
95 echo " -m hack = copy running files to config partition"
96 ) 1>&2
97 exit 2
98}
99
100# Argument parsing using getopts
101USE_API=1 # Whether or not to use the webinterface
102OPT_MOUNT=1
103OPT_RUNNING=1
104OPT_STARTUP=1
105OPT_HACK=0 # Hack for people without configuration managment and testing
106OPT_BATCH=0
107
108parse_options() {
109 while getopts "bc:nm:" OPT; do
110 case "$OPT" in
111 b) OPT_BATCH=1;;
112 c) CONFIG="${OPTARG}";;
113 n) OPT_MOUNT=0;;
114 m) case "$OPTARG" in
115 all) true;;
116 live) OPT_STARTUP=0;;
117 startup) OPT_RUNNING=0;;
118 testing) OPT_RUNNING=0; OPT_STARTUP=0; OPT_MOUNT=0;;
119 hack) OPT_RUNNING=0; OPT_STARTUP=0; OPT_HACK=1; USE_API=0;;
120 *) usage;;
121 esac;;
122 h) usage;;
123 \?) usage;;
124 esac
125 done
126 # Allow to override automatic mounting, in case of external mount 'managment'
127 if [ "$1" = "-n" ]; then
128 OPT_MOUNT=0
129 fi
130
131 if [ "${OPT_RUNNING}" -eq 1 ]; then
132 echo "# INFO: Storing new config files in running configuration"
133 fi
134
135 if [ "${OPT_STARTUP}" -eq 1 ]; then
136 echo "# INFO: Storing new config files in startup configuration"
137 fi
138
139 if [ "${OPT_HACK}" -eq 1 ]; then
140 echo "# WARN: Copy running configuration to startup configuration"
141 echo "# WARN: Please do mind to document/mention this changes somewhere"
142 fi
143
144 # New line before the real work gets started
145 echo ""
146}
147
148
149
150
151# test validity of input
152config_validator() {
153 INPUT="$1"
154 `grep -q "^${INPUT}\$" ${TMPDIR}/node_list.txt`
155 if [ $? -eq 0 ]; then
156 return 0
157 else
158 echo "WARNING: Input '${INPUT}' is not valid, some hints..."
159 grep -i "${INPUT}" ${TMPDIR}/node_list.txt
160 return 1
161 fi
162}
163
164
165
166select_node() {
167 # List of all available nodes
168 fetch -q -o ${TMPDIR}/node_list.txt ${BASEURL} || exit 1
169
170 if [ ${OPT_BATCH} -eq 1 ]; then
171 config_validator "${CONFIG}"
172 if [ $? -eq 1 ]; then
173 echo "ERROR: Please provide valid config" 1>&2
174 exit 1
175 fi
176 else
177 # Provide Nodelist and feedback
178 cat ${TMPDIR}/node_list.txt | column
179 echo ' THIS script adds the config from GENESIS to this operating system'
180 echo ' make sure you know what you are doing, if not press control-C'
181 echo ' ENTER CONFIG NAME ......(and press enter)'
182
183 # Have the user to select the right node
184 INVALID_CONFIG=1
185 while [ ${INVALID_CONFIG} -eq 1 ]; do
186 # Ask for node name, play around with prev option
187 echo -n "Name [${CONFIG}]: "
188 read INPUT
189 if [ -z "${INPUT}" ]; then
190 INPUT=${CONFIG}
191 else
192 CONFIG=${INPUT}
193 fi
194
195 config_validator "${INPUT}"
196 if [ $? -eq 0 ]; then
197 INVALID_CONFIG=0
198 fi
199 done
200 fi
201}
202
203
204
205
206# Copy file, saving some bits if no change needed
207copy_file() {
208 SOURCE=$1
209 TARGET=$2
210 diff -I '^FreeBSD ' -I '^# Generated at ' ${TARGET} ${SOURCE} 2>/dev/null
211 if [ $? -ne 0 ]; then
212 mkdir -p `dirname ${TARGET}` || exit 1
213 cp ${SOURCE} ${TARGET} || exit 1
214 return $?
215 fi
216 return 1
217}
218
219# Main function
220main() {
221 TMPDIR=`mktemp -d -t $(basename $0)`
222 # Clear out tempdir when done
223 if [ ${OPT_MOUNT} -eq 1 ]; then
224 trap "rm -Rf ${TMPDIR}; umount /cfg; mount -ro noatime /; exit" 0 1 2 3 15
225 else
226 trap "rm -Rf ${TMPDIR}; exit" 0 1 2 3 15
227
228 fi
229
230 # Mount if requested
231 if [ ${OPT_MOUNT} -eq 1 ]; then
232 mount -uwo noatime /
233 mount /cfg
234 fi
235
236 # Select node from web-interface
237 if [ ${USE_API} -eq 1 ]; then
238 select_node
239 fi
240
241 # Worker, place all files in required directory
242 for FILE in ${FILES}; do
243 if [ ${USE_API} -eq 1 ]; then
244 # Fetch needed file
245 FRESH_LOC=${TMPDIR}/${FILE}
246 fetch -q -o ${FRESH_LOC} ${BASEURL}/${CONFIG}/${FILE} || exit 1
247 fi
248
249 # Needed file details, like locations and hints
250 file_details ${FILE}
251
252 echo "# INFO: Working on file: '${FILE}'"
253 # Copy file boot location
254 if [ ${OPT_STARTUP} -eq 1 ]; then
255 copy_file ${FRESH_LOC} ${STARTUP_LOC}
256 fi
257
258 # Copy file running location
259 if [ ${OPT_RUNNING} -eq 1 ]; then
260 copy_file ${FRESH_LOC} ${RUNNING_LOC}
261 if [ $? -eq 0 ]; then
262 echo "# INFO: '${FILE}' changed"
263 if [ -n "${POST_CMD}" ]; then
264 echo "## Running post_cmd: $POST_CMD"
265 $POST_CMD
266 fi
267 if [ -n "${FILE_HINT}" ]; then
268 echo "# INFO: For instant activate: ${FILE_HINT}"
269 echo ""
270 fi
271 fi
272 fi
273
274 # Direct copy
275 if [ ${OPT_HACK} -eq 1 ]; then
276 # No checking, just dumb try to copy mode
277 cp -v ${RUNNING_LOC} ${STARTUP_LOC}
278 fi
279 done
280
281 exit 0
282}
283
284parse_options $*
285main
Note: See TracBrowser for help on using the repository browser.