-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.py
More file actions
228 lines (166 loc) · 8.67 KB
/
scan.py
File metadata and controls
228 lines (166 loc) · 8.67 KB
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
"""
The reason for this module is to use "source" and "target" in SCO.exe to scan schemas instead of configuration
files.
"""
import subprocess
import os
import argparse
import sys
import variable
import logging
import ConnectToOracle
#logging
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
scan_logger = logging.getLogger('scan-log')
if not scan_logger.handlers:
scan_fh = logging.FileHandler('scan-ddl-debug.log')
scan_logger.setLevel(logging.DEBUG)
scan_fh.setFormatter(formatter)
scan_logger.addHandler(scan_fh) #add the file handler to the logger
def ddl_scan(schema_name, diff_dir, dump_dir, direction=True, rediff=False, docker_mode=None):
"""Pass in the schema to scan to 'schema_name'. Set the direction. For example, direction=True would be an upgrade.
The 'diff_dir' parameter holds where the ignore rules files are located.
The 'dump_dir' parameter specifies where the diff scripts will be written on disk.
The 'rediff' parameter specifies if this is a rediff.
The 'docker_mode' parameter specifies which Docker container is in use if any. The TNS name aliases are changed depending on this parameter.
From the red-gate documentation, "The target is the data source that will change after you deploy."
Here is an example of an upgrade configuration:
$sco.exe /source:PDB2 /target:PDB1
"""
scan_logger.debug("The code is at the top of the 'ddl_scan' function.")
#if 'schema_name' is None, False, or empty, not initialized, stop the program.
assert schema_name, "Schema not defined to ddl_scan. Stopping."
secret_key = None
network_alias1 = None
network_alias2 = None
#get the password
if not docker_mode:
secret_key = ConnectToOracle.get_in()
else:
secret_key = ConnectToOracle.docker_cred()
#set the network alias
if variable.MODE == "FOO":
if docker_mode:
if docker_mode == "Oradb19r3Apx20r1":
network_alias1 = "FOOPDB1LOC19R3"
network_alias2 = "FOOPDB2LOC19R3"
elif docker_mode == "ORADB12R1_OEL":
network_alias1 = "FOOPDB1LOCAL"
network_alias2 = "FOOPDB2LOCAL"
else:
network_alias1 = "FOOPDB1LOCAL"
network_alias2 = "FOOPDB2LOCAL"
elif variable.MODE == "MONO":
if docker_mode:
network_alias1 = "EGGPDB1REM19NA" #Does this only work in OCI?
network_alias2 = "EGGPDB2REM19NA"
else: #not FOO - This must be FIZZ
if docker_mode:
if docker_mode == "Oradb19r3Apx20r1":
network_alias1 = "FIZZPDB1LOCAL"
network_alias2 = "FIZZPDB2LOCAL"
elif docker_mode == "Ora19r3":
network_alias1 = "FIZZPDB1R3"
network_alias2 = "FIZZPDB2R3"
else:
network_alias1 = "FIZZPDB1LOCAL"
network_alias2 = "FIZZPDB2LOCAL"
print(f"[+] MODE = {variable.MODE}")
print(f"[+] network_alias2 = {network_alias2}")
print(f"[+] network_alias1 = {network_alias1}")
print(f"[+] docker_mode = {docker_mode}")
os.chdir(diff_dir) # change to where the ignorerules JSON files are located on disk
#create the dump_dir if this folder does not exist
if not os.path.exists(dump_dir):
os.mkdir(dump_dir)
print("[+] Directory created.")
else:
scan_logger.debug(f"dump_dir already exists: {dump_dir}")
print(f"[*] Scan.py says: The dump_dir is: {dump_dir}")
if not rediff:
#create the diff script name
diff_script = schema_name + "_diff_script.sql"
else:
diff_script = schema_name + "_RE-DIFF.sql"
path_and_file = dump_dir + os.sep + diff_script
print("The name of the diff_file is: %s " % (path_and_file))
#set the ignore rules file so ZTST and $DML triggers are not included in the diff scripts.
ignore_file = "IGNORE_ignoreRules.scpf"
if "FIZZ_COMMON" in schema_name:
ignore_file = "FIZZ_COMMON_ignoreRules.scpf" #this ignores the consts$ package changes
if direction:
#PDB1 is the target - upgrade
print("The code reached this point.")
returned_data = subprocess.run(['sco', '/source',f'system/{secret_key}@{network_alias2}{{{schema_name}}}','/target', \
f'system/{secret_key}@{network_alias1}{{{schema_name}}}', \
'/b:hdgrf', '/i:sdwqvag'\
, '/verbose', '/ignorerules' \
,f'{ignore_file}', f"/scriptfile:{path_and_file}" ], \
text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if returned_data:
print(f"[+] This is 'returned_data': {returned_data}")
if returned_data.stdout:
print("[+] STDOUT UP = %s" % returned_data.stdout)
if returned_data.stderr:
print("[+] STDERR UP = %s" % returned_data.stderr)
else:
print("[+] Nothing returned from the up 'ddl_scan' run process.")
else:
#PDB2 is the target - downgrade
returned_data = subprocess.run(['sco', '/target',f'system/{secret_key}@{network_alias2}{{{schema_name}}}','/source', \
f'system/{secret_key}@{network_alias1}{{{schema_name}}}', \
'/b:hdgrf', '/i:sdwqvag' \
, '/verbose', '/ignorerules' \
,f'{ignore_file}', f"/scriptfile:{path_and_file}" ], \
text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if returned_data:
print(f"[+] This is 'returned_data': {returned_data}")
if returned_data.stdout:
print("[+] STDOUT DOWN = %s" % returned_data.stdout)
if returned_data.stderr:
print("[+] STDERR DOWN = %s" % returned_data.stderr)
else:
print("[+] Nothing returned from the down 'ddl_scan' run process.")
print(f"[+] {schema_name} ddl_scan complete.")
if __name__ == "__main__":
print("""
. ., L.
;W ,Wt EW: ,ft
f#E i#D. .. E##; t#E
.E#f f#f ;W, E###t t#E
iWW; .D#i j##, E#fE#f t#E
L##Lffi:KW, G###, E#t D#G t#E
tLLG##L t#f :E####, E#t f#E. t#E
,W#i ;#G ;W#DG##, E#t t#K: t#E
j#E. :KE. j###DW##, E#t ;#W,t#E
.D#j .DW: G##i,,G##, E#t :K#D#E
,WK, L#, :K#K: L##, E#t .E##E
EG. jt ;##D. L##, .. G#E
, ,,, .,, fE
,
""")
parse = argparse.ArgumentParser(description="""A module to create diff scripts without using configuration files.
The module uses filters to remove database objects not desired such as ZTST test package and $DML triggers.
""")
parse.add_argument("schema", help="The schema to scan in the database.", action="store") #mandatory argument
parse.add_argument("direction", action="store_true", help="Specify an upgrade or a downgrade.")
parse.add_argument("filters", action="store", help="The path to the ignore rules JSON files.") #mandatory argument
parse.add_argument("dump", action="store", help="The location where to save the files.") #mandatory argument
parse.add_argument("--rediff", action="store_true", help="Use this option to create re-diff scripts.")
parse.add_argument('--docker', action="store", help="Specify which Docker container to use.")
args = parse.parse_args()
variable.init()
variable.toggle_variables('MONO') #test MONO
print("*" * 40)
print("DEBUGGING INFO")
print(f"[+] args is type: {type(args)}")
print("Command line arguments")
print(f"args.schema = {args.schema}")
print(f"args.direction = {args.direction}")
print(f"args.filters = {args.filters}")
print(f"args.dump = {args.dump}")
print(f"args.rediff = {args.rediff}")
print(f"args.docker = {args.docker}")
print("*" * 40)
#executes if the schema, the JSON filter location, and the dump dir given but this is not a downgrade or re-diff
ddl_scan(schema_name=args.schema, diff_dir=args.filters, dump_dir=args.dump, direction=args.direction, rediff=args.rediff, docker_mode=args.docker)