forked from AnyVisionltd/automation-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerize.sh
executable file
·260 lines (217 loc) · 6.94 KB
/
containerize.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
set -eu
PUBKEY_FILE=/${HOME}/.ssh/docker-builder-key
DOCKERIZE_FORCE_BUILD=${DOCKERIZE_FORCE_BUILD:-0}
# Absolute path to this script
SCRIPT=$(readlink -f "${BASH_SOURCE[0]}")
# Absolute path to the script directory
script_dir=$(dirname "$SCRIPT")
MOUNT_PATH="${MOUNT_PATH:-$(dirname "$script_dir")}"
V=${V:=0}
function _is_debug() {
if [ "$V" == "1" ];
then
echo "1"
else
echo "0"
fi
}
function debug_print() {
if [ "$V" = "1" ]; then echo "$1"; fi
}
function docker_tag () {
local current_file=${BASH_SOURCE[0]}
local current_dir=$(dirname "$current_file")
local files_sum
local total_sum
HASH_FILES=($current_dir/Dockerfile_local ${BASH_SOURCE[0]})
HASH_FILES+=($current_dir/requirements3.txt $current_dir/entrypoint.sh)
if command -v md5sum > /dev/null 2>&1; then
files_sum="$(md5sum -- ${HASH_FILES[@]} | awk {'print $1'})"
total_sum="$(md5sum <<< "$files_sum")"
elif command -v md5 > /dev/null 2>&1; then
files_sum="$(md5 -q ${HASH_FILES[@]} | awk {'print $1'})"
total_sum="$(md5 <<< "$files_sum")"
else
>&2 echo "ERROR: please install md5 nor md5sum!"
exit 1
fi
echo $total_sum | awk '{print substr($1,1,12)}'
}
function run_ssh_agent () {
if [ -e $PUBKEY_FILE ] ; then
eval "$(ssh-agent -s | grep SSH)"
ssh-add "${PUBKEY_FILE}" &>/dev/null
else
>&2 echo "couldnt PUBKEY_FILE for ssh-agent"
exit 1
fi
}
function kill_ssh_agent () {
eval "$(ssh-agent -k &>/dev/null)"
}
function _docker_image() {
local tag=$1
echo "anyvisionltd1/automation-infra:${tag}"
}
function build_docker_image () {
local tag=$1
local image_name=$(_docker_image "${tag}")
local docker_build_cache=""
if [ "${DOCKERIZE_FORCE_BUILD}" == "0" ];
then
if docker inspect --type=image "${image_name}" &>/dev/null;
then
debug_print "image found locally ${image_name}"
return
fi
debug_print "image not found locally, try to pull image from registry"
if docker pull "$image_name";
then
debug_print "image pulled ${image_name}"
return
fi
else
docker_build_cache="--no-cache"
fi
# Try to build
local build_dir=$(dirname "${BASH_SOURCE[0]}")
echo "Building docker image ${image_name}"
DOCKER_BUILDKIT=1 \
docker build ${docker_build_cache} -t "${image_name}" \
--label "service_name=automation-infra" \
--label "tag=${tag}" \
-f "$build_dir"/Dockerfile_local "$build_dir" \
--network=host
}
function kill_container() {
docker kill "$1" || debug_print "could not kill container $1"
}
function _add_mount() {
if command -v gravity > /dev/null 2>&1 && \
gravity status | grep -i 'Status:' | grep -i -q 'active'; then
echo " --volume=/host$1"
else
echo " --volume=$1"
fi
}
function _read_passed_environment() {
local current_file=${BASH_SOURCE[0]}
local current_dir=$(dirname "$current_file")
env_variable_cmd=""
while IFS='=' read -r key value; do
local value=$(eval echo "${value}")
if [ -n "$value" ];
then
env_variable_cmd+=" -e ${key}=$(eval echo "${value}")"
fi
done < "${current_dir}"/pass_environment
echo "${env_variable_cmd}"
}
function add_subdirs_from_path () {
local base_path=$1
if [ -d $base_path ]; then
local res_path=""
for file in $(ls $base_path);
do
res_path+=":$base_path/$file/automation"
if [ $file = "protobuf-contract" ]; then
file="$file/build/python"
res_path+=":$base_path/$file"
fi
done
echo $res_path
else
>&2 echo "not valid path directory"
exit 1
fi
}
function build_python_path () {
local mount_path=$1
local python_path=""
for file in $(ls $mount_path);
do
if [ $file = "protobuf-contract" ]; then
file="$file/build/python"
python_path+=":$mount_path/$file"
elif [ $file = "automation-infra" ]; then
python_path+=":$mount_path/$file"
else
python_path+=":$mount_path/$file/automation"
fi
done
echo $python_path
}
function superintend_containerize() {
echo "running superintend with PID: ${1} name: ${2}"
tail --pid ${1} -f
echo "containerize process closed.. killing container"
docker kill --signal=9 ${2}
}
function run_docker () {
local tag="$1"
local run_cmd="$2"
ps -o stat= -p $$ | grep -q + || INTERACTIVE=false
# if we do not have terminal - no need to allocate one
# this is useful when running dockerize make inside vim
[ -t 1 ] || INTERACTIVE=false
INTERACTIVE=${INTERACTIVE:-true}
# We generate a unique name in order to be able to kill the container when the terminal is closed.
NAME=$(uuidgen)
MOUNTS=("${HOME}/.ssh:${HOME}/.ssh"
"/var/run/docker.sock:/var/run/docker.sock"
"${HOME}/.local/hardware.yaml:${HOME}/.local/hardware.yaml"
"${HOME}/.docker:${HOME}/.docker"
"${HOME}/.aws:${HOME}/.aws"
"${HOME}/.npmrc:${HOME}/.npmrc"
"${HOME}/.helm:${HOME}/.helm"
"${HOME}/.ipython:${HOME}/.ipython"
"${HOME}/.habertest:$HOME/.habertest"
"$MOUNT_PATH:$MOUNT_PATH"
"/etc/localtime:/etc/localtime:ro"
"/etc/timezone:/etc/timezone:ro"
)
mount_cmd=""
for i in "${MOUNTS[@]}"
do
mount_cmd+=$(_add_mount "$i")
done
mount_cmd+=" "
python_path=$(build_python_path $MOUNT_PATH)
env_cmd+="-e PYTHONPATH=${python_path} "
# mount source code in same location as in host
cmd="docker run -t --name ${NAME} --privileged --rm --net=host --memory=8g --memory-swap=8g --cap-add=SYS_PTRACE --security-opt seccomp=unconfined $mount_cmd $env_cmd"
cmd+=" $(_read_passed_environment)"
if [ $INTERACTIVE = true ] ; then
cmd+=" -it"
fi
# set workdir as current dir
cmd+=" --workdir=${PWD}"
cmd+=" $(_docker_image "${tag}")"
if [ -n "$run_cmd" ] ; then
cmd+=" $run_cmd"
fi
PID=$$
nohup bash -c "superintend_containerize ${PID} ${NAME}" >> /tmp/superintend.out &
debug_print "Executing ${cmd}"
${cmd}
}
function main () {
export -f superintend_containerize
local cmd="$1"
debug_print "running script with command args: $cmd"
local tag=$(docker_tag)
build_docker_image "$tag"
run_docker "$tag" "$cmd"
}
cmd=${1:-"bash"}
# If just asking for help
if [ "$cmd" == "-h" ] || [ "$cmd" == "--help" ]; then
echo "Use: V=1 $(basename "$0") for debug printing"
echo "Just running $(basename "$0") will default to $(basename "$0") bash"
echo "Or use $(basename "$0") and any args which docker run will accept"
fi
# if script is sourced dont run dockerize
if [ "$0" == "$BASH_SOURCE" ]; then
main "$*"
fi