Merge "Add python-redis to overcloud"
[apex.git] / ci / util.sh
1 #!/usr/bin/env bash
2 # Utility script used to interact with a deployment
3 # @author Tim Rozet (trozet@redhat.com)
4
5 CONFIG=${CONFIG:-'/var/opt/opnfv'}
6 RESOURCES=${RESOURCES:-"$CONFIG/images"}
7 LIB=${LIB:-"$CONFIG/lib"}
8 VALID_CMDS="undercloud overcloud debug-stack -h --help"
9
10 source $LIB/utility-functions.sh
11
12 resolve_cmd() {
13   local given=$1
14   shift
15   local list=($*)
16   local inv=(${list[*]##${given}*})
17   local OIFS=$IFS; IFS='|'; local pat="${inv[*]}"; IFS=$OIFS
18   shopt -s extglob
19   echo "${list[*]##+($pat)}"
20   shopt -u extglob
21 }
22
23 display_usage() {
24   echo -e "Usage:\n$0 subcommand [ arguments ]\n"
25   echo -e "Arguments:\n"
26   echo -e "   undercloud [ user [ command ] ]   Connect to Undercloud VM as user and optionally execute a command\n"
27   echo -e "                                     user    Optional: Defaults to 'stack'\n"
28   echo -e "                                     command Optional: Defaults to none\n"
29   echo -e ""
30   echo -e "   overcloud  [ node [ command ] ]   Connect to an Overcloud node and optionally execute a command\n"
31   echo -e "                                     node    Required: in format controller|compute<number>.  Example: controller0\n"
32   echo -e "                                     command Optional: Defaults to none\n"
33   echo -e ""
34   echo -e "   debug-stack                       Print parsed deployment failures to stdout \n"
35   echo -e ""
36   echo -e "   mock-detached on | off            Add firewall rules to the jump host to mock a detached deployment \n"
37 }
38
39 ##translates the command line argument
40 ##params: $@ the entire command line is passed
41 ##usage: parse_cmd_line() "$@"
42 parse_cmdline() {
43   local match
44
45   match=($(resolve_cmd $1 $VALID_CMDS))
46   if [ ${#match[*]} -gt 1 ]; then
47     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
48     exit 1
49   elif [ ${#match[*]} -lt 1 ]; then
50     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
51     exit 1
52   else
53     match=$(echo $match | tr -d ' ')
54   fi
55
56   case "$match" in
57         -h|--help)
58                 display_usage
59                 exit 0
60             ;;
61         undercloud)
62                 if [ -z "$2" ]; then
63                   # connect as stack by default
64                   undercloud_connect stack
65                 elif [ -z "$3" ]; then
66                   undercloud_connect "$2"
67                 else
68                   undercloud_connect "$2" "$3"
69                 fi
70                 exit 0
71             ;;
72         overcloud)
73                 if [ -z "$2" ]; then
74                   overcloud_connect
75                 elif [ -z "$3" ]; then
76                   overcloud_connect "$2"
77                 else
78                   overcloud_connect "$2" "$3"
79                 fi
80                 exit 0
81             ;;
82         debug-stack)
83                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
84                 exit 0
85             ;;
86         mock-detached)
87                 if [ "$2" == "on" ]; then
88                     echo "Blocking output http and https traffic"
89                     iptables -A OUTPUT -p tcp --dport 80 -j REJECT
90                     iptables -A OUTPUT -p tcp --dport 443 -j REJECT
91                 elif [ "$2" == "off" ]; then
92                     echo "Allowing output http and https traffic"
93                     iptables -D OUTPUT -p tcp --dport 80 -j REJECT
94                     iptables -D OUTPUT -p tcp --dport 443 -j REJECT
95                 else
96                     display_usage
97                 fi
98                 exit 0
99             ;;
100         *)
101                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
102                 echo "Use -h to display help"
103                 exit 1
104             ;;
105   esac
106 }
107
108
109 main() {
110   parse_cmdline "$@"
111 }
112
113 main "$@"