def main(): Instance_line={} params = parametertools.parse_dma_params() install_path = params['Install Path'] db2_install = db2tools.DB2Installation(install_path) db2_install.find_instances_and_das() ## Extract the instance details found on the target machine ## Instance_line = db2_install.instances ## First Instance in the instances found on the target ## if Instance_line: first_inst_name = Instance_line.keys()[0] instance_to_set = first_inst_name steplog.debug(first_inst_name) if instance_to_set: ## Setup the default DB2 Instance on target machine environment ## os.environ["DB2INSTANCE"] = instance_to_set.strip() steplog.debug(os.environ["DB2INSTANCE"]) inst_fix_pack = get_installed_fixpack_version(install_path) media_fixpack = get_db2_binary_fixpack_version(params['Stage Archive']) if not int(media_fixpack) > int(inst_fix_pack): steplog.error("A greater version or same version of Fix pack %s is already installed" % inst_fix_pack) sys.exit(1) states = get_software_states(db2_install) shutdown_software(db2_install, states) for key, value in states.items(): if isinstance(value, list): params[key] = ','.join(value) else: params[key] = value parametertools.print_header(params)
def main(): steplog.info("------------------------------------------------------") steplog.info("Shutting down the DB2 Instances to patch fixpack") steplog.info("------------------------------------------------------") Instance_line={} # params = parametertools.parse_dma_params() install_path = params['Install Path'] db2_install = db2tools.DB2Installation(install_path) db2_install.find_instances_and_das() ## Extract the instance details found on the target machine ## Instance_line = db2_install.instances ## First Instance in the instances found on the target ## if Instance_line: first_inst_name = Instance_line.keys()[0] instance_to_set = first_inst_name steplog.debug(first_inst_name) if instance_to_set: ## Setup the default DB2 Instance on target machine environment ## os.environ["DB2INSTANCE"] = instance_to_set.strip() steplog.debug(os.environ["DB2INSTANCE"]) inst_fix_pack = get_installed_fixpack_version(install_path) media_fixpack = get_db2_binary_fixpack_version(params['Stage Archive']) custom_fixpack = params['If Custom Fixpack'] custom_fixpack = custom_fixpack.strip() print custom_fixpack if custom_fixpack== "true" or custom_fixpack == "yes" or custom_fixpack == "y": steplog.info("The current version of fixpack determined from the binary is of the same version as of on the target. Custom fixpack will be installed") inst_fix_build_num = get_installed_fixpack_build(params['Install Path']) print "Installed fixpack build : %s" % inst_fix_build_num media_build_num = get_db2_binary_build_version(params['Stage Archive']) if not media_build_num or inst_fix_build_num: if not int(media_build_num) > int(inst_fix_build_num): steplog.error("A greater version or same version of Fixpack build %s is already installed" % inst_fix_build_num) sys.exit(1) else: if not int(media_fixpack) > int(inst_fix_pack): steplog.error("A greater version or same version of Fix pack %s is already installed" % inst_fix_pack) sys.exit(1) states = get_software_states(db2_install, install_path) shutdown_software(db2_install, states) for key, value in states.items(): if isinstance(value, list): params[key] = ','.join(value) else: params[key] = value parametertools.print_header(params)
def check_required_parameters(required_params_dict=dict()): """ To Check if the required parameters are set by user input or default parameter set through property attributes """ print threading.currentThread().getName(), 'Starting' is_valid = True required_params_not_set = pythontools.validate_required_parameters( required_params_dict) if len(required_params_not_set) > 0: is_valid = False msg = "Validate all required input parameters are set failed." for param in required_params_not_set: steplog.error("Required parameter %s is not set." % param) else: msg = "Validate all required input parameters are set succeeded." return is_valid, msg
def main(): print("-" * 80) steplog.info('Validating rollback fixpack parameters...') print("-" * 80) if ostools.is_windows(): steplog.error('This workflow runs on unix platforms only.') sys.exit(1) p1 = threading.Thread(name='create_backup_dir', target=create_backup_dir, args=(params, )) p2 = threading.Thread(name='validate_db2_installation', target=validate_db2_installation, args=(params['Current DB2 Installation Location'], )) p3 = threading.Thread(name='check_required_parameters', target=check_required_parameters, args=(params, )) p1.start() p2.start() p3.start() p1.join() p2.join() p3.join() Validator.register(commonvalidation.check_filespace_requirements, args=[setup_filespace_requirements()]) Validator.validate() failures = Validator.str_failed_results() if failures: msg = 'Error(s) validating parameters for Rollback DB2 Fixpack :\n\n%s' % failures steplog.info(msg) steplog.error(msg) sys.exit(1) fixpack_number = db2tools.get_installed_fixpack_version( params['Current DB2 Installation Location']) params['Fixpack Number'] = fixpack_number print_header(params) print("-" * 80) steplog.info( 'Validation for rollback fixpack parameters successful. You may proceed.' ) print("-" * 80)
def check_for_reachable_hosts(hosts): """ Attempt to ping the hostnames. Return non-zero code if any of the hostnames are unreachable. Assumes a Linux platform. """ is_valid = True hosts_reachable = 0 for hostname in hosts: if not ostools.test_ping(hostname): steplog.error("Hostname %s is not available at ping." % hostname) is_valid = False else: print "Hostname: %s reachable via TCP/IP network" % hostname hosts_reachable += 1 if hosts_reachable == len(hosts): msg = "Completed check for reachable hosts." else: msg = "Not all hosts verified are reachable." return is_valid, msg
def set_defaults(): global tns_file_location, lsnr_file_location global asm_home, asm_user if not tns_file_location: tns_file_location = '%s/network/admin/tnsnames.ora' % ORACLE_HOME if not lsnr_file_location: if ASM_DISKGROUP: asm_home, asm_sid = oracletools.get_asm_info_from_oratab_file( ORATAB_FILE_LOCATION) if not asm_home: steplog.error('Unable to detect ASM entry in %s' % ORATAB_FILE_LOCATION) sys.exit(1) steplog.info('asm_home: %r' % asm_home) asm_user = ostools.get_file_owner(asm_home) steplog.info('asm_user: %r' % asm_user) lsnr_file_location = '%s/network/admin/listener.ora' % asm_home else: lsnr_file_location = '%s/network/admin/listener.ora' % ORACLE_HOME
def check_required_parameters(): is_valid = True required_params_dict = {"Primary Node Hostnames": ",".join(PRIMARY_NODE_HOSTNAMES), "Standby Node Hostnames": ",".join(STANDBY_NODE_HOSTNAMES), "Primary DB Instance Name": PRIMARY_DB_INSTANCE_NAME, "Primary Database Name": PRIMARY_DB_NAME, "Oracle sys Password": ORACLE_SYS_PASSWORD, "Oracle Account": ORACLE_USER, "Oracle OS User": ORACLE_OS_USER, "Standby DB Prefix Name": STANDBY_PREFIX, "Data Guard Standby Type": DG_STANDBY_TYPE, "Database Protection Mode": DB_PROTECTION_MODE} required_params_not_set = pythontools.validate_required_parameters(required_params_dict) if len(required_params_not_set) > 0: is_valid = False msg = "Validate all required input parameters are set failed." for param in required_params_not_set: steplog.error("Required parameter %s is not set." % param) else: msg = "Validate all required input parameters are set succeeded." return is_valid, msg
def main(): steplog.info('Setup Network Configuration on Primary and Standby Servers') set_defaults() get_listener_ports() steplog.info("ORACLE_HOME : %s" % ORACLE_HOME) steplog.info("tnsnames file location: %s" % tns_file_location) steplog.info("listener file location: %s" % lsnr_file_location) primary_hosts = PRIMARY_NODE_HOSTNAMES standby_hosts = STANDBY_NODE_HOSTNAMES # prime the status dict with True values status = {} # key by host, boolean value for host in primary_hosts + standby_hosts: status[host] = True is_valid = update_tnsnames_all_nodes(primary_hosts, standby_hosts, status) if not is_valid: steplog.error('Error updating tnsnames on some host(s)') sys.exit(1) is_valid = update_listenerora_all_nodes(primary_hosts, standby_hosts, status) if not is_valid: steplog.error('Error updating listener.ora on some standby host(s)') sys.exit(1) # now, exit with a success code, if and only if there aren't any # False entries for any host in the status dict failures = [host for host in status.keys() if status[host] != True] if len(failures) > 0: sys.exit(1) set_params(params, standby_hosts[0]) parametertools.print_header(params) steplog.info( 'Setup Network Configuration on Primary and Standby Servers successful. You may proceed.' ) sys.exit(0)
def infer_oracle_home(primary_host, instance_name, oratab_file): global oracle_home is_valid = True cmd = '/usr/bin/ssh %s ls -l %s' % (primary_host, oratab_file) output, errors, rc = ostools.run_command(cmd) if rc != 0: return False, 'Unable to reference standard oratab file location on primary host\n' filename = output.strip().split().pop() if filename != oratab_file: steplog.error('Standard path for oratab file of: %s not valid\n' % oratab_file) is_valid = False cmd = '/usr/bin/ssh %s /bin/cat %s' % (primary_host, oratab_file) output, errors, rc = ostools.run_command(cmd) if rc != 0: steplog.error("Oratab file not accessible for unknown reason.") is_valid = False entries = [line.strip() for line in output.split('\n')] matches = [entry.split(':')[1] for entry in entries if entry.split(':')[0] == instance_name] if not matches: steplog.error('Unable to match any oratab entries to target instance name: %s\n' % instance_name) is_valid = False oracle_home = matches[0] # take the oracle_home value for first instance that matched return is_valid, "Completed Oratab file validation and ORACLE_HOME discovery"
def main(): print("-" * 80) steplog.info("Verify post rollback fixpack") print("-" * 80) steplog.info( 'Comparing instance version with fixpack version that you want to rollback.' ) fixpack_version, instance_version = db2tools.get_instance_version( params['DB2 Installation Location']) print("Current instance version", instance_version) print("-" * 80) if int(instance_version) == int(params['Fixpack Number']): steplog.error("Fixpack level matchs: %s = %s. Rollback did not occur" % (instance_version, params['Fixpack Number'])) return_code = 1 else: steplog.info( "Fixpack level do not match: %s != %s. Fixpack successfully rolled back." % (instance_version, params['Fixpack Number'])) return_code = 0 print("-" * 80) sys.exit(return_code)
def main(): steplog.info('Validate Provision Oracle Data Guard') Validator.register(check_standby_DB_name) Validator.register(check_required_parameters) Validator.register(check_for_reachable_hosts, args=[PRIMARY_NODE_HOSTNAMES]) Validator.register(check_for_reachable_hosts, args=[STANDBY_NODE_HOSTNAMES]) if DB_FILE_NAME_CONVERT: Validator.register(check_filename_convert_pattern, args=[DB_FILE_NAME_CONVERT]) if LOG_FILE_NAME_CONVERT: Validator.register(check_filename_convert_pattern, args=[LOG_FILE_NAME_CONVERT]) Validator.register(oraclevalidation.validate_optional_file_name, args=[ORATAB_LOCATION, "Oratab File Location"]) Validator.register(oraclevalidation.validate_optional_file_name, args=[TNSNAMES_FILE_LOCATION, "Tnsnames File Location"]) Validator.register(commonvalidation.validate_parameter_in_allowed_values, args=[DB_PROTECTION_MODE, VALID_DB_PRIMARY_PROTECTION_MODES, "Database Protection Mode"]) Validator.register(commonvalidation.validate_parameter_in_allowed_values, args=[DG_STANDBY_TYPE, VALID_DATA_GUARD_STANDBY_TYPES, "Data Guard Standby Type"]) Validator.register(commonvalidation.validate_parameter_in_allowed_values, args=[UPDATE_STANDBY_ORATAB_FILE, ["yes", "no"], "Update oratab file on Standby Servers"]) Validator.register(commonvalidation.validate_parameter_in_allowed_values, args=[CHG_REMOTE_LOGIN_PSWDFILE, ["yes", "no"], "Change Remote Login PasswordFile on Primary"]) Validator.validate() failures = Validator.str_failed_results() if failures: msg = 'Error(s) validating parameters for Provision Oracle Data Guard:\n\n%s' % failures steplog.info(msg) steplog.error(msg) sys.exit(1) # given the instance name in primary_db_instance_name, # identify the matching entry in the /etc/oratab on the # primary_node_hostnames[0] and pluck out the corresponding # oracle_home value Validator.register(infer_oracle_home, args=[PRIMARY_NODE_HOSTNAMES[0], PRIMARY_DB_INSTANCE_NAME, ORATAB_LOCATION]) Validator.validate() failures = Validator.str_failed_results() if failures: msg = 'Error(s) validating parameters for Provision Oracle Data Guard:\n\n%s' % failures steplog.info(msg) steplog.error(msg) sys.exit(1) Validator.register(oraclevalidation.validate_oracle_home, args=[oracle_home]) Validator.register(check_working_credential) Validator.register(check_standby_service_name) Validator.validate() failures = Validator.str_failed_results() if failures: msg = 'Error(s) validating parameters for Provision Oracle Data Guard:\n\n%s' % failures steplog.info(msg) steplog.error(msg) sys.exit(1) steplog.info('Validate Provision Oracle Data Guard successful. You may proceed.') print_header() sys.exit(0)
def check_working_credential(): """ Attempt to establish a connection to the service using the user and password provided. Return a boolean indication of whether the connection attempt was successful. Verifies that the Oracle user provided has sysdba privileges. """ passwd = '%s as sysdba' % ORACLE_SYS_PASSWORD steplog.debug('#### #### ####\npassword: %r\n#### #### ####' % passwd) oracle_sid = PRIMARY_DB_INSTANCE_NAME # here we determine the type of the environment in order to export the right ORACLE_SID if CRS_HOME: database_type = oracletools.get_RAC_database_type(oracle_home, PRIMARY_DB_INSTANCE_NAME) if database_type == "RACOneNode": # first case : when we have a One Node RAC database oracle_sid = PRIMARY_DB_INSTANCE_NAME + "_1" elif database_type == "RAC": #second case: when we have multinode RAC with policy based management is_policy_managed = oracletools.is_policy_managed(oracle_home, PRIMARY_DB_INSTANCE_NAME) if is_policy_managed is True: oracle_sid = PRIMARY_DB_INSTANCE_NAME + "_1" else: # here we treat the case when for Standard RAC using admin based management oracle_sid will become DBName1 oracle_sid = PRIMARY_DB_INSTANCE_NAME + "1" result = oracletools.get_oracle_status(oracle_home=oracle_home, oracle_sid=oracle_sid, os_user=ORACLE_OS_USER, user=ORACLE_USER, user_pass=passwd) if 'shutdown' in result: steplog.error('Database %s does not appear to be running and needs to be running for this workflow execution.' % oracle_sid) return False if not result: steplog.error('No result from oracle status query') return False elif 'ERROR' in result.upper(): steplog.error('Error during oracle status query, check output for details.') return False return True, "Completed check for valid 'sys' credential on Primary DB."
def update_listenerora_all_nodes(primary_hosts, standby_hosts, status): is_valid = True # Algorithm: # If the Listener entry or LISTENER_SCAN1 entry are missing, add them. # Next: # For each line in the listener.ora file (or the defaults added) that # starts with 'LISTENER' there must be a SID_LIST_%s % <that listener # name> with the appropriate details. for index, host in enumerate(primary_hosts + standby_hosts): steplog.debug('host: %r' % host) if host in standby_hosts: n = index + 1 - len(primary_hosts) prefix_sid_listener = STANDBY_PREFIX else: n = index + 1 prefix_sid_listener = '' if ENV_TYPE == 'GRID_CLUSTER': instance = ORACLE_SID[:-1] + str(n) else: instance = ORACLE_SID listener_lines = [] amended_content = [] catter = '/bin/cat %s' % lsnr_file_location out, err, rc = ostools.run_command(catter, host=host) if rc != 0: steplog.error('Failed to execute %s command on server %s' % (catter, host)) is_valid = False status[host] = False else: lf_lines = out.split('\n') listener_lines = [ line for line in lf_lines if line.strip() and not line.startswith('ADR_BASE') and not line.startswith('SID_LIST') and line[0] not in [' ', '#'] ] # Add defaults, unless there was an error executing the command above if len(listener_lines) == 0 and is_valid: listener_augment = LISTENER_ENTRY % ('LISTENER', DB_UNIQUE_NAME, ORACLE_SID) default_scan_lsnr_augment = LISTENER_ENTRY % ( 'LISTENER_SCAN1', DB_UNIQUE_NAME, ORACLE_SID) steplog.debug( 'No listener information found in listener.ora on host %s' % host) # Add default, add it to the listener lines too so that # sid_list_listener lines get created as well. amended_content.append(listener_augment) listener_lines.append(listener_augment) # TO RE-EVALUATE !!!! if ENV_TYPE == 'GRID_CLUSTER': amended_content.append(default_scan_lsnr_augment) listener_lines.append(default_scan_lsnr_augment) for line in listener_lines: line_items = [x.strip() for x in line.split('=')] lsnr_name = line_items[0] steplog.debug('lsnr_name: %r' % lsnr_name) dg_broker_desc = SID_LISTENER_DESC % ( "%s%s_DGMGRL" % (prefix_sid_listener, DB_UNIQUE_NAME), "%s%s" % (prefix_sid_listener, instance)) instance_desc = SID_LISTENER_DESC % ( "%s%s" % (prefix_sid_listener, DB_UNIQUE_NAME), "%s%s" % (prefix_sid_listener, instance)) s_lsnr_augment = SID_LISTENER_ENTRY % (lsnr_name, dg_broker_desc + instance_desc) # remove any already existing same SID_LIST_LISTENER entry of the corresponding listener in listener.ora file sid_list_listener_pattern = "SID_LIST_%s\s*=\s*\(SID_LIST" % lsnr_name removed = oracletools.remove_existing_network_config( lsnr_file_location, sid_list_listener_pattern, host) while removed: removed = oracletools.remove_existing_network_config( lsnr_file_location, sid_list_listener_pattern, host) # add this entry to listener.ora file amended_content.append(s_lsnr_augment) ok = True if len(amended_content) > 0: ok = ostools.write_file_remote(lsnr_file_location, '\n'.join(amended_content), host, append=True) if asm_user: ostools.set_file_owner(lsnr_file_location, asm_user, host) else: ostools.set_file_owner(lsnr_file_location, ORACLE_USER, host) if not ok or not is_valid: status[host] = False is_valid = False steplog.error('Failed attempt to augment listener.ora on host %r' % host) return is_valid
def update_tnsnames_all_nodes(primary_hosts, standby_hosts, status): is_valid = True if ENV_TYPE == 'GRID_CLUSTER': standby_scan_name = oracletools.get_RAC_scan_name( ORACLE_HOME, standby_hosts[0], ORACLE_USER) primary_scan_name = oracletools.get_RAC_scan_name( ORACLE_HOME, primary_hosts[0], ORACLE_USER) primary_scan_port = get_scan_port(ORACLE_HOME, host=primary_hosts[0], user=ORACLE_USER) standby_scan_port = get_scan_port(ORACLE_HOME, host=standby_hosts[0], user=ORACLE_USER) standby_address_entries = TNSNAMES_ADDRESS % (standby_scan_name, standby_scan_port) primary_address_entries = TNSNAMES_ADDRESS % (primary_scan_name, primary_scan_port) else: standby_address_entries = [ TNSNAMES_ADDRESS % (hostname, standby_lsnr_port_num) for hostname in standby_hosts ] primary_address_entries = [ TNSNAMES_ADDRESS % (hostname, primary_lsnr_port_num) for hostname in primary_hosts ] augment_primary_db = TEMPLATE_TNSNAMES % ( DB_UNIQUE_NAME, ''.join(primary_address_entries), DB_UNIQUE_NAME) augment_standby_db = TEMPLATE_TNSNAMES % (STANDBY_PREFIX + DB_UNIQUE_NAME, ''.join(standby_address_entries), SERVICE_NAME) augment = augment_primary_db + "\n" + augment_standby_db for host in primary_hosts + standby_hosts: steplog.debug('host: %r' % host) # check for pre-existing same database connection identifiers and delete if any # check first for the longer Database identifier (with prefix), cause the shorter regex will match the shorter config also connect_id_pattern = "%s\s*=\s*\(DESCRIPTION" % (STANDBY_PREFIX + DB_UNIQUE_NAME) removed = oracletools.remove_existing_network_config( tns_file_location, connect_id_pattern, host) # now we are getting perfectionists: remove multiple same database connect identifiers definitions, # small chances that there will be, but still... while removed: removed = oracletools.remove_existing_network_config( tns_file_location, connect_id_pattern, host) connect_id_pattern = "%s\s*=\s*\(DESCRIPTION" % DB_UNIQUE_NAME removed = oracletools.remove_existing_network_config( tns_file_location, connect_id_pattern, host) while removed: removed = oracletools.remove_existing_network_config( tns_file_location, connect_id_pattern, host) steplog.debug('new tnsnames.ora content: %r' % augment) ok = ostools.write_file_remote(tns_file_location, augment, host, append=True) ostools.set_file_owner(tns_file_location, ORACLE_USER, host) if not ok: is_valid &= False status[host] = False steplog.error( 'Failed attempt to update tnsnames.ora with augment on %s' % host) if 'GRID' not in ENV_TYPE: lsnr_name = oracletools.get_oracle_listener_name( oracle_home=ORACLE_HOME) local_listener = TNSNAME_LOCAL_LISTENER % (lsnr_name, 'localhost', primary_lsnr_port_num) ok = ostools.write_file_remote(tns_file_location, local_listener, host, append=True) ostools.set_file_owner(tns_file_location, ORACLE_USER, host) if not ok: is_valid &= False status[host] = False steplog.error( 'Failed attempt to update tnsnames.ora with local_listener on %s' % host) oracletools.connect_and_run_sql( "alter system set local_listener='%s' scope=both" % lsnr_name, user_pass='******', oracle_home=ORACLE_HOME, oracle_sid=ORACLE_SID) return is_valid