Path : /usr/bin/ |
|
Current File : //usr/bin/cloud-publish-tarball |
#!/bin/sh
#
# cloud-publish-tarball - wrapper for publishing cloud tarballs
#
# Copyright (C) 2010 Canonical Ltd.
#
# Authors: Scott Moser <smoser@canonical.com>
#
# 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, version 3 of the License.
#
# 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, see <http://www.gnu.org/licenses/>.
EC2PRE=${EC2PRE:-euca-}
TMPD=""
VERBOSITY=1
error() { echo "$@" 1>&2; }
debug() {
[ ${VERBOSITY} -ge $1 ] || return 0;
shift
error "$@"
}
log() { debug "$1" "$(date): ====== $2 ======" ; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
cleanup() {
[ -n "${TMPD}" -a -d "${TMPD}" ] || return 0;
debug 2 "cleaning up ${TMPD}"
rm -Rf "${TMPD}";
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] tarfile bucket [arch]
register a UEC tarball (ie, per http://cloud-images.ubuntu.com)
if arch is not provided, a name-based attempt is made to guess
tarfile may be a url
options:
--hook-img e invoke 'e' with full path to downloaded disk image
-k | --kernel k use previously registered kernel with id 'k'
specify 'none' for no kernel
-K | --kernel-file f bundle, upload, use file 'f' as kernel
--rename-kernel k rename kernel to 'k' (applies to loader)
-r | --ramdisk r use previously registered ramdisk with id 'r'
specify 'none' for no ramdisk
-R | --ramdisk-file f bundle, upload, use file 'f' as ramdisk
--rename-ramdisk r rename ramdisk to 'r'
--resize s resize the partition image before uploading
's' must be valid input to cloud-resize-image
--save-downloaded if the image is a url, save it to '.'
-q | --quiet be quiet, only output published ids
-l | --use-loader use the loader kernel rather than linux kernel
--rename-image f rename image to 'f' before publishing
Example:
- ${0##*/} lucid-cloud-i386.tar.gz my-lucid-bucket i386
EOF
}
upload_register() {
local out="" ret=0
out=$(cloud-publish-image "${@}") || {
ret=$?
printf "%s" "${out}"
return $ret
}
set -- ${out}
_RET=${1}
}
dl() {
# dl url, target, quiet
local url=${1} target=${2} quiet=${3:-1}
if [ -f "${url}" ]; then
[ "${target}" = "-" ] && { cat "$url"; return; }
cp "$url" "$target"
return
fi
local qflag="-q"
[ "$quiet" = "0" ] && qflag=""
wget $qflag --progress=dot:mega "$url" -O "$target" ||
return 1
}
dl_input_image() {
# this downloads an image if necessary and sets _RET to location of image
local input="$1" save_dir="${2:-.}" ret="" quiet=0
[ $VERBOSITY -eq 0 ] && quiet=1 # this differs from cloud-publish-image
case "$input" in
file://*)
ret="$save_dir/${input##*/}"
dl "${input#file://}" "$ret" $quiet || return $?;;
http://*|ftp://*|https://*)
ret="$save_dir/${input##*/}"
dl "$input" "$ret" $quiet || return $?
;;
*) ret="$input";;
esac
_RET="$ret"
}
[ "${CLOUD_UTILS_WARN_UEC:-0}" = "0" ] && _n="${0##*/}" && [ "${_n#uec}" != "${_n}" ] &&
error "WARNING: '${0##*/}' is now to 'cloud${_n#uec}'. Please update your tools or docs" &&
export CLOUD_UTILS_WARN_UEC=1
short_opts="hlk:K:qr:R:"
long_opts="help,hook-img:,kernel:,kernel-file:,quiet,use-loader,ramdisk:,ramdisk-file:,rename-image:,rename-kernel:,rename-ramdisk:,resize:,save-downloaded"
getopt_out=$(getopt --name "${0##*/}" --shell sh \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
bad_Usage
ramdisk=""
kernel=""
loader=""
eki=""
eri=""
image=""
emi=""
resize=""
use_loader=0
rename_image=""
rename_kernel=""
rename_ramdisk=""
save_dl=0
hook_img=""
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
--) shift; break;;
-h|--help) Usage; exit 0;;
--hook-img)
[ -z "${hook_img}" ] || bad_Usage "only one --hook-img supported";
[ -x "$next" ] || bad_Usage "--hook-img is not executable"
hook_img=$(readlink -f "$next") ||
bad_Usage "could not find full path to $next"
hook_img="$next"
shift;;
-k|--kernel) eki=${next}; shift;;
-K|--kernel-file)
[ -f "${next}" ] && kernel=$(readlink -f "${next}") ||
fail "failed to get path to ${next}"
shift;;
-q|--quiet) VERBOSITY=0;;
-r|--ramdisk) eri=${next}; shift;;
-R|--ramdisk-file)
[ -f "${next}" ] && ramdisk=$(readlink -f "${next}") ||
fail "failed to get path to ${next}"
shift;;
--rename-image) rename_image=${next}; shift;;
--rename-kernel) rename_kernel=${next}; shift;;
--rename-ramdisk) rename_ramdisk=${next}; shift;;
--save-downloaded) save_dl=1;;
--use-loader) use_loader=1;;
--resize) resize=${next}; shift;;
esac
shift;
done
tarball=${1}
bucket=${2}
arch=${3}
[ $# -eq 3 -o $# -eq 2 ] || bad_Usage
[ -n "${eki}" -a ${use_loader} -ne 0 ] &&
bad_Usage "--use-loader is incompatible with --kernel"
if [ -z "${arch}" ]; then
case "${tarball}" in
*i386*) arch=i386;;
*amd64*|*x86_64*) arch=amd64;;
*) fail "unable to guess arch by tarball name. give 3rd arg";;
esac
fi
[ "$arch" = "amd64" ] && iarch="x86_64" || iarch="${arch}"
# before extracting the tarball, try to verify that the environment
# is set up, by invoking another euca command (LP: #526504)
${EC2PRE}describe-images >/dev/null ||
fail "Unable to run ${EC2PRE}-describe-images. Is environment for ${EC2PRE} set up?"
utmp=${TEMPDIR:-${TMPDIR:-/tmp}}
TMPD=$(mktemp -d "${utmp}/${0##*/}.XXXXXX") || fail "failed make temp"
trap cleanup EXIT
save_dir="${TMPD}"
[ $save_dl -eq 1 ] && save_dir=$PWD
dl_input_image "$tarball" "$save_dir" && tarball="$_RET" ||
fail "failed to download image $image to $save_dir"
[ -f "${tarball}" ] && tbf=$(readlink -f "${tarball}") ||
fail "bad tarball: ${tarball}";
start=$PWD
cd "${TMPD}"
log 1 "extracting image"
tar -S -xvzf "${tbf}" >list.out || fail "failed extract ${tarball}";
while read x; do
[ -f "${x}" ] || continue
case "$x" in
*vmlinuz*)
[ -z "${kernel}" -a -z "${eki}" ] && kernel=${x};;
*initrd*)
[ -z "${ramdisk}" -a -z "${eri}" ] && ramdisk=${x};;
*.img) image=${x};;
*-loader) [ -z "${loader}" ] && loader=${x};;
esac
done < list.out
[ -z "${image}" ] && fail "can't find image";
[ -z "${loader}" -a ${use_loader} -eq 1 ] &&
fail "--use-loader specified, but no loader found in tarball"
# if loader was found, and no kernel given (or found)
# then set kernel to loader
if [ -n "${loader}" ] &&
{ [ ${use_loader} -eq 1 ] || [ -z "${kernel}" -a -z "${eki}" ]; } ; then
debug 1 "using loader ${loader##*/} as kernel"
kernel=${loader}
fi
[ -n "${kernel}" -o -n "${eki}" ] ||
bad_Usage "can't find kernel. specify '--kernel none' to register none";
[ -n "${ramdisk}" -o -n "${eri}" ] || {
debug 1 "Warning: no ramdisk found, assuming '--ramdisk none'"
eri="none";
}
debug 1 "kernel : ${eki:-${kernel}}"
debug 1 "ramdisk: ${eri:-${ramdisk}}"
debug 1 "image : ${image##*/}"
if [ -n "${resize}" ]; then
log 1 "resizing ${image##*/} to ${resize}"
out=$(resize-part-image "${image}" "${resize}" 2>&1) || {
error "${out}";
fail "failed to resize image file to ${resize}";
}
fi
if [ -n "${kernel}" ]; then
log 1 "bundle/upload kernel"
upload_register --type kernel \
${rename_kernel:+"--rename=${rename_kernel}"} \
"${iarch}" "${kernel}" "${bucket}" ||
fail "failed to upload kernel"
eki=${_RET}
fi
if [ -n "${ramdisk}" ]; then
log 1 "bundle/upload ramdisk"
upload_register --type ramdisk \
${rename_ramdisk:+"--rename=${rename_ramdisk}"} \
"${iarch}" "${ramdisk}" "${bucket}" ||
fail "failed ramdisk bundle/upload"
eri=${_RET}
fi
log 1 "bundle/upload image"
upload_register --type image \
${rename_image:+"--rename=${rename_image}"} \
${hook_img:+"--hook-img=${hook_img}"} \
"${iarch}" "${image}" "${bucket}" \
--kernel "${eki}" --ramdisk "${eri}" ||
fail "failed bundle/upload/register of image"
emi=${_RET}
log 1 "done"
printf 'emi="%s"; eri="%s"; eki="%s";\n' "${emi}" "${eri}" "${eki}"
# vi: ts=4 noexpandtab