cea9c0322eee28aecbef8fdaa5dfab830356987c
[arvados.git] / docker / run.sh
1 #!/bin/bash
2
3 ENABLE_SSH=false
4 DOCKER_ARGS="-d -i -t"
5
6 function usage {
7     echo >&2 "usage:"
8     echo >&2 "$0 start [--ssh] [--doc] [--sso] [--api] [--workbench] [--keep]"
9     echo >&2 "$0 stop"
10     echo >&2 "$0 test"
11     echo >&2 "If no switches are given, the default is to start all servers."
12 }
13
14 function ip_address {
15     local container=$1
16     echo `docker inspect $container  |grep IPAddress |cut -f4 -d\"`
17 }
18
19 function start_container {
20     local port="-p $1"
21     if [[ "$2" != '' ]]; then
22       local name="$2"
23       DOCKER_ARGS="$DOCKER_ARGS -name $name"
24     fi
25     if [[ "$3" != '' ]]; then
26       local volume="$3"
27       DOCKER_ARGS="$DOCKER_ARGS -v $volume"
28     fi
29     if [[ "$4" != '' ]]; then
30       local link="$4"
31       DOCKER_ARGS="$DOCKER_ARGS -link $link"
32     fi
33     local image=$5
34
35     if $ENABLE_SSH
36     then
37       DOCKER_ARGS="$DOCKER_ARGS -e ENABLE_SSH=$ENABLE_SSH"
38     fi
39
40     `docker ps |grep -P "$name[^/]" -q`
41     if [[ "$?" == "0" ]]; then
42       echo "You have a running container with name $name -- skipping."
43       return
44     fi
45
46     # If a container by this name already exists, remove it before
47     # starting a new one.
48     
49     echo "Starting container:"
50     echo "  docker run $DOCKER_ARGS $image"
51     container=`docker run $DOCKER_ARGS $image`
52     if [[ "$?" != "0" ]]; then
53       echo "Unable to start container"
54       exit 1
55     fi
56     if $ENABLE_SSH
57     then
58       ip=$(ip_address $container )
59       echo
60       echo "You can ssh into the container with:"
61       echo
62       echo "    ssh root@$ip"
63       echo
64     fi
65 }
66
67 declare -a keep_volumes
68
69 # Initialize the global `keep_volumes' array. If any keep volumes
70 # already appear to exist (mounted volumes with a top-level "keep"
71 # directory), use them; create temporary volumes if necessary.
72 #
73 function make_keep_volumes () {
74     # Mount a keep volume if we don't already have one
75     for mountpoint in $(cut -d ' ' -f 2 /proc/mounts); do
76       if [[ -d "$mountpoint/keep" && "$mountpoint" != "/" ]]; then
77         keep_volumes+=($mountpoint)
78       fi
79     done
80
81     # Create any keep volumes that do not yet exist.
82     while [ ${#keep_volumes[*]} -lt 2 ]
83     do
84         new_keep=$(mktemp -d)
85         echo >&2 "mounting 512M tmpfs keep volume in $new_keep"
86         sudo mount -t tmpfs -o size=512M tmpfs $new_keep
87         mkdir $new_keep/keep
88         keep_volumes+=($new_keep)
89     done
90 }
91
92 function do_start {
93     local start_doc=false
94     local start_sso=false
95     local start_api=false
96     local start_workbench=false
97     local start_keep=false
98
99     while [ $# -ge 1 ]
100     do
101         case $1 in
102             --doc)
103                 start_doc=true
104                 ;;
105             --sso)
106                 start_sso=true
107                 ;;
108             --api)
109                 start_api=true
110                 ;;
111             --workbench)
112                 start_workbench=true
113                 ;;
114             --keep)
115                 start_keep=true
116                 ;;
117             --ssh)
118                 ENABLE_SSH=true
119                 ;;
120             *)
121                 usage
122                 exit 1
123                 ;;
124         esac
125         shift
126     done
127
128     # If no options were selected, then start all servers.
129     if $start_doc || $start_sso || $start_api || $start_workbench || $start_keep
130     then
131       :
132     else
133       start_doc=true
134       start_sso=true
135       start_api=true
136       start_workbench=true
137       start_keep=true
138     fi
139
140     $start_doc && start_container "9898:80" "doc_server" '' '' "arvados/doc"
141     $start_sso && start_container "9901:443" "sso_server" '' '' "arvados/sso"
142     $start_api && start_container "9900:443" "api_server" '' "sso_server:sso" "arvados/api"
143     $start_workbench && start_container "9899:80" "workbench_server" '' "api_server:api" "arvados/workbench"
144
145     make_keep_volumes
146     $start_keep && start_container "25107:25107" "keep_server_0" "${keep_volumes[0]}:/dev/keep-0" "api_server:api" "arvados/warehouse"
147     $start_keep && start_container "25108:25107" "keep_server_1" "${keep_volumes[1]}:/dev/keep-0" "api_server:api" "arvados/warehouse"
148
149     ARVADOS_API_HOST=$(ip_address "api_server")
150     ARVADOS_API_HOST_INSECURE=yes
151     ARVADOS_API_TOKEN=$(grep '^\w' api/generated/secret_token.rb | cut -d "'" -f 2)
152
153     echo "To run a test suite:"
154     echo "export ARVADOS_API_HOST=$ARVADOS_API_HOST"
155     echo "export ARVADOS_API_HOST_INSECURE=$ARVADOS_API_HOST_INSECURE"
156     echo "export ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN"
157     echo "python -m unittest discover ../sdk/python"
158 }
159
160 if [ $# -lt 1 ]
161 then
162   usage
163   exit 1
164 fi
165
166 case $1 in
167     start)
168         shift
169         do_start $@
170         ;;
171     *)
172         usage
173         exit 1
174         ;;
175 esac