summaryrefslogtreecommitdiffstats
path: root/docker/scripts/update_dockerhub_prod_tags.sh
blob: 890beb3e30ca8335a53d48b18e1d015399121d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#! /bin/bash

# Copyright (c) 2021 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail
shopt -s extglob

# Log all output to stdout & stderr to a log file
logname="/tmp/$(basename $0).$(date -u +%Y_%m_%d_%H%M%S).log"
echo -e "\n*** Logging output to $logname ***\n"
exec > >(tee -a $logname) 2>&1

export CIMAN_DOCKER_SCRIPTS=${CIMAN_DOCKER_SCRIPTS:-"$(dirname $BASH_SOURCE)"}
. "$CIMAN_DOCKER_SCRIPTS/lib_common.sh"

# Global variables
long_bar="################################################################"
short_bar="-----"
image_not_found=""
image_user=""
image_repo=""
image_version=""
image_arch=""
image_name_prod=""
image_name_prev=""
image_name_new=""
image_realname=""
image_realname_prod=""
image_realname_prev=""
image_tags=""
image_tags_prod=""
image_tags_prev=""
image_tags_new=""
docker_id_prod=""
docker_id_prev=""
docker_id_new=""
digest_prod=""
digest_prev=""
digest_new=""
restore_cmd=""

usage() {
    local script="$(basename $0)"
    echo
    echo "Usage: $script r[evert]  <prod image>"
    echo "       $script p[romote] <new image> [<new image>]"
    echo "       $script i[nspect] <prod image>"
    echo
    echo "  revert: swaps 'prod-<arch>' and 'prod-prev-<arch>' images"
    echo "          <prod image>: e.g. fdiotools/builder-ubuntu1804:prod-x86_64"
    echo
    echo " promote: moves 'prod-<arch>' image to 'prod-prev-<arch>' tag and"
    echo "          tags <new image> with 'prod-<arch>'"
    echo "          <new image>: e.g. fdiotools/builder-ubuntu1804:2020_09_23_151655-x86_64"
    echo " inspect: prints out all tags for prod-<arch> and prod-prev-<arch>"
    echo
    exit 1
}

echo_restore_cmd() {
    echo -e "\n$long_bar\n"
    echo "To restore tags to original state, issue the following command:"
    echo -e "\n$restore_cmd\n\n$long_bar\n"
}

push_to_dockerhub() {
    echo_restore_cmd
    for image in "$@" ; do
        set +e
        echo "Pushing '$image' to docker hub..."
        if ! docker push "$image" ; then
            echo "ERROR: 'docker push $image' failed!"
            exit 1
        fi
    done
}

parse_image_name() {
    image_user="$(echo $1 | cut -d'/' -f1)"
    image_repo="$(echo $1 | cut -d'/' -f2 | cut -d':' -f1)"
    local tag="$(echo $1 | cut -d':' -f2)"
    image_version="$(echo $tag | cut -d'-' -f1)"
    image_arch="$(echo $tag | sed -e s/$image_version-//)"
    image_name_new="${image_user}/${image_repo}:${image_version}-${image_arch}"
    if [ "$1" != "$image_name_new" ] ; then
        echo "ERROR: Image name parsing failed: $1 != '$image_name_new'"
        usage
    fi
    if [[ "$image_version" =~ "prod" ]] ; then
        image_name_new=""
    fi
    image_name_prod="${image_user}/${image_repo}:prod-${image_arch}"
    image_name_prev="${image_user}/${image_repo}:prod-prev-${image_arch}"
}

format_image_tags() {
    # Note: 'grep $image_arch' & grep -v 'prod-curr' is required due to a
    #       bug in docker hub which returns old tags which were deleted via
    #       the webUI, but are still retrieved by 'docker pull -a'
    image_tags="$(docker images | grep $1 | grep $image_arch | grep -v prod-curr | sort -r | mawk '{print $1":"$2}' | tr '\n' ' ')"
    image_realname="$(docker images | grep $1 | grep $image_arch | sort -r | grep -v prod | mawk '{print $1":"$2}' || true)"
    if [ -z "${image_realname:-}" ] ; then
        image_realname="$image_tags"
    fi
}

