From 576c9e6958a4f016d2a3fda6251ca95caee7423f Mon Sep 17 00:00:00 2001 From: "Ramón M. Gómez" Date: Mon, 15 Oct 2018 10:38:58 +0200 Subject: #873: New script {{{importomage}}}. --- repoman/bin/importimage | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 repoman/bin/importimage diff --git a/repoman/bin/importimage b/repoman/bin/importimage new file mode 100755 index 00000000..36a188e4 --- /dev/null +++ b/repoman/bin/importimage @@ -0,0 +1,110 @@ +#!/bin/bash +#/** +#@file importimage +#@usage importimage [str_user] str_repo str_imagename +#@brief Imports an image file from other repository +#@param str_user username to access the remote repository (local user, by default) +#@param str_repo repository IP address or hostaname +#@param str_imagename image name to download +#@warning Program will request the repository REST token. +#@version 1.1.1 - Initial version +#@author Ramón M. Gómez, ETSII Universidad de Sevilla +#@date 2017-10-08 +#*/ + + +# Variables. +PROG="$(basename "$0")" +OPENGNSYS="/opt/opengnsys" +REPODIR="$OPENGNSYS/images" +SERVERCONF="$OPENGNSYS/etc/ogAdmServer.cfg" +let BACKUP=0 + +# Functions. +source $OPENGNSYS/lib/ogfunctions.sh + + +# Main program. + +# Error control. +[ "$USER" = "root" ] || raiseError access "Need to be root." +[ -w $REPODIR ] || raiseError access "Cannot write in local repository." +case $# in + 2) USERNAME="$SUDO_USER"; REPO="$1"; IMAGE="$2" ;; + 3) USERNAME="$1"; REPO="$2"; IMAGE="$3" ;; + *) [ "$*" == "help" ] && help || raiseError usage +esac +source $SERVERCONF &>/dev/null +[ "${REPO,,}" == "${HOSTNAME,,}" -o "${REPO,,}" == "localhost" -o "${REPO}" == "127.0.0.1" -o "${REPO,,}" == "${IPLocal,,}}" ] && raiseError access "Cannot import from local repository." + +# Fetching image info from the repository. +read -rp "Enter repository API token: " APITOKEN +IMAGEINFO="$(curl -k -H "Authorization: $APITOKEN" "https://$REPO/opengnsys/rest/repository/image/$IMAGE" 2> /dev/null | jq -r .)" +IMAGENAME="$(jq -r '.name' <<< "$IMAGEINFO" 2>/dev/null)" +case "$IMAGEINFO" in + "") # Connection error. + raiseError access "Cannot connect to $REPO" ;; + "[]") # Image not found. + raiseError notfound "Image $IMAGE in remote repository $REPO" ;; + *) # Checking REST error. + MESSAGE="$(jq -r '.message' <<< "$IMAGEINFO" 2>/dev/null)" + [ -n "$MESSAGE" ] && raiseError access "$MESSAGE" +esac +IMAGETYPE="$(jq -r '.type' <<< "$IMAGEINFO" 2>/dev/null)" +IMAGELOCKED="$(jq -r '.locked' <<< "$IMAGEINFO" 2>/dev/null)" +[ "$IMAGELOCKED" == "true" ] && raiseError access "Image locked by remote repository." +IMAGESIZE="$(jq -r '.size' <<< "$IMAGEINFO" 2>/dev/null)" +[ -z "$IMAGESIZE" ] && raiseError access "Cannot retrieve image size" +# Checking if local image is locked. +LOCKFILE="$IMAGEPATH.lock" +[ -f "$LOCKFILE" ] && raiseError access "Local image is locked, cannot write." +# Checking if local image exists. +IMAGEPATH="$REPODIR/$IMAGENAME.$IMAGETYPE" +if [ -e "$IMAGEPATH" ]; then + read -rp "Image $IMAGENAME exists in the local repository. Do you want to continue? (y/N): " ANSWER + [ "${ANSWER,,}" = "y" ] || exit + BACKUP=1 + REMOTEDATE=$(jq -r '.modified' <<< "$IMAGEINFO" 2>/dev/null) + LOCALDATE=$(stat -c "%y" "$IMAGEPATH" | cut -f1 -d.) + if [[ "$REMOTEDATE" < "$LOCALDATE" ]]; then + read -rp "Remote image seems older than the local one. Do you want to continue? (y/N): " ANSWER + [ "${ANSWER,,}" = "y" ] || exit + fi +fi + +# Trapping signal to unlock image before exit. +trap "rm -f $LOCKFILE" 1 2 3 6 9 15 +# Creating lock file. +touch $LOCKFILE +# Backing up local image. +if [ $BACKUP -eq 1 ]; then + mv -vf "$IMAGEPATH" "$IMAGEPATH.ant" 2>/dev/null + mv -vf "$IMAGEPATH.torrent" "$IMAGEPATH.torrent.ant" 2>/dev/null + mv -vf "$IMAGEPATH.sum" "$IMAGEPATH.sum.ant" 2>/dev/null + mv -vf "$IMAGEPATH.full.sum" "$IMAGEPATH.full.sum.ant" 2>/dev/null +fi +# Downloading image file. +[[ $IMAGEPATH =~ / ]] && mkdir -p "$(dirname "$IMAGEPATH")" +scp $USERNAME@$REPO:$IMAGEPATH $REPODIR +ERRCODE=$? +if [ $ERRCODE -eq 0 ]; then + # Storing creation info. + jq -r '.clonator+":"+.compressor+":"+.filesystem+":"+(.datasize|tostring)+":"' <<<"$IMAGEINFO" > "$IMAGEPATH.info" + #### Si no es backup de imagen y es repo con server, + #### crear objeto imagen asociado a nuevo perfil de software vacío. + DOWNLOADSIZE=$(stat -c "%s" $IMAGEPATH) + [ $IMAGESIZE -ne $DOWNLOADSIZE ] && echo "Warning: image sizes differ: source=$IMAGESIZE, target=$DOWNLOADSIZE." +else + raiseError download "$USERNAME@$REPO:$IMAGEPATH" + # Recovering back up, if needed. + if [ $BACKUP -eq 1 ]; then + mv -vf "$IMAGEPATH.ant" "$IMAGEPATH" 2>/dev/null + mv -vf "$IMAGEPATH.torrent.ant" "$IMAGEPATH.torrent" 2>/dev/null + mv -vf "$IMAGEPATH.sum.ant" "$IMAGEPATH.sum" 2>/dev/null + mv -vf "$IMAGEPATH.full.sum.ant" "$IMAGEPATH.full.sum" 2>/dev/null + fi +fi + +# Unlocking image. +rm -f $LOCKFILE + -- cgit v1.2.3-18-g5258