-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_emacs_image.sh
executable file
·88 lines (72 loc) · 2.11 KB
/
run_emacs_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
#!/usr/bin/env bash
# Note: this script need to be in the parent folder, not in tests/
# because it runs a container with $PWD as bind-mount, and relies on
# both tests/learn-ocaml-tests.el and learn-ocaml.el
# This file contains the Server Container ID (gen by ./run_test_backend.sh)
fcid="$PWD/learn-ocaml-server.pid"
# This file contains the Emacs Container ID (used by ./stop_emacs_image.sh)
feid="$PWD/learn-ocaml-emacs.pid"
# Print $1 in green
green () {
echo -e "\\e[32m$1\\e[0m"
}
# Print $1 in red
red () {
echo -e "\\e[31m$1\\e[0m"
}
green "Beforehand: EMACS_IMAGE_VERSION=$EMACS_IMAGE_VERSION"
# Default emacs image
: "${EMACS_IMAGE_VERSION:=pfitaxel/emacs-learn-ocaml-client:oauth-moodle-dev}"
# Do "export EMACS_IMAGE_VERSION=…" before running the script to override
green "Henceforth: EMACS_IMAGE_VERSION=$EMACS_IMAGE_VERSION\\n"
pull_ifneedbe () {
sudo docker pull "$EMACS_IMAGE_VERSION"
ret=$?
if [ "$ret" -ne 0 ]; then
red "PROBLEM, 'sudo docker pull $EMACS_IMAGE_VERSION' failed with exit status $ret"
exit $ret
fi
}
gen_emacs_cid () {
if [ -f "$feid" ]; then
red >&2 "Error: file '$feid' already exists: container is running?"
exit 1
fi
echo "learn-ocaml-emacs-$$" >"$feid"
eid=$(<"$feid")
}
read_cid () {
if [ -f "$fcid" ]; then
cid=$(<"$fcid")
else
red >&2 "Error: file '$fcid' does not exist."
exit 1
fi
}
stop_emacs () {
green "Stopping emacs..."
( set -x && \
rm -f "$feid" && \
sudo docker logs "$eid"; \
sudo docker stop "$eid" )
}
run_emacs () {
local oldopt; oldopt="$(set +o)"; set -x
# Run the image in background
sudo docker run -d -i --init --rm --name="$eid" \
-v "$PWD:/build" --network="container:$cid" \
"$EMACS_IMAGE_VERSION"
ret=$?
# hacky but working
sleep 2s
set +vx; eval "$oldopt" # has to be after "ret=$?"
if [ "$ret" -ne 0 ]; then
red "PROBLEM, 'sudo docker run -d ... $EMACS_IMAGE_VERSION' failed with exit status $ret"
stop_emacs
exit $ret
fi
}
pull_ifneedbe
read_cid
gen_emacs_cid
run_emacs