Port command module over to new common code. Notice that this has to subclass AnsibleModule -- this should be the only

one that has to do that.
reviewable/pr18780/r1
Michael DeHaan 13 years ago
parent 72465c6fe1
commit aa20ae5c02

@ -16,12 +16,6 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
try:
import json
except ImportError:
import simplejson as json
import subprocess import subprocess
import sys import sys
@ -29,79 +23,83 @@ import datetime
import traceback import traceback
import shlex import shlex
import os import os
import syslog
def main():
argfile = sys.argv[1]
args = open(argfile, 'r').read() # the command module is the one ansible module that does not take key=value args
syslog.openlog('ansible-%s' % os.path.basename(__file__)) # hence don't copy this one if you are looking to build others!
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args) module = CommandModule(argument_spec=dict())
shell = False shell = module.params['shell']
args = module.params['args']
if args.find("#USE_SHELL") != -1:
args = args.replace("#USE_SHELL", "") if not shell:
shell = True args = shlex.split(args)
startd = datetime.datetime.now()
check_args = shlex.split(args)
for x in check_args: try:
if x.startswith("creates="): cmd = subprocess.Popen(args, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# do not run the command if the line contains creates=filename out, err = cmd.communicate()
# and the filename already exists. This allows idempotence except (OSError, IOError), e:
# of command executions. module.fail_json(cmd=args, msg=str(e))
(k,v) = x.split("=",1) except:
if os.path.exists(v): module.fail_json(msg=traceback.format_exc())
print json.dumps({
"cmd" : args, endd = datetime.datetime.now()
"stdout" : "skipped, since %s exists" % v, delta = endd - startd
"skipped" : True,
"changed" : False, if out is None:
"stderr" : "", out = ''
"rc" : 0, if err is None:
}) err = ''
sys.exit(0)
args = args.replace(x,'') module.exit_json(
cmd = args,
stdout = out.strip(),
if not shell: stderr = err.strip(),
args = shlex.split(args) rc = cmd.returncode,
start = str(startd),
startd = datetime.datetime.now() end = str(endd),
delta = str(delta),
try: changed = True
cmd = subprocess.Popen(args, shell=shell, )
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate() # include magic from lib/ansible/module_common.py
except (OSError, IOError), e: #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
print json.dumps({
"cmd" : args, # only the command module should ever need to do this
"failed" : 1, # everything else should be simple key=value
"msg" : str(e),
}) class CommandModule(AnsibleModule):
sys.exit(1)
except: def _load_params(self):
print json.dumps({ ''' read the input and return a dictionary and the arguments string '''
"failed" : 1, args = base64.b64decode(MODULE_ARGS)
"msg" : traceback.format_exc() items = shlex.split(args)
}) params = {}
sys.exit(1) params['shell'] = False
if args.find("#USE_SHELL") != -1:
endd = datetime.datetime.now() args = args.replace("#USE_SHELL", "")
delta = endd - startd params['shell'] = True
if out is None: check_args = shlex.split(args)
out = '' for x in check_args:
if err is None: if x.startswith("creates="):
err = '' # do not run the command if the line contains creates=filename
# and the filename already exists. This allows idempotence
result = { # of command executions.
"cmd" : args, (k,v) = x.split("=",1)
"stdout" : out.strip(), if os.path.exists(v):
"stderr" : err.strip(), self.exit_json(
"rc" : cmd.returncode, cmd=args,
"start" : str(startd), stdout="skipped, since %s exists" % v,
"end" : str(endd), skipped=True,
"delta" : str(delta), changed=False,
"changed" : True stderr=False,
} rc=0
)
print json.dumps(result) args = args.replace(x,'')
params['args'] = args
return (params, args)
main()

Loading…
Cancel
Save