-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
154 lines (133 loc) · 3.38 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
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
# get directory of this script
# https://stackoverflow.com/questions/59895/how-can-i-get-the-source-directory-of-a-bash-script-from-within-the-script-itsel
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if ! cd "$SCRIPT_DIR"
then
echo "Could not change directory to script directory '$SCRIPT_DIR'"
exit 1
fi
echo "Working directory is $SCRIPT_DIR"
if which docker > /dev/null
then
container=docker
elif which podman > /dev/null
then
container=podman
else
echo "Docker or Podman Desktop needs to be installed!"
exit 1
fi
args=()
while [ $# -gt 0 ]
do
case "$1"
in
--podman) container=podman;;
--docker) container=docker;;
*) args+=("$1");;
esac
shift
done
echo "Using $container..."
is_windows() {
if which cygpath > /dev/null # git-bash/msys/cygwin
then
return 0
fi
if [ -r /proc/version ] && grep -q Microsoft /proc/version
then
return 0
fi
return 1
}
is_selinux() {
if which sestatus > /dev/null
then
if sestatus | grep -q enabled
then
return 0
fi
fi
return 1
}
optZ() {
if [ "$container" = "podman" ]
then
if is_selinux
then
echo -n ":Z"
fi
fi
}
winenv() {
if is_windows
then
winpty "$@"
else
"$@"
fi
}
# Try to convert for git-bash/msys/cygwin/wsl...
winpath() {
if which cygpath > /dev/null # git-bash/msys/cygwin
then
cygpath -w "$@"
elif [ -r /proc/version ] && grep -q Microsoft /proc/version
then
echo readlink -m "$@" | sed 's|^/mnt/\([a-z]\)|\U\1:|' | sed 's|/|\\|g'
else
echo "$@"
fi
}
# use the directory name as the tag name for podman
PARENT_DIR=$(basename "$SCRIPT_DIR")
CONTAINER_TAG=$container-$PARENT_DIR
CONTAINER_TAG=$(echo "$CONTAINER_TAG" | tr '[:upper:]' '[:lower:]')
# replace spaces with hypens
CONTAINER_TAG=$(echo "$CONTAINER_TAG" | sed 's/ /-/g')
# Host (source) directory to mount in container
HOST_DIR="$(winpath "$SCRIPT_DIR")"
# Guest (target) directory where host directoy is mounted
GUEST_DIR=/home/user/$PARENT_DIR
# Configure container git to use the host's .gitconfig
g_dir=$(cat ~/.gitconfig | grep "$GUEST_DIR")
if [ -z "$g_dir" ]; then
git config --global --add safe.directory "$GUEST_DIR"
fi
git config core.hooksPath .githooks
if [ ! -f Dockerfile ]
then
echo "'$SCRIPT_DIR' does not contain a Dockerfile."
exit 1
fi
echo "$container build '$SCRIPT_DIR/Dockerfile' with tag '$CONTAINER_TAG'..."
if $container build -t "$CONTAINER_TAG" .
then
echo "$container build ok."
else
echo "$container build failed."
exit 1
fi
if [ ${#args[@]} -eq 0 ]
then
args=("bash" "-c" "cd $GUEST_DIR; sudo bash script.sh; zsh")
fi
echo "$container run '$CONTAINER_TAG' (mounting host '$HOST_DIR' as '$GUEST_DIR'):" \
"${args[@]}"
USER_HOME="/home/user"
SSH_DIR="$(winpath "${HOME}/.ssh")"
GIT_CONFIG="$(winpath "${HOME}/.gitconfig")"
ZSH_HISTORY="$(winpath "${HOME}/.zsh_history")"
KATTISRC="$(winpath "${HOME}/.kattisrc")"
if [ ! -f $ZSH_HISTORY ]; then
touch $ZSH_HISTORY # window mounts it as directory if doesn't exist
fi
winenv $container run -it --rm \
-v "$HOST_DIR:$GUEST_DIR$(optZ)" \
-v "$SSH_DIR:$USER_HOME/.ssh" \
-v "$GIT_CONFIG:$USER_HOME/.gitconfig" \
-v "$ZSH_HISTORY:$USER_HOME/.zsh_history" \
-v "$KATTISRC:$USER_HOME/.kattisrc" \
-h debian \
"$CONTAINER_TAG" \
"${args[@]}"