-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm
executable file
·55 lines (46 loc) · 1.08 KB
/
vm
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
#!/usr/bin/env bash
docker ps > /dev/null || exit 255
version=$(basename "$0")
version=${version#vm}
version=${version#-}
if [ ! "$version" ]; then
version='latest'
fi
name="vm_${version}_$(pwd | sha1sum | awk '{print $1}')"
argv=()
while [[ $# -gt 0 ]]; do
if [[ $1 == -* ]]; then
argv+=("$1 $2")
shift
shift
else
break
fi
done
args="${argv[*]}"
cmd="zsh"
if [ $# -gt 0 ]; then
cmd="$*"
fi
>&2 echo "name: $name"
>&2 echo "args: $args"
>&2 echo "cmd: $cmd"
exist=$(docker ps -q -f "name=$name")
already_run=$(pgrep -af 'docker run' | grep -c "$name")
if [[ ! $exist ]] && [[ $already_run -eq 0 ]]; then
>&2 echo -n 'start container: '
docker run --rm -dt \
--name "$name" \
-v "$PWD:/data" \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
$args \
nella17/vm:$version
fi
docker exec -it -w '/data' "$name" zsh -c "source ~/.zshrc && $cmd"
count=$(pgrep -af 'docker exec' | grep -c "$name")
exist=$(docker ps -q -f "name=$name")
if [[ $count -eq 0 ]] && [[ $exist ]]; then
>&2 echo -n 'shutdown container: '
docker stop "$name"
fi