jean-cloud-services/provisioning/roles/deploy_all/files/bin/deploy_service.sh

183 lines
4.8 KiB
Bash
Raw Normal View History

2023-08-28 18:25:32 +00:00
#!/bin/bash
. driglibash-base
. /etc/jeancloud.env
2023-09-15 08:57:47 +00:00
[ ! -f /data/mounted ] && die "/data is not mounted"
2023-08-28 18:25:32 +00:00
noreload=false
deploy=true
2023-09-16 18:17:34 +00:00
service=
2023-08-28 18:25:32 +00:00
if [ "$#" -ge 2 ] && [ "$2" = noreload ] ; then
noreload=true
2023-09-29 07:51:22 +00:00
fi
if [ "$#" -ge 3 ] && [ "$3" = undeploy ] ; then
2023-08-28 18:25:32 +00:00
deploy=false
fi
2023-09-29 07:51:22 +00:00
# die "Usage: $0 <service> [no]reload [un]deploy"
2023-08-28 18:25:32 +00:00
if [ -d "/docker/$1" ] ; then
service="$1"
2023-09-07 17:50:05 +00:00
elif [ -d "$1" ] && [[ "$1" = /docker/* ]] ; then
2023-08-28 18:25:32 +00:00
service="$(basename "$1")"
else
die "/docker/$service not found"
fi
if [ ! -d "$new_nginx_conf_path" ] ; then
2023-09-29 07:51:22 +00:00
die "Cant deploy service in degraded state. $new_nginx_conf_path dir is missing, please run deployall.sh first"
2023-08-28 18:25:32 +00:00
fi
2024-01-02 16:50:14 +00:00
IFS=';' read id username _ server < <(grep ";$service;" /docker/services.csv)
2024-02-22 00:43:01 +00:00
if [ -z "$id" ] ; then
die "Service $service not found in list"
fi
2024-01-02 16:50:14 +00:00
2024-02-22 00:43:01 +00:00
uid="$(($services_uid_start + $id))"
2023-08-28 18:25:32 +00:00
docker_service="$(echo "$service" | tr '.' '_')"
driglibash_section_prefix="[$service] "
2023-09-07 17:50:05 +00:00
section "---------- Start -------------"
2023-08-28 18:25:32 +00:00
cd "/docker/$service"
2023-09-07 17:50:05 +00:00
# Source and export env file
[ -f .env ] && set -a && . .env && set +a
2023-08-28 18:25:32 +00:00
###############################################################################
# Useful directories
###############################################################################
if "$deploy" ; then
2023-12-20 17:06:09 +00:00
run mkdir -p "$DATA_DIR" "$HTTP_DIR"
2024-01-02 16:50:14 +00:00
run chown $uid "$DATA_DIR"
run chmod 751 "$DATA_DIR"
2023-12-20 17:06:09 +00:00
run chown $uid:www-data -R "$HTTP_DIR"
2024-01-02 16:50:14 +00:00
if [ -d "$SECRET_DIR" ] ; then
run chown $uid "$SECRET_DIR" -R
run chmod 751 "$SECRET_DIR" -R
fi
2023-08-28 18:25:32 +00:00
else
[ -d "$HTTP_DIR" ] && rm -r "$HTTP_DIR"
fi
###############################################################################
# Run scripts
###############################################################################
2023-12-20 17:06:09 +00:00
# Did deploy failed
returncode=0
2023-08-28 18:25:32 +00:00
if "$deploy" ; then
2023-12-20 17:06:09 +00:00
if [ -x deploy.sh ] ; then
run ./deploy.sh
[ "$?" -ne 0 ] && echo "Erreur deploy.sh" && returncode=1
fi
if [ -x deploy_user.sh ] ; then
deploy_as "$service"
[ "$?" -ne 0 ] && echo "Erreur deploy_user.sh" && returncode=1
fi
2023-08-28 18:25:32 +00:00
else
2023-12-20 17:06:09 +00:00
[ -x undeploy.sh ] && run ./undeploy.sh
2023-08-28 18:25:32 +00:00
fi
###############################################################################
# Docker containers
###############################################################################
# If there is a docker-compose file and it has services in it
if [ -f "/docker/$service/docker-compose.yml" ] && [ -n "$(grep '^[^#]*services' "/docker/$service/docker-compose.yml")" ] ; then
if $deploy ; then
section "Logging to registry"
# XXX Login to docker registry
section "Pulling images"
2023-12-20 17:06:09 +00:00
docker-compose pull
if [ "$?" -ne 0 ] ; then
echo "PULL FAILED"
fi
2023-08-28 18:25:32 +00:00
section "Starting service"
run docker-compose up -d --remove-orphans
2023-12-20 17:06:09 +00:00
[ "$?" -ne 0 ] && echo "Erreur docker compose" && returncode=1
2023-08-28 18:25:32 +00:00
else
section "Removing containers"
2023-12-20 17:06:09 +00:00
run docker-compose down --rmi all --remove-orphans
2023-08-28 18:25:32 +00:00
fi
fi
if ! "$deploy" ; then
section "Remove stray containers"
while read container ; do
2023-09-29 07:51:22 +00:00
[ -z "$container" ] && continue || true
2023-08-28 18:25:32 +00:00
echo "Removing $container"
run docker rm "$container"
done <<< "$(docker ps | grep "$docker_service" | cut -d ' ' -f 1)"
fi
###############################################################################
# wireguard interface
###############################################################################
# If there is a wireguard vpn script
for file in $( find "/docker/$service" -name "wg-*.sh") ; do
section "Managing wg interface $(basename "$file")"
if [ -x "$file" ] ; then
2024-02-22 00:43:01 +00:00
wgnum="$(basename "$file")"
wgnum="${wgnum:3:-3}"
varname="WG_NAME_$wgnum"
wgif="${!varname}"
if [ -z "$wgif" ] ; then
echo "No wireguard name for $file"
returncode=1
continue
fi
2023-08-28 18:25:32 +00:00
"$file" $wgif > "/etc/wireguard/$wgif.conf"
if "$deploy" ; then
2023-12-20 17:06:09 +00:00
run systemctl enable "wg-quick@$wgif"
run startwg.sh "$wgif"
[ "$?" -ne 0 ] && echo "Erreur wireguard" && returncode=1
2023-08-28 18:25:32 +00:00
else
if [ -z "$(ip a | grep "$wgif")" ] ; then
2023-12-20 17:06:09 +00:00
run wg-quick down "$wgif"
2023-08-28 18:25:32 +00:00
fi
fi
fi
done
###############################################################################
# Nginx conf
###############################################################################
# If there is a nginx conf file
if [ -f "/docker/$service/nginx_server.conf" ] ; then
section "Copy nginx conf"
run cp "/docker/$service/nginx_server.conf" "$new_nginx_conf_path/$service"
section "Template nginx conf with vars from '.env' file"
run template.sh "/docker/$service/.env" < "/docker/$service/nginx_server.conf" > "$new_nginx_conf_path/$service"
2023-10-31 15:42:06 +00:00
fi
2023-08-28 18:25:32 +00:00
section "Testing nginx conf"
run nginx -t -c /etc/nginx/new_nginx.conf
2023-12-20 17:06:09 +00:00
[ "$?" -ne 0 ] && echo "Erreur nginx" && returncode=1
2023-08-28 18:25:32 +00:00
if [ "$noreload" == false ] ; then
2023-12-20 17:06:09 +00:00
run restart_nginx.sh
2023-08-28 18:25:32 +00:00
fi
2024-02-22 00:43:01 +00:00
2023-08-28 18:25:32 +00:00
section "Cleaning"
2024-01-02 16:50:14 +00:00
rmdir "$DATA_DIR" "$HTTP_DIR" 2>/dev/null || true
2023-08-28 18:25:32 +00:00
2023-12-20 17:06:09 +00:00
clean
exit "$returncode"