-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_addon.sh
executable file
·156 lines (127 loc) · 3.74 KB
/
run_addon.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Error trapping from https://gist.github.com/oldratlee/902ad9a398affca37bfcfab64612e7d1
__error_trapper() {
local parent_lineno="$1"
local code="$2"
local commands="$3"
echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR
set -euE -o pipefail
shopt -s failglob
exec 2>&1
maindir="$(readlink -f "$(dirname "$0")")"
lbcsdir="$(dirname "$(readlink -f "$0")")"
if [[ ! ${1-} ]]
then
echo "Need container name as first argument."
exit 1
fi
if [[ ! ${2-} ]]
then
echo "Need addon name as second argument."
exit 1
fi
container="$1"
containerdir="$maindir/containers/$container"
addon="$2"
addondir="$containerdir/addons/$addon"
if [[ ! -d $containerdir ]]
then
echo "Can't find container dir $containerdir"
exit 1
fi
if [[ ! -d $addondir ]]
then
echo "Can't find addon dir $addondir"
exit 1
fi
# Make shellcheck happy
name=''
run_program=''
after_containers=''
# shellcheck disable=SC1091
. "$lbcsdir/config"
# shellcheck disable=SC1091
. "$maindir/config"
if [[ -f $maindir/secrets ]]
then
# shellcheck disable=SC1091
. "$maindir/secrets"
fi
# shellcheck disable=SC1091
. "$containerdir/config"
if [[ -f $containerdir/secrets ]]
then
# shellcheck disable=SC1091
. "$containerdir/secrets"
fi
# shellcheck disable=SC1091
. "$addondir/config"
if [[ -f $addondir/secrets ]]
then
# shellcheck disable=SC1091
. "$addondir/secrets"
fi
if [[ ! ${bundle-} ]]
then
echo "No bundle name (tag 'bundle') found in $maindir/config ; please set. (Used to be called 'service'.)"
exit 1
fi
if [[ $bundle = "$name" ]]
then
echo "The bundle name ($bundle) and the addon name ($name) can't be the same; modify one of the config files to fix please."
exit 1
fi
if [[ ! ${run_program-} ]]
then
echo "No addon run program (tag 'run_program') found in $addondir/config ; there's no point in having an addon without one."
exit 1
fi
after_containers="$after_containers $container"
for after_container in $after_containers
do
# shellcheck disable=SC2034
for num in $(seq 1 60)
do
if [[ $($CONTAINER_BIN container inspect --format '{{.State.Status}}' "$after_container") == 'running' ]]
then
break
fi
echo -e "\nWaiting for required container $after_container to start.\n"
sleep 5
done
if [[ $($CONTAINER_BIN container inspect --format '{{.State.Status}}' "$after_container") == 'running' ]]
then
echo -e "\nRequired container $after_container has started.\n"
else
echo -e "\nRequired container $after_container has not started; exiting.\n"
exit 1
fi
done
if [[ ${files_to_erb_on_run-} ]]
then
echo -e "\nERBing runtime files\n"
for file in $files_to_erb_on_run
do
echo "ERBing $maindir/$file.erb to $maindir/$file"
"$lbcsdir/lbcserb" "$maindir" "$lbcsdir" "$container" "$maindir/$file.erb" "$maindir/$file" addon "$addon"
done
fi
if [[ ${run_pre_script-} ]]
then
echo -e "\nRunning pre-script for addon $addon in container $container\n"
bash -c "$(eval "$run_pre_script")"
echo -e "\nDone running pre-script for addon $addon in container $container\n"
fi
echo -e "\nRunning addon $name for container $container in bundle $bundle\n"
# Need the eval to expand variables in $run_args itself; probably a better way
# to do this but meh
eval "$CONTAINER_BIN" exec -it "$container" "$run_program" 2>&1
if [[ ${run_post_script-} ]]
then
echo -e "\nRunning post-script for addon $addon in container $container\n"
bash -c "$(eval "$run_post_script")"
echo -e "\nDone running post-script for addon $addon in container $container\n"
fi
echo "Running addon $addon complete."