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