コード例 #1
0
def report(
    server_url: str,
    status: str,
    deployment_id: str,
    server_certificate: str,
    JWT: str,
    deployment_logger: Optional[menderlog.DeploymentLogHandler] = None,
) -> bool:
    """Report update :param status to the Mender server"""
    if not status:
        log.error("No status given to report")
        return False
    try:
        headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + JWT
        }
        response = http_request(
            requests.put,
            server_url + "/api/devices/v1/deployments/device/deployments/" +
            deployment_id + "/status",
            headers=headers,
            verify=server_certificate or True,
            json={"status": status},
        )
        if response.status_code != 204:
            log.error(f"Failed to upload the deployment status '{status}',\
                error: {response.status_code}: {response.reason}")
            return False
        if status == STATUS_FAILURE:
            menderlog.add_sub_updater_log(
                os.path.join(settings.PATHS.data_store, "sub-updater.log"))
            if deployment_logger:
                logdata = deployment_logger.marshal()
            else:
                log.error("No deployment log handler given")
                return True

            response = http_request(
                requests.put,
                server_url +
                "/api/devices/v1/deployments/device/deployments/" +
                deployment_id + "/log",
                headers=headers,
                verify=server_certificate or True,
                json={
                    "messages": logdata,
                },
            )
            if response.status_code != 204:
                log.error(f"Failed to upload the deployment log,\
                    error: {response.status_code}: {response.reason} {response.text}"
                          )
                return False
    except MenderRequestsException as e:
        log.error(e)
        return False
    return True
コード例 #2
0
def report(server_url: str, status: str, deployment_id: str,
           server_certificate: str, JWT: str) -> bool:
    """Report update :param status to the Mender server"""
    if not status:
        log.error("No status given to report")
        return False
    try:
        headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + JWT
        }
        response = requests.put(
            server_url + "/api/devices/v1/deployments/device/deployments/" +
            deployment_id + "/status",
            headers=headers,
            verify=server_certificate if server_certificate else True,
            json={"status": status},
        )
        if response.status_code != 204:
            log.error(f"Failed to upload the deployment status '{status}',\
                error: {response.status_code}: {response.reason}")
            return False
        if status == STATUS_FAILURE:
            menderlog.add_sub_updater_log(
                os.path.join(settings.PATHS.deployment_log, "deployment.log"))
            response = requests.put(
                server_url +
                "/api/devices/v1/deployments/device/deployments/" +
                deployment_id + "/log",
                headers=headers,
                verify=server_certificate if server_certificate else True,
                json={
                    "messages": [
                        # Dummy data
                        {
                            "timestamp": "2016-03-11T13:03:17.063493443Z",
                            "level": "INFO",
                            "message": "OK",
                        }
                    ]
                },
            )
            if response.status_code != 204:
                log.error(f"Failed to upload the deployment log,\
                    error: {response.status_code}: {response.reason} {response.text}"
                          )
                return False
    except (
            requests.RequestException,
            requests.ConnectionError,
            requests.URLRequired,
            requests.TooManyRedirects,
            requests.Timeout,
    ) as e:
        log.error(e)
        return False
    return True