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