source: repoman/bin/importimage @ 3b8ef0d

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 3b8ef0d was 532b351, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#873: Rearrange script code and fix SQL sentences.

  • Property mode set to 100755
File size: 7.1 KB
Line 
1#!/bin/bash
2#/**
3#@file    importimage
4#@usage   importimage [str_user] str_repo str_imagename
5#@brief   Imports an image file from other repository
6#@param   str_user      username to access the remote repository (local user, by default)
7#@param   str_repo      repository IP address or hostaname
8#@param   str_imagename image name to download
9#@warning Program will request the repository REST token.
10#@version 1.1.1 - Initial version
11#@author  Ramón M. Gómez, ETSII Universidad de Sevilla
12#@date    2017-10-08
13#*/
14
15
16# Variables.
17PROG="$(basename "$0")"
18OPENGNSYS="/opt/opengnsys"
19REPODIR="$OPENGNSYS/images"
20SERVERCONF="$OPENGNSYS/etc/ogAdmServer.cfg"
21DEFAULTFILE="/etc/default/opengnsys"
22MYCNF=$(mktemp /tmp/.my.cnf.XXXXX)
23let BACKUP=0
24source $DEFAULTFILE
25
26# Functions.
27source $OPENGNSYS/lib/ogfunctions.sh
28
29
30# Main program.
31
32# Error control.
33[ "$USER" == "root" ] || raiseError access "Need to be root."
34[ "$RUN_OGADMREPO" == "yes" ] || raiseError access "This server is not defined as image repository."
35[ -w $REPODIR ] || raiseError access "Cannot write in local repository."
36case $# in
37    2)  USERNAME="$SUDO_USER"; REPO="$1"; IMAGE="$2" ;;
38    3)  USERNAME="$1"; REPO="$2"; IMAGE="$3" ;;
39    *)  [ "$*" == "help" ] && help || raiseError usage
40esac
41source $SERVERCONF &>/dev/null
42[ "${REPO,,}" == "${HOSTNAME,,}" ] || [ "${REPO,,}" == "localhost" ] || [ "${REPO}" == "127.0.0.1" ] || [ "${REPO,,}" == "${IPlocal,,}}" ] && raiseError access "Cannot import from local repository."
43
44# Fetching image info from the repository.
45read -rp "Enter repository API token: " APITOKEN
46IMAGEINFO="$(curl -k -H "Authorization: $APITOKEN" "https://$REPO/opengnsys/rest/repository/image/$IMAGE" 2> /dev/null | jq -r .)"
47IMAGENAME="$(jq -r '.name' <<< "$IMAGEINFO" 2>/dev/null)"
48case "$IMAGEINFO" in
49    "") # Connection error.
50        raiseError access "Cannot connect to $REPO" ;;
51    "[]") # Image not found.
52        raiseError notfound "Image $IMAGE in remote repository $REPO" ;;
53    *)  # Checking REST error.
54        MESSAGE="$(jq -r '.message' <<< "$IMAGEINFO" 2>/dev/null)"
55        [ -n "$MESSAGE" ] && raiseError access "$MESSAGE"
56esac
57IMAGETYPE="$(jq -r '.type' <<< "$IMAGEINFO" 2>/dev/null)"
58IMAGELOCKED="$(jq -r '.locked' <<< "$IMAGEINFO" 2>/dev/null)"
59[ "$IMAGELOCKED" == "true" ] && raiseError access "Image locked by remote repository."
60IMAGESIZE="$(jq -r '.size' <<< "$IMAGEINFO" 2>/dev/null)"
61[ -z "$IMAGESIZE" ] && raiseError access "Cannot retrieve image size"
62# Checking if local image exists.
63IMAGEPATH="$REPODIR/$IMAGENAME.$IMAGETYPE"
64if [ -e "$IMAGEPATH" ]; then
65    # Checking if local image is locked.
66    LOCKFILE="$IMAGEPATH.lock"
67    [ -f "$LOCKFILE" ] && raiseError access "Local image is locked, cannot write."
68    # Confirm image download.
69    read -rp "Image $IMAGENAME exists in the local repository. Do you want to continue? (y/N): " ANSWER
70    [ "${ANSWER,,}" = "y" ]  || exit
71    BACKUP=1
72    REMOTEDATE=$(jq -r '.modified' <<< "$IMAGEINFO" 2>/dev/null)
73    LOCALDATE=$(stat -c "%y" "$IMAGEPATH" | cut -f1 -d.)
74    if [[ "$REMOTEDATE" < "$LOCALDATE" ]]; then
75        read -rp "Remote image seems older than the local one. Do you want to continue? (y/N): " ANSWER
76        [ "${ANSWER,,}" = "y" ]  || exit
77    fi
78fi
79
80# Trapping signal to unlock image before exit.
81trap "rm -f $LOCKFILE $MYCNF" 1 2 3 6 9 15
82# Creating lock file.
83touch $LOCKFILE
84# Backing up local image.
85if [ $BACKUP -eq 1 ]; then
86    mv -vf "$IMAGEPATH" "$IMAGEPATH.ant" 2>/dev/null
87    mv -vf "$IMAGEPATH.torrent" "$IMAGEPATH.torrent.ant" 2>/dev/null
88    mv -vf "$IMAGEPATH.sum" "$IMAGEPATH.sum.ant" 2>/dev/null
89    mv -vf "$IMAGEPATH.full.sum" "$IMAGEPATH.full.sum.ant" 2>/dev/null
90fi
91# Downloading image file.
92[[ $IMAGEPATH =~ / ]] && mkdir -p "$(dirname "$IMAGEPATH")"
93scp "$USERNAME@$REPO:$IMAGEPATH" $REPODIR
94ERRCODE=$?
95if [ $ERRCODE -eq 0 ]; then
96    # Storing creation info.
97    jq -r '.clonator+":"+.compressor+":"+.filesystem+":"+(.datasize|tostring)+":"' <<<"$IMAGEINFO" > "$IMAGEPATH.info"
98    # Updating the database when the repo is also configured as Administration Server.
99    if [ "$RUN_OGADMREPO" == "yes" ]; then
100        # Creating credentials file.
101        cat << EOT > $MYCNF
102[client]
103user=$USUARIO
104password=$PASSWORD
105EOT
106        if [ $BACKUP -eq 1 ]; then
107            # If the image exists, increase its revision number.
108            mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
109                    "UPDATE imagenes
110                        SET revision = revision + 1
111                      WHERE nombreca='$IMAGE';" || \
112                            echo "Warning: database cannot be updated."
113        else
114            # Obtaining defined Organizational Units.
115            while read -re DATA; do
116                OUS[${#OUS[@]}]="$DATA"
117            done <<<$(mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -Nse \
118                              "SELECT idcentro, nombrecentro FROM centros;")
119            if [ ${#OUS[@]} -eq 1 ]; then
120                # Only 1 OU is defined.
121                let OUID="${OUS%% *}"
122            else
123                # Choose image OU.
124                echo "Choose Organization Unit:"
125                for ((i=0; i<${#OUS[@]}; i++)); do
126                    echo "  $i:  ${OUS[i]#* }"
127                done
128                read -rp "Enter number (0 by default): " ANSWER
129                let OUID="${OUS[ANSWER]%% *}" 2>/dev/null || let OUID="${OUS[0]%% *}"
130            fi
131            # Creating a new image associated with an empty software profile.
132            mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \
133                    "SET @repoid = (SELECT idrepositorio FROM repositorios
134                                     WHERE ip='$IPlocal' LIMIT 1),
135                         @profname = '$IMAGE imported from $REPO';
136                     INSERT INTO perfilessoft (descripcion, idcentro, grupoid)
137                            SELECT @profname, '$OUID', 0 FROM DUAL
138                             WHERE NOT EXISTS
139                                   (SELECT descripcion FROM perfilessoft
140                                     WHERE descripcion=@profname AND idcentro='$OUID')
141                             LIMIT 1;
142                     SET @profid = LAST_INSERT_ID();
143                     INSERT INTO imagenes
144                            (nombreca, revision, idperfilsoft, idcentro, comentarios, grupoid, idrepositorio, fechacreacion)
145                     VALUES ('$IMAGE', 1, @profid, '$OUID', 'Image imported from repo $REPO', 0, @repoid, NOW());" || \
146                            echo "Warning: database cannot be updated."
147        fi
148    fi
149    # Cheking image size.
150    DOWNLOADSIZE=$(stat -c "%s" "$IMAGEPATH")
151    [ $IMAGESIZE -ne $DOWNLOADSIZE ] && echo "Warning: image sizes differ: source=$IMAGESIZE, target=$DOWNLOADSIZE."
152else
153    # On download error, trying to recover backup.
154    raiseError download "$USERNAME@$REPO:$IMAGEPATH"
155    if [ $BACKUP -eq 1 ]; then
156        mv -vf "$IMAGEPATH.ant" "$IMAGEPATH" 2>/dev/null
157        mv -vf "$IMAGEPATH.torrent.ant" "$IMAGEPATH.torrent" 2>/dev/null
158        mv -vf "$IMAGEPATH.sum.ant" "$IMAGEPATH.sum" 2>/dev/null
159        mv -vf "$IMAGEPATH.full.sum.ant" "$IMAGEPATH.full.sum" 2>/dev/null
160    fi
161fi
162
163# Unlocking image and removing temporary file.
164rm -f $LOCKFILE $MYCNF
165
Note: See TracBrowser for help on using the repository browser.