-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_docker.sh
executable file
·114 lines (100 loc) · 3.37 KB
/
start_docker.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
#!/bin/sh
usage() { echo "USAGE: sh $0 [OPTIONS]
OPTIONS:
-h display help
-b <resultfile> benchmark mode: write timestamp ID to the given file once docker exits
-e <dockerimage> execute the given docker image (should be built from ./Dockerfile)
-i <path/to/image> original image file path
-o <outfilename> write start and end timestamps to the given output file
-p <outdir> write output file and work dir to the given directory
-t <thumbnail> write thumbnail with the given filename
-w <path/to/work/dir> mount this directory to the docker container -- thumbnail will be written here
-x <width> width of thumbnail -- defaults to 150
-y <height> height of thumbnail -- defailts to match width
-c crop the image to exactly fill the given thumbnail dimensions
" 1>&2; exit 1; }
BENCHFILE=
DOCKERIMG="rusty-nail-docker"
OUTFILE=
OUTDIR=
IMAGE=
THUMBNAIL=
WORKDIR=
WIDTH=150
HEIGHT=
CROP=
while getopts ":hb:e:i:o:p:t:w:x:y:c" OPT; do
case "$OPT" in
h)
usage
;;
b)
BENCHFILE="$OPTARG"
;;
e)
DOCKERIMG="$OPTARG"
;;
i)
IMAGE="$OPTARG"
;;
o)
OUTFILE="$OPTARG"
;;
p)
OUTDIR="$OPTARG"
;;
t)
THUMBNAIL="$OPTARG"
;;
w)
WORKDIR="$OPTARG"
;;
x)
WIDTH="$OPTARG"
;;
y)
HEIGHT="$OPTARG"
;;
c)
CROP="-c"
;;
*)
echo "ERROR: unknown option: $OPT"
usage
;;
esac
done
shift $((OPTIND - 1)) # isolate remaining args (which should be script filenames)
[ -n "$IMAGE" ] || { echo "ERROR: missing required argument: -i <path/to/image>"; usage; }
[ -f "$IMAGE" ] || { echo "ERROR: image file does not exist: $IMAGE"; exit 1; }
[ -n "$HEIGHT" ] || HEIGHT="$WIDTH"
TS="$(date +%s%N)" # get current time in nanoseconds -- good enough for unique timestamp
NAME="docker-$TS"
[ -n "$OUTFILE" ] || OUTFILE="$NAME.output"
[ -n "$THUMBNAIL" ] || THUMBNAIL="thumbnail.png"
[ -n "$OUTDIR" ] && mkdir -p "$OUTDIR" || OUTDIR="$(pwd)"
[ -n "$WORKDIR" ] || WORKDIR="$NAME"
OUTFILE="$OUTDIR/$OUTFILE"
WORKDIR="$OUTDIR/$WORKDIR"
mkdir -p "$WORKDIR"
ORIG="${NAME}_original.png"
ln "$IMAGE" "${WORKDIR}/${ORIG}" # can't use soft links, but don't want to copy the whole image
CWD="$(pwd)"
WORKDIR="$(cd "$(dirname "$WORKDIR")" && pwd)/$(basename "$WORKDIR")" # get absolute path
cd "$CWD"
##### BEGIN RUNNING DOCKER #####
echo "$(date +%s%N) Docker initiated" >> "$OUTFILE"
/usr/bin/time -o "$OUTFILE" --append --portability docker run --rm --user "$(id -u)":"$(id -g)" \
-v "$WORKDIR":/images -w /images \
"$DOCKERIMG" "rusty-nail" -i "$ORIG" -t "$THUMBNAIL" -x "$WIDTH" -y "$HEIGHT" $CROP >> "$OUTFILE"
ECODE=$?
END_TS="$(date +%s%N)"
if [ $ECODE -eq 0 ]; then
echo "$END_TS Docker exited successfully" >> "$OUTFILE"
[ -n "$BENCHFILE" ] && echo "$TS" >> "$BENCHFILE"
true
else
echo "$END_TS Docker exited with error code $ECODE" >> "$OUTFILE"
#[ -n "$BENCHFILE" ] && echo "$TS" >> "$BENCHFILE" # docker actually fails under load, so don't write failures as successes
true
fi