Some refactoring of runner.py -- more to come to eliminate repeated code
This commit is contained in:
parent
6cceaa5f6a
commit
b053df4182
1 changed files with 79 additions and 91 deletions
|
@ -98,36 +98,29 @@ class Runner(object):
|
|||
except:
|
||||
return [ False, traceback.format_exc() ]
|
||||
|
||||
def _executor(self, host):
|
||||
'''
|
||||
callback executed in parallel for each host.
|
||||
returns (hostname, connected_ok, extra)
|
||||
where extra is the result of a successful connect
|
||||
or a traceback string
|
||||
'''
|
||||
# TODO: try/catch around JSON handling
|
||||
|
||||
ok, conn = self._connect(host)
|
||||
if not ok:
|
||||
return [ host, False, conn ]
|
||||
|
||||
if self.module_name not in [ 'copy', 'template' ]:
|
||||
# transfer a module, set it executable, and run it
|
||||
outpath = self._copy_module(conn)
|
||||
self._exec_command(conn, "chmod +x %s" % outpath)
|
||||
cmd = self._command(outpath)
|
||||
result = self._exec_command(conn, cmd)
|
||||
self._exec_command(conn, "rm -f %s" % outpath)
|
||||
def _return_from_module(self, conn, host, result):
|
||||
conn.close()
|
||||
try:
|
||||
return [ host, True, json.loads(result) ]
|
||||
except:
|
||||
return [ host, False, result ]
|
||||
|
||||
elif self.module_name == 'copy':
|
||||
def _delete_remote_files(self, conn, files):
|
||||
for filename in files:
|
||||
self._exec_command(conn, "rm -f %s" % filename)
|
||||
|
||||
# TODO: major refactoring pending
|
||||
# do sftp then run actual copy module to get change info
|
||||
def _execute_normal_module(self, conn, host):
|
||||
''' transfer a module, set it executable, and run it '''
|
||||
|
||||
outpath = self._copy_module(conn)
|
||||
self._exec_command(conn, "chmod +x %s" % outpath)
|
||||
cmd = self._command(outpath)
|
||||
result = self._exec_command(conn, cmd)
|
||||
self._delete_remote_files(conn, outpath)
|
||||
return self._return_from_module(conn, host, result)
|
||||
|
||||
def _execute_copy(self, conn, host):
|
||||
''' transfer a file + copy module, run copy module, clean up '''
|
||||
|
||||
self.remote_log(conn, 'COPY remote:%s local:%s' % (self.module_args[0], self.module_args[1]))
|
||||
source = self.module_args[0]
|
||||
|
@ -149,28 +142,11 @@ class Runner(object):
|
|||
self.module_args = [ tmp_dest, dest ]
|
||||
cmd = self._command(outpath)
|
||||
result = self._exec_command(conn, cmd)
|
||||
self._delete_remote_files(conn, [outpath, tmp_dest])
|
||||
|
||||
# remove the module
|
||||
self._exec_command(conn, "rm -f %s" % outpath)
|
||||
# remove the temp file
|
||||
self._exec_command(conn, "rm -f %s" % tmp_dest)
|
||||
|
||||
conn.close()
|
||||
try:
|
||||
return [ host, True, json.loads(result) ]
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return [ host, False, result ]
|
||||
|
||||
return [ host, True, 1 ]
|
||||
|
||||
elif self.module_name == 'template':
|
||||
# template runs COPY then the template module
|
||||
# TODO: DRY/refactor these
|
||||
# TODO: things like _copy_module should take the name as a param
|
||||
# TODO: make it possible to override the /etc/ansible/setup file
|
||||
# location for templating files as non-root
|
||||
return self._return_from_module(conn, host, result)
|
||||
|
||||
def _execute_template(self, conn, host):
|
||||
source = self.module_args[0]
|
||||
dest = self.module_args[1]
|
||||
metadata = '/etc/ansible/setup'
|
||||
|
@ -192,17 +168,29 @@ class Runner(object):
|
|||
self.module_args = [ temppath, dest, metadata ]
|
||||
result = self._exec_command(conn, self._command(outpath))
|
||||
# clean up
|
||||
self._exec_command(conn, "rm -f %s" % outpath)
|
||||
self._exec_command(conn, "rm -f %s" % temppath)
|
||||
self._delete_remote_files(conn, [ outpath, temppath ])
|
||||
return self._return_from_module(conn, host, result)
|
||||
|
||||
conn.close()
|
||||
try:
|
||||
return [ host, True, json.loads(result) ]
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return [ host, False, result ]
|
||||
|
||||
return [ host, False, 1 ]
|
||||
def _executor(self, host):
|
||||
'''
|
||||
callback executed in parallel for each host.
|
||||
returns (hostname, connected_ok, extra)
|
||||
where extra is the result of a successful connect
|
||||
or a traceback string
|
||||
'''
|
||||
|
||||
ok, conn = self._connect(host)
|
||||
if not ok:
|
||||
return [ host, False, conn ]
|
||||
if self.module_name not in [ 'copy', 'template' ]:
|
||||
return self._execute_normal_module(conn, host)
|
||||
elif self.module_name == 'copy':
|
||||
return self._execute_copy(conn, host)
|
||||
elif self.module_name == 'template':
|
||||
return self._execute_template(conn, host)
|
||||
else:
|
||||
raise Exception("???")
|
||||
|
||||
def _command(self, outpath):
|
||||
''' form up a command string '''
|
||||
|
|
Loading…
Reference in a new issue