-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb2-driver-installer.py
executable file
·549 lines (405 loc) · 16.5 KB
/
db2-driver-installer.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#! /usr/bin/env python
# -----------------------------------------------------------------------------
# db2-driver-installer.py handles installation of the Db2 driver client package.
# -----------------------------------------------------------------------------
import argparse
import grp
import json
import logging
import os
import pwd
import shutil
import signal
import sys
import tarfile
import time
import zipfile
__all__ = []
__version__ = "1.0.5" # See https://www.python.org/dev/peps/pep-0396/
__date__ = '2019-08-06'
__updated__ = '2023-04-03'
SENZING_PRODUCT_ID = "5008" # See https://github.com/senzing-garage/knowledge-base/blob/main/lists/senzing-product-ids.md
log_format = '%(asctime)s %(message)s'
# Working with bytes.
KILOBYTES = 1024
MEGABYTES = 1024 * KILOBYTES
GIGABYTES = 1024 * MEGABYTES
# The "configuration_locator" describes where configuration variables are in:
# 1) Command line options, 2) Environment variables, 3) Configuration files, 4) Default values
configuration_locator = {
"debug": {
"default": False,
"env": "SENZING_DEBUG",
"cli": "debug"
},
"sleep_time_in_seconds": {
"default": 0,
"env": "SENZING_SLEEP_TIME_IN_SECONDS",
"cli": "sleep-time-in-seconds"
},
"subcommand": {
"default": None,
"env": "SENZING_SUBCOMMAND",
}
}
# Enumerate keys in 'configuration_locator' that should not be printed to the log.
keys_to_redact = [
]
# -----------------------------------------------------------------------------
# Define argument parser
# -----------------------------------------------------------------------------
def get_parser():
''' Parse commandline arguments. '''
subcommands = {
'install': {
"help": 'Install the Db2 driver code.',
"arguments": {
"--debug": {
"dest": "debug",
"action": "store_true",
"help": "Enable debugging. (SENZING_DEBUG) Default: False"
},
"--password": {
"dest": "password",
"metavar": "SENZING_PASSWORD",
"help": "Example of information redacted in the log. Default: None"
},
"--senzing-dir": {
"dest": "senzing_dir",
"metavar": "SENZING_DIR",
"help": "Location of Senzing. Default: /opt/senzing"
},
},
},
'sleep': {
"help": 'Do nothing but sleep. For Docker testing.',
"arguments": {
"--sleep-time-in-seconds": {
"dest": "sleep_time_in_seconds",
"metavar": "SENZING_SLEEP_TIME_IN_SECONDS",
"help": "Sleep time in seconds. DEFAULT: 0 (infinite)"
},
},
},
'version': {
"help": 'Print version of program.',
},
'docker-acceptance-test': {
"help": 'For Docker acceptance testing.',
},
}
parser = argparse.ArgumentParser(prog="db2-driver-installer.py", description="Install the Db2 driver code. For more information, see https://github.com/senzing-garage/docker-db2-driver-installer")
subparsers = parser.add_subparsers(dest='subcommand', help='Subcommands (SENZING_SUBCOMMAND):')
for subcommand_key, subcommand_values in subcommands.items():
subcommand_help = subcommand_values.get('help', "")
subcommand_arguments = subcommand_values.get('arguments', {})
subparser = subparsers.add_parser(subcommand_key, help=subcommand_help)
for argument_key, argument_values in subcommand_arguments.items():
subparser.add_argument(argument_key, **argument_values)
return parser
# -----------------------------------------------------------------------------
# Message handling
# -----------------------------------------------------------------------------
# 1xx Informational (i.e. logging.info())
# 3xx Warning (i.e. logging.warning())
# 5xx User configuration issues (either logging.warning() or logging.err() for Client errors)
# 7xx Internal error (i.e. logging.error for Server errors)
# 9xx Debugging (i.e. logging.debug())
MESSAGE_INFO = 100
MESSAGE_WARN = 300
MESSAGE_ERROR = 700
MESSAGE_DEBUG = 900
message_dictionary = {
"100": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}I",
"150": "{0} copied to {1}.",
"292": "Configuration change detected. Old: {0} New: {1}",
"293": "For information on warnings and errors, see https://github.com/senzing-garage/stream-loader#errors",
"294": "Version: {0} Updated: {1}",
"295": "Sleeping infinitely.",
"296": "Sleeping {0} seconds.",
"297": "Enter {0}",
"298": "Exit {0}",
"299": "{0}",
"300": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}W",
"350": "Cannot copy {0} to {1}.",
"499": "{0}",
"500": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"695": "Unknown database scheme '{0}' in database url '{1}'",
"696": "Bad SENZING_SUBCOMMAND: {0}.",
"697": "No processing done.",
"698": "Program terminated with error.",
"699": "{0}",
"700": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"886": "G2Engine.addRecord() bad return code: {0}; JSON: {1}",
"888": "G2Engine.addRecord() G2ModuleNotInitialized: {0}; JSON: {1}",
"889": "G2Engine.addRecord() G2ModuleGenericException: {0}; JSON: {1}",
"890": "G2Engine.addRecord() Exception: {0}; JSON: {1}",
"891": "Original and new database URLs do not match. Original URL: {0}; Reconstructed URL: {1}",
"892": "Could not initialize G2Product with '{0}'. Error: {1}",
"893": "Could not initialize G2Hasher with '{0}'. Error: {1}",
"894": "Could not initialize G2Diagnostic with '{0}'. Error: {1}",
"895": "Could not initialize G2Audit with '{0}'. Error: {1}",
"896": "Could not initialize G2ConfigMgr with '{0}'. Error: {1}",
"897": "Could not initialize G2Config with '{0}'. Error: {1}",
"898": "Could not initialize G2Engine with '{0}'. Error: {1}",
"899": "{0}",
"900": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}D",
"999": "{0}",
}
def message(index, *args):
index_string = str(index)
template = message_dictionary.get(index_string, "No message for index {0}.".format(index_string))
return template.format(*args)
def message_generic(generic_index, index, *args):
index_string = str(index)
return "{0} {1}".format(message(generic_index, index), message(index, *args))
def message_info(index, *args):
return message_generic(MESSAGE_INFO, index, *args)
def message_warning(index, *args):
return message_generic(MESSAGE_WARN, index, *args)
def message_error(index, *args):
return message_generic(MESSAGE_ERROR, index, *args)
def message_debug(index, *args):
return message_generic(MESSAGE_DEBUG, index, *args)
def get_exception():
''' Get details about an exception. '''
exception_type, exception_object, traceback = sys.exc_info()
frame = traceback.tb_frame
line_number = traceback.tb_lineno
filename = frame.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, line_number, frame.f_globals)
return {
"filename": filename,
"line_number": line_number,
"line": line.strip(),
"exception": exception_object,
"type": exception_type,
"traceback": traceback,
}
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
def get_configuration(args):
''' Order of precedence: CLI, OS environment variables, INI file, default. '''
result = {}
# Copy default values into configuration dictionary.
for key, value in list(configuration_locator.items()):
result[key] = value.get('default', None)
# "Prime the pump" with command line args. This will be done again as the last step.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Copy OS environment variables into configuration dictionary.
for key, value in list(configuration_locator.items()):
os_env_var = value.get('env', None)
if os_env_var:
os_env_value = os.getenv(os_env_var, None)
if os_env_value:
result[key] = os_env_value
# Copy 'args' into configuration dictionary.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Add program information.
result['program_version'] = __version__
result['program_updated'] = __updated__
# Special case: subcommand from command-line
if args.subcommand:
result['subcommand'] = args.subcommand
# Special case: Change boolean strings to booleans.
booleans = ['debug']
for boolean in booleans:
boolean_value = result.get(boolean)
if isinstance(boolean_value, str):
boolean_value_lower_case = boolean_value.lower()
if boolean_value_lower_case in ['true', '1', 't', 'y', 'yes']:
result[boolean] = True
else:
result[boolean] = False
# Special case: Change integer strings to integers.
integers = [
'sleep_time_in_seconds'
]
for integer in integers:
integer_string = result.get(integer)
result[integer] = int(integer_string)
return result
def validate_configuration(config):
''' Check aggregate configuration from commandline options, environment variables, config files, and defaults. '''
user_warning_messages = []
user_error_messages = []
# Perform subcommand specific checking.
subcommand = config.get('subcommand')
if subcommand in ['install']:
pass
# Log warning messages.
for user_warning_message in user_warning_messages:
logging.warning(user_warning_message)
# Log error messages.
for user_error_message in user_error_messages:
logging.error(user_error_message)
# Log where to go for help.
if len(user_warning_messages) > 0 or len(user_error_messages) > 0:
logging.info(message_info(293))
# If there are error messages, exit.
if len(user_error_messages) > 0:
exit_error(597)
def redact_configuration(config):
''' Return a shallow copy of config with certain keys removed. '''
result = config.copy()
for key in keys_to_redact:
result.pop(key)
return result
# -----------------------------------------------------------------------------
# Utility functions
# -----------------------------------------------------------------------------
def create_signal_handler_function(args):
''' Tricky code. Uses currying technique. Create a function for signal handling.
that knows about "args".
'''
def result_function(signal_number, frame):
logging.info(message_info(298, args))
sys.exit(0)
return result_function
def bootstrap_signal_handler(signal, frame):
sys.exit(0)
def entry_template(config):
''' Format of entry message. '''
debug = config.get("debug", False)
config['start_time'] = time.time()
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(297, config_json)
def exit_template(config):
''' Format of exit message. '''
debug = config.get("debug", False)
stop_time = time.time()
config['stop_time'] = stop_time
config['elapsed_time'] = stop_time - config.get('start_time', stop_time)
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(298, config_json)
def exit_error(index, *args):
''' Log error message and exit program. '''
logging.error(message_error(index, *args))
logging.error(message_error(698))
sys.exit(1)
def exit_silently():
''' Exit program. '''
sys.exit(1)
# -----------------------------------------------------------------------------
# Utility functions
# -----------------------------------------------------------------------------
def common_prolog(config):
'''Common steps for most do_* functions.'''
validate_configuration(config)
logging.info(entry_template(config))
# -----------------------------------------------------------------------------
# do_* functions
# Common function signature: do_XXX(config)
# -----------------------------------------------------------------------------
def do_docker_acceptance_test(config):
'''Sleep.'''
# Prolog.
logging.info(entry_template(config))
# Epilog.
logging.info(exit_template(config))
def do_install(args):
'''Install Senzing_API.tgz package. Backup existing version.'''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(args)
# Prolog.
common_prolog(config)
# Copy directory inside of container to mounted volume.
source = "/opt/IBM-template"
staging = "/opt/IBM/staging"
target = "/opt/IBM/"
# Copying source to a mounted target is a little tricky.
try:
shutil.copytree(source, staging, symlinks=True)
shutil.move("{0}/db2".format(staging), target)
shutil.rmtree(staging)
logging.info(message_info(150, source, target))
except:
logging.info(message_warning(350, source, target))
# Epilog.
logging.info(exit_template(config))
def do_sleep(args):
''' Sleep. Used for debugging. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(args)
# Prolog.
logging.info(entry_template(config))
# Pull values from configuration.
sleep_time_in_seconds = config.get('sleep_time_in_seconds')
# Sleep
if sleep_time_in_seconds > 0:
logging.info(message_info(296, sleep_time_in_seconds))
time.sleep(sleep_time_in_seconds)
else:
sleep_time_in_seconds = 3600
while True:
logging.info(message_info(295))
time.sleep(sleep_time_in_seconds)
# Epilog.
logging.info(exit_template(config))
def do_version(args):
''' Log version information. '''
logging.info(message_info(294, __version__, __updated__))
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
# Configure logging. See https://docs.python.org/2/library/logging.html#levels
log_level_map = {
"notset": logging.NOTSET,
"debug": logging.DEBUG,
"info": logging.INFO,
"fatal": logging.FATAL,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL
}
log_level_parameter = os.getenv("SENZING_LOG_LEVEL", "info").lower()
log_level = log_level_map.get(log_level_parameter, logging.INFO)
logging.basicConfig(format=log_format, level=log_level)
# Trap signals temporarily until args are parsed.
signal.signal(signal.SIGTERM, bootstrap_signal_handler)
signal.signal(signal.SIGINT, bootstrap_signal_handler)
# Parse the command line arguments.
subcommand = os.getenv("SENZING_SUBCOMMAND", None)
parser = get_parser()
if len(sys.argv) > 1:
args = parser.parse_args()
subcommand = args.subcommand
elif subcommand:
args = argparse.Namespace(subcommand=subcommand)
else:
parser.print_help()
if len(os.getenv("SENZING_DOCKER_LAUNCHED", "")):
subcommand = "sleep"
args = argparse.Namespace(subcommand=subcommand)
do_sleep(args)
exit_silently()
# Catch interrupts. Tricky code: Uses currying.
signal_handler = create_signal_handler_function(args)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Transform subcommand from CLI parameter to function name string.
subcommand_function_name = "do_{0}".format(subcommand.replace('-', '_'))
# Test to see if function exists in the code.
if subcommand_function_name not in globals():
logging.warning(message_warning(596, subcommand))
parser.print_help()
exit_silently()
# Tricky code for calling function based on string.
globals()[subcommand_function_name](args)