-
Notifications
You must be signed in to change notification settings - Fork 13
/
wait.py
131 lines (116 loc) · 4.88 KB
/
wait.py
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
################################################################################
# wait.py
#
# Copyright (C) 2016 Justin Paul <[email protected]>
#
# @author: Justin Paul
#
# This program is free software: you can redistribute it and/or modify
# it as long as you retain the name of the original author and under
# the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
import os
import sys
import time
import getopt
import shutil
import socket
import subprocess
def wait_for_database(options, arguments):
base_dir = os.path.dirname(sys.argv[0])
if base_dir in [".", ""]: base_dir = os.getcwd()
if os.path.isfile(os.path.join(base_dir, "metadata.json")):
mfile = open(os.path.join(base_dir, "metadata.json"), "r")
metadata = eval(mfile.read())
mfile.close()
else:
print "Mandatory file %s cannot be located in %s." %("metadata.json", base_dir)
sys.exit(1)
timeout = int(options.get("--timeout", 3600))
delay = int(options.get("--delay", 240))
wait_time = int(options.get("--wait", 60))
fmw_home = options.get("-f")
db_conn = options.get("-c", metadata.get("database").get("connect-string"))
dba_user = options.get("--dba_user", "SYS")
dba_pass = options.get("--dba_password", metadata.get("database").get("sys-password"))
db_prfix = db_prfix = options.get("-m", "TEST")
pwd_file = options.get("-w", "")
if os.path.isfile(pwd_file):
with open(pwd_file, "r") as bfile:
fcontents = bfile.read().splitlines()
dba_pass = fcontents[0]
command = [os.path.join(fmw_home, "oracle_common", "bin", "rcu"),
'-silent', '-listSchemas', '-connectString', db_conn,
'-dbUser', dba_user, '-dbRole', 'sysdba', '-schemaPrefixes', db_prfix]
now=time.time()
later = time.time()
while int(later - now) < timeout:
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = process.communicate(input=dba_pass)
logdir = os.path.dirname(os.path.dirname(out.splitlines()[1].split()[2]))
if os.path.isdir(logdir): shutil.rmtree(logdir, True)
if "ERROR - RCU-" in out:
later = time.time()
time.sleep(delay)
else:
break
time.sleep(wait_time)
def wait_for_socket(options, arguments):
base_dir = os.path.dirname(sys.argv[0])
if base_dir in [".", ""]: base_dir = os.getcwd()
if os.path.isfile(os.path.join(base_dir, "metadata.json")):
mfile = open(os.path.join(base_dir, "metadata.json"), "r")
metadata = eval(mfile.read())
mfile.close()
else:
print "Mandatory file %s cannot be located in %s." %("metadata.json", base_dir)
sys.exit(1)
host = options.get("-h")
port = int(options.get("-p", metadata.get("wls").get("as-port")))
timeout = int(options.get("--timeout", 3600))
delay = int(options.get("--delay", 60))
socket_timeout = int(options.get("--socket_timeout", 5))
wait_time = int(options.get("--wait", 0))
now=time.time()
later = time.time()
while int(later - now) < timeout:
try:
conn=socket.socket()
conn.settimeout(socket_timeout)
conn.connect((host, port))
conn.close()
break
except socket.error:
later = time.time()
time.sleep(delay)
except:
print "ERROR: An unexpected error has occurred."
sys.exit(2)
time.sleep(wait_time)
if __name__ == "__main__":
options, arguments = getopt.getopt(sys.argv[1:], "?f:h:p:c:w:",
["timeout=", "delay=", "wait=",
"socket_timeout=", "dba_user=",
"dba_password="])
options = dict(options)
if "-?" in options:
print "Usage: python %s %s %s %s %s" %("wait.py",
"[-?] -h host -p port -c db_connect_string [-w password_file]",
"[--wait wait_secs] [--timeout timeout_in_secs] [--delay delay_in_secs]",
"[--socket_timeout socket_timeout_in_secs] [--dba_user dba_user]",
"[--dba_password dba_password]")
sys.exit(0)
if "-c" in options:
wait_for_database(options, arguments)
if "-h" in options:
wait_for_socket(options, arguments)