source: hybrid/trunk/nanobsd/files/tools/wl-config@ 10172

Last change on this file since 10172 was 10172, checked in by richardvm, 13 years ago

I want gformat

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