Skip to content

Commit 10aab83

Browse files
authored
Merge pull request #220 from pythian/rel/oratk-57-orachk
Add ORAchk to the Oracle Toolkit (#35)
2 parents 7f067af + 028aeb6 commit 10aab83

File tree

7 files changed

+919
-48
lines changed

7 files changed

+919
-48
lines changed

check-oracle.sh

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#!/bin/bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# used for oracle ORAchk. install, run and uninstall ORAchk
17+
#
18+
# Check if we're using the Mac stock getopt and fail if true
19+
# shellcheck disable=SC2034
20+
out="$(getopt -T)"
21+
if [ $? != 4 ]; then
22+
echo -e "Your getopt does not support long parameters, possibly you're on a Mac, if so please install gnu-getopt with brew"
23+
echo -e "\thttps://brewformulas.org/Gnu-getopt"
24+
exit 1
25+
fi
26+
27+
# do not display skipped hosts -
28+
# a misnomer, as it does not display all 'skipped' tasks
29+
export ANSIBLE_EXTRA_VARS=''
30+
# use ANSIBLE_DISPLAY_SKIPPED_HOSTS=true ./check-oracle.sh to see skipped tasks
31+
: ${ANSIBLE_DISPLAY_SKIPPED_HOSTS:=false}
32+
export ANSIBLE_DISPLAY_SKIPPED_HOSTS
33+
#echo ANSIBLE_DISPLAY_SKIPPED_HOSTS: $ANSIBLE_DISPLAY_SKIPPED_HOSTS
34+
35+
export INVENTORY_FILE=''
36+
export AHF_LOCATION=''
37+
38+
export AHF_LOCATION_PARAM='^gs://.+[^/]$'
39+
40+
export ORACLE_SERVER=''
41+
export AHF_DIR='AHF'
42+
export AHF_FILE=''
43+
export AHF_INSTALL=0
44+
export AHF_UNINSTALL=0
45+
export RUN_ORACHK=0
46+
47+
export GETOPT_MANDATORY="instance-ip-addr:"
48+
export GETOPT_OPTIONAL="extra-vars:,ahf-location:,db-name:,inventory-file:,ahf-install,ahf-uninstall,run-orachk,help,debug"
49+
50+
export GETOPT_LONG="$GETOPT_MANDATORY,$GETOPT_OPTIONAL"
51+
export GETOPT_SHORT="h"
52+
53+
options="$(getopt --longoptions "$GETOPT_LONG" --options "$GETOPT_SHORT" -- "$@")"
54+
55+
# shellcheck disable=SC2181
56+
[ $? -eq 0 ] || {
57+
echo "Invalid options provided: $*" >&2
58+
exit 1
59+
}
60+
61+
eval set -- "$options"
62+
63+
help () {
64+
65+
echo -e "\tUsage: $(basename "$0")"
66+
echo "${GETOPT_MANDATORY}" | sed 's/,/\n/g' | sed 's/:/ <value>/' | sed 's/\(.\+\)/\t --\1/'
67+
echo "${GETOPT_OPTIONAL}" | sed 's/,/\n/g' | sed 's/:/ <value>/' | sed 's/\(.\+\)/\t [ --\1 ]/'
68+
echo
69+
echo "--ahf-install and --run-orachk may be combined to install and run"
70+
echo "--extra-vars is used to pass extra ansible vars"
71+
echo " example: --extra-vars "var1=val1 var2=val2 ...""
72+
73+
}
74+
75+
# check if both install and run were specified together
76+
export RUN_ENABLED=0
77+
export INSTALL_ENABLED=0
78+
79+
while true
80+
do
81+
82+
case "$1" in
83+
84+
--extra-vars)
85+
ANSIBLE_EXTRA_VARS="$2"
86+
;;
87+
88+
--ahf-location)
89+
AHF_LOCATION="$2"
90+
;;
91+
92+
--debug)
93+
export ANSIBLE_DEBUG=1
94+
export ANSIBLE_DISPLAY_SKIPPED_HOSTS=true
95+
;;
96+
97+
--help | -h)
98+
help >&2
99+
exit 0
100+
;;
101+
102+
--inventory-file)
103+
INVENTORY_FILE="$2"
104+
shift;
105+
;;
106+
107+
--db-name)
108+
ORACLE_SID="$2"
109+
shift
110+
;;
111+
112+
--instance-ip-addr)
113+
ORACLE_SERVER="$2"
114+
shift
115+
;;
116+
117+
--ahf-install)
118+
: ${ORACLE_SID:='NOOP'}
119+
AHF_UNINSTALL=1
120+
AHF_INSTALL=1
121+
INSTALL_ENABLED=1
122+
;;
123+
124+
--ahf-uninstall)
125+
ORACLE_SID='NOOP'
126+
AHF_UNINSTALL=1
127+
AHF_INSTALL=0
128+
;;
129+
130+
--run-orachk)
131+
AHF_UNINSTALL=0
132+
AHF_INSTALL=0
133+
RUN_ORACHK=1
134+
RUN_ENABLED=1
135+
;;
136+
137+
--)
138+
shift
139+
break
140+
;;
141+
142+
esac
143+
144+
shift
145+
146+
done
147+
148+
# one of install, uninstall or run must be called
149+
150+
[[ $AHF_INSTALL -eq 0 ]] && [[ $AHF_UNINSTALL -eq 0 ]] && [[ $RUN_ORACHK -eq 0 ]] && { help; exit 1; }
151+
152+
153+
[[ -n $ANSIBLE_EXTRA_VARS ]] && {
154+
155+
if [[ ! "$ANSIBLE_EXTRA_VARS" =~ ^([a-zA-Z_][a-zA-Z0-9_]*=[^[:space:]]+)([[:space:]]+[a-zA-Z_][a-zA-Z0-9_]*=[^[:space:]]+)*$ ]]; then
156+
echo "Invalid format for --extra-vars"
157+
echo "the extra vars should be a string of 1 or more name=value pairs separated by a space"
158+
echo "example: varname1=value varname2=value2 varname3=value3"
159+
exit 1
160+
fi
161+
162+
}
163+
164+
[ "$RUN_ENABLED" -eq 1 ] && [ "$INSTALL_ENABLED" -eq 1 ] && {
165+
AHF_UNINSTALL=1
166+
AHF_INSTALL=1
167+
INSTALL_ENABLED=1
168+
}
169+
170+
[[ -z $ORACLE_SID ]] && { echo "please specify --db-name"; echo; help; exit 1; }
171+
172+
# if an ip address is passed for --instance-ip-address, and inventory file is not specified on the cli, check for an inventory file by lookup of target hostname
173+
[[ "$ORACLE_SERVER" =~ ^[[:digit:]]{1,3}[.][[:digit:]]{1,3}[.][[:digit:]]{1,3}[.][[:digit:]]{1,3}$ ]] && [[ -z "$INVENTORY_FILE" ]] && {
174+
TARGET_HOSTNAME="$( dig +short -x $ORACLE_SERVER | cut -f1 -d\. )"
175+
TEST_INVENTORY_FILE=inventory_files/inventory_${TARGET_HOSTNAME}_${ORACLE_SID}
176+
[[ -r $TEST_INVENTORY_FILE ]] && INVENTORY_FILE="$TEST_INVENTORY_FILE"
177+
}
178+
179+
# if the inventory file is specified on the cli, then the --instance-ip-addr or ORACLE_SERVER are not required
180+
if [[ -z $INVENTORY_FILE ]]; then
181+
[[ -z $ORACLE_SERVER ]] && { echo "please specify --instance-ip-addr"; echo; help; exit 1; }
182+
INVENTORY_FILE=inventory_files/inventory_${ORACLE_SERVER}_${ORACLE_SID}
183+
fi
184+
185+
[[ -r $INVENTORY_FILE ]] || {
186+
echo "cannot read inventory file '$INVENTORY_FILE'"
187+
echo " please check and --instance-ip-addr"
188+
echo " and --inventory-file"
189+
exit 1
190+
}
191+
192+
# fail early if the AFH file format is incorrect and/or the file does not exist.
193+
[[ -n "$AHF_LOCATION" ]] || [[ $AHF_INSTALL -eq 1 ]] && {
194+
195+
[[ ! "$AHF_LOCATION" =~ $AHF_LOCATION_PARAM ]] && {
196+
echo "Incorrect parameter provided for AHF_LOCATION: $AHF_LOCATION"
197+
echo "Example: gs://my-gcs-bucket/ahf/ahf.zip"
198+
exit 1
199+
}
200+
201+
( gsutil ls "$AHF_LOCATION" >/dev/null 2>&1 ) || {
202+
echo "--ahf-location file '$AHF_LOCATION' not found"
203+
exit 1;
204+
}
205+
}
206+
207+
# Uninstall AHF
208+
[[ $AHF_UNINSTALL -eq 1 ]] && {
209+
210+
ansible-playbook -i "$INVENTORY_FILE" check-oracle.yml \
211+
--extra-vars "uninstall_ahf=true $ANSIBLE_EXTRA_VARS"
212+
}
213+
214+
# Install AHF
215+
[[ $AHF_INSTALL -eq 1 ]] && {
216+
217+
218+
ansible-playbook -i "$INVENTORY_FILE" check-oracle.yml \
219+
--extra-vars "uninstall_ahf=false AHF_LOCATION=$AHF_LOCATION $ANSIBLE_EXTRA_VARS"
220+
}
221+
222+
# run ORAchk
223+
[[ $RUN_ORACHK -eq 1 ]] && {
224+
225+
ansible-playbook -i "$INVENTORY_FILE" check-oracle.yml \
226+
--extra-vars "uninstall_ahf=false run_orachk=true ORACLE_SID=$ORACLE_SID $ANSIBLE_EXTRA_VARS"
227+
}
228+
229+

0 commit comments

Comments
 (0)