-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·110 lines (85 loc) · 2.69 KB
/
run.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
#!/usr/bin/env bash
### Source common config.
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
source "${script_dir}/config.sh"
### Utility functions.
trim_string() {
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
container_status() {
container_name="${1}"
docker inspect --format '{{.State.Status}}' "${container_name}"
}
wait_for_running() {
container_name="${1}"
echo 'Waiting for container to be running...'
while [ "$(container_status "${container_name}")" != running ]; do
sleep 1
done
}
### Set container name.
container_name_suffix=$(trim_string "${1}")
container_name="${IMAGE_NAME}"
if [ -n "${container_name_suffix}" ]; then
container_name="${container_name}_${container_name_suffix}"
fi
echo "Target container is [${container_name}]".
### Check if container exists.
container_exists=$(
docker ps --all --format '{{.Names}}' \
| grep --word-regexp "${container_name}"
)
if [ -z "${container_exists}" ]; then
echo 'Container does not exist. Creating it...'
# FIXME: Running container as user isn't working.
# Add `--user "${USER_ID}:${GROUP_ID}"` to `docker run` when it's fixed.
# FIXME: SSH bind mount target should be the container user home directory instead
# of the hardcoded `/root/.ssh`.
docker run \
-it \
--detach \
--name "${container_name}" \
--network host \
--ipc host \
--device /dev/kfd \
--device /dev/dri \
--security-opt seccomp=unconfined \
--cap-add SYS_PTRACE \
--group-add video \
--group-add render \
--shm-size=16G \
--ulimit memlock=-1 \
--ulimit stack=67108864 \
--mount "type=bind,source=${HOME},target=/triton_dev/hhome" \
--mount "type=bind,source=${HOME}/.ssh,target=/root/.ssh,readonly" \
--workdir /triton_dev \
"${IMAGE_NAME}" \
> /dev/null
else
echo 'Container already exists.'
fi
### Check the status and take appropriate actions.
status=$(container_status "${container_name}")
case "${status}" in
exited)
echo 'Container is exited. Restarting it...'
docker restart "${container_name}" > /dev/null
wait_for_running "${container_name}"
;;
paused)
echo 'Container is paused. Unpausing it...'
docker unpause "${container_name}" > /dev/null
wait_for_running "${container_name}"
;;
running)
echo 'Container is running.'
;;
*)
echo "Unexpected container state: [${status}]"
echo 'Exiting...'
exit 1
;;
esac
docker exec -it "${container_name}" bash