forked from AlexsLemonade/refinebio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_image.sh
executable file
·105 lines (94 loc) · 2.71 KB
/
prepare_image.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/bash
print_description() {
echo "Prepares an image specified by -i."
echo "Must be called from the repo root."
}
print_options() {
echo "Options:"
echo " -h Prints the help message"
echo " -i IMAGE The image to be prepared. This must be specified."
echo " -s SERVICE The service to seach for a dockerfile."
echo " The default option is 'workers'"
echo " -p Pull the latest version of the image from Dockerhub"
echo " -d REPO The docker repo to pull images from."
echo " The default option is 'ccdl'"
echo
echo "Examples:"
echo " Build the image ccdl/dr_downloaders:"
echo " ./prepare_image.sh -i downloaders -d ccdl"
}
while getopts "phi:d:s:" opt; do
case $opt in
i)
image=$OPTARG
;;
d)
dockerhub_repo=$OPTARG
;;
p)
pull="True"
;;
s)
service=$OPTARG
;;
h)
print_description
echo
print_options
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_options >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
print_options >&2
exit 1
;;
esac
done
if [[ -z $image ]]; then
echo "Error: you must specify an image with -i" >&2
exit 1
fi
source common.sh
if [[ -z $service ]]; then
service="workers"
fi
if [[ -z $dockerhub_repo ]]; then
dockerhub_repo="ccdlstaging"
fi
# Default to "local" for system version if we're not running in the cloud.
if [[ -z $SYSTEM_VERSION ]]; then
SYSTEM_VERSION="local$(date +%s)"
fi
# We want to check if a test image has been built for this branch. If
# it has we should use that rather than building it slowly.
image_name="$dockerhub_repo/dr_$image"
if [[ "$(docker_img_exists $image_name $branch_name)" ]] ; then
docker pull $image_name:$branch_name
elif [[ ! -z $pull ]]; then
docker pull $image_name
else
echo ""
echo "Rebuilding the $image_name image."
finished=1
attempts=0
while (( finished != 0 && attempts < 3 )); do
if (( attempts > 0 )); then
echo "Failed to build $image_name, trying again."
fi
docker build \
-t "$image_name" \
-f $service/dockerfiles/Dockerfile.$image \
--build-arg SYSTEM_VERSION=$SYSTEM_VERSION .
finished=$?
attempts=$[$attempts+1]
done
if (( finished != 0 && attempts >= 3 )); then
echo "Could not build $image_name after three attempts."
exit 1
fi
fi