1a931d0b3c0d0c5f4bdead380ff42641fcca3083
[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 BASE=${BASE:-'/var/opt/opnfv'}
6 IMAGES=${IMAGES:-"$BASE/images"}
7 LIB=${LIB:-"$BASE/lib"}
8 VALID_CMDS="undercloud overcloud opendaylight debug-stack mock-detached -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"
27   echo -e "                                     user    Optional: Defaults to 'stack'"
28   echo -e "                                     command Optional: Defaults to none"
29   echo -e ""
30   echo -e "   opendaylight                      Connect to OpenDaylight Karaf console"
31   echo -e ""
32   echo -e "   overcloud  [ node [ command ] ]   Connect to an Overcloud node and optionally execute a command"
33   echo -e "                                     node    Required: in format controller|compute<number>.  Example: controller0"
34   echo -e "                                     command Optional: Defaults to none"
35   echo -e ""
36   echo -e "   debug-stack                       Print parsed deployment failures to stdout"
37   echo -e ""
38   echo -e "   mock-detached on | off            Add firewall rules to the jump host to mock a detached deployment\n"
39 }
40
41 ##translates the command line argument
42 ##params: $@ the entire command line is passed
43 ##usage: parse_cmd_line() "$@"
44 parse_cmdline() {
45   local match
46
47   match=($(resolve_cmd $1 $VALID_CMDS))
48   if [ ${#match[*]} -gt 1 ]; then
49     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
50     exit 1
51   elif [ ${#match[*]} -lt 1 ]; then
52     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
53     exit 1
54   else
55     match=$(echo $match | tr -d ' ')
56   fi
57
58   case "$match" in
59         -h|--help)
60                 display_usage
61                 exit 0
62             ;;
63         undercloud)
64                 if [ -z "$2" ]; then
65                   # connect as stack by default
66                   undercloud_connect stack
67                 elif [ -z "$3" ]; then
68                   undercloud_connect "$2"
69                 else
70                   undercloud_connect "$2" "$3"
71                 fi
72                 exit 0
73             ;;
74         overcloud)
75                 if [ -z "$2" ]; then
76                   overcloud_connect
77                 elif [ -z "$3" ]; then
78                   overcloud_connect "$2"
79                 else
80                   overcloud_connect "$2" "$3"
81                 fi
82                 exit 0
83             ;;
84         opendaylight)
85                 opendaylight_connect
86                 exit 0
87             ;;
88         debug-stack)
89                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
90                 exit 0
91             ;;
92         mock-detached)
93                 if [ "$2" == "on" ]; then
94                     echo "Ensuring we can talk to gerrit.opnfv.org"
95                     iptables -A OUTPUT -p tcp -d gerrit.opnfv.org --dport 443 -j ACCEPT
96                     echo "Blocking output http (80) traffic"
97                     iptables -A OUTPUT -p tcp --dport 80 -j REJECT
98                     iptables -A FORWARD -p tcp --dport 80 -j REJECT
99                     echo "Blocking output https (443) traffic"
100                     iptables -A OUTPUT -p tcp --dport 443 -j REJECT
101                     iptables -A FORWARD -p tcp --dport 443 -j REJECT
102                     echo "Blocking output dns (53) traffic"
103                     iptables -A FORWARD -p tcp --dport 53 -j REJECT
104                 elif [ "$2" == "off" ]; then
105                     echo "Cleaning gerrit.opnfv.org specific rule"
106                     iptables -D OUTPUT -p tcp -d gerrit.opnfv.org --dport 443 -j ACCEPT
107                     echo "Allowing output http (80) traffic"
108                     iptables -D OUTPUT -p tcp --dport 80 -j REJECT
109                     iptables -D FORWARD -p tcp --dport 80 -j REJECT
110                     echo "Allowing output https (443) traffic"
111                     iptables -D OUTPUT -p tcp --dport 443 -j REJECT
112                     iptables -D FORWARD -p tcp --dport 443 -j REJECT
113                     echo "Allowing output dns (53) traffic"
114                     iptables -D OUTPUT -p tcp --dport 53 -j REJECT
115                     iptables -D FORWARD -p tcp --dport 53 -j REJECT
116                 else
117                     display_usage
118                 fi
119                 exit 0
120             ;;
121         *)
122                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
123                 echo "Use -h to display help"
124                 exit 1
125             ;;
126   esac
127 }
128
129
130 main() {
131   parse_cmdline "$@"
132 }
133
134 main "$@"