-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_images.sh
executable file
·93 lines (71 loc) · 1.69 KB
/
transfer_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
#!/bin/sh
src=''
dest=''
matchers=''
pattern=''
push=0
usage() {
echo "$(basename "$0") [-h] [[-s source] -d destination -m matcher[,matcher2,matcher3...] [-p]]
where:
-h print usage
-s source registry (optional)
example: registry.hub.docker.com
-d destination registry (required)
example: registry.mydomain.com
-m matchers (required)
all the images that will be tagged with the destination
example nodejs,jenkins,chronos
-p push images (optional)
push all tagged images
"
}
while getopts 'phs:d:m:' flag; do
case "${flag}" in
h)
usage
exit 0
;;
s) src="${OPTARG}" ;;
d) dest="${OPTARG}" ;;
m) matchers="${OPTARG}" ;;
p) push=1 ;;
*)
usage
exit 0
;;
esac
done
buildPattern(){
pattern="$TARGET"
if [ ! -z "$pattern" ]; then
pattern="${pattern}/"
fi
pattern="${pattern}($(echo $matchers | sed -e 's/,/\|/g'))"
}
grepImages(){
docker images | grep -E "$pattern"
}
## Main
if [ -z "$dest" ]; then
echo "Destination must be provided"
exit 1
elif [ -z "$matchers" ]; then
echo "Matchers must be provided"
exit 1
fi
TARGET="$src" buildPattern
sourceImages="$(grepImages | awk '{print $1":"$2}')"
while read -r sourceImage; do
prefix=$src
targetImage=''
if [ ! -z "$prefix" ]; then
prefix="$prefix/"
fi
targetImage="$dest/${sourceImage#$prefix}"
echo "Tagging $targetImage"
docker tag $sourceImage $targetImage
if [ $push -eq 1 ]; then
echo "Pushing $targetImage"
docker push $targetImage
fi
done <<< "$sourceImages"