Merge "Increases open files limit for mariadb"
[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 # Backwards compat for old ENV Vars
6 # Remove in E Release
7 if [ -n "$CONFIG" ]; then
8     echo -e "${red}WARNING: ENV var CONFIG is Deprecated, please unset CONFIG and export BASE in its place${reset}"
9     echo -e "${red}WARNING: CONFIG will be removed in E${reset}"
10     BASE=$CONFIG
11 fi
12 if [ -n "$RESOURCES" ]; then
13     echo -e "${red}WARNING: ENV var RESOURCES is Deprecated, please unset RESOURCES and export IMAGES in its place${reset}"
14     echo -e "${red}WARNING: RESOURCES will be removed in E${reset}"
15     IMAGES=$RESOURCES
16 fi
17
18 BASE=${BASE:-'/var/opt/opnfv'}
19 IMAGES=${IMAGES:-"$BASE/images"}
20 LIB=${LIB:-"$BASE/lib"}
21 VALID_CMDS="undercloud overcloud opendaylight debug-stack mock-detached -h --help"
22
23 source $LIB/utility-functions.sh
24
25 resolve_cmd() {
26   local given=$1
27   shift
28   local list=($*)
29   local inv=(${list[*]##${given}*})
30   local OIFS=$IFS; IFS='|'; local pat="${inv[*]}"; IFS=$OIFS
31   shopt -s extglob
32   echo "${list[*]##+($pat)}"
33   shopt -u extglob
34 }
35
36 display_usage() {
37   echo -e "Usage:\n$0 subcommand [ arguments ]\n"
38   echo -e "Arguments:\n"
39   echo -e "   undercloud [ user [ command ] ]   Connect to Undercloud VM as user and optionally execute a command"
40   echo -e "                                     user    Optional: Defaults to 'stack'"
41   echo -e "                                     command Optional: Defaults to none"
42   echo -e ""
43   echo -e "   opendaylight                      Connect to OpenDaylight Karaf console"
44   echo -e ""
45   echo -e "   overcloud  [ node [ command ] ]   Connect to an Overcloud node and optionally execute a command"
46   echo -e "                                     node    Required: in format controller|compute<number>.  Example: controller0"
47   echo -e "                                     command Optional: Defaults to none"
48   echo -e ""
49   echo -e "   debug-stack                       Print parsed deployment failures to stdout"
50   echo -e ""
51   echo -e "   mock-detached on | off            Add firewall rules to the jump host to mock a detached deployment\n"
52 }
53
54 ##translates the command line argument
55 ##params: $@ the entire command line is passed
56 ##usage: parse_cmd_line() "$@"
57 parse_cmdline() {
58   local match
59
60   match=($(resolve_cmd $1 $VALID_CMDS))
61   if [ ${#match[*]} -gt 1 ]; then
62     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
63     exit 1
64   elif [ ${#match[*]} -lt 1 ]; then
65     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
66     exit 1
67   else
68     match=$(echo $match | tr -d ' ')
69   fi
70
71   case "$match" in
72         -h|--help)
73                 display_usage
74                 exit 0
75             ;;
76         undercloud)
77                 if [ -z "$2" ]; then
78                   # connect as stack by default
79                   undercloud_connect stack
80                 elif [ -z "$3" ]; then
81                   undercloud_connect "$2"
82                 else
83                   undercloud_connect "$2" "$3"
84                 fi
85                 exit 0
86             ;;
87         overcloud)
88                 if [ -z "$2" ]; then
89                   overcloud_connect
90                 elif [ -z "$3" ]; then
91                   overcloud_connect "$2"
92                 else
93                   overcloud_connect "$2" "$3"
94                 fi
95                 exit 0
96             ;;
97         opendaylight)
98                 opendaylight_connect
99                 exit 0
100             ;;
101         debug-stack)
102                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
103                 exit 0
104             ;;
105         mock-detached)
106                 if [ "$2" == "on" ]; then
107                     echo "Ensuring we can talk to gerrit.opnfv.org"
108                     iptables -A OUTPUT -p tcp -d gerrit.opnfv.org --dport 443 -j ACCEPT
109                     echo "Blocking output http (80) traffic"
110                     iptables -A OUTPUT -p tcp --dport 80 -j REJECT
111                     iptables -A FORWARD -p tcp --dport 80 -j REJECT
112                     echo "Blocking output https (443) traffic"
113                     iptables -A OUTPUT -p tcp --dport 443 -j REJECT
114                     iptables -A FORWARD -p tcp --dport 443 -j REJECT
115                     echo "Blocking output dns (53) traffic"
116                     iptables -A FORWARD -p tcp --dport 53 -j REJECT
117                 elif [ "$2" == "off" ]; then
118                     echo "Cleaning gerrit.opnfv.org specific rule"
119                     iptables -D OUTPUT -p tcp -d gerrit.opnfv.org --dport 443 -j ACCEPT
120                     echo "Allowing output http (80) traffic"
121                     iptables -D OUTPUT -p tcp --dport 80 -j REJECT
122                     iptables -D FORWARD -p tcp --dport 80 -j REJECT
123                     echo "Allowing output https (443) traffic"
124                     iptables -D OUTPUT -p tcp --dport 443 -j REJECT
125                     iptables -D FORWARD -p tcp --dport 443 -j REJECT
126                     echo "Allowing output dns (53) traffic"
127                     iptables -D OUTPUT -p tcp --dport 53 -j REJECT
128                     iptables -D FORWARD -p tcp --dport 53 -j REJECT
129                 else
130                     display_usage
131                 fi
132                 exit 0
133             ;;
134         *)
135                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
136                 echo "Use -h to display help"
137                 exit 1
138             ;;
139   esac
140 }
141
142
143 main() {
144   parse_cmdline "$@"
145 }
146
147 main "$@"