-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
executable file
·105 lines (81 loc) · 2.45 KB
/
docker-entrypoint.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
#!/bin/sh
set -eu
WORKDIR=$(dirname "$(readlink -f "$0")")
export WORKDIR
. $WORKDIR/scripts/functions.sh
if [ -z "$INPUT_REMOTE_DOCKER_PORT" ]; then
INPUT_REMOTE_DOCKER_PORT=22
fi
if [ -z "$INPUT_REMOTE_DOCKER_HOST" ]; then
error "Input remote_docker_host is required."
fi
if [ -z "$INPUT_REMOTE_DOCKER_USERNAME" ]; then
error "Input remote_docker_username is required."
fi
if [ -z "$INPUT_SSH_PRIVATE_KEY" ]; then
error "Input ssh_private_key is required."
fi
if [ -z "$INPUT_KEEP_FILES" ]; then
INPUT_KEEP_FILES=4
else
INPUT_KEEP_FILES=$((INPUT_KEEP_FILES+1))
fi
if [ -z "$INPUT_DEPLOY_PATH" ]; then
INPUT_DEPLOY_PATH=~/docker-deployment
fi
if [ -z "$INPUT_STACK_FILE_PATH" ]; then
INPUT_STACK_FILE_PATH=docker-compose.yaml
fi
if [ ! -f "$INPUT_STACK_FILE_PATH" ]; then
error "Docker compose file \"$INPUT_STACK_FILE_PATH\" didn't exists."
fi
set +e
if ! docker compose -f "$INPUT_STACK_FILE_PATH" config >/dev/null 2>&1; then
error "Docker compose file \"$INPUT_STACK_FILE_PATH\" syntax is invalid."
fi
set -e
# Copy docker compose file to temp file
TEMP_FILE=$(mktemp)
cp "$INPUT_STACK_FILE_PATH" "$TEMP_FILE"
# Get docker compose file name (without path of folder)
STACK_LOCAL_FILE=$(basename "$INPUT_STACK_FILE_PATH")
export STACK_LOCAL_FILE
# Remplace docker compose file var by temp file
INPUT_STACK_FILE_PATH="$TEMP_FILE"
export INPUT_STACK_FILE_PATH
# Get local path to temp file
STACK_LOCAL_FOLDER=$(dirname "$INPUT_STACK_FILE_PATH")
export STACK_LOCAL_FOLDER
# Create format of user@host for ssh
DOCKER_USER_HOST="$INPUT_REMOTE_DOCKER_USERNAME@$INPUT_REMOTE_DOCKER_HOST"
export DOCKER_USER_HOST
# Create ssh config
setup_ssh
# Create docker remote config
setup_remote_docker
case $INPUT_DEPLOYMENT_MODE in
# Deploy to docker swarm
docker-swarm)
if [ -z "$INPUT_STACK_NAME" ]; then
error "Input input_stack_name is required."
fi
# Rotate secret if any
POST_SCRIPTS_FOLDER=""
if [ -n "${INPUT_SECRETS+set}" ] && [ -n "$INPUT_SECRETS" ]; then
POST_SCRIPTS_FOLDER="/opt/scripts/post"
export POST_SCRIPTS_FOLDER
"$WORKDIR/scripts/docker_secrets.sh" "$INPUT_STACK_FILE_PATH" "$INPUT_STACK_NAME" $INPUT_SECRETS
fi
"$WORKDIR/scripts/docker_swarm.sh"
;;
# Deploy to docker compose
*)
"$WORKDIR/scripts/docker_compose.sh"
;;
esac
# Execute post commands if any
if [ -n "$POST_SCRIPTS_FOLDER" ] && [ -d "$POST_SCRIPTS_FOLDER" ]; then
find "$POST_SCRIPTS_FOLDER" -type f -executable -exec sh {} \;
fi
# Delete temp file
rm "$INPUT_STACK_FILE_PATH"