-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdocker_lucid.sh
executable file
·414 lines (389 loc) · 12.8 KB
/
docker_lucid.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#### Dockerize lucid build environment ####
# This script compiles and executes Lucid in a docker image,
# then prints or copies the output back to your machine.
# requirements: docker
USAGE=$(cat <<-END
Usage:
./docker_lucid.sh interp <prog.dpt> -- run lucid interpreter inside of the docker image.
./docker_lucid.sh compile <prog.dpt> -- run lucid compiler inside the docker image.
./docker_lucid.sh rebuild -- rebuild the lucid compiler in your docker image, from this local repo.
./docker_lucid.sh pull -- pull most recently published version of lucid image.
./docker_lucid.sh enter_dev -- enter a lucid development image where the local repo is mounted at /lucid.
./docker_lucid.sh pull_dev -- pull most recently published version of lucid dev image.
./docker_lucid.sh rebuild_and_push -- rebuild docker image and publish (admin only)
./docker_lucid.sh rebuild_and_push_dev -- rebuild docker image and publish (admin only)
END
)
DOCKER_IMAGE="jsonch/lucid:lucid"
DOCKER_DEV_IMAGE="jsonch/lucid:lucid_dev"
DOCKER_CMD="docker run --rm -it"
DOCKER_PWD="/app"
ensure_docker()
{
if ! command -v docker -v &> /dev/null
then
echo "docker not found. Please install. Desktop version: https://www.docker.com/products/docker-desktop/"
exit 1
fi
}
check_lucid_dir()
{
if [ -f "./dpt.opam" ];
then
echo 1 > /dev/null
else
echo "error: you must run this command in the root of the lucid repo."
exit 1
fi
}
to_parent_dir()
{
cd $1 && cd ../ && pwd
}
is_file_or_dir()
{
retval=1
if [ -d "${1}" ] || [ -f "${1}" ]
then
retval=0
fi
return $retval
}
to_absolute_path()
{
ABSOLUTE_PATH=$(cd $(dirname ${1}); pwd)
BASE=$(basename ${1})
echo $ABSOLUTE_PATH/$BASE
}
strip_whitespace()
{
echo $1 | tr -d '[:space:]'
}
# get the first-level includes of a file
# we can't mount all the files at once, because
# we don't know which files to mount yet
get_includes()
{
DOCKER_UTIL="bin/dockerUtils includes"
next=$1
# >&2 echo "get_includes is processing: $next"
# local and remote mount points -- you can still use next as the arg
local=$(to_absolute_path $next)
remote="$DOCKER_PWD/$next"
# >&2 echo "get_includes local: $local"
# >&2 echo "get_includes remote: $remote"
# GICMD="$DOCKER_CMD $(mount_file_str ${next}) /bin/sh -c \""
# command to get includes of curent file
GICMD="docker run --rm -it --mount type=bind,source=$local,target=$remote $DOCKER_IMAGE /bin/sh -c \"$DOCKER_UTIL $next\""
# >&2 echo "GICMD:$GICMD"
next_includes=$(eval "$GICMD")
rv=$?
if [ "$rv" -ne "0" ]; then
>&2 echo "*** docker wrapper error *** parse error in file: $next"
>&2 echo "$next_includes"
>&2 echo "*** end docker wrapper error ***"
exit $rv
fi
next_includes=$(strip_whitespace $next_includes)
# `docker run --rm -it --mount type=bind,source=$local,target=$remote $DOCKER_IMAGE /bin/sh -c "bin/dockerUtils includes $next"`
# >&2 echo "result: $next_includes"
echo "$next_includes"
return 0
}
# get included files, recursively to any depth
get_all_includes()
{
src=$1
# >&2 echo "get_all_includes is processing: $src"
src_includes_str=$(get_includes $src)
rv=$?; if [ "$rv" -ne "0" ]; then return $rv; fi
# make sure the operation was a success.
IFS=':' read -ra incl_arr <<< "$src_includes_str"
for dep_src in "${incl_arr[@]}"; do
# >&2 echo "get_all_includes is processing dependency: $dep_src"
# get dependencies of dependent
recursive_includes_str=$(get_all_includes $dep_src)
rv=$?
if [ "$rv" -ne "0" ]; then return $rv; fi
# if there are any 2nd-level dependencies, add them
if [ -z "$recursive_includes_str" ]
then
src_includes_str="$src_includes_str"
else
src_includes_str="$src_includes_str:$recursive_includes_str"
fi
# >&2 echo "src_includes_str from $src-- $src_includes_str"
done
echo "$src_includes_str"
return 0
}
# get the main source file
get_main()
{
DOCKER_PARSE="bin/dockerUtils main"
# echo "docker run --rm -it $DOCKER_IMAGE /bin/sh -c \"$DOCKER_PARSE $@\""
main=`docker run --rm -it $DOCKER_IMAGE /bin/sh -c "$DOCKER_PARSE $@"`
main=$(strip_whitespace $main)
echo "$main"
}
# return a space-delimited list
# of the source files used by the program
get_all_sources()
{
main=$(get_main $@)
# 1. figure out name of main file.
all_includes_str=$(get_all_includes $main)
rv=$?; if [ "$rv" -ne "0" ]; then return $rv; fi
all_files_str="$main:$all_includes_str"
IFS=':' read -ra all_files_arr <<< "$all_files_str"
# unique elements of array
IFS=" " read -r -a all_files_arr <<< "$(echo "${all_files_arr[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
echo "${all_files_arr[@]}"
return 0
}
# get the docker-local filename of a file passed as an argument to this script
docker_local_fn ()
{
remote="/app/inputs/$(basename ${1})"
echo "$remote"
}
# get the absolute local filename of a file passed as an argument to this script
local_abs_fn ()
{
local=$(to_absolute_path "$1")
echo "$local"
}
mount_file_str ()
{
echo "--mount type=bind,source=$(local_abs_fn ${1}),target=$(docker_local_fn ${1})"
}
interpret_cmd ()
{
main=$(get_main $@)
sources_str=$(get_all_sources $@)
rv=$?; if [ "$rv" -ne "0" ]; then return $rv; fi
IFS=' ' read -ra sources <<< "$sources_str"
>&2 echo "program: $main"
>&2 echo "sources: $sources_str"
SPEC_FOUND=0
ARGS=""
MOUNT=""
COMPILER="./dpt"
while [[ $# -gt 0 ]]; do
# skip main
if [[ "$1" == "$main" ]]
then
# >&2 echo "skipping main arg"
# replace with argument: docker local file
ARGS="$ARGS $(docker_local_fn ${1})"
# mount all of the sources to docker
for src in "${sources[@]}"; do
MOUNT="$MOUNT $(mount_file_str ${src})"
done
shift
else
case $1 in
--spec)
SPEC_FOUND=1
ARGS="$ARGS --spec $(docker_local_fn ${2})"
MOUNT="$MOUNT $(mount_file_str ${2})"
shift
shift
;;
--symb)
ARGS="$ARGS --symb $(docker_local_fn ${2})"
MOUNT="$MOUNT $(mount_file_str ${2})"
shift
shift
;;
# everything else: just a flag. append to arg.
*)
ARGS="$ARGS $1"
shift
;;
esac
fi
done
# if there is no provided spec file, check for a <src>.json
if [[ $SPEC_FOUND == 0 ]]
then
>&2 echo "no interp spec file provided"
spec=${main%.dpt}.json
>&2 echo "checking if spec exists: $spec"
if [[ -f "$spec" ]]
then
>&2 echo "spec exists, adding args"
ARGS="$ARGS --spec $(docker_local_fn ${spec})"
MOUNT="$MOUNT $(mount_file_str ${spec})"
fi
fi
# finally, prepare the command string.
CMD="$DOCKER_CMD $MOUNT $DOCKER_IMAGE /bin/sh -c \"$COMPILER$ARGS\""
>&2 echo "running command: $CMD"
eval $CMD
}
# build dir is global -- we want to print it out at the end as it might be derived.
BUILD_DIR=""
compile_cmd ()
{
main=$(get_main $@)
sources_str=$(get_all_sources $@)
rv=$?; if [ "$rv" -ne "0" ]; then return $rv; fi
IFS=' ' read -ra sources <<< "$sources_str"
SPEC_FOUND=0
ARGS=""
MOUNT=""
COMPILER="./dptc"
while [[ $# -gt 0 ]]; do
if [[ "$1" == "$main" ]]
then
# replace main filename with docker local filename
ARGS="$ARGS $(docker_local_fn ${1})"
# mount all of the sources to docker (incl main)
for src in "${sources[@]}"; do
MOUNT="$MOUNT $(mount_file_str ${src})"
done
shift
else
case $1 in
# --spec)
# SPEC_FOUND=1
# ARGS="$ARGS --spec $(docker_local_fn ${2})"
# MOUNT="$MOUNT $(mount_file_str ${2})"
# shift
# shift
# ;;
# output directory, process this at the end.
-o)
BUILD_DIR="$2"
shift
shift
;;
# everything else: just a flag. append to arg.
*)
ARGS="$ARGS $1"
shift
;;
esac
fi
done
# if there is no provided spec file, check for a <src>.json
# if [[ $SPEC_FOUND == 0 ]]
# then
# >&2 echo "no interp spec file provided"
# spec=${main%.dpt}.json
# >&2 echo "checking if spec exists: $spec"
# if [[ -f "$spec" ]]
# then
# >&2 echo "spec exists, adding args"
# ARGS="$ARGS --spec $(docker_local_fn ${spec})"
# MOUNT="$MOUNT $(mount_file_str ${spec})"
# fi
# fi
# build directory arg.
# 1. if no build is given, create a default.
if [[ $BUILD_DIR == "" ]]
then
BUILD_DIR=${main%.dpt}_build
fi
>&2 echo "build dir: $BUILD_DIR"
# 2. if build dir doesn't exist locally, create it.
if [ -f "${BUILD_DIR}" ]
then
echo "ERROR: build directory $BUILD_DIR is an already existing FILE."
exit 1
fi
if [ ! -d "${BUILD_DIR}" ]
then
mkdir -p "$BUILD_DIR"
fi
# 3. get the absolute path to the build directory.
BUILD_DIR=$(to_absolute_path "$BUILD_DIR")
# 4. build in a staging directory, then copy over to where $BUILD_DIR is mounted
BUILD_STAGE_DIR="/app/build"
BUILD_FINAL_DIR="/app/final_build"
# mount the final build dir
MOUNT="$MOUNT -v $BUILD_DIR:$BUILD_FINAL_DIR"
# 5. add staging dir as argument to compiler
ARGS="$ARGS -o $BUILD_STAGE_DIR"
# 6. after compiler finishes, copy staging build to final build.
COPY_TO_FINAL="rm -rf $BUILD_FINAL_DIR/*; mv $BUILD_STAGE_DIR/* $BUILD_FINAL_DIR/"
>&2 echo "build dir: $BUILD_DIR"
CMD="$DOCKER_CMD $MOUNT $DOCKER_IMAGE /bin/sh -c \"$COMPILER$ARGS;$COPY_TO_FINAL\""
>&2 echo "compiler CMD:$CMD"
eval $CMD
echo "local p4 build is in: $BUILD_DIR"
}
# --- MAIN ---
ensure_docker
case $1 in
# get latest from docker hub
pull)
shift
CMD="docker pull $DOCKER_IMAGE"
eval "$CMD"
;;
interp)
shift
interpret_cmd "$@"
;;
interpret)
shift
interpret_cmd "$@"
;;
compile)
shift
compile_cmd "$@"
;;
# rebuild lucid -- run when you change lucid's source code.
rebuild)
shift
check_lucid_dir
# note: this command builds the final image defined in the local dockerfile (.) and tags it as
# $DOCKER_IMAGE. _even if the dockerfile does not name the final image $DOCKER_IMAGE_.
CMD="docker build -t $DOCKER_IMAGE ."
eval "$CMD"
;;
# (admin only) rebuild a new lucid binary and push to docker hub.
# note: this relies on the dev image, so if the dependencies have changed,
# that image should be updated first.
rebuild_and_push)
shift
check_lucid_dir
CMD="docker build -t $DOCKER_IMAGE . && docker push $DOCKER_IMAGE"
eval "$CMD"
;;
# enter an instance of the lucid dev image
# with the directory that this script is in mounted to /lucid
enter_dev)
# directory that this script is in
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# directory of the lucid repo
mount_args="-v $SCRIPT_DIR:/lucid"
cmd="$DOCKER_CMD --workdir /lucid $mount_args $DOCKER_DEV_IMAGE"
# echo "command: $cmd"
eval "$cmd"
;;
# pull the base version of the dev image. Use this if you mess up
# your local copy of the dev image
pull_dev)
shift
CMD="docker pull $DOCKER_DEV_IMAGE"
eval "$CMD"
;;
# (admin only) rebuild the lucid dev image and push to public repo --
# the dev image is used to build lucid for the main image, and for
# local development. It does not have a local copy of lucid,
# but rather just lucid's dependencies.
# So the dev image should only change when lucid's dependencies change.
rebuild_and_push_dev)
shift
check_lucid_dir
eval "docker build --target lucid_dev --tag jsonch/lucid:lucid_dev . && docker push jsonch/lucid:lucid_dev"
;;
*)
echo "unknown command argument."
echo "$USAGE"
exit 1;
;;
esac