-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost-exec
More file actions
executable file
·130 lines (118 loc) · 2.83 KB
/
host-exec
File metadata and controls
executable file
·130 lines (118 loc) · 2.83 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -e
if [[ -z $TOP_DIR ]]; then
echo "TOP_DIR not set"
exit 1
fi
service_name="$1"
service_program="$2"
shift 2
space=$' '
tab=$'\t'
newline=$'\n'
cr=$'\r'
# The docker-compose .env file is *not* compatible with shell. Quotes
# become part of the variable. Hence the need for this complex
# function.
compose_env_has_been_read=
declare -A compose_envvar
compose_env_read() {
[[ ! -e $1 ]] && return
local continuation= line name value
while read line; do
if [[ $continuation ]]; then
if [[ $line =~ '\\'$ ]]; then
value+="${line%\\}"
else
compose_envvar["$name"]="$value$line"
continuation=
fi
else
if [[ $line =~ ^[$space$tab]*'#' ]]; then
: #comment
elif [[ $line =~ ^[$space$tab]*([^=$space$tab]+)'='(.*)$ ]]; then
name="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
if [[ $value =~ \\$ ]]; then
value="${value%\\}"
continuation=1
else
compose_envvar["$name"]="$value"
fi
elif [[ $line = '' ]]; then
:
else
echo "Can't parse line: $line"
exit 1
fi
fi
done < "$1"
}
compose_env_load() {
compose_env_read "$TOP_DIR/.env.defaults"
compose_env_read "$TOP_DIR/.env"
compose_env_has_been_read=1
# redefine the current function, to speed up subsequence runs.
compose_env_load() { return; }
}
if [[ -z $COMPOSE_PROJECT_NAME ]]; then
compose_env_load
COMPOSE_PROJECT_NAME="${compose_envvar[COMPOSE_PROJECT_NAME]}"
fi
if [[ -z $COMPOSE_PROJECT_NAME ]]; then
COMPOSE_PROJECT_NAME="${TOP_DIR##*/}"
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME//[_ ]}"
fi
export COMPOSE_PROJECT_NAME
container_name="${COMPOSE_PROJECT_NAME}_${service_name}_1"
run_compose() {
docker-compose -f "$1" up -d "$service_name"
# TODO: implement wait for healthcheck
}
start_compose_service() {
declare file
for file in docker-compose.yml docker-compose.yaml; do
if [[ -f $TOP_DIR/$file ]]; then
run_compose "$TOP_DIR/$file"
return
fi
done
for file in docker-compose.localdev.yml docker-compose.production.yml; do
if [[ -f $TOP_DIR/$file ]]; then
ln -sf "$file" "$TOP_DIR/docker-compose.yml"
run_compose "$TOP_DIR/docker-compose.yml"
return
fi
done
echo "Could not find docker-compose.yml!" 1>&2
exit 1
}
while :; do
status="$(docker container inspect -f '{{.State.Status}}' "$container_name" 2>/dev/null)" || true
case "$status" in
(running)
break
;;
(exited)
docker container start "$container_name"
;;
('')
start_compose_service
;;
(*)
echo "Unknown status: $status"
exit 1
;;
esac
done
t_flag=-i
tty -s && t_flag="-i -t"
exec docker exec \
-e MAP_TERM="$([[ $TERM ]] && tput cols):$([[ $TERM ]] && tput lines):$TERM" \
-e MAP_TOP_DIR="$TOP_DIR" \
-e MAP_PWD="$PWD" \
-e MAP_USER="$(id)" \
-e MAP_UMASK="$(umask)" \
$t_flag "$container_name" \
/usr/local/share/container/scripts/exec "${service_program}" \
"$@"