From 0a80b89fdb01a15a1c601dc32c461713059834c9 Mon Sep 17 00:00:00 2001 From: dufourj Date: Sun, 5 Feb 2006 21:20:14 +0000 Subject: PC104 - script d'update : - gestion des messages ; - gestion des paramêtres ; - gestion de la compression. --- i/pc104/initrd/update.sh | 194 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 156 insertions(+), 38 deletions(-) diff --git a/i/pc104/initrd/update.sh b/i/pc104/initrd/update.sh index ae7b0cf..1fe8c58 100755 --- a/i/pc104/initrd/update.sh +++ b/i/pc104/initrd/update.sh @@ -1,7 +1,7 @@ #!/bin/bash # Script for updating the marvin program in the initrd. -set -e +#set -e set -u # Variables - Default values @@ -14,25 +14,95 @@ ROBOT_START_SCRIPT="start.sh" # Build directory ROBOT_BUILD_DIR="." # Config directory -ROBOT_CONF_DIR="$ROBOT_BUILD_DIR/../runtime" +ROBOT_CONF_DIR="$ROBOT_BUILD_DIR/rc" # Max size of the config directory (in bytes) -ROBOT_CONF_MAX_SIZE="100000" +ROBOT_MAX_SIZE="4000000" # Hostname of the robot ROBOT_HOST="nenuphar" # In autonomous mode, wait time for bringing up the ssh deamon ROBOT_WAIT_TIME="3s" +# Compression +ROBOT_NOCOMPRESS=0 +# Quiet ? +ROBOT_QUIET=0 +# SSH options +ROBOT_SSH_OPTS="-o \"ConnectionAttempts 4\" -o \"ConnectTimeout 1\"" + +if [ $# -gt 6 ] +then + usage +fi +while getopts "b:s:r:zqh" option +do + case $option in + b) ROBOT_BUILD_DIR="$OPTARG";; +# d) ROBOT_LOCAL_DIR="$OPTARG";; + s) ROBOT_CONF_MAX_SIZE="$OPTARG";; + r) ROBOT_HOST="$OPTARG";; + z) ROBOT_NOCOMPRESS=1;; + q) ROBOT_QUIET=1;; + h) help_initrd;; + \?) help_initrd;; + esac +done + +# Log function +# First parameter : "error message" +# Seconde parameter : "level" +log () +{ + if (( $ROBOT_QUIET == 0 )); then + log_prefix="[II]" + log_line=1 + if [ $# -eq 2 ]; then + case $2 in + 1) log_prefix="[II]";; + 2) log_prefix=" >>>";; + 3) log_line=0;; + *) log_prefix="[II]";; + esac + fi + if (( $log_line == 1 )); then + echo -en "$log_prefix $1 : " + else + echo -e "$1" + fi + fi +} + +# Log error function +# First parameter : "error message" +log_error () +{ + log "Error" 3 + echo -e "[EE] $1" >&2 +} + +# Usage - Help +usage () +{ + echo "Usage: $0 [-b ] [-s ] [-r ] [-z]" + echo " -b : build directory of marvin" + echo " -s : size max of binaries and config (in bytes)" + echo " -r : robot host" + echo " -z : disable compression" + echo " -q : be quiet (expect for errors)" + echo " -h : this message" + exit +} check_rsync () { if [[ "x$(whereis rsync | cut -d ':' -f 2)" == "x" ]] then - echo "Please install rsync" + log_error "Please install rsync" exit 1 fi } # Check the structure (directory robot and startup script) check_create_structure () { + log "Checking directory structure" 1 # Check the local directory exist if [[ ! -d $ROBOT_LOCAL_DIR ]] then @@ -50,19 +120,21 @@ check_create_structure () EOF chmod +x $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT # Need to tune it by the user - echo "Please tune your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT." + log_error "Please tune your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT" # XXX vim it ? exit 2 fi + log "Ok" 3 } # Check that there is a line in the startup script for launchin a program and # a binary existing check_install_binary () { + log "Checking binary and libraries" 1 if [[ $(grep -c "^\\./" $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT) -eq 0 ]] then - echo "No command found in your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT." + log_error "No command found in your $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT" exit 3 fi for line in "$(grep "^\\./" $ROBOT_LOCAL_DIR/$ROBOT_START_SCRIPT)" @@ -70,7 +142,7 @@ check_install_binary () bin="$(echo "$line" | cut -d ' ' -f 1)" if [[ ! -x $ROBOT_BUILD_DIR/$bin ]] then - echo "Binary not found : $bin." + log_error "Binary not found : $bin" exit 4 else # Copy the binary to the local dir @@ -84,80 +156,126 @@ check_install_binary () done fi done + log "Ok" 3 } # Install the configuration check_install_config () { - # First we check the size - if [[ $(du -bs --exclude=.svn $ROBOT_CONF_DIR | cut -f 1) -gt $ROBOT_CONF_MAX_SIZE ]] + log "Checking configuration ($ROBOT_CONF_DIR)" 1 + if [ ! -d $ROBOT_CONF_DIR ] then - echo "Config directory ($ROBOT_CONF_DIR) is too big." + log_error "Configuration directory not found ($ROBOT_CONF_DIR)" exit 5 fi # There is no way to know the files needed, so we rsync everything - rsync -aq --exclude=.svn $ROBOT_CONF_DIR/ $ROBOT_LOCAL_DIR + rsync -azLu --exclude=.svn $ROBOT_CONF_DIR $ROBOT_LOCAL_DIR/ + log "Ok" 3 +} + +check_size () +{ + log "Checking size" 1 + # First we check the size + if [[ $(du -bs $ROBOT_LOCAL_DIR | cut -f 1) -gt $ROBOT_MAX_SIZE ]] + then + log_error "$ROBOT_LOCAL_DIR is too big (> $ROBOT_MAX_SIZE)" + exit 6 + fi + log "Ok" 3 } upload_data () { + log "Uploading data to $ROBOT_HOST" 1 + log "" 3 + log "Checking $ROBOT_HOST is up" 2 # Check if host exist and is up - echo "Ping" ping -c 1 -w 1 -q $ROBOT_HOST > /dev/null case $? in 0) # Robot is online ! + log "Ok" 3 ;; 1) # Host exist but is offline - echo "$ROBOT_HOST is currently down." + log_error "$ROBOT_HOST is currently down" exit 6 ;; 2) # Host does not exist - echo "$ROBOT_HOST does not exist." + log_error "$ROBOT_HOST does not exist" exit 7 ;; *) # Unknown error... - echo "ping failed, unknown error." + log_error "Ping failed, unknown error" exit 8 ;; esac - echo "nc" + + ROBOT_AUTONOMOUS=0 # Are we in automous mode ? - if [[ $(nc -w 1 $ROBOT_HOST 1234) -eq 0 ]] + log "Autonomous mode" 2 + nc -w 1 $ROBOT_HOST 1234 > /dev/null 2>&1 + if [[ $? -eq 0 ]] then + log "Yes" 3 + ROBOT_AUTONOMOUS=1 sleep $ROBOT_WAIT_TIME + else + log "No" 3 fi + # Mount the initrd - echo "Mounting" - # TODO Best manage compressed initrd or not -# && gunzip -c /initrd/last > /tmp/initrd_cur \ - ssh -o "ConnectTimeout 1" root@$ROBOT_HOST "mount -oremount,rw / \ - && cp /initrd/last /tmp/initrd_cur \ - && mount -o loop /tmp/initrd_cur /mnt/initrd" - # Rsyncer - echo "rsync" - # TODO Remove old files ! - rsync -av $ROBOT_LOCAL_DIR/ root@$ROBOT_HOST:/mnt/initrd/$ROBOT_REMOTE_DIR/ - echo "umount" + log "Mouting the initrd" 2 + + if (( $ROBOT_AUTONOMOUS == 1 )); then + ssh $ROBOT_SSH_OPTS root@$ROBOT_HOST "\ + mount -oremount,rw / && \ + gunzip -f -c /initrd/last > /tmp/initrd_cur && \ + mount -o loop /tmp/initrd_cur /mnt/initrd" + else + false + # TODO not implemented yet + fi + log "Ok" 3 + + # Rsync data + log "Rsyncing data" + rsync -au $ROBOT_LOCAL_DIR/ root@$ROBOT_HOST:/mnt/initrd/$ROBOT_REMOTE_DIR/ + log "Ok" 3 + # Démonter et recompresser l'initrd voir rotater tout - # TODO Best manage compressed initrd or not # TODO Backup, rotation, et less cp ! -# && gzip -f -9 -c /tmp/initrd_cur > /initrd/initrd1.gz\ - ssh -o "ConnectTimeout 1" root@$ROBOT_HOST "umount /mnt/initrd \ - && cp /tmp/initrd_cur /initrd/initrd1.gz \ - && sync \ - && mount -oremount,ro /" -# TODO Change between NFS/Autonomous... -# && kill -SIGTERM 1" + log "Umounting the initrd and move it" 2 + if (( $ROBOT_AUTONOMOUS == 1 )); then + if (( $ROBOT_NOCOMPRESS == 1 )); then + ssh $ROBOT_SSH_OPTS root@$ROBOT_HOST "\ + umount /mnt/initrd && \ + mv /tmp/initrd_cur /initrd/initrd1.gz && \ + sync && \ + mount -oremount,ro / && \ + kill -SIGTERM 1" + else + # XXX Remove /tmp/initrd_cur after compression ? + ssh $ROBOT_SSH_OPTS root@$ROBOT_HOST "\ + umount /mnt/initrd && \ + gzip -f -1 -c /tmp/initrd_cur > /initrd/initrd1.gz && \ + sync && \ + mount -oremount,ro / && \ + kill -SIGTERM 1" + fi + else + # TODO + false + fi + log "Ok... Rebooting" 3 } -# TODO Parameter - check_rsync check_create_structure check_install_binary check_install_config +check_size upload_data -- cgit v1.2.3