forked from mzedeler/dsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsh
executable file
·108 lines (93 loc) · 3.56 KB
/
dsh
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
#!/bin/sh
# Parameter parsing
while test $# != 0 -a "$1" != "--"; do
case $1 in
-b|--build) OPT_BUILD=1 ;;
-s|--samba) OPT_SAMBA=1 ;;
--host-workspace) OPT_HOST_WORKSPACE=$2; shift ;;
--volumes-from) OPT_VOLUMES_FROM="${OPT_VOLUMES_FROM} ${2}"; shift ;;
-v) OPT_HOST_VOLUME="${OPT_HOST_VOLUME} ${2}"; shift ;;
esac
shift
done
if test "$1" = "--"; then
shift
fi
if test -n "$OPT_VOLUMES_FROM"; then
for VOL in ${OPT_VOLUMES_FROM}; do
DOCKER_OPTS="$DOCKER_OPTS --volumes-from=${VOL}"
done
fi
if test -n "$OPT_HOST_VOLUME"; then
for VOL in ${OPT_HOST_VOLUME}; do
DOCKER_OPTS="$DOCKER_OPTS -v ${VOL}"
done
fi
if test -t 1; then
DOCKER_OPTS="$DOCKER_OPTS -t"
fi
if test -n "$OPT_HOST_WORKSPACE"; then
DOCKER_OPTS="$DOCKER_OPTS -v $(readlink -f $OPT_HOST_WORKSPACE):/workspace"
else
DOCKER_OPTS="$DOCKER_OPTS --volumes-from dsh-ws"
fi
# Set up environment for connecting to docker
if test -n "$(uname -a | grep boot2docker)"; then
# Running inside boot2docker vm
HOST_IP=$(ifconfig eth0 | grep 'inet addr:' | awk -F: '{print $2}' | awk '{print $1}')
DOCKER_PARAMS="--add-host boot2docker:$HOST_IP"
DOCKER_PARAMS="$DOCKER_PARAMS -e DOCKER_HOST=tcp://boot2docker:2376"
DOCKER_PARAMS="$DOCKER_PARAMS -e DOCKER_CERT_PATH=/etc/docker-certs"
DOCKER_PARAMS="$DOCKER_PARAMS -e DOCKER_TLS_VERIFY=1"
DOCKER_PARAMS="$DOCKER_PARAMS -v /var/lib/boot2docker/tls:/etc/docker-certs"
else
# Using docker on some other system
DOCKER_PARAMS=" -v /var/run/docker.sock:/var/run/docker.sock"
if test ! -z $DOCKER_CERT_PATH; then
DOCKER_PARAMS="$DOCKER_PARAMS -v $DOCKER_CERT_PATH:/etc/docker-certs"
DOCKER_PARAMS="$DOCKER_PARAMS -e DOCKER_CERT_PATH=/etc/docker-certs"
fi
if test ! -z $DOCKER_TLS_VERIFY ; then
DOCKER_PARAMS="$DOCKER_PARAMS -e DOCKER_TLS_VERIFY=$DOCKER_TLS_VERIFY"
fi
if test -f $HOME/.gitconfig; then
DOCKER_OPTS="$DOCKER_OPTS -v $HOME/.gitconfig:/workspace/.gitconfig"
fi
if test -n "$SSH_AUTH_SOCK"; then
DOCKER_OPTS="$DOCKER_OPTS -v $(dirname $SSH_AUTH_SOCK):/tmp/ssh-agent"
DOCKER_OPTS="$DOCKER_OPTS -e SSH_AUTH_SOCK=/tmp/ssh-agent/$(basename $SSH_AUTH_SOCK)"
fi
fi
DSH_IMAGE=dsh
if test -n "$OPT_BUILD" -o -z "$(docker images | grep '^dsh[[:space:]]')"; then
echo 'Docker image not found. Rebuilding.'
docker build -t dsh - <<END_DOCKERFILE
FROM phusion/baseimage
RUN curl -sSL https://get.docker.com | sh
RUN perl -pe s{root:/root}{root:/workspace} -i /etc/passwd
CMD login -p -f root
END_DOCKERFILE
fi
if docker images | grep -e '^dsh-custom[[:space:]]' >/dev/null; then
DSH_IMAGE=dsh-custom
fi
# Set up data container for workspace (if it doesn't exist already)
EXIT_CODE=$(docker inspect --format "{{.State.ExitCode}}" dsh-ws 2>/dev/null)
if test ${EXIT_CODE:-1} != 0; then
docker run -t --name dsh-ws -v /workspace busybox true 2>/dev/null
docker run --rm --volumes-from=dsh-ws phusion/baseimage /bin/bash -c "cp /etc/skel/.* /workspace/ 2>/dev/null"
fi
if test -n "$OPT_SAMBA"; then
# Start samba file sharing (if it hasn't started already)
RUNNING=$(docker inspect --format "{{.State.Running}}" samba-server 2>/dev/null)
if test ${RUNNING:-false} = false; then
docker run -e USER=root -e USERID=0 --rm -v "$(which docker):/docker" -v /var/run/docker.sock:/docker.sock svendowideit/samba dsh-ws
fi
fi
# Now run the docker shell container
docker run $DOCKER_PARAMS \
-i \
$DOCKER_OPTS \
--rm \
-e HOME=/workspace \
$DSH_IMAGE "$@"