From 7af767f43517e1d7c5310f86315ea0c3bf97b004 Mon Sep 17 00:00:00 2001 From: Mike Mars Date: Fri, 3 Jun 2016 11:40:02 -0700 Subject: [PATCH] 1) Modified error handling to work with Python 2.4. 2) Fixed octal codes to fall in line with the ansible guide, "Porting Modules to Python 3" 3) updated the requirements. All changes have been verified against Python 2.4, 2.6, & 3.5. --- .../modules/utilities/helper/accelerate.py | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/ansible/modules/utilities/helper/accelerate.py b/lib/ansible/modules/utilities/helper/accelerate.py index 92ec31219ff..bc0cfb22006 100644 --- a/lib/ansible/modules/utilities/helper/accelerate.py +++ b/lib/ansible/modules/utilities/helper/accelerate.py @@ -64,7 +64,7 @@ options: notes: - See the advanced playbooks chapter for more about using accelerated mode. requirements: - - "python >= 2.6" + - "python >= 2.4" - "python-keyczar" author: "James Cammarata (@jimi-c)" ''' @@ -168,9 +168,10 @@ def daemonize_self(module, password, port, minutes, pid_file): vvv("exiting pid %s" % pid) # exit first parent module.exit_json(msg="daemonized accelerate on port %s for %s minutes with pid %s" % (port, minutes, str(pid))) - except OSError as e: - log("fork #1 failed: %d (%s)" % (e.errno, e.strerror)) - sys.exit(1) + except OSError: + e = get_exception() + message = "fork #2 failed: {} ({})".format(e.errno, e.strerror) + module.fail_json(message) # decouple from parent environment os.chdir("/") @@ -187,9 +188,10 @@ def daemonize_self(module, password, port, minutes, pid_file): pid_file.close() vvv("pid file written") sys.exit(0) - except OSError as e: - log("fork #2 failed: %d (%s)" % (e.errno, e.strerror)) - sys.exit(1) + except OSError: + e = get_exception() + message = 'fork #2 failed: {} ({})'.format(e.errno, e.strerror) + module.fail_json(message) dev_null = file('/dev/null','rw') os.dup2(dev_null.fileno(), sys.stdin.fileno()) @@ -219,9 +221,9 @@ class LocalSocketThread(Thread): # make sure the directory is accessible only to this # user, as socket files derive their permissions from # the directory that contains them - os.chmod(dir, Oo700) + os.chmod(dir, int('0700', 8)) elif not os.path.exists(dir): - os.makedirs(dir, Oo700) + os.makedirs(dir, int('O700', 8)) except OSError: pass self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -260,7 +262,8 @@ class LocalSocketThread(Thread): self.server.last_event = datetime.datetime.now() finally: self.server.last_event_lock.release() - except Exception as e: + except Exception: + e = get_exception() vv("key loaded locally was invalid, ignoring (%s)" % e) conn.sendall("BADKEY\n") finally: @@ -520,7 +523,8 @@ class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): if response.get('failed',False): log("got a failed response from the master") return dict(failed=True, stderr="Master reported failure, aborting transfer") - except Exception as e: + except Exception: + e = get_exception() fd.close() tb = traceback.format_exc() log("failed to fetch the file: %s" % tb) @@ -541,7 +545,7 @@ class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): tmp_path = os.path.expanduser('~/.ansible/tmp/') if not os.path.exists(tmp_path): try: - os.makedirs(tmp_path, Oo700) + os.makedirs(tmp_path, int('O700', 8)) except: return dict(failed=True, msg='could not create a temporary directory at %s' % tmp_path) (fd,out_path) = tempfile.mkstemp(prefix='ansible.', dir=tmp_path) @@ -618,7 +622,8 @@ def daemonize(module, password, port, timeout, minutes, use_ipv6, pid_file): server = ThreadedTCPServer(address, ThreadedTCPRequestHandler, module, password, timeout, use_ipv6=use_ipv6) server.allow_reuse_address = True break - except Exception as e: + except Exception: + e = get_exception() vv("Failed to create the TCP server (tries left = %d) (error: %s) " % (tries,e)) tries -= 1 time.sleep(0.2) @@ -641,7 +646,8 @@ def daemonize(module, password, port, timeout, minutes, use_ipv6, pid_file): v("server thread terminated, exiting!") sys.exit(0) - except Exception as e: + except Exception: + e = get_exception() tb = traceback.format_exc() log("exception caught, exiting accelerated mode: %s\n%s" % (e, tb)) sys.exit(0) @@ -685,11 +691,16 @@ def main(): # process, other than tell the calling program # whether other signals can be sent os.kill(daemon_pid, 0) - except OSError as e: + except OSError: + e = get_exception() + message = 'the accelerate daemon appears to be running' + message += 'as a different user that this user cannot access' + message += 'pid={}'.format(daemon_pid) + if e.errno == errno.EPERM: # no permissions means the pid is probably # running, but as a different user, so fail - module.fail_json(msg="the accelerate daemon appears to be running as a different user that this user cannot access (pid=%d)" % daemon_pid) + module.fail_json(msg=message) else: daemon_running = True except ValueError: