import os
import pecan
import tempfile


def generate_inventory_file(group_name, hosts, task_uuid, tmp_dir=None):
    """
    Generates a host file to use with an ansible-playbook call.

    Given a group_name, a lists of hosts and a task UUID a file
    will be written to disk and a path to the file will be returned.
    """
    result = []
    result.append("[{0}]".format(group_name))
    if not isinstance(hosts, list):
        hosts = [hosts]
    result.extend(hosts)
    result_str = "\n".join(result)
    # if not None the NamedTemporaryFile will be created in the given directory
    tempfile.tempdir = tmp_dir
    inventory_file = tempfile.NamedTemporaryFile(prefix="{0}_".format(task_uuid), delete=False)
    inventory_file.write(result_str)
    inventory_file.close()
    return inventory_file.name

@@ -805,8 +805,992 @@
 le.name%0A
+%0A%0Adef api_endpoint(endpoint=None, *args):%0A    %22%22%22%0A    Puts together the API url for mita, so that we can talk to it. Optionally, the endpoint%0A    argument allows to return the correct url for specific needs. For example, to create a node:%0A%0A        http://0.0.0.0/api/nodes/%0A%0A    %22%22%22%0A    server = pecan.conf%5B'server'%5D%5B'host'%5D%0A    port = pecan.conf%5B'server'%5D%5B'port'%5D%0A    base = %22http://%25s:%25s/api%22 %25 (server, port)%0A    endpoints = %7B%0A        'nodes': '%25s/nodes/' %25 base,%0A    %7D%0A    url = base%0A    if endpoint:%0A        url = endpoints%5Bendpoint%5D%0A%0A    if args:%0A        for part in args:%0A            url = os.path.join(url, part)%0A    return url%0A%0A%0Adef which(executable):%0A    locations = (%0A        '/usr/local/bin',%0A        '/bin',%0A        '/usr/bin',%0A        '/usr/local/sbin',%0A        '/usr/sbin',%0A        '/sbin',%0A    )%0A%0A    for location in locations:%0A        executable_path = os.path.join(location, executable)%0A        if os.path.exists(executable_path):%0A            return executable_path%0A

create some utilities for running system commands