Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions oracle_db
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ def apply_restart_changes(module,msg,oracle_home,db_name,db_unique_name,sid,inst
for sql in change_restart_sql:
execute_sql(module,msg,cursor,sql)
if stop_db(module,msg,oracle_home,db_name,db_unique_name,sid):
if start_db(module,msg,oracle_home,db_name,db_unique_name, sid):
start_db_state = start_db(module,msg,oracle_home,db_name,db_unique_name, sid)
if start_db_state in ('ok', 'changed'):
if newdb:
msg = 'Database %s successfully created (%s) ' % (db_name, archcomp)
if output == 'verbose':
Expand Down Expand Up @@ -707,13 +708,25 @@ def start_db (module,msg, oracle_home, db_name, db_unique_name, sid):
if gimanaged:
if db_unique_name is not None:
db_name = db_unique_name
command = '%s/bin/srvctl start database -d %s' % (oracle_home,db_name)
command = '%s/bin/srvctl status database -d %s' % (oracle_home,db_name)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
elif 'Database is running' in stdout:
return 'ok'
elif 'Database is not running' in stdout:

command = '%s/bin/srvctl start database -d %s' % (oracle_home,db_name)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
return 'changed'
else:
return True
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
if sid is not None:
os.environ['ORACLE_SID'] = sid
Expand All @@ -734,7 +747,7 @@ def start_db (module,msg, oracle_home, db_name, db_unique_name, sid):
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, startup_sql)
module.fail_json(msg=msg, changed=False)
else:
return True
return 'changed'


def start_instance (module,msg, oracle_home, db_name, db_unique_name,sid, open_mode, instance_name, host_name, israc):
Expand Down Expand Up @@ -964,9 +977,13 @@ def main():
msg = "Database not found. %s" % msg
module.fail_json(msg=msg, changed=False)
else:
if start_db (module, msg, oracle_home, db_name, db_unique_name, sid):
start_db_state = start_db (module, msg, oracle_home, db_name, db_unique_name, sid)
if start_db_state == 'changed':
msg = "Database started."
module.exit_json(msg=msg, changed=True)
if start_db_state == 'ok':
msg = "Database already running."
module.exit_json(msg=msg, changed=False)
else:
msg = "Startup failed. %s" % msg
module.fail_json(msg=msg, changed=False)
Expand Down
52 changes: 39 additions & 13 deletions oracle_opatch
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ options:
required: False
default: None
aliases: ['version_added']
exclude_upi:
description:
- The unique patch identifier that should not be rolled back
e.g 22990084
- This option will be honored only when state=absent.
- It helps to write idempotent playbooks when changing interim patches that are dependent on a specific PSU/RU.
required: False
default: None
aliases: ['exclude_unique_patch_id']
opatch_minversion:
description:
- The minimum version of opatch needed
Expand Down Expand Up @@ -155,7 +164,7 @@ def get_file_owner(module, msg, oracle_home):
msg = 'Could not determine owner of %s ' % (checkfile)
module.fail_json(msg=msg)

