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