Common module code upgrades

pull/630/head
Michael DeHaan 12 years ago
parent 9006d4557d
commit a94ec130d2

@ -79,16 +79,11 @@ cmd = subprocess.Popen("%s %s" % (modfile, argspath),
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
(out, err) = cmd.communicate() (out, err) = cmd.communicate()
if err and err != '':
print "***********************************"
print "RECIEVED DATA ON STDERR, THIS WILL CRASH YOUR MODULE"
print err
sys.exit(1)
try: try:
print "***********************************" print "***********************************"
print "RAW OUTPUT" print "RAW OUTPUT"
print out print out
print err
results = utils.parse_json(out) results = utils.parse_json(out)
except: except:

@ -42,26 +42,39 @@ class AnsibleModule(object):
def __init__(self, argument_spec, bypass_checks=False, no_log=False): def __init__(self, argument_spec, bypass_checks=False, no_log=False):
''' '''
@argument_spec: a hash of argument names, where the values are none if common code for quickly building an ansible module in Python
the types are NOT checked, or a list of valid types where the argument (although you can write modules in anything that can return JSON)
must be one of those values. All possible arguments must be listed. see library/slurp and others for examples
@required_arguments: a list of arguments that must be sent to the module
''' '''
self.argument_spec = argument_spec self.argument_spec = argument_spec
(self.params, self.args) = self._load_params() (self.params, self.args) = self._load_params()
self._handle_aliases()
self._set_defaults()
if not bypass_checks: if not bypass_checks:
self._check_required_arguments() self._check_required_arguments()
self._check_argument_types() self._check_argument_types()
if not no_log: if not no_log:
self._log_invocation() self._log_invocation()
def _handle_aliases(self):
for (k,v) in self.argument_spec.iteritems():
aliases = v.get('aliases', None)
if aliases is None:
continue
if type(aliases) != list:
self.fail_json(msg='internal error: aliases must be a list')
for alias in aliases:
if alias in self.params:
self.params[k] = self.params[alias]
def _check_required_arguments(self): def _check_required_arguments(self):
''' ensure all required arguments are present ''' ''' ensure all required arguments are present '''
missing = [] missing = []
for (k,v) in self.argument_spec.iteritems(): for (k,v) in self.argument_spec.iteritems():
(type_spec, required) = v required = v.get('required', False)
if required and k not in self.params: if required and k not in self.params:
missing.append(k) missing.append(k)
if len(missing) > 0: if len(missing) > 0:
@ -70,13 +83,23 @@ class AnsibleModule(object):
def _check_argument_types(self): def _check_argument_types(self):
''' ensure all arguments have the requested values, and there are no stray arguments ''' ''' ensure all arguments have the requested values, and there are no stray arguments '''
for (k,v) in self.argument_spec.iteritems(): for (k,v) in self.argument_spec.iteritems():
(type_spec, required) = v choices = v.get('choices',None)
if type_spec is not None: if choices is None:
if type(spec) == list: continue
if v not in spec: if type(choices) == list:
self.fail_json(msg="value of %s must be one of: %s, recieved: %s" % (k, ",".join(spec), v)) if k in self.params:
else: if self.params[k] not in choices:
self.fail_json(msg="internal error: do not know how to interpret argument_spec") choices_str=",".join(choices)
msg="value of %s must be one of: %s, got: %s" % (k, choices_str, self.params[k])
self.fail_json(msg=msg)
else:
self.fail_json(msg="internal error: do not know how to interpret argument_spec")
def _set_defaults(self):
for (k,v) in self.argument_spec.iteritems():
default = v.get('default', '__NO_DEFAULT__')
if default != '__NO_DEFAULT__' and k not in self.params:
self.params[k] = default
def _load_params(self): def _load_params(self):
''' read the input and return a dictionary and the arguments string ''' ''' read the input and return a dictionary and the arguments string '''
@ -97,17 +120,17 @@ class AnsibleModule(object):
syslog.openlog('ansible-%s' % os.path.basename(__file__)) syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % self.args) syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % self.args)
def exit_json(self, rc=0, **kwargs): def exit_json(self, **kwargs):
''' return from the module, without error ''' ''' return from the module, without error '''
kwargs['rc'] = rc
print json.dumps(kwargs) print json.dumps(kwargs)
sys.exit(rc) sys.exit(0)
def fail_json(self, **kwargs): def fail_json(self, **kwargs):
''' return from the module, with an error message ''' ''' return from the module, with an error message '''
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required" assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
kwargs['failed'] = True kwargs['failed'] = True
self.exit_json(rc=1, **kwargs) print json.dumps(kwargs)
sys.exit(1)
# == END DYNAMICALLY INSERTED CODE === # == END DYNAMICALLY INSERTED CODE ===

@ -24,7 +24,7 @@ import base64
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
src=(None,True), src = dict(required=True),
) )
) )
source = module.params['src'] source = module.params['src']

Loading…
Cancel
Save