get_image_id_tags() {
    for image in "$image_name_new" "$image_name_prod" "$image_name_prev" ; do
        if [ -z "$image" ] ; then
            continue
        fi
        # ensure image exists
        set +e
        local image_found="$(docker images | mawk '{print $1":"$2}' | grep $image)"
        set -e
        if [ -z "$image_found" ] ; then
            if [ "$image" = "$image_name_prev" ] ; then
                if [ "$action" = "revert" ] ; then
                    echo "ERROR: Image '$image' not found!"
                    echo "Unable to revert production image '$image_name_prod'!"
                    usage
                else
                    continue
                fi
            else
                echo "ERROR: Image '$image' not found!"
                usage
            fi
        fi
        set +e
        local id="$(docker image inspect $image | mawk -F':' '/Id/{print $3}')"
        local digest="$(docker image inspect $image | grep -A1 RepoDigests | grep -v RepoDigests | mawk -F':' '{print $2}')"
        local retval="$?"
        set -e
        if [ "$retval" -ne "0" ] ; then
            echo "ERROR: Docker ID not found for '$image'!"
            usage
        fi
        if [ "$image" = "$image_name_prod" ] ; then
            docker_id_prod="${id::12}"
            digest_prod="${digest::12}"
            format_image_tags "$docker_id_prod"
            image_tags_prod="$image_tags"
            if [ -z "$image_realname_prod" ] ; then
                image_realname_prod="$image_realname"
            fi
        elif [ "$image" = "$image_name_prev" ] ; then
            docker_id_prev="${id::12}"
            digest_prev="${digest::12}"
            format_image_tags "$docker_id_prev"
            image_tags_prev="$image_tags"
            if [ -z "$image_realname_prev" ] ; then
                image_realname_prev="$image_realname"
            fi
        else
            docker_id_new="${id::12}"
            digest_new="${digest::12}"
            format_image_tags "$docker_id_new" "NEW"
            image_tags_new="$image_tags"
        fi
    done
    if [ -z "$restore_cmd" ] ; then
        restore_cmd="sudo $0 p $image_realname_prev $image_realname_prod"
    fi
}

get_all_tags_from_dockerhub() {
    local dh_repo="$image_user/$image_repo"
    echo -e "Pulling all tags from docker hub repo '$dh_repo':\n$long_bar"
    if ! docker pull -a "$dh_repo" ; then
        echo "ERROR: Repository '$dh_repo' not found on docker hub!"
        usage
    fi
    echo "$long_bar"
}

verify_image_version_date_format() {
    version="$1"
    # TODO: Remove regex1 when legacy nomenclature is no longer on docker hub.
    local regex1="^[0-9]{4}_[0-1][0-9]_[0-3][0-9]_[0-2][0-9][0-5][0-9][0-5][0-9]$"
    local regex2="^[0-9]{4}_[0-1][0-9]_[0-3][0-9]_[0-2][0-9][0-5][0-9][0-5][0-9]_UTC$"
    if [[ "$version" =~ $regex1 ]] || [[ "$version" =~ $regex2 ]]; then
        return 0
    fi
    return 1
}

verify_image_name() {
    image_not_found=""
    # Invalid user
    if [ "$image_user" != "fdiotools" ] ; then
        image_not_found="true"
        echo "ERROR: invalid user '$image_user' in '$image_name_new'!"
    fi
    # Invalid version
    if [ -z "$image_not_found" ] \
           && [ "$image_version" != "prod" ] \
           && ! verify_image_version_date_format "$image_version"  ]] ; then
        image_not_found="true"
        echo "ERROR: invalid version '$image_version' in '$image_name_new'!"
    fi
    # Invalid arch
    if [ -z "$image_not_found" ] \
           && ! [[ "$EXECUTOR_ARCHS" =~ .*"$image_arch".* ]] ; then
        image_not_found="true"
        echo "ERROR: invalid arch '$image_arch' in '$image_name_new'!"
    fi
    if [ -n "$image_not_found" ] ; then
        echo "ERROR: Invalid image '$image_name_new'!"
        usage
    fi
}

docker_tag_image() {
    echo ">>> docker tag $1 $2"
    set +e
    docker tag "$1" "$2"
    local retval="$?"
    set -e
    if [ "$retval" -ne "0" ] ; then
        echo "WARNING: 'docker tag $1 $2' failed!"
    fi
}

docker_rmi_tag() {
    set +e
    echo ">>> docker rmi $1"
    docker rmi "$1"
    local retval="$?"
    set -e
    if [ "$retval" -ne "0" ] ; then
        echo "WARNING: 'docker rmi $1' failed!"
    fi
}

print_image_list() {
    if [ -z "$2" ] ; then
        echo "$1 Image Not Found"
        return
    fi
    echo "$1 (Id $2, Digest $3):"
    for image in $4 ; do
        echo -e "\t$image"
    done
}

