-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror-images.sh
executable file
·128 lines (115 loc) · 3.15 KB
/
mirror-images.sh
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
#!/bin/bash -efu
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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 LICENSE for more details.
#
# Copyright: Red Hat Inc. 2021
# Author: Andrei Stepanov <[email protected]>
debug() {
if [ -n "$DEBUG" ]; then
echo "$*" >&2
fi
}
PROG="${PROG:-${0##*/}}"
DEBUG="${DEBUG:-}"
DEF_PREFIX="mirror"
msg_usage() {
cat << EOF
Mirror docker.io images to quay.io
Usage:
$PROG <options>
Options:
-h, --help display this help and exit
-v, --verbose turn on debug
-u, --user=USER quay.io user, env: SYNC_USER
-p, --password=PASSWORD quay.io password, env: SYNC_PASSWORD
-n, --namespace=NS quay.io destination namespace. env: SYNC_DST_NAMESPACE
-f, --file=FILE path to file with repos, env: SYNC_INPUT_FILE
--prefix=PREFIX add prefix to destination repos. Default: '${DEF_PREFIX}'
EOF
}
opt_str="$@"
opt=$(getopt -n "$0" --options "hvu:p:f:" --longoptions "help,verbose,user:,password:,file:,prefix:,namespace:" -- "$@")
eval set -- "$opt"
while [[ $# -gt 0 ]]; do
case "$1" in
-r|--repo)
OPT_REPO="$2"
shift 2
;;
-u|--user)
OPT_USER="$2"
shift 2
;;
-p|--password)
OPT_PASSWORD="$2"
shift 2
;;
-f|--file)
OPT_FILE="$2"
shift 2
;;
--prefix)
OPT_PREFIX="$2"
shift 2
;;
-n|--namespace)
OPT_NAMESPACE="$2"
shift 2
;;
-v|--verbose)
DEBUG="yes"
shift
;;
-h|--help)
msg_usage
exit 0
;;
--)
shift
;;
*)
msg_usage
exit 1
esac
done
# Command-line opts take priority ower env vars
DEBUG="${DEBUG:-}"
USER="${OPT_USER:-${SYNC_USER:-}}"
FILE="${OPT_FILE:-${SYNC_INPUT_FILE:-}}"
NAMESPACE="${OPT_NAMESPACE:-${SYNC_DST_NAMESPACE:-}}"
PASSWORD="${OPT_PASSWORD:-${SYNC_PASSWORD:-}}"
PREFIX="${OPT_PREFIX:-${DEF_PREFIX}}"
# Test correct invocation
if [ "${NAMESPACE}" = '' -o "${USER}" = '' -o "${FILE}" = '' -o "${PASSWORD}" = '' ]; then
echo "Use: $PROG -h for help."
exit
fi
if ! [ -r "$FILE" ]; then
echo "Cannot open input file: $FILE"
exit 1
fi
OLDIFS=$IFS
IFS=$'\n'
REPOS=($(cat "$FILE"))
IFS=$OLDIFS
for i in "${REPOS[@]}"; do
if [ -z "${i###*}" ]; then
# ignore comments, lines starting in '#'
continue
fi
# Expected format: docker://docker.io/centos/nodejs-10-centos7:latest
src="${i}"
image="${src#*//*/}"
image="${image////-}"
dst="docker://quay.io/${NAMESPACE}/${PREFIX}-${image}"
echo "[COPY] $src -> $dst"
skopeo copy --dest-creds "${USER}:${PASSWORD}" "$src" "$dst"
done