Adds overcloud ssh support and other fixes
[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 [arguments] \n"
25   echo -e "   undercloud <user> <command> : Connect to Undercloud VM as <user> and execute command <command>\n"
26   echo -e "                                 <user> Optional: Defaults to 'stack', <command> Optional: Defaults to none\n"
27   echo -e "   overcloud <node> <command> :  Connect to an Overcloud <node> and execute command <command>\n"
28   echo -e "                                 <node> Required in format controller|compute<number>.  Example: controller0\n"
29   echo -e "                                 <command> Optional: Defaults to none\n"
30   echo -e "   debug-stack : Print parsed deployment failures to stdout \n"
31 }
32
33 ##translates the command line argument
34 ##params: $@ the entire command line is passed
35 ##usage: parse_cmd_line() "$@"
36 parse_cmdline() {
37   local match
38
39   match=($(resolve_cmd $1 $VALID_CMDS))
40   if [ ${#match[*]} -gt 1 ]; then
41     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
42     exit 1
43   elif [ ${#match[*]} -lt 1 ]; then
44     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
45     exit 1
46   else
47     match=$(echo $match | tr -d ' ')
48   fi
49
50   case "$match" in
51         -h|--help)
52                 display_usage
53                 exit 0
54             ;;
55         undercloud)
56                 if [ -z "$2" ]; then
57                   # connect as stack by default
58                   undercloud_connect stack
59                 elif [ -z "$3" ]; then
60                   undercloud_connect "$2"
61                 else
62                   undercloud_connect "$2" "$3"
63                 fi
64                 exit 0
65             ;;
66         overcloud)
67                 if [ -z "$2" ]; then
68                   overcloud_connect
69                 elif [ -z "$3" ]; then
70                   overcloud_connect "$2"
71                 else
72                   overcloud_connect "$2" "$3"
73                 fi
74                 exit 0
75             ;;
76         debug-stack)
77                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
78                 exit 0
79             ;;
80         *)
81                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
82                 echo "Use -h to display help"
83                 exit 1
84             ;;
85   esac
86 }
87
88
89 main() {
90   parse_cmdline "$@"
91 }
92
93 main "$@"