-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeployment.sh
executable file
·101 lines (85 loc) · 2.7 KB
/
deployment.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
#!/usr/bin/env bash
error()
{
echo "Error:" "$@" 1>&2
}
# If Docker socket is not mounted
if [[ ! -S /var/run/docker.sock ]] ; then
error "Please bind mount in the docker socket to /var/run/docker.sock"
error "docker run -v /var/run/docker.sock:/var/run/docker.sock"
error "...or make sure you have access to the docker socket at /var/run/docker.sock"
exit 1
fi
ENV=".env"
source ".env"
remove_container_and_untag_image() {
local NAME=$1
local IMAGE=$2
echo "Removing the container $NAME (if it exists)..."
docker container stop $NAME >/dev/null 2>&1
docker container rm $NAME >/dev/null 2>&1
echo "Untagging the image $IMAGE..."
docker image rm $IMAGE >/dev/null 2>&1
}
# Check if the -r flag is provided
REMOVE=false
while getopts ":r" opt; do
case ${opt} in
r)
REMOVE=true
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
remove_container_and_untag_image() {
local NAME=$1
local IMAGE=$2
echo "Removing the container $NAME (if it exists)..."
docker container stop $NAME >/dev/null 2>&1
docker container rm $NAME >/dev/null 2>&1
echo "Untagging the image $IMAGE..."
docker image rm $IMAGE >/dev/null 2>&1
}
# Check if the -r flag is provided
REMOVE=false
while getopts ":r" opt; do
case ${opt} in
r)
REMOVE=true
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
# Create volumes used by the profile
# https://stackoverflow.com/a/45674488/13179591
VOLUMES=("rabbitmq" "postgres" "keycloak" "prometheus")
for volume in "${VOLUMES[@]}"
do
docker volume create --name "${volume}"-"${PROFILE}"
done
# Create the network for the profile
NETWORK="carp-${PROFILE}"
docker network inspect "${NETWORK}" >/dev/null 2>&1 || docker network create --driver bridge "${NETWORK}"
# Prepare database script
# https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files/112024#112024
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sed -i "s/ OWNER = .*/ OWNER = ${ADMIN_USER}/g" ./deployment/postgres/init.sql || exit 1
elif [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/ OWNER = .*/ OWNER = ${ADMIN_USER}/g" ./deployment/postgres/init.sql || exit 1
fi
if [ "$REMOVE" = true ]; then
# If the -r flag is provided, remove the container and untag the image
remove_container_and_untag_image "carp-ws-${PROFILE}" "carp-ws:${profile}"
fi
# Instantiate containers
# https://docs.docker.com/compose/environment-variables/
# https://docs.docker.com/compose/profiles/
docker compose --profile "${PROFILE}" -p "carp-webservices-${PROFILE}" up -d