-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.sh
executable file
·240 lines (211 loc) · 7.53 KB
/
main.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
ACT_VERSION="vx.y.z"
ACT_LAST_COMMIT="xxxxxxxxxx"
USER_CONFIG="$HOME/.appdynamics/act/config.sh"
GLOBAL_CONFIG="/etc/appdynamics/act/config.sh"
CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt"
# Configure default output verbosity. May contain a combination of the following strings:
# - debug
# - error
# - warn
# - info
# - output (msg to stdout)
# An empty string silents all output
CONFIG_OUTPUT_VERBOSITY="error,output"
CONFIG_OUTPUT_COMMAND=0
CONFIG_OUTPUT_FORMAT="XML"
# Default Colors
COLOR_WARNING="\033[0;33m"
COLOR_INFO="\033[0;32m"
COLOR_ERROR="\033[0;31m"
COLOR_DEBUG="\033[0;35m"
COLOR_RESET="\033[0m"
EOL="
"
TAB=" "
GLOBAL_COMMANDS=""
GLOBAL_HELP=""
declare -i GLOBAL_COMMANDS_COUNTER=0
declare -a GLOBAL_LONG_HELP_STRINGS
declare -a GLOBAL_LONG_HELP_COMMANDS
declare -a GLOBAL_EXAMPLE_COMMANDS
declare -a GLOBAL_EXAMPLE_STRINGS
declare -a GLOBAL_DOC_NAMESPACES
declare -a GLOBAL_DOC_STRINGS
SCRIPTNAME=$(basename "$0")
VERBOSITY_COUNTER=0
declare -i VERBOSITY_COUNTER
# register namespace_command help
register() {
GLOBAL_COMMANDS_COUNTER+=1
GLOBAL_COMMANDS="${GLOBAL_COMMANDS} $1"
GLOBAL_HELP="${GLOBAL_HELP}${EOL}$*"
}
describe() {
GLOBAL_LONG_HELP_COMMANDS[${#GLOBAL_LONG_HELP_COMMANDS[@]}]="$1"
read -r -d '' GLOBAL_LONG_HELP_STRINGS[${#GLOBAL_LONG_HELP_STRINGS[@]}]
}
example() {
GLOBAL_EXAMPLE_COMMANDS[${#GLOBAL_EXAMPLE_COMMANDS[@]}]="$1"
read -r -d '' GLOBAL_EXAMPLE_STRINGS[${#GLOBAL_EXAMPLE_STRINGS[@]}]
}
doc() {
GLOBAL_DOC_NAMESPACES[${#GLOBAL_DOC_STRINGS[@]}]="$1"
read -r -d '' GLOBAL_DOC_STRINGS[${#GLOBAL_DOC_STRINGS[@]}]
}
rde() {
register "${1}" "${2}"
describe "${1}" <<RRREOF
${2} ${3}
RRREOF
example "${1}" <<RRREOF
${4}
RRREOF
}
#script_placeholder
if [ -f "${GLOBAL_CONFIG}" ]; then
debug "Sourcing global config from ${GLOBAL_CONFIG} "
. ${GLOBAL_CONFIG}
else
warning "File ${GLOBAL_CONFIG} not found!"
fi
if [ -f "${USER_CONFIG}" ]; then
debug "Sourcing user config from ${USER_CONFIG} "
. ${USER_CONFIG}
else
warning "File ${USER_CONFIG} not found!"
fi
# Parse global options
USAGE_DESCRIPTION="$SCRIPTNAME [-H <controller-host>] [-C <controller-credentials>] [-D <output-verbosity>] [-E <environment>] [-J <cookie-location>] [-P <plugin-directory>] [-F <controller-info-xml>] [-A <application-name>] [-O] [-N] [-Q] [-v[vv]] <namespace> <command>"
read -r -d '' AVAILABLE_GLOBAL_OPTIONS <<- EOM
|-H <controller-host> |specify the host of the controller you want to connect to|
|-C <controller-credentials> |provide the credentials for the controller. Format: user@tenant:password|
|-D <output-verbosity> |Change the output verbosity. Provide a list of the following values: debug,error,warn,info,output|
|-E <environment> |Call the controller within the given environment|
|-A <application-name> |Provide a default application.|
|-J <cookie-location> |Store the session cookie at a different location.|
|-F <controller-info-xml> |Read the controller credentials from a given controller-info.xml|
|-O |Don't execute the command and just print the curl call.|
|-N |Don't use colors for the verbose output.|
|-Q |If possible set the output format to JSON.|
|-v[vv] |Increase application verbosity: v = warn, vv = warn,info, vvv = warn,info,debug|
EOM
while getopts "A:H:C:E:J:D:OP:S:F:NQv" opt;
do
case "${opt}" in
E)
CONFIG_ENVIRONMENT="${OPTARG}"
debug "Set CONFIG_ENVIRONMENT=${CONFIG_ENVIRONMENT}"
ENVIRONMENT_CONFIG="${HOME}/.appdynamics/act/config.${CONFIG_ENVIRONMENT}.sh"
if [ -f "${ENVIRONMENT_CONFIG}" ]; then
debug "Sourcing user config from ${ENVIRONMENT_CONFIG} "
. ${ENVIRONMENT_CONFIG}
else
warning "File ${ENVIRONMENT_CONFIG} not found!"
fi
;;
H)
CONFIG_CONTROLLER_HOST=${OPTARG}
debug "Set CONFIG_CONTROLLER_HOST=${CONFIG_CONTROLLER_HOST}"
;;
C)
CONFIG_CONTROLLER_CREDENTIALS=${OPTARG}
debug "Set CONFIG_CONTROLLER_CREDENTIALS=${CONFIG_CONTROLLER_CREDENTIALS}"
;;
J)
CONFIG_CONTROLLER_COOKIE_LOCATION=${OPTARG}
debug "Set CONFIG_CONTROLLER_COOKIE_LOCATION=${CONFIG_CONTROLLER_COOKIE_LOCATION}"
;;
D)
CONFIG_OUTPUT_VERBOSITY=${OPTARG}
debug "Set CONFIG_OUTPUT_VERBOSITY=${CONFIG_OUTPUT_VERBOSITY}"
;;
A)
CONFIG_CONTROLLER_DEFAULT_APPLICATION=${OPTARG}
debug "Set CONFIG_CONTROLLER_DEFAULT_APPLICATION=${CONFIG_CONTROLLER_DEFAULT_APPLICATION}"
;;
P)
CONFIG_USER_PLUGIN_DIRECTORY=${OPTARG}
debug "Set CONFIG_USER_PLUGIN_DIRECTORY=${CONFIG_USER_PLUGIN_DIRECTORY}"
;;
O)
CONFIG_OUTPUT_COMMAND=1
debug "Set CONFIG_OUTPUT_COMMAND=${CONFIG_OUTPUT_COMMAND}"
;;
Q)
CONFIG_OUTPUT_FORMAT="JSON"
debug "Set CONFIG_OUTPUT_FORMAT=${CONFIG_OUTPUT_FORMAT}"
;;
S)
CONFIG_PORTAL_CREDENTIALS=${OPTARG}
debug "Set CONFIG_PORTAL_CREDENTIALS=${CONFIG_PORTAL_CREDENTIALS}"
;;
N)
COLOR_WARNING=""
COLOR_INFO=""
COLOR_ERROR=""
COLOR_DEBUG=""
COLOR_RESET=""
;;
F)
CONTROLLER_INFO_XML=${OPTARG}
debug "Reading CONFIG_CONTROLLER_HOST from $CONTROLLER_INFO_XML"
CONTROLLER_INFO_XML_HOST="$(sed -n -e "s/<controller-host>\(.*\)<\/controller-host>/\1/p" $CONTROLLER_INFO_XML)"
CONTROLLER_INFO_XML_PORT="$(sed -n -e "s/<controller-port>\(.*\)<\/controller-port>/\1/p" $CONTROLLER_INFO_XML)"
CONTROLLER_INFO_XML_SSL_ENABLED="$(sed -n -e "s/<controller-ssl-enabled>\(.*\)<\/controller-ssl-enabled>/\1/p" $CONTROLLER_INFO_XML)"
[[ ${CONTROLLER_INFO_XML_SSL_ENABLED// /} == "true" ]] && CONTROLLER_INFO_XML_SCHEMA=https || CONTROLLER_INFO_XML_SCHEMA=http
CONFIG_CONTROLLER_HOST=${CONTROLLER_INFO_XML_SCHEMA}://${CONTROLLER_INFO_XML_HOST// /}:${CONTROLLER_INFO_XML_PORT// /}
debug "Set CONFIG_CONTROLLER_HOST=${CONFIG_CONTROLLER_HOST}"
;;
v)
case $VERBOSITY_COUNTER in
0)
CONFIG_OUTPUT_VERBOSITY="${CONFIG_OUTPUT_VERBOSITY},warn"
;;
1)
CONFIG_OUTPUT_VERBOSITY="${CONFIG_OUTPUT_VERBOSITY},info"
;;
2)
CONFIG_OUTPUT_VERBOSITY="${CONFIG_OUTPUT_VERBOSITY},debug"
;;
esac
VERBOSITY_COUNTER=${VERBOSITY_COUNTER}+1
;;
esac
done
shiftOptInd
shift $SHIFTS
debug "CONFIG_CONTROLLER_HOST=$CONFIG_CONTROLLER_HOST"
debug "CONFIG_CONTROLLER_CREDENTIALS=$CONFIG_CONTROLLER_CREDENTIALS"
debug "CONFIG_CONTROLLER_COOKIE_LOCATION=$CONFIG_CONTROLLER_COOKIE_LOCATION"
debug "CONFIG_OUTPUT_VERBOSITY=$CONFIG_OUTPUT_VERBOSITY"
debug "CONFIG_USER_PLUGIN_DIRECTORY=$CONFIG_USER_PLUGIN_DIRECTORY"
recursiveSource "${CONFIG_USER_PLUGIN_DIRECTORY}"
NAMESPACE=$1
COMMAND_RESULT=""
if [ "$#" -eq 0 ] ; then
_usage
# Check if the namespace is used
elif [ "${GLOBAL_COMMANDS/${NAMESPACE}_}" != "$GLOBAL_COMMANDS" ] ; then
debug "${NAMESPACE} has commands"
COMMAND=$2
if [ "$COMMAND" == "" ] || [ "$COMMAND" == "help" ] ; then
debug "Will display help for $NAMESPACE"
_help ${NAMESPACE}
elif [ "${GLOBAL_COMMANDS/${NAMESPACE}_${COMMAND}}" != "$GLOBAL_COMMANDS" ] ; then
debug "${NAMESPACE}_${COMMAND} is a valid command"
shift 2
${NAMESPACE}_${COMMAND} "$@"
else
COMMAND_RESULT="Unknown command: $*"
fi
# Check if this is a global command
elif [ "${GLOBAL_COMMANDS/_${NAMESPACE}}" != "$GLOBAL_COMMANDS" ] ; then
debug "_${NAMESPACE} found as global command"
shift 1
_${NAMESPACE} "$@"
else
COMMAND_RESULT="Unknown command: $*"
fi
printf '%s\n' "$COMMAND_RESULT"
debug END