def get_database_list(instance=None):
    """
    This function is to extract and return the list of databases on the given target DB2 Instance.
    Usage:
            database_list = get_database_list('db2inst1')
            db_list = [database for database in database_list]
    """
    databases = list()
    global db2_profile
    steplog.info('Pull the list of databases from the current instance.')
    if not instance:
        raise ValueError("You must provide the DB2 instance name")

    if not db2_profile:
        raise SystemError("Missing db2profile default path")

    if is_non_zero_file(db2_profile):
        cmd = '. ~%s/%s ; cd / ; db2 list database directory | grep "Database name"' % (
            instance, db2_profile)
        out, err, rc = db2tools.run_instance_command(instance, cmd)
        steplog.debug(out)
        steplog.debug(rc)
        if rc == 0:
            if out:
                for line in out.splitlines():
                    if line.strip().startswith('Database name'):
                        database_name = line.split('=')[1].strip()
                        databases.append(database_name)
                return databases
            else:
                steplog.info("No database found for the current instance: %s" %
                             instance)
        else:
            raise EnvironmentError(
                "Command could not run successfully. Please check again")
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)
Beispiel #3
0
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)
Beispiel #4
0
def get_db2_binary_build_version(STAGE_DIRECTORY):
    """
    This method is to find DB2 version from the unzip binary
    """
    val = ""
    path_for_base_db2_file = ""
    db2_version_list = ['10.1', '10.5', '9.7', '9.5']
    dir_list = glob.glob("%s/*" % (STAGE_DIRECTORY))
    for elm in dir_list:
        fixpack_type = os.path.split(elm)[-1]
        if fixpack_type.startswith('universal'):
            steplog.info("This is a universal fixpack binary : %s" % fixpack_type)
            server_dirname = elm
            steplog.debug("Server Directory Name : %s " % server_dirname)
            steplog.debug("Stage Directory Path : %s " % STAGE_DIRECTORY)
            if ostools.is_aix():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2')
            elif ostools.is_linux():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2')
        elif fixpack_type.startswith('server'):
            steplog.info("This is a server fixpack binary : %s" % fixpack_type)
            server_dirname = elm
            steplog.debug("Server Directory Name : %s " % server_dirname)
            steplog.debug("Stage Directory Path : %s " % STAGE_DIRECTORY)
            if ostools.is_aix():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2')
            elif ostools.is_linux():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2')
    if os.path.exists(path_for_base_db2_file):
        steplog.info("DB2 binary is found and it is uncompressed")
        try:
            files = os.listdir(path_for_base_db2_files)
            for item in files:
                if item.startswith('spec'):
                    val = item
                    steplog.debug("File to extract the fixpack version build number : %s" % val)
                    spec_file = os.path.join(path_for_base_db2_files, 'spec')
                    
                    if os.path.isfile(spec_file):
                        print ("Spac file exists and reading")
                        with open(spec_file) as fh:
                            for line in fh:
                                if "special_" in line:
                                    build_level_list = line.split("=")
                                    build_level = build_level_list[-1]
                                    build_num = build_level[8:]
                                    steplog.info("Special fixpack build level : %s " % build_num)
                                    return build_num    
        except IOError:
            steplog.info('There is no file exists')
    else:
        steplog.info("DB2 binary may have not unzipped")
Beispiel #5
0
def main():
    print "----------------------------------------------------------------------------------------"
    steplog.info('Binding packages with database objects')
    print "----------------------------------------------------------------------------------------"
    instance_line = {}
    database_list = []
    instance_to_set = ""
    install_path = params['DB2 Installation Location']
    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 not instance_line:
        steplog.warn(
            "No instance(s) found for %s DB2 Installation. There is nothing to bind....."
            % params['DB2 Installation Location'])
        sys.exit(1)
    else:
        first_inst_name = instance_line.keys()[0]
        if first_inst_name:
            instance_to_set = first_inst_name
        steplog.info(
            "Instances are found in %s DB2 setup. Upgrade process will run.." %
            params['DB2 Existing Installation Location'])
        steplog.debug(instance_to_set)

    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"])

    ## First Instance in the instances found on the target ##
    if instance_line:
        count = len(instance_line)
        for instance in instance_line:
            steplog.info("Instance Counter : %s" % count)
            count -= 1
            instance_to_bind = instance_line.keys()[count]
            steplog.info("Instance found for binding its databases : %s" %
                         instance_to_bind)
            database_list = get_databases(instance_to_bind)
            if database_list:
                bind_db2schema_packages(database_list, instance_to_bind)
                bind_db2client_packages(database_list, instance_to_bind)
                bind_db2ubind_packages(database_list, instance_to_bind)
            else:
                steplog.info(
                    "No database found under instance %s. No binding will occure"
                    % instance_to_bind)
