Rebasing opnfv-tht Colorado with ODL patches
[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=/var/opt/opnfv
6 VALID_CMDS="undercloud debug-stack -h --help"
7
8 source $CONFIG/utility-functions.sh
9
10 resolve_cmd() {
11   local given=$1
12   shift
13   local list=($*)
14   local inv=(${list[*]##${given}*})
15   local OIFS=$IFS; IFS='|'; local pat="${inv[*]}"; IFS=$OIFS
16   shopt -s extglob
17   echo "${list[*]##+($pat)}"
18   shopt -u extglob
19 }
20
21 display_usage() {
22   echo -e "Usage:\n$0 [arguments] \n"
23   echo -e "   undercloud <user> : Connect to Undercloud VM as <user>\n"
24   echo -e "   debug-stack : Print parsed deployment failures to stdout \n"
25 }
26
27 ##translates the command line argument
28 ##params: $@ the entire command line is passed
29 ##usage: parse_cmd_line() "$@"
30 parse_cmdline() {
31   local match
32
33   match=($(resolve_cmd $1 $VALID_CMDS))
34   if [ ${#match[*]} -gt 1 ]; then
35     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
36     exit 1
37   elif [ ${#match[*]} -lt 1 ]; then
38     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
39     exit 1
40   else
41     match=$(echo $match | tr -d ' ')
42   fi
43
44   case "$match" in
45         -h|--help)
46                 display_usage
47                 exit 0
48             ;;
49         undercloud)
50                 if [ -z "$2" ]; then
51                   # connect as stack by default
52                   undercloud_connect stack
53                 else
54                   undercloud_connect $2
55                 fi
56                 exit 0
57             ;;
58         debug-stack)
59                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
60                 exit 0
61             ;;
62         *)
63                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
64                 echo "Use -h to display help"
65                 exit 1
66             ;;
67   esac
68 }
69
70
71 main() {
72   parse_cmdline "$@"
73 }
74
75 main "$@"