inspect_images() {
    echo -e "\n${1}Production Docker Images:"
    echo "$short_bar"
    if [ -n "$image_tags_new" ] ; then
        print_image_list "NEW" "$docker_id_new" "$digest_new" "$image_tags_new"
        echo
    fi
    print_image_list "prod-$image_arch" "$docker_id_prod" "$digest_prod" \
                     "$image_tags_prod"
    echo
    print_image_list "prod-prev-$image_arch" "$docker_id_prev" "$digest_prev" \
                     "$image_tags_prev"
    echo -e "$short_bar\n"
}

revert_prod_image() {
    inspect_images "EXISTING "
    docker_tag_image "$docker_id_prod" "$image_name_prev"
    docker_tag_image "$docker_id_prev" "$image_name_prod"
    get_image_id_tags
    inspect_images "REVERTED "

    local yn=""
    while true; do
        read -p "Push Reverted tags to '$image_user/$image_repo' (yes/no)? " yn
        case ${yn:0:1} in
            y|Y )
                break ;;
            n|N )
                echo -e "\nABORTING REVERT!\n"
                docker_tag_image $docker_id_prev $image_name_prod
                docker_tag_image $docker_id_prod $image_name_prev
                get_image_id_tags
                inspect_images "RESTORED LOCAL "
                exit 1 ;;
            * )
                echo "Please answer yes or no." ;;
        esac
    done
    echo
    push_to_dockerhub $image_name_prev $image_name_prod
    inspect_images ""
    echo_restore_cmd
}

promote_new_image() {
    inspect_images "EXISTING "
    docker_tag_image "$docker_id_prod" "$image_name_prev"
    docker_tag_image "$docker_id_new" "$image_name_prod"
    get_image_id_tags
    inspect_images "PROMOTED "

    local yn=""
    while true; do
        read -p "Push promoted tags to '$image_user/$image_repo' (yes/no)? " yn
        case "${yn:0:1}" in
            y|Y )
                break ;;
            n|N )
                echo -e "\nABORTING PROMOTION!\n"
                docker_tag_image "$docker_id_prev" "$image_name_prod"
                local restore_both="$(echo $restore_cmd | mawk '{print $5}')"
                if [[ -n "$restore_both" ]] ; then
                    docker_tag_image "$image_realname_prev" "$image_name_prev"
                else
                    docker_rmi_tag "$image_name_prev"
                    image_name_prev=""
                    docker_id_prev=""
                fi
                get_image_id_tags
                inspect_images "RESTORED "
                exit 1 ;;
            * )
                echo "Please answer yes or no." ;;
        esac
    done
    echo
    push_to_dockerhub "$image_name_new" "$image_name_prev" "$image_name_prod"
    inspect_images ""
    echo_restore_cmd
}

must_be_run_as_root_or_docker_group

# Validate arguments
num_args="$#"
if [ "$num_args" -lt "1" ] ; then
    usage
fi
action=""
case "$1" in
    r?(evert))
        action="revert"
        if [ "$num_args" -ne "2" ] ; then
            echo "ERROR: Invalid number of arguments: $#"
            usage
        fi ;;
    p?(romote))
        if [ "$num_args" -eq "2" ] || [ "$num_args" -eq "3" ] ; then
            action="promote"
        else
            echo "ERROR: Invalid number of arguments: $#"
            usage
        fi ;;
    i?(nspect))
        action="inspect"
        if [ "$num_args" -ne "2" ] ; then
            echo "ERROR: Invalid number of arguments: $#"
            usage
        fi ;;
    *)
        echo "ERROR: Invalid option '$1'!"
        usage ;;
esac
shift
docker login >& /dev/null

# Update local tags
tags_to_push=""
for image in "$@" ; do
    parse_image_name "$image"
    verify_image_name "$image"
    get_all_tags_from_dockerhub
    get_image_id_tags
    if [ "$action" = "promote" ] ; then
        if [ -n "$image_name_new" ] ; then
            promote_new_image
        else
            echo "ERROR: No new image specified to promote!"
            usage
        fi
    elif [ "$action" = "revert" ] ; then
        if [ "$image_version" = "prod" ] ; then
            revert_prod_image
        else
            echo "ERROR: Non-production image '$image' specified!"
            usage
        fi
    else
        if [ "$image_version" = "prod" ] ; then
            inspect_images ""
        else
            echo "ERROR: Non-production image '$image' specified!"
            usage
        fi
    fi
done