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