Makefile: add pickrand
[cmccabe-bin] / dssh
1 #!/usr/bin/env bash
2
3 #
4 # Get a login for a docker container.
5 #
6
7 die() {
8     echo $@
9     exit 1
10 }
11
12 usage() {
13     cat <<EOF
14 dssh: log in to a docker node
15
16 usage: dssh: [options]
17
18 options:
19 -h:           show this help message.
20 -i [ID]:      ssh into a docker node with this container ID
21 -n [name]:    ssh into a docker node with this name
22 -t [0 or 1]:  0 to avoid allocating a TTY; 1 to allocate one.
23               The default will be set based on whether this appears
24               to be an interactive shell.
25 EOF
26 }
27
28 DOCKER_IMAGE_ID=""
29 DOCKER_IMAGE_NAME=""
30 if [ -t 0 ]; then
31     ALLOCATE_TTY=1
32 else
33     ALLOCATE_TTY=0
34 fi
35 while getopts  "hi:n:t:" flag; do
36     case $flag in
37     h) usage; exit 0;;
38     i) DOCKER_IMAGE_ID=${OPTARG};;
39     n) DOCKER_IMAGE_NAME=${OPTARG};;
40     t) ALLOCATE_TTY=${OPTARG};;
41     *) echo "getopts error"
42         echo
43         usage
44         exit 1;;
45     esac
46 done
47 shift $(expr $OPTIND - 1)
48 if [ $# -eq 0 ]; then
49     RUN_COMMAND="/bin/bash"
50 else
51     RUN_COMMAND=""
52 fi
53
54 which docker &>/dev/null || die "docker must be on the PATH."
55
56 if [ "x${DOCKER_IMAGE_NAME}" == "x" ]; then
57     if [ "x${DOCKER_IMAGE_ID}" == "x" ]; then
58         usage
59         exit 1
60     fi
61 else
62     if [ "x${DOCKER_IMAGE_ID}" == "x" ]; then
63         :
64     else
65         echo "You must not supply both an ID and a name."
66         exit 1
67     fi
68     DOCKER_IMAGE_ID=$(docker ps -f name=${DOCKER_IMAGE_NAME} -q)
69     [ "x${DOCKER_IMAGE_ID}" == "x" ] && \
70         die "failed to find a docker image named ${DOCKER_IMAGE_NAME}"
71 fi
72
73 if [ ${ALLOCATE_TTY} == 1 ]; then
74     docker exec -it "${DOCKER_IMAGE_ID}" "${@}" ${RUN_COMMAND}
75 else
76     docker exec -i "${DOCKER_IMAGE_ID}" "${@}" ${RUN_COMMAND} &
77     wait
78 fi