Exemplo n.º 1
0
def scan_listener():
    global srvctl, shorthostname, iscrs, networks, scans
    out = {}
    for n in networks.keys():
        output = exec_program_lines(
            [srvctl, 'config', 'scan_listener', '-k', n])
        for line in output:
            endpoints = None
            # 19c
            m = re.search('Endpoints: (.+)', line)
            if m is not None:
                endpoints = m.group(1)
            else:
                # 18c, 12c
                m = re.search('SCAN Listener (.+) exists. Port: (.+)', line)
                if m is not None:
                    endpoints = m.group(2)
            if endpoints:
                out[n] = {
                    'network': n,
                    'scan_address': scans[n]['fqdn'],
                    'endpoints': endpoints,
                    'ipv4': scans[n]['ipv4'],
                    'ipv6': scans[n]['ipv6']
                }
                for proto in endpoints.split('/'):
                    p = proto.split(':')
                    out[n][p[0].lower()] = p[1]
                break
    return out
def check_db_exists(module, oracle_home, db_name, sid, db_unique_name):
    if gimanaged:
        if db_unique_name is not None:
            checkdb = db_unique_name
        else:
            checkdb = db_name
        command = "%s/bin/srvctl config database -d %s " % (oracle_home, checkdb)
        (rc, stdout, stderr) = module.run_command(command)
        if rc != 0:
            if '%s' % db_name in stdout:  # <-- db doesn't exist
                return False
            else:
                return False
        elif 'Database name: %s' % db_name in stdout:  # <-- Database already exist
            return True
    else:
        existingdbs = []
        oratabfile = '/etc/oratab'
        if os.path.exists(oratabfile):
            with open(oratabfile) as oratab:
                for line in oratab:
                    if line.startswith('#') or line.startswith(' '):
                        continue
                    elif re.search('^%s:' % db_name, line) or (sid is not None and re.search('^%s:' % sid, line)):
                        existingdbs.append(line)

        if not existingdbs:  # <-- db doesn't exist
            return False
        else:
            for dbs in existingdbs:
                if sid != '':
                    if '%s:' % db_name in dbs or '%s:' % sid in dbs:
                        if dbs.split(':')[1] != oracle_home.rstrip(
                                '/'):  # <-- DB is created, but with a different ORACLE_HOME
                            msg = 'Database %s already exists in a different ORACLE_HOME (%s)' % (
                                db_name, dbs.split(':')[1])
                            module.fail_json(msg=msg, changed=False)
                        elif dbs.split(':')[1] == oracle_home.rstrip('/'):  # <-- Database already exist
                            return True
                    else:
                        if '%s:' % db_name in dbs:
                            if dbs.split(':')[1] != oracle_home.rstrip(
                                    '/'):  # <-- DB is created, but with a different ORACLE_HOME
                                msg = 'Database %s already exists in a different ORACLE_HOME (%s)' % (
                                    db_name, dbs.split(':')[1])
                                module.fail_json(msg=msg, changed=False)
                        elif dbs.split(':')[1] == oracle_home.rstrip('/'):  # <-- Database already exist
                            return True
Exemplo n.º 3
0
 def _is(self, line, matched_by):
     if matched_by == None:
         return False
     if matched_by == self.skip_line_pattern:
         return False
     
     escaped_expected_line = self._escape(matched_by, self.regex_start_delimiter, self.regex_end_delimiter)
     return re.search("^{}$".format(escaped_expected_line), line)
