-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloadimages.sh
executable file
·141 lines (125 loc) · 3.95 KB
/
loadimages.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
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
echo -e "\033[1;31mImportant! Please ensure that you had login to the target Docker registry in advance. \033[0m"
echo -e "\033[1;31mImportant! The load image sample script is for Linux x86_64 platform only.\n \033[0m"
ARCH=$(arch)
case ${ARCH} in
amd64|x86_64)
echo "Supported arch: ${ARCH}"
;;
*)
echo "Unsupported arch: ${ARCH}"
exit -1
;;
esac
function showHelp {
echo -e "\nUsage: loadimages.sh -p path/to/ppa_archive.tgz -r docker_registry [-l]\n"
echo "Options:"
echo " -h Display help"
echo " -p PPA archive files location or archive filename"
echo " For example: /Downloads/PPA or /Downloads/PPA/ImageArchive.tgz"
echo " -r Target Docker registry and namespace"
echo " For example: mycorp-docker-local.mycorp.com/image-space"
echo " -l Optional: Target a local registry"
}
# initialize variables
unset ppa_path
unset target_docker_repo
local_registry=false
OPTIND=1 # Reset in case getopts has been used previously in the shell.
if [[ $1 == "" ]]
then
showHelp
exit -1
else
while getopts ":hlp:r:" opt; do
case "$opt" in
h|\?)
showHelp
exit 0
;;
p) ppa_path=${OPTARG}
;;
r) target_docker_repo=${OPTARG}
;;
l) local_registry=true
;;
:) echo "Invalid option: -$OPTARG requires an argument"
showHelp
exit -1
;;
esac
done
fi
shift $((OPTIND-1))
# check required parameters
if [ -z "$ppa_path" ]
then
echo "Need to input PPA archive files location or name value."
showHelp
exit -1
elif `test -f $ppa_path` || `test -d $ppa_path`
then
arr_ppa_tgz=( $(find ${ppa_path} -name "*.tgz") )
else
echo "Input PPA archive files location or name invalid! ($ppa_path) Exit and try again."
showHelp
exit -1
fi
if [ -z "$target_docker_repo" ]
then
echo "Need to input target Docker registry and namespace value."
showHelp
exit -1
fi
# reset counter
_ind=0
for ppa_file in ${arr_ppa_tgz[@]}
do
echo -e "\nCheck image archives in the PPA package - "$ppa_file
# check manifest.json
tar -zxvf $ppa_file manifest.json
# get image archive files list in current PPA
arr_img_gz=( $(grep archive manifest.json | awk '{print $2}' | sed 's/\"//g') )
echo "Image archives list in ${ppa_file}:"
echo ${arr_img_gz[@]}
echo "Image archives in "$ppa_file" count : "${#arr_img_gz[@]}
echo -e "\nLoad docker images from image archives into local registry."
if [ ${#arr_img_gz[@]} -gt 0 ]
then
for img_gz_file in ${arr_img_gz[@]}
do
echo "Loading image file ... "$img_gz_file
# echo "tar -zxf ${ppa_file} ${img_gz_file} -O | docker load -q"
load_cmd_output=`tar -zxf ${ppa_file} ${img_gz_file} -O | docker load -q`
echo $load_cmd_output
arr_img_load[$_ind]=${load_cmd_output#*Loaded image: }
#
docker tag ${arr_img_load[$_ind]} ${target_docker_repo}/${arr_img_load[$_ind]}
if ! $local_registry
then
docker push ${target_docker_repo}/${arr_img_load[$_ind]} | grep -e repository -e digest -e unauthorized
docker rmi -f ${arr_img_load[$_ind]} ${target_docker_repo}/${arr_img_load[$_ind]} | grep -e unauthorized
echo "Pushed image: "${target_docker_repo}/${arr_img_load[$_ind]}
fi
let _ind++
done
echo "PPA package - "$ppa_file" was processed completely."
else
echo "No image archive found in "$ppa_file
continue
fi
done
# summary list
if $local_registry
then
status="load"
else
status="push"
fi
echo -e "\nDocker images ${status} to ${target_docker_repo} completed, and check following images in the Docker registry."
for img_load in ${arr_img_load[@]}
do
echo " - ${target_docker_repo}/${img_load}"
done
#
rm -rf manifest.json