-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstartup.sh
executable file
·172 lines (151 loc) · 5.04 KB
/
startup.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
#!/bin/bash
set -o nounset
set -o errexit
scriptdir=$(dirname "$0")
cd "$scriptdir"
setup_only=0
own_vm=0
DEVENVTAG=latest
IMAGE=opencontrailnightly/developer-sandbox
# variables that can be redefined outside
REGISTRY_PORT=${REGISTRY_PORT:-6666}
REGISTRY_IP=${REGISTRY_IP:-""}
while getopts ":t:i:sb" opt; do
case $opt in
i)
IMAGE=$OPTARG
;;
t)
DEVENVTAG=$OPTARG
;;
s)
setup_only=1
;;
b)
own_vm=1
;;
\?)
echo "Invalid option: $opt"
exit 1
;;
esac
done
function is_created () {
local container=$1
docker ps -a --format '{{ .Names }}' | grep "$container" > /dev/null
return $?
}
function is_up () {
local container=$1
docker inspect --format '{{ .State.Status }}' $container | grep "running" > /dev/null
return $?
}
echo contrail-dev-env startup
echo
echo '[docker setup]'
distro=$(cat /etc/*release 2>/dev/null | egrep '^ID=' | awk -F= '{print $2}' | tr -d \")
[[ $distro ]] || distro=$(uname)
echo $distro detected.
if [ x"$distro" == x"centos" ]; then
which docker || yum install -y docker
systemctl start docker
systemctl stop firewalld || true
systemctl start docker
grep 'dm.basesize=20G' /etc/sysconfig/docker-storage || sed -i 's/DOCKER_STORAGE_OPTIONS=/DOCKER_STORAGE_OPTIONS=--storage-opt dm.basesize=20G /g' /etc/sysconfig/docker-storage
systemctl restart docker
elif [ x"$distro" == x"ubuntu" ]; then
which docker || apt install -y docker.io
fi
test "$setup_only" -eq 1 && exit
echo
echo '[environment setup]'
if [[ "$own_vm" -eq 0 ]]; then
rpm_source=$(docker volume create --name contrail-dev-env-rpm-volume)
else
contrail_dir=$(realpath ${scriptdir}/../contrail)
rpm_source=${contrail_dir}/RPMS
mkdir -p ${rpm_source}
fi
echo "${rpm_source} created."
# The IP through which the containers access the host
host_ip=$(docker network inspect bridge --format '{{(index .IPAM.Config 0).Gateway}}')
# Default REGISTRY_IP on Mac is localhost, because docker push to the gateway IP may fail.
default_registry_ip=$host_ip
[[ Darwin != "$distro" ]] || default_registry_ip=localhost
REGISTRY_IP=${REGISTRY_IP:-$default_registry_ip}
if ! is_created "contrail-dev-env-rpm-repo"; then
docker run -t --privileged --name contrail-dev-env-rpm-repo \
-d -p 6667:80 \
-v ${rpm_source}:/var/www/localhost/htdocs \
m4rcu5/lighttpd >/dev/null
echo contrail-dev-env-rpm-repo created.
else
if is_up "contrail-dev-env-rpm-repo"; then
echo "contrail-dev-env-rpm-repo already running."
else
echo $(docker start contrail-dev-env-rpm-repo) started.
fi
fi
if ! is_created "contrail-dev-env-registry"; then
docker run --privileged --name contrail-dev-env-registry \
-d -p $REGISTRY_PORT:5000 \
registry:2 >/dev/null
echo contrail-dev-env-registry created.
else
if is_up "contrail-dev-env-registry"; then
echo "contrail-dev-env-registry already running."
else
echo $(docker start contrail-dev-env-registry) started.
fi
fi
if [[ "$own_vm" -eq 0 ]]; then
if ! is_created "contrail-developer-sandbox"; then
if [[ x"$DEVENVTAG" == x"latest" ]]; then
docker pull ${IMAGE}:${DEVENVTAG}
fi
docker run --privileged --name contrail-developer-sandbox \
-w /root -itd \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ${rpm_source}:/root/contrail/RPMS \
-v $(pwd):/root/contrail-dev-env \
${IMAGE}:${DEVENVTAG} >/dev/null
echo contrail-developer-sandbox created.
else
if is_up "contrail-developer-sandbox"; then
echo "contrail-developer-sandbox already running."
else
echo $(docker start contrail-developer-sandbox) started.
fi
fi
fi
echo
echo '[configuration update]'
rpm_repo_ip=${host_ip}
if [[ ! $rpm_repo_ip ]]; then
echo "ERROR: RPM repo IP not set. Is docker installed correctly?"
exit 1
fi
echo "INFO: rpm_repo_ip = ${rpm_repo_ip}"
registry_ip=${REGISTRY_IP}
if [[ ! $registry_ip ]]; then
echo "ERROR: registry IP not set. Is docker installed correctly?"
exit 1
fi
echo "INFO: registry_ip = ${registry_ip}"
sed -e "s/rpm-repo/${rpm_repo_ip}/g" -e "s/registry/${registry_ip}/g" -e "s/6666/${REGISTRY_PORT}/g" common.env.tmpl > common.env
sed -e "s/rpm-repo/${rpm_repo_ip}/g" -e "s/contrail-registry/${registry_ip}/g" -e "s/6666/${REGISTRY_PORT}/g" vars.yaml.tmpl > vars.yaml
sed -e "s/rpm-repo/${rpm_repo_ip}/g" -e "s/registry/${registry_ip}/g" dev_config.yaml.tmpl > dev_config.yaml
sed -e "s/registry/${registry_ip}/g" -e "s/6666/${REGISTRY_PORT}/g" daemon.json.tmpl > daemon.json
if [ x"$distro" == x"centos" ]; then
if ! diff daemon.json /etc/docker/daemon.json; then
cp daemon.json /etc/docker/daemon.json
systemctl restart docker
docker start contrail-dev-env-rpm-repo contrail-dev-env-registry
test "$own_vm" -eq 0 && docker start contrail-developer-sandbox
fi
elif [ x"$distro" == x"ubuntu" ]; then
diff daemon.json /etc/docker/daemon.json || (cp daemon.json /etc/docker/daemon.json && service docker reload)
fi
echo
echo '[READY]'
test "$own_vm" -eq 0 && echo "You can now connect to the sandbox container by using: $ docker attach contrail-developer-sandbox"