2012-03-03 03:08:48 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-03-03 03:16:29 +01:00
|
|
|
###############################################################
|
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
import sys
|
2012-03-03 04:10:51 +01:00
|
|
|
import os
|
2012-03-15 02:16:15 +01:00
|
|
|
import shlex
|
2012-03-20 00:32:38 +01:00
|
|
|
import re
|
2012-03-20 00:23:14 +01:00
|
|
|
import jinja2
|
2012-03-20 03:42:31 +01:00
|
|
|
import yaml
|
2012-03-15 02:16:15 +01:00
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2012-03-20 00:23:14 +01:00
|
|
|
from ansible import errors
|
|
|
|
|
2012-03-03 03:16:29 +01:00
|
|
|
###############################################################
|
|
|
|
# UTILITY FUNCTIONS FOR COMMAND LINE TOOLS
|
|
|
|
###############################################################
|
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
def err(msg):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' print an error message to stderr '''
|
2012-03-03 03:08:48 +01:00
|
|
|
print >> sys.stderr, msg
|
|
|
|
|
|
|
|
def exit(msg, rc=1):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' quit with an error to stdout and a failure code '''
|
2012-03-03 03:08:48 +01:00
|
|
|
err(msg)
|
|
|
|
sys.exit(rc)
|
|
|
|
|
2012-03-03 03:43:46 +01:00
|
|
|
def bigjson(result):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' format JSON output (uncompressed) '''
|
2012-03-21 03:29:21 +01:00
|
|
|
# hide some internals magic from command line userland
|
|
|
|
result2 = result.copy()
|
|
|
|
if 'invocation' in result2:
|
|
|
|
del result2['invocation']
|
|
|
|
return json.dumps(result2, sort_keys=True, indent=4)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
2012-03-03 03:43:46 +01:00
|
|
|
def smjson(result):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' format JSON output (compressed) '''
|
2012-03-21 03:29:21 +01:00
|
|
|
# hide some internals magic from command line userland
|
|
|
|
result2 = result.copy()
|
|
|
|
if 'invocation' in result2:
|
|
|
|
del result2['invocation']
|
|
|
|
return json.dumps(result2, sort_keys=True)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
2012-03-03 03:43:46 +01:00
|
|
|
def task_start_msg(name, conditional):
|
|
|
|
if conditional:
|
2012-03-21 02:44:01 +01:00
|
|
|
return "\nNOTIFIED: [%s] **********\n" % name
|
2012-03-03 03:43:46 +01:00
|
|
|
else:
|
2012-03-21 02:44:01 +01:00
|
|
|
return "\nTASK: [%s] *********\n" % name
|
2012-03-03 03:43:46 +01:00
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
def regular_generic_msg(hostname, result, oneline, caption):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output on the result of a module run that is not command '''
|
2012-03-03 03:08:48 +01:00
|
|
|
if not oneline:
|
2012-03-10 19:35:46 +01:00
|
|
|
return "%s | %s >> %s\n" % (hostname, caption, bigjson(result))
|
2012-03-03 03:08:48 +01:00
|
|
|
else:
|
2012-03-10 19:35:46 +01:00
|
|
|
return "%s | %s >> %s\n" % (hostname, caption, smjson(result))
|
2012-03-03 03:08:48 +01:00
|
|
|
|
|
|
|
def regular_success_msg(hostname, result, oneline):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output the result of a successful module run '''
|
2012-03-03 03:08:48 +01:00
|
|
|
return regular_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
|
|
|
|
def regular_failure_msg(hostname, result, oneline):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output the result of a failed module run '''
|
2012-03-03 03:08:48 +01:00
|
|
|
return regular_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
|
|
|
|
def command_generic_msg(hostname, result, oneline, caption):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output the result of a command run '''
|
2012-03-03 03:08:48 +01:00
|
|
|
rc = result.get('rc', '0')
|
|
|
|
stdout = result.get('stdout','')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
|
msg = result.get('msg', '')
|
|
|
|
if not oneline:
|
|
|
|
buf = "%s | %s | rc=%s >>\n" % (hostname, caption, result.get('rc',0))
|
|
|
|
if stdout:
|
2012-03-03 04:03:03 +01:00
|
|
|
buf += stdout
|
2012-03-03 03:08:48 +01:00
|
|
|
if stderr:
|
2012-03-03 04:03:03 +01:00
|
|
|
buf += stderr
|
2012-03-03 03:08:48 +01:00
|
|
|
if msg:
|
2012-03-03 04:03:03 +01:00
|
|
|
buf += msg
|
2012-03-10 19:40:08 +01:00
|
|
|
buf += "\n"
|
2012-03-03 03:08:48 +01:00
|
|
|
return buf
|
|
|
|
else:
|
|
|
|
if stderr:
|
2012-03-10 19:40:08 +01:00
|
|
|
return "%s | %s | rc=%s | (stdout) %s (stderr) %s\n" % (hostname, caption, rc, stdout, stderr)
|
2012-03-03 03:08:48 +01:00
|
|
|
else:
|
2012-03-10 19:40:08 +01:00
|
|
|
return "%s | %s | rc=%s | (stdout) %s\n" % (hostname, caption, rc, stdout)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
|
|
|
def command_success_msg(hostname, result, oneline):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output from a successful command run '''
|
2012-03-03 03:08:48 +01:00
|
|
|
return command_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
|
|
|
|
def command_failure_msg(hostname, result, oneline):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' output from a failed command run '''
|
2012-03-03 03:08:48 +01:00
|
|
|
return command_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
|
2012-03-03 04:10:51 +01:00
|
|
|
def write_tree_file(tree, hostname, buf):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' write something into treedir/hostname '''
|
|
|
|
# TODO: might be nice to append playbook runs per host in a similar way
|
|
|
|
# in which case, we'd want append mode.
|
2012-03-03 04:10:51 +01:00
|
|
|
path = os.path.join(tree, hostname)
|
2012-03-03 03:08:48 +01:00
|
|
|
fd = open(path, "w+")
|
|
|
|
fd.write(buf)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def is_failed(result):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' is a given JSON result a failed result? '''
|
2012-03-03 03:08:48 +01:00
|
|
|
failed = False
|
|
|
|
rc = 0
|
|
|
|
if type(result) == dict:
|
|
|
|
failed = result.get('failed', 0)
|
|
|
|
rc = result.get('rc', 0)
|
|
|
|
if rc != 0:
|
|
|
|
return True
|
|
|
|
return failed
|
|
|
|
|
|
|
|
def host_report_msg(hostname, module_name, result, oneline):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' summarize the JSON results for a particular host '''
|
2012-03-03 03:08:48 +01:00
|
|
|
buf = ''
|
|
|
|
failed = is_failed(result)
|
2012-03-27 03:17:11 +02:00
|
|
|
if module_name in [ 'command', 'shell' ] and 'ansible_job_id' not in result:
|
2012-03-03 03:08:48 +01:00
|
|
|
if not failed:
|
|
|
|
buf = command_success_msg(hostname, result, oneline)
|
|
|
|
else:
|
|
|
|
buf = command_failure_msg(hostname, result, oneline)
|
|
|
|
else:
|
|
|
|
if not failed:
|
|
|
|
buf = regular_success_msg(hostname, result, oneline)
|
|
|
|
else:
|
|
|
|
buf = regular_failure_msg(hostname, result, oneline)
|
|
|
|
return buf
|
|
|
|
|
|
|
|
def prepare_writeable_dir(tree):
|
2012-03-03 03:16:29 +01:00
|
|
|
''' make sure a directory exists and is writeable '''
|
2012-03-03 03:08:48 +01:00
|
|
|
if tree != '/':
|
2012-03-03 04:10:51 +01:00
|
|
|
tree = os.path.realpath(os.path.expanduser(tree))
|
2012-03-03 03:08:48 +01:00
|
|
|
if not os.path.exists(tree):
|
|
|
|
try:
|
|
|
|
os.makedirs(tree)
|
|
|
|
except (IOError, OSError), e:
|
|
|
|
exit("Could not make dir %s: %s" % (tree, e))
|
|
|
|
if not os.access(tree, os.W_OK):
|
|
|
|
exit("Cannot write to path %s" % tree)
|
|
|
|
|
2012-03-03 16:53:15 +01:00
|
|
|
def path_dwim(basedir, given):
|
2012-03-11 23:40:35 +01:00
|
|
|
''' make relative paths work like folks expect '''
|
2012-03-03 16:53:15 +01:00
|
|
|
if given.startswith("/"):
|
|
|
|
return given
|
|
|
|
elif given.startswith("~/"):
|
|
|
|
return os.path.expanduser(given)
|
|
|
|
else:
|
|
|
|
return os.path.join(basedir, given)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
2012-03-12 01:54:54 +01:00
|
|
|
def async_poll_status(jid, host, clock, result):
|
|
|
|
if 'finished' in result:
|
|
|
|
return "<job %s> finished on %s" % (jid, host)
|
|
|
|
elif 'failed' in result:
|
|
|
|
return "<job %s> FAILED on %s" % (jid, host)
|
2012-03-11 23:40:35 +01:00
|
|
|
else:
|
2012-03-12 01:54:54 +01:00
|
|
|
return "<job %s> polling on %s, %s remaining" % (jid, host, clock)
|
2012-03-11 23:40:35 +01:00
|
|
|
|
2012-03-18 22:53:58 +01:00
|
|
|
def json_loads(data):
|
|
|
|
return json.loads(data)
|
|
|
|
|
2012-03-15 02:16:15 +01:00
|
|
|
def parse_json(data):
|
2012-03-18 22:53:58 +01:00
|
|
|
''' this version for module return data only '''
|
2012-03-15 02:16:15 +01:00
|
|
|
try:
|
|
|
|
return json.loads(data)
|
|
|
|
except:
|
|
|
|
# not JSON, but try "Baby JSON" which allows many of our modules to not
|
|
|
|
# require JSON and makes writing modules in bash much simpler
|
|
|
|
results = {}
|
|
|
|
tokens = shlex.split(data)
|
|
|
|
for t in tokens:
|
|
|
|
if t.find("=") == -1:
|
2012-03-18 22:16:12 +01:00
|
|
|
raise errors.AnsibleError("failed to parse: %s" % data)
|
2012-03-15 02:16:15 +01:00
|
|
|
(key,value) = t.split("=", 1)
|
|
|
|
if key == 'changed' or 'failed':
|
2012-03-16 03:32:14 +01:00
|
|
|
if value.lower() in [ 'true', '1' ]:
|
2012-03-15 02:16:15 +01:00
|
|
|
value = True
|
|
|
|
elif value.lower() in [ 'false', '0' ]:
|
|
|
|
value = False
|
|
|
|
if key == 'rc':
|
|
|
|
value = int(value)
|
|
|
|
results[key] = value
|
2012-03-16 02:53:14 +01:00
|
|
|
if len(results.keys()) == 0:
|
|
|
|
return { "failed" : True, "parsed" : False, "msg" : data }
|
2012-03-15 02:16:15 +01:00
|
|
|
return results
|
|
|
|
|
2012-03-20 00:32:38 +01:00
|
|
|
_KEYCRE = re.compile(r"\$(\w+)")
|
|
|
|
|
|
|
|
def varReplace(raw, vars):
|
|
|
|
'''Perform variable replacement of $vars
|
|
|
|
|
|
|
|
@param raw: String to perform substitution on.
|
|
|
|
@param vars: Dictionary of variables to replace. Key is variable name
|
|
|
|
(without $ prefix). Value is replacement string.
|
|
|
|
@return: Input raw string with substituted values.
|
|
|
|
'''
|
|
|
|
# this code originally from yum
|
|
|
|
|
|
|
|
done = [] # Completed chunks to return
|
|
|
|
|
|
|
|
while raw:
|
|
|
|
m = _KEYCRE.search(raw)
|
|
|
|
if not m:
|
|
|
|
done.append(raw)
|
|
|
|
break
|
|
|
|
|
|
|
|
# Determine replacement value (if unknown variable then preserve
|
|
|
|
# original)
|
|
|
|
varname = m.group(1).lower()
|
2012-03-22 05:30:05 +01:00
|
|
|
replacement = str(vars.get(varname, m.group()))
|
2012-03-20 00:32:38 +01:00
|
|
|
|
|
|
|
start, end = m.span()
|
|
|
|
done.append(raw[:start]) # Keep stuff leading up to token
|
|
|
|
done.append(replacement) # Append replacement value
|
|
|
|
raw = raw[end:] # Continue with remainder of string
|
|
|
|
|
|
|
|
return ''.join(done)
|
|
|
|
|
2012-03-20 00:23:14 +01:00
|
|
|
def template(text, vars):
|
|
|
|
''' run a text buffer through the templating engine '''
|
2012-03-22 05:30:05 +01:00
|
|
|
text = varReplace(str(text), vars)
|
2012-03-20 00:23:14 +01:00
|
|
|
template = jinja2.Template(text)
|
|
|
|
return template.render(vars)
|
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
def double_template(text, vars):
|
|
|
|
return template(template(text, vars), vars)
|
|
|
|
|
2012-03-20 00:23:14 +01:00
|
|
|
def template_from_file(path, vars):
|
|
|
|
''' run a file through the templating engine '''
|
|
|
|
data = file(path).read()
|
|
|
|
return template(data, vars)
|
2012-03-20 03:42:31 +01:00
|
|
|
|
|
|
|
def parse_yaml(data):
|
|
|
|
return yaml.load(data)
|
2012-03-20 00:23:14 +01:00
|
|
|
|
2012-03-20 03:42:31 +01:00
|
|
|
def parse_yaml_from_file(path):
|
2012-03-21 03:29:21 +01:00
|
|
|
try:
|
|
|
|
data = file(path).read()
|
|
|
|
except IOError:
|
|
|
|
raise errors.AnsibleError("file not found: %s" % path)
|
2012-03-20 03:42:31 +01:00
|
|
|
return parse_yaml(data)
|
2012-03-20 00:23:14 +01:00
|
|
|
|
2012-03-30 18:18:10 +02:00
|
|
|
def unquote_string(string):
|
|
|
|
''' remove single or double quotes from beginning/end of string'''
|
|
|
|
if (string.startswith('"') and string.endswith('"')) or \
|
|
|
|
(string.startswith("'") and string.endswith("'")):
|
|
|
|
return string[1:-1]
|
|
|
|
else:
|
|
|
|
return string
|
|
|
|
|
|
|
|
def parse_kv(args, unquote=True):
|
2012-03-22 04:39:09 +01:00
|
|
|
''' convert a string of key/value items to a dict '''
|
|
|
|
options = {}
|
|
|
|
for x in args:
|
|
|
|
if x.find("=") != -1:
|
|
|
|
k, v = x.split("=")
|
2012-03-31 03:56:10 +02:00
|
|
|
if unquote:
|
|
|
|
options[k]=unquote_string(v)
|
|
|
|
else:
|
|
|
|
v
|
2012-03-22 04:39:09 +01:00
|
|
|
return options
|
2012-03-31 03:56:10 +02:00
|
|
|
|