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

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


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
install_secrets()
BAIRE Anthony's avatar
BAIRE Anthony committed
36
{
37
	# install the tokens certificate into the registry external volume
38 39
	local src=data/django/ro/certs/tokens.crt
	local dst=data/registry/ro/certs/tokens.crt
40
	if [ -f "$src" ] && [ -d "`dirname "$dst"`" ] && [[ "$TODO" =~ dev-django|dev-registry ]] ; then
41 42
		echo "Install tokens certificate as $dst/tokens.crt"
		cp "$src" "$dst"
43
		docker-compose restart dev-registry
BAIRE Anthony's avatar
BAIRE Anthony committed
44
	fi
45 46

	# install the controller token into the controller external volume
47 48
	local src=data/django/ro/controller_token
	local dst=data/controller/ro/config.yml
49
	if [ -f "$src" ] && [ -f "$dst" ] && [[ "$TODO" =~ dev-django|dev-controller ]] ; then
50
		echo "Install controller token into $dst"
51 52
		# copy the file with a docker command because it is owned by root
		docker run --rm -i -v "$PWD/$dst:/config.yml" busybox sh -c "cat >>/config.yml" <<EOF
53 54 55 56
registry_auth: {"username": "\$token", "password": "`cat "$src"`"} 
EOF
		docker-compose restart dev-controller
	fi
BAIRE Anthony's avatar
BAIRE Anthony committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

# 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
71 72 73 74 75 76
		if [ -z "$FORCE" ] ; then
			echo "warning: data dir '$data_dir' already exists"
			echo -n "remove it [y/N]? "
			read confirm
			[ "$confirm" = "y" ] || die "aborted"
		fi
BAIRE Anthony's avatar
BAIRE Anthony committed
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

		(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
107

BAIRE Anthony's avatar
BAIRE Anthony committed
108 109 110 111 112 113 114 115 116 117 118 119
# seed the db & registry
seed()
{
	if [ -z "$NOSEED" ] ; then
		if [[ "$TODO" =~ "dev-django" ]] ; then
			django/tools/seed-dev.sh --django dev localhost:5000
		fi
		if [[ "$TODO" =~ "dev-registry" ]] ; then
			django/tools/seed-dev.sh --registry dev localhost:5000
		fi
	fi
}
BAIRE Anthony's avatar
BAIRE Anthony committed
120

BAIRE Anthony's avatar
BAIRE Anthony committed
121
##################################################################
CAMPION Sebastien's avatar
CAMPION Sebastien committed
122

BAIRE Anthony's avatar
BAIRE Anthony committed
123 124 125

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
126 127
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
128 129
if [ "$1" == "-h" ] ; then
	cat <<EOF
BAIRE Anthony's avatar
BAIRE Anthony committed
130
usage: $0 [-n|--nobuild|--noseed] [CONTAINER ...]
131

BAIRE Anthony's avatar
BAIRE Anthony committed
132 133 134 135 136 137
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).

138 139
Options:
  -n,--nobuild	do not rebuild the images
BAIRE Anthony's avatar
BAIRE Anthony committed
140
  --noseed	do not seed the db & registry
141 142 143
  --nostart	do not start the containers at the end (actually this stops the
  		containers at the end of the process because they need to be
		started to be initialised)
144 145
  --force	do not ask for confirmation before deleting data (removing
  		external volumes when bootstraping a container over an existing one)
146

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

BAIRE Anthony's avatar
BAIRE Anthony committed
150 151
NOBUILD=
NOSEED=
152
NOSTART=
153
FORCE=
BAIRE Anthony's avatar
BAIRE Anthony committed
154 155 156 157 158 159 160 161
while true ; do
	case "$1" in
	-n|--nobuild)
		NOBUILD=1
		shift;;
	--noseed)
		NOSEED=1
		shift;;
162 163 164
	--nostart)
		NOSTART=1
		shift;;
165 166 167
	--force)
		FORCE=1
		shift;;
BAIRE Anthony's avatar
BAIRE Anthony committed
168 169 170 171
	*)
		break;;
	esac
done
172

BAIRE Anthony's avatar
BAIRE Anthony committed
173
# selection of the containers to be generated
BAIRE Anthony's avatar
BAIRE Anthony committed
174 175 176 177
if [ -n "$*" ] ; then
	TODO="$*"
else
	docker-compose down
BAIRE Anthony's avatar
BAIRE Anthony committed
178
	TODO="$CONTAINERS"
BAIRE Anthony's avatar
BAIRE Anthony committed
179 180
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
181
set -e
BAIRE Anthony's avatar
BAIRE Anthony committed
182

BAIRE Anthony's avatar
BAIRE Anthony committed
183 184
# purge containers (container + external volumes) that will be bootstraped
for name in $TODO
BAIRE Anthony's avatar
BAIRE Anthony committed
185
do
BAIRE Anthony's avatar
BAIRE Anthony committed
186
	purge_container "$name"
BAIRE Anthony's avatar
BAIRE Anthony committed
187 188
done

BAIRE Anthony's avatar
BAIRE Anthony committed
189 190
generate_env_file

191 192 193
if [ -z "$NOBUILD" ] ; then
	# build base image (if not present)
	(set -x ; make base-debian)
BAIRE Anthony's avatar
BAIRE Anthony committed
194

195 196 197
	# build the requested images images
	docker-compose build $TODO
fi
BAIRE Anthony's avatar
BAIRE Anthony committed
198 199 200 201 202 203 204

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

205 206
install_secrets

207 208 209 210 211
# force restarting the nginx frontend because the IP address of
# dev-django/dev-registry may have changed (and nginx does not support IP
# address changes in upstream servers)
docker-compose restart dev-nginx

BAIRE Anthony's avatar
BAIRE Anthony committed
212 213
seed

214 215 216 217
if [ -n "$NOSTART" ] ; then
	docker-compose down
fi

BAIRE Anthony's avatar
BAIRE Anthony committed
218 219
# display running containers
docker-compose ps