Esempio n. 1
0
def signal(
        url, creds, status, error=None, script_result_id=None,
        files: dict=None, exit_status=None, script_version_id=None,
        power_type=None, power_params=None):
    """Send a node signal to a given maas_url."""
    params = {
        b'op': b'signal',
        b'status': status.encode('utf-8'),
    }

    if error is not None:
        params[b'error'] = error.encode('utf-8')

    if script_result_id is not None:
        params[b'script_result_id'] = str(script_result_id).encode('utf-8')

    if exit_status is not None:
        params[b'exit_status'] = str(exit_status).encode('utf-8')

    if script_version_id is not None:
        params[b'script_version_id'] = str(script_version_id).encode('utf-8')

    if None not in (power_type, power_params):
        params[b'power_type'] = power_type.encode('utf-8')
        user, power_pass, power_address, driver = power_params.split(",")
        # OrderedDict is used to make testing easier.
        power_params = OrderedDict([
            ('power_user', user),
            ('power_pass', power_pass),
            ('power_address', power_address),
        ])
        if power_type == 'moonshot':
            power_params['power_hwaddress'] = driver
        else:
            power_params['power_driver'] = driver
        params[b'power_parameters'] = json.dumps(power_params).encode()

    data, headers = encode_multipart_data(
        params, ({} if files is None else files))

    try:
        payload = geturl(url, creds=creds, headers=headers, data=data)
        if payload != b'OK':
            raise SignalException(
                "Unexpected result sending region commissioning data: %s" % (
                    payload))
    except urllib.error.HTTPError as exc:
        raise SignalException("HTTP error [%s]" % exc.code)
    except urllib.error.URLError as exc:
        raise SignalException("URL error [%s]" % exc.reason)
    except socket.timeout as exc:
        raise SignalException("Socket timeout [%s]" % exc)
    except TypeError as exc:
        raise SignalException(str(exc))
    except Exception as exc:
        raise SignalException("Unexpected error [%s]" % exc)
Esempio n. 2
0
def signal(
    url,
    creds,
    status,
    error=None,
    script_result_id=None,
    files: dict = None,
    exit_status=None,
    script_version_id=None,
    power_type=None,
    power_params=None,
):
    """Send a node signal to a given maas_url."""
    params = {b"op": b"signal", b"status": status.encode("utf-8")}

    if error is not None:
        params[b"error"] = error.encode("utf-8")

    if script_result_id is not None:
        params[b"script_result_id"] = str(script_result_id).encode("utf-8")

    if exit_status is not None:
        params[b"exit_status"] = str(exit_status).encode("utf-8")

    if script_version_id is not None:
        params[b"script_version_id"] = str(script_version_id).encode("utf-8")

    if None not in (power_type, power_params):
        params[b"power_type"] = power_type.encode("utf-8")
        if power_type == "moonshot":
            user, power_pass, power_address, driver = power_params.split(",")
        else:
            (
                user,
                power_pass,
                power_address,
                driver,
                boot_type,
            ) = power_params.split(",")
        # OrderedDict is used to make testing easier.
        power_params = OrderedDict([
            ("power_user", user),
            ("power_pass", power_pass),
            ("power_address", power_address),
        ])
        if power_type == "moonshot":
            power_params["power_hwaddress"] = driver
        else:
            power_params["power_driver"] = driver
            power_params["power_boot_type"] = boot_type
        params[b"power_parameters"] = json.dumps(power_params).encode()

    data, headers = encode_multipart_data(params,
                                          ({} if files is None else files))

    try:
        ret = geturl(url, creds=creds, headers=headers, data=data)
        if ret.status != 200:
            raise SignalException(
                "Unexpected status(%d) sending region commissioning data: %s" %
                (ret.status, ret.read().decode()))
    except urllib.error.HTTPError as exc:
        raise SignalException("HTTP error [%s]" % exc.code)
    except urllib.error.URLError as exc:
        raise SignalException("URL error [%s]" % exc.reason)
    except socket.timeout as exc:
        raise SignalException("Socket timeout [%s]" % exc)
    except TypeError as exc:
        raise SignalException(str(exc))
    except Exception as exc:
        raise SignalException("Unexpected error [%s]" % exc)