-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdocker-entrypoint.sh
executable file
·137 lines (123 loc) · 3.95 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
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
#!/bin/sh
set -u
readonly CHARS_LINE="============================"
readonly RC_IMAGE_TAG="rc-cli"
readonly MODEL_BUILD_TIMEOUT=$((12*60*60))
readonly MODEL_APPLY_TIMEOUT=$((4*60*60))
readonly APP_DEST_MNT="/home/app/data"
wait_for_docker() {
while ! docker ps; do
sleep 1
done
}
#######################################
# Load a Docker image created by rc-cli.
# Globals:
# None
# Arguments:
# image_file
# Returns:
# None
#######################################
load_image() {
image_file=$1
printf "Loading the Image... "
load_stdout=$(docker load --quiet --input "/mnt/${image_file}" 2> /dev/null)
old_image_tag="${load_stdout:14}"
new_image_tag="${image_file:0:-7}:${RC_IMAGE_TAG}"
# Force the image tag to be that of the tar archive filename.
if [ "${old_image_tag}" != "${new_image_tag}" ]; then
docker tag ${old_image_tag} ${new_image_tag}
docker rmi ${old_image_tag} > /dev/null 2>&1
fi
printf "done\n"
}
# Convert a number of seconds to the ISO 8601 standard.
secs_to_iso_8601() {
printf "%dh:%dm:%ds" $(($1 / 3600)) $(($1 % 3600 / 60)) $(($1 % 60))
}
# Get a status message from a given stderr value
get_status() {
error=$1
case ${error} in
Killed)
printf "\nWARNING! production-test: Timeout has occurred when running '$1'\n" >&2
printf "timeout"
;;
"")
printf "success"
;;
*)
printf "\n${error}\n" >&2
printf "failure"
;;
esac
}
#######################################
# Send the output and time stats of the running app container
# to the standard output and a given output file.
# Globals:
# None
# Arguments:
# secs, error, out_file
# Returns:
# None
#######################################
print_stdout_stats() {
secs=$1
error=$2
out_file=$3
printf "{ \"time\": ${secs}, \"status\": \"$(get_status "${error}")\" }" > ${out_file}
printf "\nTime Elapsed: $(secs_to_iso_8601 ${secs})\n"
}
#######################################
# Run a snapshot (Docker image) for a given 'model-*' command
# Globals:
# None
# Arguments:
# cmd, image_name, timeout_in_secs, run_opts
# Returns:
# None
#######################################
run_app_image() {
cmd=$1
image_name=$2
timeout_in_secs=$3
run_opts=$4
printf "\n${CHARS_LINE}\n"
printf "Running the Image [${image_name}] (${cmd}):\n\n"
start_time=$(date +%s)
# TODO: Improve redirection to avoid using a file for stderr
timeout -s KILL ${timeout_in_secs} \
docker run --rm --entrypoint "${cmd}.sh" ${run_opts} \
--volume "/data/${cmd}_inputs:${APP_DEST_MNT}/${cmd}_inputs:ro" \
--volume "/data/${cmd}_outputs:${APP_DEST_MNT}/${cmd}_outputs" \
${image_name}:${RC_IMAGE_TAG} 2>/var/tmp/error
secs=$(($(date +%s) - start_time))
[ -f /var/tmp/error ] && error=$(cat /var/tmp/error) || error=""
print_stdout_stats "${secs}" "${error}" \
"/data/model_score_timings/${cmd}_time.json"
}
printf "Starting the Docker daemon... "
/usr/local/bin/dockerd-entrypoint.sh dockerd > /dev/null 2>&1 &
wait_for_docker > /dev/null 2>&1
printf "done\n"
load_image ${IMAGE_FILE}
image_name=${IMAGE_FILE:0:-7} # Remove the '.tar.gz' extension
run_app_image "model_build" ${image_name} ${MODEL_BUILD_TIMEOUT} ""
run_app_image "model_apply" ${image_name} ${MODEL_APPLY_TIMEOUT} \
"--volume /data/model_build_outputs:${APP_DEST_MNT}/model_build_outputs:ro"
printf "\n"
load_image ${SCORING_IMAGE}
scoring_name=${SCORING_IMAGE:0:-7}
printf "\n${CHARS_LINE}\n"
printf "Running the Scoring Image [${scoring_name}]:\n\n"
# The time stats file is mounted in a different directory
docker run --rm \
--volume "/data/model_apply_inputs:${APP_DEST_MNT}/model_apply_inputs:ro" \
--volume "/data/model_apply_outputs:${APP_DEST_MNT}/model_apply_outputs:ro" \
--volume "/data/model_score_inputs:${APP_DEST_MNT}/model_score_inputs:ro" \
--volume "/data/model_score_timings:${APP_DEST_MNT}/model_score_timings:ro" \
--volume "/data/model_score_outputs:${APP_DEST_MNT}/model_score_outputs" \
${scoring_name}:${RC_IMAGE_TAG}
exec "$@"