Exemplo n.º 4
0
def main():
    global module, shorthostname, hostname, srvctl, crsctl, cemutlo, iscrs, vips, networks, scans
    msg = ['']
    module = AnsibleModule(
        argument_spec=dict(oracle_home=dict(required=False)),
        supports_check_mode=True)
    # Preparation
    facts = {}
    if module.params["oracle_home"]:
        os.environ['ORACLE_HOME'] = module.params["oracle_home"]
    srvctl = os.path.join(os.environ['ORACLE_HOME'], 'bin', 'srvctl')
    crsctl = os.path.join(os.environ['ORACLE_HOME'], 'bin', 'crsctl')
    cemutlo = os.path.join(os.environ['ORACLE_HOME'], 'bin', 'cemutlo')
    if not is_executable(srvctl) or not is_executable(crsctl):
        module.fail_json(
            changed=False,
            msg="Are you sure ORACLE_HOME=%s points to GI home?"
            " I can't find executables srvctl or crsctl under bin/." %
            os.environ['ORACLE_HOME'])
    iscrs = True  # This needs to be dynamically set if it is full clusterware or Oracle restart
    hostname = gethostname()
    shorthostname = hostname.split('.')[0]
    #
    if module.check_mode:
        module.exit_json(changed=False)
    # Cluster name
    if iscrs:
        facts.update({'clustername': exec_program([cemutlo, '-n'])})
    else:
        facts.update({'clustername': 'ORACLE_RESTART'})
    # Cluster version
    if iscrs:
        version = exec_program([crsctl, 'query', 'crs', 'activeversion'])
    else:
        version = exec_program([crsctl, 'query', 'has', 'releaseversion'])
    m = re.search(r'\[([0-9.]+)\]$', version)
    facts.update({'version': m.group(1)})
    # VIPS
    vips = get_vips()
    facts.update({'vip': vips.values()})
    # Networks
    networks = get_networks()
    facts.update({'network': networks.values()})
    # SCANs
    scans = get_scans()
    facts.update({'scan': scans.values()})
    # Listener
    facts.update({'local_listener': local_listener()})
    facts.update({'scan_listener': scan_listener().values() if iscrs else []})
    # Databases
    facts.update(
        {'database_list': exec_program_lines([srvctl, 'config', 'database'])})
    # Output
    module.exit_json(msg=", ".join(msg), changed=False, ansible_facts=facts)
Exemplo n.º 5
0
def get_scans():
    global srvctl, shorthostname, iscrs
    out = {}
    item = {}
    output = exec_program_lines([srvctl, 'config', 'scan', '-all'])
    for line in output:
        if line.startswith('SCAN name:'):
            if "network" in item.keys():
                out[item['network']] = item
            m = re.search('SCAN name: (.+), Network: ([0-9]+)', line)
            item = {
                'network': m.group(2),
                'name': m.group(1),
                'ipv4': [],
                'ipv6': []
            }
            item['fqdn'] = hostname_to_fqdn(item['name'])
        else:
            m = re.search('SCAN [0-9]+ (IPv[46]) VIP: (.+)', line)
            if m is not None:
                item[m.group(1).lower()] += [m.group(2)]
    if "network" in item.keys():
        out[item['network']] = item
    return out
Exemplo n.º 6
0
def get_networks():
    global srvctl, shorthostname, iscrs
    out = {}
    item = {}
    output = exec_program_lines([srvctl, 'config', 'network'])
    for line in output:
        m = re.search('Network ([0-9]+) exists', line)
        if m is not None:
            if "network" in item.keys():
                out[item['network']] = item
            item = {'network': m.group(1)}
        elif line.startswith('Subnet IPv4:'):
            item['ipv4'] = line[13:]
        elif line.startswith('Subnet IPv6:'):
            item['ipv6'] = line[13:]
    if "network" in item.keys():
        out[item['network']] = item
    return out
Exemplo n.º 7
0
def get_vips():
    global srvctl, shorthostname, iscrs
    output = exec_program_lines([srvctl, 'config', 'vip', '-n', shorthostname])
    vip = {}
    out = {}
    for line in output:
        if line.startswith('VIP exists:'):
            if "network" in vip.keys():
                out[vip['network']] = vip
            vip = {}
            m = re.search('network number ([0-9]+),', line)
            vip['network'] = m.group(1)
        elif line.startswith('VIP Name:'):
            vip['name'] = line[10:]
            vip['fqdn'] = hostname_to_fqdn(vip['name'])
        elif line.startswith('VIP IPv4 Address:'):
            vip['ipv4'] = line[18:]
        elif line.startswith('VIP IPv6 Address:'):
            vip['ipv6'] = line[18:]
    if "network" in vip.keys():
        out[vip['network']] = vip
    return out