Merge "adding control of number of virtual compute nodes"
[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 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> : Connect to Undercloud VM as <user>\n"
26   echo -e "   debug-stack : Print parsed deployment failures to stdout \n"
27 }
28
29 ##translates the command line argument
30 ##params: $@ the entire command line is passed
31 ##usage: parse_cmd_line() "$@"
32 parse_cmdline() {
33   local match
34
35   match=($(resolve_cmd $1 $VALID_CMDS))
36   if [ ${#match[*]} -gt 1 ]; then
37     echo "$1 is ambiguous, possible matches: ${match[*]}" >&2
38     exit 1
39   elif [ ${#match[*]} -lt 1 ]; then
40     echo "$1 is not a recognized command.  Use -h to see acceptable list" >&2
41     exit 1
42   else
43     match=$(echo $match | tr -d ' ')
44   fi
45
46   case "$match" in
47         -h|--help)
48                 display_usage
49                 exit 0
50             ;;
51         undercloud)
52                 if [ -z "$2" ]; then
53                   # connect as stack by default
54                   undercloud_connect stack
55                 else
56                   undercloud_connect $2
57                 fi
58                 exit 0
59             ;;
60         debug-stack)
61                 undercloud_connect stack "$(typeset -f debug_stack); debug_stack"
62                 exit 0
63             ;;
64         *)
65                 echo -e "\n\nThis script is used to interact with Apex deployments\n\n"
66                 echo "Use -h to display help"
67                 exit 1
68             ;;
69   esac
70 }
71
72
73 main() {
74   parse_cmdline "$@"
75 }
76
77 main "$@"