Ejemplo n.º 1
0
def get_url_json_unlimited(socket_path, url, total_fn=None):
    """Return the JSON results of a GET request

    For URLs that use offset/limit arguments, this command will
    fetch all results for the given request.

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to request
    :type url: str
    :returns: The json response from the server
    :rtype: dict
    """
    def default_total_fn(data):
        """Return the total number of available results"""
        return data["total"]

    http = UnixHTTPConnectionPool(socket_path)

    # Start with limit=0 to just get the number of objects
    total_url = append_query(url, "limit=0")
    r_total = http.request("GET", total_url)
    json_total = json.loads(r_total.data.decode('utf-8'))

    # Where to get the total from
    if not total_fn:
        total_fn = default_total_fn

    # Add the "total" returned by limit=0 as the new limit
    unlimited_url = append_query(url, "limit=%d" % total_fn(json_total))
    r_unlimited = http.request("GET", unlimited_url)
    return json.loads(r_unlimited.data.decode('utf-8'))
Ejemplo n.º 2
0
def run_module():
    """Module main function
    """
    # define available arguments/parameters a user can pass to the module
    module_args = dict(id=dict(type='str', required=True),
                       dest=dict(type='str', required=True))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, ansible_module_results='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications
    if module.check_mode:
        module.exit_json(**result)

    http = UnixHTTPConnectionPool(SOCKET)
    response = http.request("GET",
                            '/api/v1/compose/image/' + module.params['id'],
                            preload_content=False)
    if response.status == 400:
        err = json.loads(response.data.decode("utf-8"))
        if not err["status"]:
            msgs = [e["msg"] for e in err["errors"]]
            raise RuntimeError(", ".join(msgs))

    downloadpath = module.params['dest']

    if os.path.isdir(downloadpath):
        downloadpath = os.path.join(downloadpath,
                                    client.get_filename(response.headers))

    with open(downloadpath, "wb") as image_file:
        while True:
            data = response.read(10 * 1024**2)
            if not data:
                break
            image_file.write(data)

    result['ansible_module_results'] = {
        'dest': downloadpath,
        'status': response.status
    }
    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)
Ejemplo n.º 3
0
def get_url_json(socket_path, url):
    """Return the JSON results of a GET request

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to request
    :type url: str
    :returns: The json response from the server
    :rtype: dict
    """
    http = UnixHTTPConnectionPool(socket_path)
    r = http.request("GET", url)
    return json.loads(r.data.decode('utf-8'))
Ejemplo n.º 4
0
def delete_url_json(socket_path, url):
    """Send a DELETE request to the url and return JSON response

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to send DELETE to
    :type url: str
    :returns: The json response from the server
    :rtype: dict
    """
    http = UnixHTTPConnectionPool(socket_path)
    r = http.request("DELETE", url)
    return json.loads(r.data.decode("utf-8"))
Ejemplo n.º 5
0
def post_url(socket_path, url, body):
    """POST raw data to the URL

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to send POST to
    :type url: str
    :param body: The data for the body of the POST
    :type body: str
    :returns: The json response from the server
    :rtype: dict
    """
    http = UnixHTTPConnectionPool(socket_path)
    r = http.request("POST", url, body=body.encode("utf-8"))
    return json.loads(r.data.decode("utf-8"))
Ejemplo n.º 6
0
def get_url_raw(socket_path, url):
    """Return the raw results of a GET request

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to request
    :type url: str
    :returns: The raw response from the server
    :rtype: str
    """
    http = UnixHTTPConnectionPool(socket_path)
    r = http.request("GET", url)
    if r.status == 400:
        err = json.loads(r.data.decode("utf-8"))
        if "status" in err and err["status"] == False:
            raise RuntimeError(", ".join(err["errors"]))

    return r.data.decode('utf-8')
Ejemplo n.º 7
0
def download_file(socket_path, url, progress=True):
    """Download a file, saving it to the CWD with the included filename

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param url: URL to send POST to
    :type url: str
    """
    http = UnixHTTPConnectionPool(socket_path)
    r = http.request("GET", url, preload_content=False)
    if r.status == 400:
        err = json.loads(r.data.decode("utf-8"))
        if not err["status"]:
            msgs = [e["msg"] for e in err["errors"]]
            raise RuntimeError(", ".join(msgs))

    filename = get_filename(r.headers)
    if os.path.exists(filename):
        msg = "%s exists, skipping download" % filename
        log.error(msg)
        raise RuntimeError(msg)

    with open(filename, "wb") as f:
        while True:
            data = r.read(10 * 1024**2)
            if not data:
                break
            f.write(data)

            if progress:
                data_written = f.tell()
                if data_written > 5 * 1024**2:
                    sys.stdout.write("%s: %0.2f MB    \r" %
                                     (filename, data_written / 1024**2))
                else:
                    sys.stdout.write("%s: %0.2f kB\r" %
                                     (filename, data_written / 1024))
                sys.stdout.flush()

    print("")
    r.release_conn()

    return 0