mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 03:41:25 -07:00
utilities modules: PEP8 compliancy (#27741)
This commit is contained in:
parent
f30589c91d
commit
eee09565b1
4 changed files with 75 additions and 65 deletions
|
@ -27,9 +27,11 @@ PY3 = sys.version_info[0] == 3
|
|||
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
||||
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:]))
|
||||
|
||||
|
||||
def notice(msg):
|
||||
syslog.syslog(syslog.LOG_NOTICE, msg)
|
||||
|
||||
|
||||
def daemonize_self():
|
||||
# daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
|
||||
try:
|
||||
|
@ -60,6 +62,7 @@ def daemonize_self():
|
|||
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
||||
|
||||
|
||||
# NB: this function copied from module_utils/json_utils.py. Ensure any changes are propagated there.
|
||||
# FUTURE: AnsibleModule-ify this module so it's Ansiballz-compatible and can use the module_utils copy of this function.
|
||||
def _filter_non_json_lines(data):
|
||||
|
@ -121,7 +124,7 @@ def _run_module(wrapped_cmd, jid, job_path):
|
|||
|
||||
tmp_job_path = job_path + ".tmp"
|
||||
jobfile = open(tmp_job_path, "w")
|
||||
jobfile.write(json.dumps({ "started" : 1, "finished" : 0, "ansible_job_id" : jid }))
|
||||
jobfile.write(json.dumps({"started": 1, "finished": 0, "ansible_job_id": jid}))
|
||||
jobfile.close()
|
||||
os.rename(tmp_job_path, job_path)
|
||||
jobfile = open(tmp_job_path, "w")
|
||||
|
@ -163,9 +166,9 @@ def _run_module(wrapped_cmd, jid, job_path):
|
|||
e = sys.exc_info()[1]
|
||||
result = {
|
||||
"failed": 1,
|
||||
"cmd" : wrapped_cmd,
|
||||
"cmd": wrapped_cmd,
|
||||
"msg": str(e),
|
||||
"outdata": outdata, # temporary notice only
|
||||
"outdata": outdata, # temporary notice only
|
||||
"stderr": stderr
|
||||
}
|
||||
result['ansible_job_id'] = jid
|
||||
|
@ -173,11 +176,11 @@ def _run_module(wrapped_cmd, jid, job_path):
|
|||
|
||||
except (ValueError, Exception):
|
||||
result = {
|
||||
"failed" : 1,
|
||||
"cmd" : wrapped_cmd,
|
||||
"data" : outdata, # temporary notice only
|
||||
"failed": 1,
|
||||
"cmd": wrapped_cmd,
|
||||
"data": outdata, # temporary notice only
|
||||
"stderr": stderr,
|
||||
"msg" : traceback.format_exc()
|
||||
"msg": traceback.format_exc()
|
||||
}
|
||||
result['ansible_job_id'] = jid
|
||||
jobfile.write(json.dumps(result))
|
||||
|
@ -186,16 +189,13 @@ def _run_module(wrapped_cmd, jid, job_path):
|
|||
os.rename(tmp_job_path, job_path)
|
||||
|
||||
|
||||
####################
|
||||
## main ##
|
||||
####################
|
||||
if __name__ == '__main__':
|
||||
|
||||
if len(sys.argv) < 5:
|
||||
print(json.dumps({
|
||||
"failed" : True,
|
||||
"msg" : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile> [-preserve_tmp] "
|
||||
"Humans, do not call directly!"
|
||||
"failed": True,
|
||||
"msg": "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile> [-preserve_tmp] "
|
||||
"Humans, do not call directly!"
|
||||
}))
|
||||
sys.exit(1)
|
||||
|
||||
|
@ -225,8 +225,8 @@ if __name__ == '__main__':
|
|||
os.makedirs(jobdir)
|
||||
except:
|
||||
print(json.dumps({
|
||||
"failed" : 1,
|
||||
"msg" : "could not create: %s" % jobdir
|
||||
"failed": 1,
|
||||
"msg": "could not create: %s" % jobdir
|
||||
}))
|
||||
# immediately exit this process, leaving an orphaned process
|
||||
# running which immediately forks a supervisory timing process
|
||||
|
@ -241,8 +241,8 @@ if __name__ == '__main__':
|
|||
# this probably could be done with some IPC later. Modules should always read
|
||||
# the argsfile at the very first start of their execution anyway
|
||||
notice("Return async_wrapper task started.")
|
||||
print(json.dumps({ "started" : 1, "finished" : 0, "ansible_job_id" : jid, "results_file" : job_path,
|
||||
"_ansible_suppress_tmpdir_delete": not preserve_tmp}))
|
||||
print(json.dumps({"started": 1, "finished": 0, "ansible_job_id": jid, "results_file": job_path,
|
||||
"_ansible_suppress_tmpdir_delete": not preserve_tmp}))
|
||||
sys.stdout.flush()
|
||||
time.sleep(1)
|
||||
sys.exit(0)
|
||||
|
@ -263,16 +263,16 @@ if __name__ == '__main__':
|
|||
# set the child process group id to kill all children
|
||||
os.setpgid(sub_pid, sub_pid)
|
||||
|
||||
notice("Start watching %s (%s)"%(sub_pid, remaining))
|
||||
notice("Start watching %s (%s)" % (sub_pid, remaining))
|
||||
time.sleep(step)
|
||||
while os.waitpid(sub_pid, os.WNOHANG) == (0, 0):
|
||||
notice("%s still running (%s)"%(sub_pid, remaining))
|
||||
notice("%s still running (%s)" % (sub_pid, remaining))
|
||||
time.sleep(step)
|
||||
remaining = remaining - step
|
||||
if remaining <= 0:
|
||||
notice("Now killing %s"%(sub_pid))
|
||||
notice("Now killing %s" % (sub_pid))
|
||||
os.killpg(sub_pid, signal.SIGKILL)
|
||||
notice("Sent kill to group %s"%sub_pid)
|
||||
notice("Sent kill to group %s " % sub_pid)
|
||||
time.sleep(1)
|
||||
if not preserve_tmp:
|
||||
shutil.rmtree(os.path.dirname(wrapped_module), True)
|
||||
|
@ -283,9 +283,9 @@ if __name__ == '__main__':
|
|||
sys.exit(0)
|
||||
else:
|
||||
# the child process runs the actual module
|
||||
notice("Start module (%s)"%os.getpid())
|
||||
notice("Start module (%s)" % os.getpid())
|
||||
_run_module(cmd, jid, job_path)
|
||||
notice("Module complete (%s)"%os.getpid())
|
||||
notice("Module complete (%s)" % os.getpid())
|
||||
sys.exit(0)
|
||||
|
||||
except SystemExit:
|
||||
|
@ -295,9 +295,9 @@ if __name__ == '__main__':
|
|||
|
||||
except Exception:
|
||||
e = sys.exc_info()[1]
|
||||
notice("error: %s"%(e))
|
||||
notice("error: %s" % e)
|
||||
print(json.dumps({
|
||||
"failed" : True,
|
||||
"msg" : "FATAL ERROR: %s" % str(e)
|
||||
"failed": True,
|
||||
"msg": "FATAL ERROR: %s" % e
|
||||
}))
|
||||
sys.exit(1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue