1#/bin/bash
2#
3# Copyright 2018 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18SCRIPT_NAME=$(basename $0)
19usage()
20{
21  echo "Usage: $SCRIPT_NAME <task_name> [options]"
22  echo ""
23  echo "Options:"
24  echo "  -p PASSWORD         password for hosts."
25  echo "  -H PATH             path to a .py file contains hosts information"
26  echo "  -i PATH             path to a .py file contains ip address information of certified machines"
27  echo "  -f GCS_URL          URL to the vtslab package file to be deployed."
28  exit 1
29}
30
31FABRIC_EXISTS=$(pip show fabric)
32if [ -z "$FABRIC_EXISTS" ]; then
33  INSTALL_FABRIC=true
34else
35  FABRIC_VERSION=$(fab -V | grep Fabric)
36  if [ "${FABRIC_VERSION:7:1}" -ne 1 ]; then
37    INSTALL_FABRIC=true
38  fi
39fi
40if [ "$INSTALL_FABRIC" == true ]; then
41  sudo pip install fabric==1.14.0 --force
42fi
43
44TASK=$1
45if [[ ${TASK:0:1} == "-" ]]; then
46  usage
47fi
48
49shift
50PASSWORD=""
51HOSTS_PATH="hosts.py"
52IPADDRESSES_PATH=""
53VTSLAB_PACKAGE_GCS_URL=""
54
55while getopts ":p:H:i:f:" opt; do
56  case $opt in
57    p)
58      PASSWORD=$OPTARG
59      ;;
60    H)
61      HOSTS_PATH=$OPTARG
62      ;;
63    i)
64      IPADDRESSES_PATH=$OPTARG
65      ;;
66    f)
67      VTSLAB_PACKAGE_GCS_URL=$OPTARG
68      ;;
69    \?)
70      echo "Invalid option: -$OPTARG"
71      usage
72      ;;
73    :)
74      echo "Option -$OPTARG requires an argument."
75      usage
76      ;;
77  esac
78done
79
80if [ "$TASK" == "SetupIptables" ]; then
81  fab SetPassword:$PASSWORD GetHosts:$HOSTS_PATH $TASK:$IPADDRESSES_PATH
82elif [ "$TASK" == "SetupPackages" ]; then
83  if [ -z "$IPADDRESSES_PATH" ]; then
84    fab SetPassword:$PASSWORD GetHosts:$HOSTS_PATH $TASK
85  else
86    fab SetPassword:$PASSWORD GetHosts:$HOSTS_PATH $TASK:$IPADDRESSES_PATH
87  fi
88elif [ "$TASK" == "DeployVtslab" ] || [ "$TASK" == "DeployGCE" ]; then
89  if [ -z "$VTSLAB_PACKAGE_GCS_URL" ]; then
90    echo "Please specify vtslab package file URL using -f option."
91    exit
92  fi
93  fab SetPassword:$PASSWORD GetHosts:$HOSTS_PATH $TASK:$VTSLAB_PACKAGE_GCS_URL
94else
95  fab SetPassword:$PASSWORD GetHosts:$HOSTS_PATH $TASK
96fi
97