def check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto):
def check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto, exclude_upi):
'''
Gets all patches already applied and compares to the
intended patch
Expand All @@ -169,8 +178,8 @@ def check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatc
(rc, stdout, stderr) = module.run_command(command)
#module.exit_json(msg=stdout, changed=False)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
if opatchauto:
chk = '%s' % (patch_version)
Expand All @@ -180,7 +189,17 @@ def check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatc
chk = '%s' % (patch_id)

if chk in stdout:
return True
if exclude_upi is None:
return True
else:
command += ' -id %s' % patch_id
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
chk = 'unique_patch_id:%s' % exclude_upi
return (chk not in stdout)
else:
return False

Expand Down Expand Up @@ -296,7 +315,8 @@ def stop_process(module, oracle_home):
elif len(line.split(':')) >= 2 and line.split(':')[1] == oracle_home:

# Find listener for ORACLE_HOME
p = subprocess.Popen('ps -elf| grep "[0-9] %s/bin/tnslsnr"' % (line.split(':')[1]) , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
tnslsnr = "%s/bin/tnslsnr" % (line.split(':')[1])
p = subprocess.Popen('ps -elf| grep "[0-9] %s"' % tnslsnr, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
p_status = p.wait()

Expand All @@ -305,7 +325,7 @@ def stop_process(module, oracle_home):

linelist = lline.split(' ')
if len(lline) > 3:
listener_name = linelist[-2]
listener_name = linelist[linelist.index(tnslsnr) + 1]
lsnrctl_bin = '%s/bin/lsnrctl' % (oracle_home)
try:
p = subprocess.check_call([lsnrctl_bin, 'stop', '%s' % listener_name])
Expand Down Expand Up @@ -337,7 +357,7 @@ def stop_process(module, oracle_home):
if msg:
module.fail_json(msg=msg, changed=False)

def remove_patch (module, msg, oracle_home, patch_base, patch_id, opatchauto, ocm_response_file,output):
def remove_patch (module, msg, oracle_home, patch_base, patch_id, opatchauto, ocm_response_file, stop_processes, output):
'''
Removes the patch
'''
Expand All @@ -350,6 +370,9 @@ def remove_patch (module, msg, oracle_home, patch_base, patch_id, opatchauto, oc

command = '%s/OPatch/%s %s -oh %s ' % (oracle_home,opatch_cmd, patch_base, oracle_home)
else:
if stop_processes:
stop_process(module, oracle_home)

opatch_cmd = 'opatch rollback'
command = '%s/OPatch/%s -id %s -silent' % (oracle_home,opatch_cmd, patch_id)

Expand Down Expand Up @@ -443,6 +466,7 @@ def main():
patch_base = dict(default=None, aliases = ['path','source','patch_source','phBaseDir']),
patch_id = dict(default=None, aliases = ['id']),
patch_version = dict(required=None, aliases = ['version']),
exclude_upi = dict(required=None, aliases = ['exclude_unique_patch_id']),
opatch_minversion = dict(default=None, aliases = ['opmv']),
opatchauto = dict(default='False', type='bool',aliases = ['autopatch']),
rolling = dict(default='True', type='bool',aliases = ['rolling']),
Expand All @@ -454,7 +478,7 @@ def main():
output = dict(default="short", choices = ["short","verbose"]),
state = dict(default="present", choices = ["present", "absent", "opatchversion"]),
hostname = dict(required=False, default = 'localhost', aliases = ['host']),
port = dict(required=False, default = 1521),
port = dict(required=False, type='int', default = 1521),



Expand All @@ -466,6 +490,7 @@ def main():
patch_base = module.params["patch_base"]
patch_id = module.params["patch_id"]
patch_version = module.params["patch_version"]
exclude_upi = module.params["exclude_upi"]
opatch_minversion = module.params["opatch_minversion"]
opatchauto = module.params["opatchauto"]
rolling = module.params["rolling"]
Expand Down Expand Up @@ -526,7 +551,7 @@ def main():
module.exit_json(msg=opatch_version,changed=False)

if state == 'present':
if not check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto):
if not check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto, None):
if apply_patch(module, msg, oracle_home, patch_base, patch_id,patch_version, opatchauto,ocm_response_file,offline,stop_processes,rolling,output):
if patch_version is not None:
msg = 'Patch %s (%s) successfully applied to %s' % (patch_id,patch_version, oracle_home)
Expand All @@ -542,8 +567,8 @@ def main():
module.exit_json(msg=msg, changed=False)

elif state == 'absent':
if check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto):
if remove_patch(module, msg, oracle_home, patch_base, patch_id, opatchauto,ocm_response_file, output):
if check_patch_applied(module, msg, oracle_home, patch_id, patch_version, opatchauto, exclude_upi):
if remove_patch(module, msg, oracle_home, patch_base, patch_id, opatchauto,ocm_response_file, stop_processes, output):
if patch_version is not None:
msg = 'Patch %s (%s) successfully removed from %s' % (patch_id,patch_version, oracle_home)
else:
Expand All @@ -553,8 +578,9 @@ def main():
module.fail_json(msg=msg, changed=False)
else:
if patch_version is not None:
msg = 'Patch %s (%s) is not applied to %s' % (patch_id,patch_version, oracle_home)

msg = 'Patch %s (%s) is not applied to %s' % (patch_id, patch_version, oracle_home)
elif exclude_upi is not None:
msg = 'Patch %s (UPI %s) has already been applied and will not be removed from %s' % (patch_id, exclude_upi, oracle_home)
else:
msg = 'Patch %s is not applied to %s' % (patch_id, oracle_home)

Expand Down