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