-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathigor.sh
executable file
·118 lines (103 loc) · 3.19 KB
/
igor.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
#! /bin/bash
# This is igor.
# Original project: https://github.com/felixb/igor
# Install / update:
# sudo curl https://raw.githubusercontent.com/felixb/igor/master/igor.sh -o /usr/local/bin/igor
# sudo chmod +x /usr/local/bin/igor
set -e
# set up default values
IGOR_DOCKER_IMAGE=busybox # select docker image
IGOR_DOCKER_COMMAND=sh # run this command inside the docker conainer
IGOR_DOCKER_PULL=0 # force pulling the image before starting the container (0/1)
IGOR_DOCKER_RM=1 # remove container on exit (0/1)
IGOR_DOCKER_TTY=1 # open an interactive tty (0/1)
IGOR_DOCKER_USER=$(id -u) # run commands inside the container with this user
IGOR_DOCKER_GROUP=$(id -g) # run commands inside the container with this group
IGOR_DOCKER_ARGS='' # default arguments to docker run
IGOR_PORTS='' # space separated list of ports to expose
IGOR_MOUNT_PASSWD=0 # mount /etc/passwd inside the container (0/1)
IGOR_MOUNT_GROUP=0 # mount /etc/group inside the container (0/1)
IGOR_MOUNTS_RO='' # space separated list of volumes to mount read only
IGOR_MOUNTS_RW='' # space separated list of volumes to mount read write
IGOR_WORKDIR=${PWD} # use this workdir inside the container
IGOR_WORKDIR_MODE=rw # mount the workdir with this mode (ro/rw)
IGOR_ENV='' # space separated list of environment variables set inside the container
igor_config=.igor.sh
function usage() {
echo "$0 [-v] [-c path-to-igor-config] [--help]"
echo ''
echo 'Opens a shell in your favorite docker container mounting your current workspace into the container'
echo ''
echo 'configuration files:'
echo ''
echo '~/.igor.sh'
echo './.igor.sh or file specified with -c option'
echo ''
echo 'default config:'
echo ''
grep '^IGOR_' $0
echo ''
exit 1
}
# ugly command line parsing
if [ "${1}" == '-v' ]; then
shift
set -x
fi
if [ "${1}" == '-c' ]; then
if [ -z "${2}" ] || ! [ -e "${2}" ]; then
usage
fi
igor_config="${2}"
shift
shift
fi
if [ "${1}" == '--help' ]; then
usage
fi
# load config from home
if [ -e "${HOME}/.igor.sh" ]; then
. "${HOME}/.igor.sh"
fi
# load config from current working dir
if [ -e "${igor_config}" ]; then
. "${igor_config}"
fi
# assamble command line
if [ ${IGOR_DOCKER_PULL} -gt 0 ]; then
docker pull "${IGOR_DOCKER_IMAGE}"
fi
args=''
if [ ${IGOR_DOCKER_RM} -gt 0 ]; then
args="${args} --rm"
fi
if [ ${IGOR_DOCKER_TTY} -gt 0 ]; then
args="${args} -ti"
fi
for p in ${IGOR_PORTS}; do
args="${args} -p ${p}:${p}"
done
if [ ${IGOR_MOUNT_PASSWD} -gt 0 ]; then
args="${args} -v /etc/passwd:/etc/passwd:ro"
fi
if [ ${IGOR_MOUNT_GROUP} -gt 0 ]; then
args="${args} -v /etc/group:/etc/group:ro"
fi
for v in ${IGOR_MOUNTS_RO}; do
args="${args} -v ${v}:${v}:ro"
done
for v in ${IGOR_MOUNTS_RW}; do
args="${args} -v ${v}:${v}:rw"
done
for e in ${IGOR_ENV}; do
args="${args} -e ${e}=${!e}"
done
# execute!
exec docker run \
${args} \
-u "${IGOR_DOCKER_USER}:${IGOR_DOCKER_GROUP}" \
-v "${PWD}:${IGOR_WORKDIR}:rw" \
-w "${IGOR_WORKDIR}" \
${IGOR_DOCKER_ARGS} \
"${IGOR_DOCKER_IMAGE}" \
${IGOR_DOCKER_COMMAND}