forked from tungstenfabric/tf-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-container.sh
executable file
·199 lines (179 loc) · 6.35 KB
/
build-container.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
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
#!/bin/bash
# TODO: move CONTRAIL_REPO from test-test to test-base to decrease time build
REGISTRY_SERVER="opencontrail"
SKU=""
CONTRAIL_REPO=""
OPENSTACK_REPO=""
TAG=""
download_pkg () {
local pkg=$1
local dir=$2
if [[ $pkg =~ ^http[s]*:// ]]; then
wget --spider $pkg
filename="${pkg##*/}"
wget $pkg -O $dir/$filename
elif [[ $pkg =~ ^ssh[s]*:// ]]; then
server=$(echo $pkg | sed 's=scp://==;s|\/.*||')
path=$(echo $pkg |sed -r 's#scp://[a-zA-Z0-9_\.\-]+##')
yum install -y sshpass
sshpass -e scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${sshuser_sub}${server}:${path} $dir
else
echo "ERROR, Unknown url format, only http[s], ssh supported" >&2
exit 1
fi
}
docker_build_test_sku () {
local dir=${1%/}
local name=$2
local tag=$3
local build_arg_opts=''
local dockerfile=${dir}'/Dockerfile'
docker_ver=$(sudo docker -v | awk -F' ' '{print $3}' | sed 's/,//g')
if [[ "$docker_ver" < '17.06' ]] ; then
cat $dockerfile | sed \
-e 's/\(^ARG REGISTRY_SERVER=.*\)/#\1/' \
-e "s|\$REGISTRY_SERVER|${REGISTRY_SERVER}|g" \
-e 's/\(^ARG BASE_TAG=.*\)/#\1/' \
-e "s/\$BASE_TAG/$BASE_TAG/g" \
> ${dockerfile}.nofromargs
dockerfile="${dockerfile}.nofromargs"
else
build_arg_opts+=" --build-arg REGISTRY_SERVER=${REGISTRY_SERVER}"
build_arg_opts+=" --build-arg BASE_TAG=${BASE_TAG}"
fi
build_arg_opts+=" --build-arg SKU=${SKU}"
build_arg_opts+=" --build-arg CONTRAIL_REPO=${CONTRAIL_REPO}"
build_arg_opts+=" --build-arg OPENSTACK_REPO=${OPENSTACK_REPO}"
build_arg_opts+=" --build-arg DOCKERFILE_DIR=${dir}"
echo "Building test container ${name}:${tag} with opts ${build_arg_opts}"
sudo docker build --network host -t ${name}:${tag} ${build_arg_opts} -f $dockerfile . || exit 1
echo "Built test container ${name}:${tag}"
}
docker_build_test () {
usage () {
cat <<EOF
Build test container
Usage: $0 test [OPTIONS]
-h|--help Print help message
--tag TAG Docker container tag, default to sku
--base-tag BASE_TAG Specify contrail-base-test container tag to use. Defaults to 'latest'.
--sku SKU Openstack version. Defaults to ocata
--contrail-repo CONTRAIL_REPO Contrail Repository, mandatory
--openstack-repo OPENSTACK_REPO Openstack Repository, mandatory
--registry-server REGISTRY_SERVER Docker registry hosting the base test container, Defaults to docker.io/opencontrail
--post POST Upload the test container to the registy-server, if specified
EOF
}
if ! options=$(getopt -o h -l help,base-tag:,tag:,sku:,contrail-repo:,openstack-repo:,package-url:,registry-server:,post -- "$@"); then
usage
exit 1
fi
eval set -- "$options"
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage; exit;;
--base-tag) BASE_TAG=$2; shift;;
--tag) TAG=$2; shift;;
--sku) SKU=$2; shift;;
--contrail-repo) CONTRAIL_REPO=$2; shift;;
--openstack-repo) OPENSTACK_REPO=$2; shift;;
--registry-server) REGISTRY_SERVER=$2; shift;;
--post) POST=1; shift;;
esac
shift
done
if [[ -z $CONTRAIL_REPO ]]; then
echo "Need to specify either --contrail-repo"; echo
usage
exit 1
fi
if [[ -z $REGISTRY_SERVER ]]; then
echo "--registry-server is unspecified, using docker.io/opencontrail"; echo
fi
if [[ -z $SKU ]]; then
echo "SKU(--sku) is unspecified. Assuming ocata"; echo
SKU=ocata
fi
if [[ -z $TAG ]]; then
echo "TAG(--tag) is unspecified. using $SKU"; echo
TAG=$SKU
fi
if [[ -z $BASE_TAG ]]; then
echo "BASE_TAG(--base-tag) is unspecified, using 'latest'."; echo
BASE_TAG='latest'
fi
if [[ -z $OPENSTACK_REPO ]]; then
OPENSTACK_REPO="http://mirror.centos.org/centos/7/cloud/x86_64/openstack-${SKU}"
fi
docker_build_test_sku "docker/test" "contrail-test-test" "$TAG"
sudo docker tag contrail-test-test:$TAG $REGISTRY_SERVER/contrail-test-test:$TAG
if [[ -n $POST ]]; then
sudo docker push $REGISTRY_SERVER/contrail-test-test:$TAG
fi
}
docker_build_base () {
REGISTRY_SERVER=""
usage () {
cat <<EOF
Build base container
Usage: $0 base
-h|--help Print help message
--registry-server REGISTRY_SERVER Docker registry hosting the base test container, specify if the image needs to be pushed
--tag TAG Docker container tag, default to sku
--post POST Upload the test container to the registy-server, if specified
EOF
}
if ! options=$(getopt -o h -l help,post,registry-server:,tag: -- "$@"); then
usage
exit 1
fi
eval set -- "$options"
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage; exit;;
--tag) TAG=$2; shift;;
--registry-server) REGISTRY_SERVER=$2; shift;;
--post) POST=1; shift;;
esac
shift
done
if [[ -z $TAG ]]; then
echo "TAG(--tag) is unspecified. using latest"; echo
TAG=latest
fi
echo "Building base container"
sudo docker build --network host -t contrail-test-base:$TAG docker/base || exit 1
if [[ -n $REGISTRY_SERVER ]]; then
sudo docker tag contrail-test-base:$TAG $REGISTRY_SERVER/contrail-test-base:$TAG
fi
if [[ -n $POST ]]; then
sudo docker push $REGISTRY_SERVER/contrail-test-base:$TAG
fi
echo "Built base container contrail-test-base:$TAG"
}
usage () {
cat <<EOF
Build of contrail test container
Usage: $0 (base|test) [OPTIONS]
Subcommands:
base Build base test container which is openstack/contrail version agnostic
test Build contrail test container openstack/contrail version specific
Run $0 <Subcommand> -h|--help to get subcommand specific help
EOF
}
if [[ -n $SSHUSER ]]; then
sshuser_sub="${SSHUSER}@"
fi
subcommand=$1; shift;
if [[ $subcommand == '-h' || $subcommand == '' || $subcommand == '--help' ]]; then
usage
exit
elif [[ $subcommand == 'test' ]]; then
docker_build_test $@
elif [[ $subcommand == 'base' ]]; then
docker_build_base $@
else
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$0 --help' for a list of known subcommands." >&2
exit 1
fi