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