Skip to content

Commit 696df0d

Browse files
committed
feat(script): add helper script
1 parent 79d1fcc commit 696df0d

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

dock-warrior

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
#
4+
# Wrapper script for using dock-warrior
5+
#
6+
# Usage:
7+
# dock-warrior [ -b ] command args ...
8+
# Example:
9+
# dock-warrior task +ACTIVE
10+
# dock-warrior -b timew export ...
11+
#
12+
# or even:
13+
# alias task='dock-warrior task'
14+
# alias task-b='dock-warrior -b task'
15+
#
16+
# - '-b' goes into non-interactive mode.
17+
# - must set BASE
18+
19+
20+
cmd=$1
21+
shift
22+
ITCMD=-it
23+
QUIET=n
24+
# keepalive time for container
25+
SLEEPTIME=1800
26+
KEEPRUNNING=yes
27+
if [[ "${cmd}" = "-b" ]];
28+
then
29+
ITCMD=
30+
QUIET=y
31+
# pick up the real command
32+
cmd=$1
33+
shift
34+
fi
35+
BASE=/PATH/TO/DATA
36+
if [ ! -d "${BASE}" ];
37+
then
38+
echo "# $0: Script error, set BASE to point at your data." >&2
39+
exit 1
40+
fi
41+
42+
# transcript goes here
43+
LOGDIR=${BASE}/data/log
44+
NOW=$(date "+%Y-%m-%dT%H:%M:%S")
45+
TODAY=$(date "+%Y-%m-%d")
46+
LOGFILE=${LOGDIR}/${TODAY}-${HOSTNAME}.log
47+
# write to the log
48+
(echo "${NOW}: ${cmd} $@" >> ${LOGFILE} ) || exit 1 # bail if can't write logfile
49+
CONTAINER=dock-warrior
50+
IMAGE=srl295/dock-warrior:dev
51+
# make sure the container is running
52+
53+
if [[ "${KEEPRUNNING}" = "yes" ]];
54+
then
55+
containers=$(docker ps -q -f name=${CONTAINER} 2>/dev/null)
56+
if [[ "${containers}" = "" ]];
57+
then
58+
echo "# ${CONTAINER} not running, restarting" >&2
59+
# start it
60+
# https://stackoverflow.com/a/55734437/185799
61+
# TODO: if QUIET..
62+
docker rm -f ${CONTAINER} >&2 && docker >&2 run -d --name ${CONTAINER} -v ${BASE}/data:/data:rw -v ${BASE}/config:/config:rw ${IMAGE} /bin/bash -c "trap : TERM INT; sleep ${SLEEPTIME} & wait"
63+
fi
64+
exec docker exec ${ITCMD} ${CONTAINER} ${cmd} "$@"
65+
else
66+
# Don't keep the container around.
67+
exec docker run --rm ${ITCMD} -v ${BASE}/data:/data:rw -v ${BASE}/config:/config:rw ${IMAGE} ${cmd} "$@"
68+
fi

0 commit comments

Comments
 (0)