bootstrap 4.34 KB
Newer Older
BAIRE Anthony's avatar
BAIRE Anthony committed
1
2
#!/bin/bash

BAIRE Anthony's avatar
BAIRE Anthony committed
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
CONTAINERS="dev-mysql dev-controller dev-ssh dev-rails dev-django dev-nginx dev-smtpsink dev-registry"


die()
{
	echo "error: $*" >&2
	exit 1
}

# generate the .env file for docker-compose
#
# DOCKERUSER is set to the uid:gid of the current user
#
# If the file already exists, it will never be replaced. The function just
# prints a warning if it differs from the would-be generated file.
generate_env_file()
{
	cat >".env.tmp" <<EOF
DOCKERUSER=`id -u`:`id -g`
EOF
	if [ ! -e .env ] ; then
		mv .env.tmp .env
	else
		if cmp --quiet .env .env.tmp ; then
			rm .env.tmp
		else
			echo "warning: config file '.env' already exists and is different from the generate config. Remove it if you want it to be overwritten."
			diff -u .env .env.tmp || true
		fi
	fi
}

35
36
# install the tokens certificate into the registry external volume
install_secrets()
BAIRE Anthony's avatar
BAIRE Anthony committed
37
{
38
	src=data/django/ro/certs/tokens.crt
39
	dst=data/registry/ro/certs/tokens.crt
40

41
42
43
	if [ -f "$src" ] && [ -d "`dirname "$dst"`" ] && ! cmp -s "$src" "$dst" ; then
		echo "Install tokens certificate as $dst/tokens.crt"
		cp "$src" "$dst"
44
		docker-compose restart dev-registry
BAIRE Anthony's avatar
BAIRE Anthony committed
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
	fi
}

# generate the factories
#
# this function is run after the rails container is initialised because the
# list of factories is read from the database
build_factories()
{
	# build the factories
	for name in `echo 'select concat(name, ":", version) as img from docker_os order by img;' | docker exec -i dev-mysql mysql -uroot allgo | grep -v '^img$'`
	do
		tag="localhost:8002/allgo/dev/factory/$name"
		if docker inspect --type image -- "$name" >/dev/null 2>/dev/null
		then
			echo "$name: already pulled"
			(set -x
			docker tag  -- "$name" "$tag"
			docker push -- "$tag"
			docker rmi  -- "$name" || true
			)
		else
			echo "$name:"
			(set -x
			docker pull -- "$name"
			docker tag  -- "$name" "$tag"
			docker push -- "$tag"
			docker rmi  -- "$name"
			)
		fi
	done
}

# remove container and its data
purge_container()
{
	local name="$1"

	(set -x ; docker-compose rm -f "$name")

	# ensure $name is well formatted (to avoid disasters with rm -rf)
	[[ "$name" =~ ^dev-[a-z][a-z0-9-]*$ ]] || die "bad container name: $name"
	local data_dir="data/${name/dev-/}"

	if [ -e "$data_dir" ] ; then
		echo "warning: data dir '$data_dir' already exists"
		echo -n "remove it [y/N]? "
		read confirm
		[ "$confirm" = "y" ] || die "aborted"

		(set -x ; rm -rf -- "$data_dir")
	fi
}

# init a container
init_container()
{
	local name="$1"
	local data_dir="data/${name/dev-/}"

	(set -x

	# create the directory before running the container
	# so that it is owned by the current user (not by root)
	mkdir -p -- "$data_dir"


	# run the /dk/container_init script if present
	#
	# FIXME: the "sleep 1" is because of a race condition in
	# docker-compose (if the command finishes too quickly, then it
	# is run twice)
	docker-compose run --rm "$name" sh -e -c \
	  'if [ -f /dk/container_init ] ; then sleep 1; /dk/container_init ; fi'

	# start the container
	docker-compose up -d "$name"
	)
}
BAIRE Anthony's avatar
BAIRE Anthony committed
124
125


BAIRE Anthony's avatar
BAIRE Anthony committed
126
##################################################################
CAMPION Sebastien's avatar
CAMPION Sebastien committed
127

BAIRE Anthony's avatar
BAIRE Anthony committed
128
129
130

if [ ! -f docker-compose.yml ] || [ ! -f bootstrap ] ; then
	die "the 'bootstrap' script must be run from the root of the allgo repository"
CAMPION Sebastien's avatar
CAMPION Sebastien committed
131
132
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
133
134
if [ "$1" == "-h" ] ; then
	cat <<EOF
135
usage: $0 [-n|--nobuild] [CONTAINER ...]
136

BAIRE Anthony's avatar
BAIRE Anthony committed
137
138
139
140
141
142
The bootstrap script initialises the environment and the selected containers
(or by default all the containers).

If a container is already initialised, then the script asks for confirmation
before purging its data (to bootstrap it again).

143
144
145
Options:
  -n,--nobuild	do not rebuild the images

BAIRE Anthony's avatar
BAIRE Anthony committed
146
147
EOF
fi
BAIRE Anthony's avatar
BAIRE Anthony committed
148

149
150
151
152
153
154
155
if [ "$1" == "-n" ] || [ "$1" = "--nobuild" ] ; then
	NOBUILD=1
	shift
else
	NOBUILD=
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
156
# selection of the containers to be generated
BAIRE Anthony's avatar
BAIRE Anthony committed
157
158
159
160
if [ -n "$*" ] ; then
	TODO="$*"
else
	docker-compose down
BAIRE Anthony's avatar
BAIRE Anthony committed
161
	TODO="$CONTAINERS"
BAIRE Anthony's avatar
BAIRE Anthony committed
162
163
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
164
set -e
BAIRE Anthony's avatar
BAIRE Anthony committed
165

BAIRE Anthony's avatar
BAIRE Anthony committed
166
167
# purge containers (container + external volumes) that will be bootstraped
for name in $TODO
BAIRE Anthony's avatar
BAIRE Anthony committed
168
do
BAIRE Anthony's avatar
BAIRE Anthony committed
169
	purge_container "$name"
BAIRE Anthony's avatar
BAIRE Anthony committed
170
171
done

BAIRE Anthony's avatar
BAIRE Anthony committed
172
173
generate_env_file

174
175
176
if [ -z "$NOBUILD" ] ; then
	# build base image (if not present)
	(set -x ; make base-debian)
BAIRE Anthony's avatar
BAIRE Anthony committed
177

178
179
180
	# build the requested images images
	docker-compose build $TODO
fi
BAIRE Anthony's avatar
BAIRE Anthony committed
181
182
183
184
185
186
187
188

# initialise the containers
for name in $TODO
do
	init_container "$name"
done

# build the factories (if the rail container was initialised)
189
if [ -z "$NOBUILD" ] && echo "$TODO" | grep -q dev-rails ; then
BAIRE Anthony's avatar
BAIRE Anthony committed
190
191
192
	build_factories
fi

193
194
install_secrets

BAIRE Anthony's avatar
BAIRE Anthony committed
195
196
# display running containers
docker-compose ps