Beispiel #6
0
def get_databases(instance):
    """
    This function is to extract the list of databases on the given target DB2 Instance
    """
    databases = []
    steplog.info('Getting list of databases.')
    cmd = '. ~%s/sqllib/db2profile ; cd / ; db2 list database directory | grep "Database name"' % instance
    out, err, retCode = db2tools.run_instance_command(instance, cmd)
    steplog.debug(out)
    if out:
        for line in out.splitlines():
            if line.strip().startswith('Database name'):
                database_name = line.split('=')[1].strip()
                databases.append(database_name)
        return databases
Beispiel #7
0
def get_db2_binary_fixpack_version(STAGE_DIRECTORY):
    """
    This method is to find DB2 version from the unzip binary
    """
    val = ""
    path_for_base_db2_file = ""
    db2_version_list = ['10.1', '10.5', '9.7', '9.5']
    dir_list = glob.glob("%s/*" % (STAGE_DIRECTORY))
    for elm in dir_list:
        fixpack_type = os.path.split(elm)[-1]
        if fixpack_type.startswith('universal'):
            steplog.info("This is a universal fixpack binary : %s" % fixpack_type)
            server_dirname = elm
            steplog.debug("Server Directory Name : %s " % server_dirname)
            steplog.debug("Stage Directory Path : %s " % STAGE_DIRECTORY)
            if ostools.is_aix():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2', 'aix', 'FILES')
            elif ostools.is_linux():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2', 'linuxamd64', 'FILES')
                
        elif fixpack_type.startswith('server'):
            steplog.info("This is a server fixpack binary : %s" % fixpack_type)
            server_dirname = elm
            steplog.debug("Server Directory Name : %s " % server_dirname)
            steplog.debug("Stage Directory Path : %s " % STAGE_DIRECTORY)
            if ostools.is_aix():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2', 'aix', 'FILES')
            elif ostools.is_linux():
                path_for_base_db2_files = os.path.join(STAGE_DIRECTORY, server_dirname, 'db2', 'linuxamd64', 'FILES')
    if os.path.exists(path_for_base_db2_file):
        steplog.info("DB2 binary is found and it is uncompressed")
        try:
            files = os.listdir(path_for_base_db2_files)
            for item in files:
                if item.startswith('INSTANCE_SETUP_SUPPORT_'):
                    val = item
                    steplog.info("Found the file to extract the fixpack version : %s" % val)
                    break
            s1 = re.compile(r'_(\d+\.\d+)\.', re.DOTALL)
            match_found = s1.search(val)
            if match_found:
                db2_version_from_file = match_found.group(1)
                if db2_version_from_file in db2_version_list:
                    steplog.info("This is the valid DB2 version : %s" % db2_version_from_file)
                    pat = re.compile(r"INSTANCE_SETUP_SUPPORT_\d+\.\d+\.\d+\.(\d+)_")
                    fp_found = pat.search(val)
                    return fp_found.group(1)
                else:
                    return False
        except IOError:
            steplog.info('There is no file exists')
    else:
        steplog.info("DB2 binary may have not unzipped")
Beispiel #8
0
 def binding_wrapper(databases, instance):
     print("I am decorating the database binding process")
     if not databases:
         steplog.warn(
             "No database(s) found on this instance. Database preupgrade check utility cannot be run"
         )
         return ""
     for database in databases:
         dbconn = connect(instance, database)
         sql = decorate_binding_process(instance)
         result_set = dbconn.query_with_db2_shell_prompt(
             sql, parse=True, use_db2_shell_prompt=True)
         if result_set:
             steplog.debug("Attempting to run the binding....")
             print result_set
         else:
             steplog.warn(
                 "Error occured while running the binding of packages with database "
             )
Beispiel #9
0
def get_scan_port(oracle_home, host=None, user=None):
    """
    Gets the RAC scan name
    """
    env_str = oracletools.get_oracle_env_string(oracle_home)
    if host:
        command = 'ssh %s "%s; %s/bin/srvctl config scan_listener"' % (
            host, env_str, oracle_home)
    else:
        command = "%s; %s/bin/srvctl config scan_listener" % (env_str,
                                                              oracle_home)
    if user:
        output, err, rc = ostools.sudo_run_command(command, user=user)
    else:
        output, err, rc = ostools.run_command(command)
    if rc != 0:
        raise ValueError('Unable to run srvctl config scan command')
    m = re.search("TCP:(.*)", output, re.IGNORECASE)
    if not m:
        raise ValueError('Unable to find scan port')
    steplog.debug("RAC Scan name = %s" % m.group(1))
    return m.group(1)
Beispiel #10
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."
Beispiel #11
0
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
Beispiel #12
0
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