Fix: First boot from floppy, not CD-ROM
[ta/remote-installer.git] / scripts / start.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -e
17
18 cd "$(dirname "$0")"/..
19
20 API_PORT="15101"
21 BASE_DIR=""
22 CONT_NAME="remote-installer"
23 EXT_IP=""
24 HTTPS_PORT="443"
25 SSH_PORT="22222"
26 IMG_NAME="remote-installer"
27 ROOT_PW="root"
28
29 error()
30 {
31     echo "ERROR : $1"
32     [ -z $2 ] || help
33     exit 1
34 }
35
36 help()
37 {
38     echo -e "$(basename $0) [-h -a <api-port> -c <cont> -i <image> -r <pw> -s <https-port> ] -b <basedir> -e <ip-addr>"
39     echo -e "   -h  display this help"
40     echo -e "   -a  rest API port, default $API_PORT"
41     echo -e "   -b  base directory, which contains images, certificates, etc."
42     echo -e "   -c  container name, default $CONT_NAME"
43     echo -e "   -d  use docker bridged networking, default host"
44     echo -e "   -e  external ip address of  the docker"
45     echo -e "   -i  image name, default $IMG_NAME"
46     echo -e "   -l  login port for ssh, default $SSH_NAME"
47     echo -e "   -p  root password, default $ROOT_PW"
48     echo -e "   -s  secure https port, default $HTTPS_PORT"
49 }
50
51 while getopts "ha:b:de:l:s:c:p:i:" arg; do
52     case $arg in
53         h)
54             help
55             exit 0
56             ;;
57         b)
58             BASE_DIR="$OPTARG"
59             ;;
60         e)
61             EXT_IP="$OPTARG"
62             ;;
63         s)
64             HTTPS_PORT="$OPTARG"
65             ;;
66         a)
67             API_PORT="$OPTARG"
68             ;;
69         c)
70             CONT_NAME="$OPTARG"
71             ;;
72         i)
73             IMG_NAME="$OPTARG"
74             ;;
75         p)
76             ROOT_PW="$OPTARG"
77             ;;
78         l)
79             SSH_PORT="$OPTARG"
80             ;;
81         d)
82             DOCKER_BRIDGE="YES"
83             ;;
84         *)
85             error "Unknow argument!" showhelp
86             ;;
87   esac
88 done
89
90 [ -n "$EXT_IP" ] || error "No external IP defined!" showhelp
91 [ -n "$BASE_DIR" ] || error "No base directory defined!" showhelp
92
93 DOCKER_ENV="--env API_PORT=$API_PORT \
94         --env HOST_ADDR=$EXT_IP \
95         --env HTTPS_PORT=$HTTPS_PORT \
96         --env PW=$ROOT_PW \
97         --env SSH_PORT=$SSH_PORT "
98
99
100 if [ -n "$DOCKER_BRIDGE" ]
101 then
102     echo -e "Start container with bridged networking..."
103     cont_id="$(docker run --detach --rm --privileged \
104         $DOCKER_ENV \
105         --network=bridge \
106         --volume "$BASE_DIR":/opt/remoteinstaller \
107         --publish "$HTTPS_PORT":"$HTTPS_PORT" --publish 2049:2049 --publish "$API_PORT":"$API_PORT" \
108         --name "$CONT_NAME" "$IMG_NAME")" \
109         || error "failed to start container"
110     echo -e "IP : $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$cont_id")"
111
112 else
113     echo -e "Start container with host networking..."
114     cont_id="$(docker run --detach --rm --privileged \
115         $DOCKER_ENV \
116         --network=host \
117         --volume "$BASE_DIR":/opt/remoteinstaller \
118         --name "$CONT_NAME" "$IMG_NAME")" \
119         || error "failed to start container"
120 fi
121 echo -e "Container successfully started"
122 echo -e "ID : $cont_id"
123 echo -e "Using ssh port : $SSH_PORT"