snf-image-update-helper.in 4.73 KB
Newer Older
1 2
#!/bin/bash

3
# Copyright (C) 2011 GRNET S.A.
Nikos Skalkotos's avatar
Nikos Skalkotos committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

20
set -e
21
set -o errtrace
22 23 24

. @osdir@/common.sh

25 26 27 28 29 30 31 32
DEFAULT_FILE="@sysconfdir@/default/snf-image-update-helper"

if [ -f "$DEFAULT_FILE" ]; then
    . "$DEFAULT_FILE"
fi

: ${HELPER_URL:="@HELPER_URL@"}

33 34 35 36 37 38 39 40
usage() {
    local rc="$1"

    cat <<EOF

Usage: $0 [options]

This script will download a suitable fixed snf-image-helper image and will
41
install it under $HELPER_DIR.
42 43

OPTIONS:
44 45 46 47
    -c Don't check the checksum of the helper image

    -f Force the installation even if the snf-image and helper versions don't match

48 49
    -h Print this message

50 51 52
    -u URL
       Download URL to use for the helper image instead of $HELPER_URL

53 54 55 56 57 58 59
    -y Assume Yes to all queries and do not prompt

EOF

    exit "$rc"
}

60
while getopts "hu:yf" opt; do
61
    case $opt in
62 63 64 65
        c) NO_CHECKSUM="yes"
            ;;
        f) FORCE="yes"
            ;;
66 67
        h) usage 0
            ;;
68 69
        u) HELPER_URL="$OPTARG"
            ;;
70 71 72 73 74 75 76 77
        y) NO_PROMPT="yes"
            ;;
        ?) log_error "User \`-h' for help"; exit 1
            ;;
    esac
done

cat >&1 <<EOF
78

79
$(basename $0) will download a snf-image-helper image from:
80
\`$HELPER_URL' and will install it under $HELPER_DIR.
81
The following files will be overwritten if present:
82 83 84 85
  \`$HELPER_DIR/initrd'
  \`$HELPER_DIR/kernel'
  \`$HELPER_DIR/image'
  \`$HELPER_DIR/packages'
86
  \`$HELPER_DIR/version'
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
EOF

while [[ 1 ]]; do
    echo -n "Do you want to continue [y/N]? "
    if [ "x$NO_PROMPT" = "xyes" ]; then
        echo "y";
        break;
    fi

    read answer
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
        log_error "Abort."
        exit 1
    fi
done

104 105
IMAGE_DIR=$(mktemp -d)
add_cleanup rmdir "$IMAGE_DIR"
106

107
cd "$IMAGE_DIR"
108

109
IMAGE=$(basename "$HELPER_URL")
110 111
echo >&2
echo "Downloading helper image from $HELPER_URL ..." >&2
112
add_cleanup rm -f "$IMAGE_DIR/$IMAGE"
113
set +e
114
$CURL -O -L -f "$HELPER_URL"
115 116 117 118 119 120 121 122 123 124 125
RET=$?
set -e

# Treat SIGPIPE as SUCCESS. There is a cURL bug that may trigger SIGPIPE even
# if the download was successful:
# http://sourceforge.net/p/curl/bugs/1180/
if [ $RET -ne 0 -a $RET -ne 141 ]; then
    log_error "Downloading "$IMAGE" from "$HELPER_URL" failed"
    exit 1
fi

126 127 128 129 130 131 132 133 134 135 136 137 138
if [ "x$NO_CHECKSUM" != "xyes" ]; then
    echo "Checking the helper image checksum ..." >&2
    add_cleanup rm -f "$IMAGE_DIR/$IMAGE.md5sum"
    set +e
    $CURL -O -L -f "$HELPER_URL.md5sum"
    RET=$?
    set -e

    # See above for an explanation
    if [ $RET -ne 0 -a $RET -ne 141 ]; then
        log_error "Downloading "$IMAGE.md5sum" from "$HELPER_URL.md5sum" failed"
        exit 1
    fi
139

140 141 142 143 144 145 146 147 148 149
    # The filename listed inside the md5sum file may be different than the one
    # of the image we have downloaded. This is due to redirections and the fact
    # that we are not using --remote-header-name which would use the
    # server-specified Content-Disposition filename instead of extracting a
    # filename from the URL. This option is not used because some versions of
    # curl will not attempt to decode %-sequences which may lead to unexpected
    # filenames. It's better if we just replace the filename inside the md5sum
    # file with the one we know.
    sed -i 's/ .\+$/'" $IMAGE"'/' "$IMAGE_DIR/$IMAGE.md5sum"

150
    $MD5SUM -c "$IMAGE_DIR/$IMAGE.md5sum"
151 152
fi

153
cd "$HELPER_DIR"
154

155 156
echo >&2
echo "Extracting helper image under \`$HELPER_DIR':" >&2
157
tar -xvf "$IMAGE_DIR/$IMAGE"
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
if [ "x$FORCE" != "xyes" ]; then
    echo >&2
    echo "Checking helper image version ..." >&2
    if [ ! -f "$HELPER_DIR/version" ]; then
        log_error "File: \`$HELPER_DIR/version' is missing!"
        exit 1
    fi
    helper_version="$(cat $HELPER_DIR/version)"
    if [ "x$SNF_IMAGE_VERSION" != "x$helper_version" ]; then
        log_error "snf-image version (=$SNF_IMAGE_VERSION) and " \
            "helper image version (=$helper_version) don't match!"
        log_error "Use \`-f' to bypass the version check."
        exit 1
    fi
fi

175 176
echo >&2
echo "Helper image was installed successfully!" >&2
177 178 179
exit 0

# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :