-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer-actions.sh
More file actions
executable file
·178 lines (150 loc) · 4.82 KB
/
container-actions.sh
File metadata and controls
executable file
·178 lines (150 loc) · 4.82 KB
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
#!/bin/bash
# Build a container if necessary and run one of the following by specifying it as an argument
#
# setup-dev - setup the container with all the repos, etc for a development environment, which exits after completion
# bash - a simple command prompt that also keeps the container running
# test (or blank) - run the full test suite
#
# optionally specify 'clean' as an argument to clean up the containers, source code and database before starting
# or 'clean-output' to just clean the source code and database
#
# For example:
# Run the test suite
# ./test.sh
# Setup the dev environment
# ./test.sh setup-dev
# Run bash terminal and leave the container running
# (VSCode can connect directly, or see the build-vars-sample.sh file for SSH details)
# ./test.sh bash
cd -P -- "$(dirname -- "$0")"
script_args="$@"
source shared/build-vars.sh
CONTAINER_NAME=${USER}-restructure-test
function has_arg() {
for i in ${script_args}; do
if [ "$1" == "${i}" ]; then
echo ${i}
return 0
fi
done
return 1
}
function args_excluding() {
echo ${script_args} | sed 's/\b'$1'\b//g'
}
if ! groups | grep -q '\bdocker\b'; then
cat << EOF
Current user is not a member of the 'docker' group.
Create the docker group.
sudo groupadd docker
Add your user to the docker group.
sudo usermod -aG docker $USER
Run the following to activate the changes to groups.
newgrp docker
EOF
exit 2
fi
if [ ! -s shared/.netrc ]; then
echo "shared/.netrc file is not set up. See README.md for more info."
exit
fi
if [ ! -s shared/build-vars.sh ]; then
echo "shared/build-vars.sh file is not set up. See README.md for more info."
exit
fi
if [ "$(which modprobe)" ]; then
# This only makes sense on Linux
modprobe fuse
if [ $? != 0 ]; then
echo "Failed to modprobe fuse - this is required to be installed on the host"
exit
fi
fi
if has_arg 'clean' || has_arg 'clean-only'; then
echo 'Clean has been requested - will start in 5 seconds - Ctrl-C to exit now'
sleep 5
docker container rm ${CONTAINER_NAME} --force
docker image rm consected/restructure-test --force
sleep 5
if has_arg 'clean-only'; then
echo 'clean "only" completed'
exit
fi
fi
if [ -z "$(docker images | grep consected/restructure-test)" ]; then
docker build . -t consected/restructure-test
fi
if [ -z "$(docker images | grep consected/restructure-test)" ]; then
echo Container not available
else
C_EXTRA_ARG='-t'
if has_arg 'setup-dev'; then
echo 'Setup dev'
C_CMD="/shared/test-restructure.sh setup-dev"
CAN_CLEAN=true
elif has_arg 'bash'; then
echo 'Execute bash'
C_CMD=/shared/run-dev.sh
elif [ -z "$(args_excluding 'clean|clean-output')" ] || has_arg 'test'; then
echo 'Run parallel test suite'
C_CMD="/shared/test-restructure.sh test"
CAN_CLEAN=true
unset C_EXTRA_ARG
elif has_arg 'interactive'; then
C_CMD="$(args_excluding 'clean|clean-output|interactive')"
echo "Execute alternative command interactively: ${C_CMD}"
else
C_CMD="$(args_excluding 'clean|clean-output')"
echo "Execute alternative command: ${C_CMD}"
unset C_EXTRA_ARG
fi
if [ "${CAN_CLEAN}" ]; then
if has_arg 'clean' || has_arg 'clean-output'; then
C_CMD="${C_CMD} clean-output"
fi
fi
MUST_RUN=true
# Does a container exist? If not, we must run it
STATUS=$(./status.sh)
if [ "${STATUS}" == 'running' ] || [ "${STATUS}" == 'stopped' ] || [ "${STATUS}" == 'other' ]; then
unset MUST_RUN
if [ "${STATUS}" == 'stopped' ]; then
# A container exists but is not started: we must start it
echo "Starting container"
docker container start ${CONTAINER_NAME}
while [ "$(./status.sh)" == 'stopped' ]; do
sleep 2
echo "Waiting for container to start"
done
fi
# In the running container, execute a command if one has been specified, otherwise just attach to the container
if [ "${C_CMD}" ]; then
echo "Executing command in running container: ${C_EXTRA_ARG} ${C_CMD}"
docker exec -i ${C_EXTRA_ARG} ${CONTAINER_NAME} ${C_CMD}
else
echo "Attaching to running container"
docker attach ${CONTAINER_NAME}
fi
fi
if [ "${MUST_RUN}" ]; then
# Run the container from the image consected/restructure-test, and call the specified command
echo "Running container because it is not yet running: $(./status.sh)"
# ${C_EXTRA_ARG}
docker run -dt \
--name=${CONTAINER_NAME} ${MAPPED_PORTS} \
--device /dev/fuse \
--cap-add SYS_ADMIN \
consected/restructure-test
RES=$?
if [ $? != 0 ]; then
echo 'Failed to run container'
echo "Result: ${RES}"
exit 7
fi
while [ "$(./status.sh)" != 'created' ] && [ "$(./status.sh)" != 'running' ]; do
sleep 2
echo "Waiting for run to complete: $(./status.sh)"
done
./container-actions.sh $(args_excluding 'clean')
fi
fi