docs: Extend the usage examples
[snf-image] / snf-image-host / snf-image-update-helper.in
1 #!/bin/bash
2
3 # Copyright (C) 2011 GRNET S.A.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 set -e
21 set -o errtrace
22
23 . @osdir@/common.sh
24
25 DEFAULT_FILE="@sysconfdir@/default/snf-image-update-helper"
26
27 if [ -f "$DEFAULT_FILE" ]; then
28     . "$DEFAULT_FILE"
29 fi
30
31 : ${HELPER_URL:="@HELPER_URL@"}
32
33 usage() {
34     local rc="$1"
35
36     cat <<EOF
37
38 Usage: $0 [options]
39
40 This script will download a suitable fixed snf-image-helper image and will
41 install it under $IMAGE_DIR.
42
43 OPTIONS:
44     -h Print this message
45
46     -u URL
47        Download URL to use for the helper image instead of $HELPER_URL
48
49     -y Assume Yes to all queries and do not prompt
50
51     -f Force the installation even if the snf-image and helper versions don't match
52
53 EOF
54
55     exit "$rc"
56 }
57
58 while getopts "hu:yf" opt; do
59     case $opt in
60         h) usage 0
61             ;;
62         u) HELPER_URL="$OPTARG"
63             ;;
64         y) NO_PROMPT="yes"
65             ;;
66         f) FORCE="yes"
67             ;;
68         ?) log_error "User \`-h' for help"; exit 1
69             ;;
70     esac
71 done
72
73 cat >&1 <<EOF
74
75 $(basename $0) will download a snf-image-helper image from:
76 \`$HELPER_URL' and will install it under $IMAGE_DIR.
77 The following files will be overwritten if present:
78   \`$HELPER_DIR/initrd'
79   \`$HELPER_DIR/kernel'
80   \`$HELPER_DIR/image'
81   \`$HELPER_DIR/packages'
82   \`$HELPER_DIR/version'
83 EOF
84
85 while [[ 1 ]]; do
86     echo -n "Do you want to continue [y/N]? "
87     if [ "x$NO_PROMPT" = "xyes" ]; then
88         echo "y";
89         break;
90     fi
91
92     read answer
93     [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
94     if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
95         log_error "Abort."
96         exit 1
97     fi
98 done
99
100 IMAGE_DIR=$(mktemp -d)
101 add_cleanup rmdir "$IMAGE_DIR"
102
103 cd "$IMAGE_DIR"
104
105 IMAGE=$(basename "$HELPER_URL")
106 echo >&2
107 echo "Downloading helper image from $HELPER_URL ..." >&2
108 $CURL -O -L -f "$HELPER_URL"
109 add_cleanup rm -f "$IMAGE_DIR/$IMAGE"
110
111 cd "$HELPER_DIR"
112
113 echo >&2
114 echo "Extracting helper image under \`$HELPER_DIR':" >&2
115 tar -xvf "$IMAGE_DIR/$IMAGE"
116
117 if [ "x$FORCE" != "xyes" ]; then
118     echo >&2
119     echo "Checking helper image version ..." >&2
120     if [ ! -f "$HELPER_DIR/version" ]; then
121         log_error "File: \`$HELPER_DIR/version' is missing!"
122         exit 1
123     fi
124     helper_version="$(cat $HELPER_DIR/version)"
125     if [ "x$SNF_IMAGE_VERSION" != "x$helper_version" ]; then
126         log_error "snf-image version (=$SNF_IMAGE_VERSION) and " \
127             "helper image version (=$helper_version) don't match!"
128         log_error "Use \`-f' to bypass the version check."
129         exit 1
130     fi
131 fi
132
133 echo >&2
134 echo "Helper image was installed successfully!" >&2
135 exit 0
136
137 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :