-
Notifications
You must be signed in to change notification settings - Fork 6
/
runOracle.sh
executable file
·197 lines (161 loc) · 6.01 KB
/
runOracle.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
#!/bin/bash
# LICENSE CDDL 1.0 + GPL 2.0
#
# Copyright (c) 1982-2016 Oracle and/or its affiliates. All rights reserved.
#
# Since: November, 2016
# Author: [email protected]
# Description: Runs the Oracle Database inside the container
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
########### Move DB files ############
function moveFiles {
if [ ! -d $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID ]; then
mkdir -p $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
fi;
mv $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
mv $ORACLE_HOME/dbs/orapw$ORACLE_SID $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
mv $ORACLE_HOME/network/admin/sqlnet.ora $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
mv $ORACLE_HOME/network/admin/listener.ora $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
mv $ORACLE_HOME/network/admin/tnsnames.ora $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
# oracle user does not have permissions in /etc, hence cp and not mv
cp /etc/oratab $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/
symLinkFiles;
}
########### Symbolic link DB files ############
function symLinkFiles {
if [ ! -L $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora ]; then
ln -s $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/spfile$ORACLE_SID.ora $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora
fi;
if [ ! -L $ORACLE_HOME/dbs/orapw$ORACLE_SID ]; then
ln -s $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/orapw$ORACLE_SID $ORACLE_HOME/dbs/orapw$ORACLE_SID
fi;
if [ ! -L $ORACLE_HOME/network/admin/sqlnet.ora ]; then
ln -s $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/sqlnet.ora $ORACLE_HOME/network/admin/sqlnet.ora
fi;
if [ ! -L $ORACLE_HOME/network/admin/listener.ora ]; then
ln -s $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/listener.ora $ORACLE_HOME/network/admin/listener.ora
fi;
if [ ! -L $ORACLE_HOME/network/admin/tnsnames.ora ]; then
ln -s $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/tnsnames.ora $ORACLE_HOME/network/admin/tnsnames.ora
fi;
# oracle user does not have permissions in /etc, hence cp and not ln
cp $ORACLE_BASE/oradata/dbconfig/$ORACLE_SID/oratab /etc/oratab
}
########### SIGINT handler ############
function _int() {
echo "Stopping container."
echo "SIGINT received, shutting down database!"
sqlplus / as sysdba <<EOF
shutdown immediate;
exit;
EOF
lsnrctl stop
}
########### SIGTERM handler ############
function _term() {
echo "Stopping container."
echo "SIGTERM received, shutting down database!"
sqlplus / as sysdba <<EOF
shutdown immediate;
exit;
EOF
lsnrctl stop
}
########### SIGKILL handler ############
function _kill() {
echo "SIGKILL received, shutting down database!"
sqlplus / as sysdba <<EOF
shutdown abort;
exit;
EOF
lsnrctl stop
}
###################################
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
############# MAIN ################
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
###################################
# Check whether container has enough memory
# Github issue #219: Prevent integer overflow,
# only check if memory digits are less than 11 (single GB range and below)
if [ `cat /sys/fs/cgroup/memory/memory.limit_in_bytes | wc -c` -lt 11 ]; then
if [ `cat /sys/fs/cgroup/memory/memory.limit_in_bytes` -lt 2147483648 ]; then
echo "Error: The container doesn't have enough memory allocated."
echo "A database container needs at least 2 GB of memory."
echo "You currently only have $((`cat /sys/fs/cgroup/memory/memory.limit_in_bytes`/1024/1024/1024)) GB allocated to the container."
exit 1;
fi;
fi;
# Set SIGINT handler
trap _int SIGINT
# Set SIGTERM handler
trap _term SIGTERM
# Set SIGKILL handler
trap _kill SIGKILL
# Default for ORACLE SID
if [ "$ORACLE_SID" == "" ]; then
export ORACLE_SID=ORCLCDB
else
# Check whether SID is no longer than 12 bytes
# Github issue #246: Cannot start OracleDB image
if [ "${#ORACLE_SID}" -gt 12 ]; then
echo "Error: The ORACLE_SID must only be up to 12 characters long."
exit 1;
fi;
# Check whether SID is alphanumeric
# Github issue #246: Cannot start OracleDB image
if [[ "$ORACLE_SID" =~ [^a-zA-Z0-9] ]]; then
echo "Error: The ORACLE_SID must be alphanumeric."
exit 1;
fi;
fi;
# Default for ORACLE PDB
export ORACLE_PDB=${ORACLE_PDB:-ORCLPDB1}
# Default for ORACLE CHARACTERSET
export ORACLE_CHARACTERSET=${ORACLE_CHARACTERSET:-AL32UTF8}
# Check whether database already exists
if [ -d $ORACLE_BASE/oradata/$ORACLE_SID ]; then
symLinkFiles;
# Make sure audit file destination exists
if [ ! -d $ORACLE_BASE/admin/$ORACLE_SID/adump ]; then
mkdir -p $ORACLE_BASE/admin/$ORACLE_SID/adump
fi;
# Start database
$ORACLE_BASE/$START_FILE;
else
# Remove database config files, if they exist
rm -f $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora
rm -f $ORACLE_HOME/dbs/orapw$ORACLE_SID
rm -f $ORACLE_HOME/network/admin/sqlnet.ora
rm -f $ORACLE_HOME/network/admin/listener.ora
rm -f $ORACLE_HOME/network/admin/tnsnames.ora
# Create database
$ORACLE_BASE/$CREATE_DB_FILE $ORACLE_SID $ORACLE_PDB $ORACLE_PWD;
# Move database operational files to oradata
moveFiles;
# Execute custom provided setup scripts
$ORACLE_BASE/$USER_SCRIPTS_FILE $ORACLE_BASE/scripts/setup
fi;
# Check whether database is up and running
$ORACLE_BASE/$CHECK_DB_FILE
if [ $? -eq 0 ]; then
echo "#########################"
echo "DATABASE IS READY TO USE!"
echo "#########################"
# Execute custom provided startup scripts
$ORACLE_BASE/$USER_SCRIPTS_FILE $ORACLE_BASE/scripts/startup
else
echo "#####################################"
echo "########### E R R O R ###############"
echo "DATABASE SETUP WAS NOT SUCCESSFUL!"
echo "Please check output for further info!"
echo "########### E R R O R ###############"
echo "#####################################"
fi;
# Tail on alert log and wait (otherwise container will exit)
echo "The following output is now a tail of the alert.log:"
tail -f $ORACLE_BASE/diag/rdbms/*/*/trace/alert*.log &
childPID=$!
wait $childPID