-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-jumpshell.sh
executable file
·100 lines (92 loc) · 2.41 KB
/
docker-jumpshell.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
#! /bin/bash
set -e
function pick_logs() {
DIR=`dirname $0`
MSG="Follow logs of which container?"
CONTAINER=$( "$DIR/docker-jumpshell-helper.sh" ls | xargs whiptail --title "$MSG" --menu "$MSG" 20 70 13 3>&1 1>&2 2>&3 )
[ $? -ne 0 ] && {
echo "canceled"
} || {
exec "$DIR/docker-jumpshell-helper.sh" logs "$CONTAINER"
}
}
function pick_container() {
DIR=`dirname $0`
MSG="Choose a container"
CONTAINER=$( ( echo _ logs; "$DIR/docker-jumpshell-helper.sh" ls ) | xargs whiptail --title "$MSG" --menu "$MSG" 20 70 13 3>&1 1>&2 2>&3 )
[ $? -ne 0 ] && {
echo "canceled"
} || {
if [ "x$CONTAINER" == "x_" ]
then
pick_logs
else
exec "$DIR/docker-jumpshell-helper.sh" exec "$CONTAINER"
fi
}
}
function get_containers() {
DIR=`dirname $0`
"$DIR/docker-jumpshell-helper.sh" ls
}
function error() {
echo "ERROR: $@" >&2
exit -1
}
DIR=`dirname $0`
if [ $# -ne 0 ]
then
[ $# -ne 2 ] && error "expecting -c COMMAND"
[ "x$1" != "x-c" ] && error "expecting leading -c got $@"
# remove leading -c
shift
ARG="$1"
CONTAINER=$( echo "$ARG" | cut -d ' ' -f 1 )
REST=$( echo "$ARG" | cut -s -d ' ' -f 2- )
if [ "x$CONTAINER" == "xmosh-server" ]
then
exec bash -l -c "exec $CONTAINER $REST"
elif [ "x$CONTAINER" == "xpicker" ]
then
pick_container
elif [ "x$CONTAINER" == "xdocker_logs" ]
then
if [ "x$REST" == "x" ]
then
pick_logs
else
exec "$DIR/docker-jumpshell-helper.sh" logs "$REST"
fi
elif [ "x$CONTAINER" == "xls" ]
then
get_containers
exit 0
fi
if [ "x$REST" == "x" ]
then
exec "$DIR/docker-jumpshell-helper.sh" exec "$CONTAINER"
else
exec "$DIR/docker-jumpshell-helper.sh" exec "$CONTAINER" -c "$REST"
fi
fi
if tmux ls
then
# we have tmux sessions
[ "x$TMUX" == "x" ] && {
# not inside an already exising tmux, then attach to one
exec tmux attach
} || {
# already inside tmux (ex. new window)
pick_container
}
else
# no tmux sessions, create a new one with a window for each container
DIR=`dirname $0`
TMUX_CMD=new-session
get_containers | while read CONTAINER NAME
do
tmux $TMUX_CMD -d -n "$NAME" "$CONTAINER"
TMUX_CMD=new-window
done
exec tmux attach
fi