Exemplo n.º 1
0
    def test_request_succeeded(self):
        self.assertFalse(restutil.request_succeeded(None))

        resp = Mock()
        for status in restutil.OK_CODES:
            resp.status = status
            self.assertTrue(restutil.request_succeeded(resp))

        self.assertFalse(httpclient.BAD_REQUEST in restutil.OK_CODES)
        resp.status = httpclient.BAD_REQUEST
        self.assertFalse(restutil.request_succeeded(resp))

        self.assertTrue(
            restutil.request_succeeded(
                resp, ok_codes=[httpclient.BAD_REQUEST]))
Exemplo n.º 2
0
    def test_request_succeeded(self):
        self.assertFalse(restutil.request_succeeded(None))

        resp = Mock()
        for status in restutil.OK_CODES:
            resp.status = status
            self.assertTrue(restutil.request_succeeded(resp))

        self.assertFalse(httpclient.BAD_REQUEST in restutil.OK_CODES)
        resp.status = httpclient.BAD_REQUEST
        self.assertFalse(restutil.request_succeeded(resp))

        self.assertTrue(
            restutil.request_succeeded(resp,
                                       ok_codes=[httpclient.BAD_REQUEST]))
Exemplo n.º 3
0
 def download_ext_handler_pkg(self, uri, headers=None, use_proxy=True):
     try:
         resp = restutil.http_get(uri, headers=headers, use_proxy=use_proxy)
         if restutil.request_succeeded(resp):
             return resp.read()
     except Exception as e:
         logger.warn("Failed to download from: {0}".format(uri), e)
Exemplo n.º 4
0
    def _fetch(self, uri, headers=None, use_proxy=True):
        package = None
        try:
            is_healthy = True
            error_response = ''
            resp = restutil.http_get(uri, use_proxy=use_proxy, headers=headers)
            if restutil.request_succeeded(resp):
                package = resp.read()
                fileutil.write_file(self.get_agent_pkg_path(),
                                    bytearray(package),
                                    asbin=True)
                logger.verbose(u"Agent {0} downloaded from {1}", self.name, uri)
            else:
                error_response = restutil.read_response_error(resp)
                logger.verbose("Fetch was unsuccessful [{0}]", error_response)
                is_healthy = not restutil.request_failed_at_hostplugin(resp)

            if self.host is not None:
                self.host.report_fetch_health(uri, is_healthy, source='GuestAgent', response=error_response)

        except restutil.HttpError as http_error:
            if isinstance(http_error, ResourceGoneError):
                raise

            logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                           self.name,
                           uri,
                           http_error)

        return package is not None
Exemplo n.º 5
0
    def _fetch(self, uri, headers=None, use_proxy=True):
        package = None
        try:
            is_healthy = True
            error_response = ''
            resp = restutil.http_get(uri, use_proxy=use_proxy, headers=headers)
            if restutil.request_succeeded(resp):
                package = resp.read()
                fileutil.write_file(self.get_agent_pkg_path(),
                                    bytearray(package),
                                    asbin=True)
                logger.verbose(u"Agent {0} downloaded from {1}", self.name,
                               uri)
            else:
                error_response = restutil.read_response_error(resp)
                logger.verbose("Fetch was unsuccessful [{0}]", error_response)
                is_healthy = not restutil.request_failed_at_hostplugin(resp)

            if self.host is not None:
                self.host.report_fetch_health(uri,
                                              is_healthy,
                                              source='GuestAgent',
                                              response=error_response)

        except restutil.HttpError as http_error:
            if isinstance(http_error, ResourceGoneError):
                raise

            logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                           self.name, uri, http_error)

        return package is not None
Exemplo n.º 6
0
 def get_health(self):
     """
     Call the /health endpoint
     :return: True if 200 received, False otherwise
     """
     url = URI_FORMAT_HEALTH.format(self.endpoint, HOST_PLUGIN_PORT)
     logger.verbose("HostGAPlugin: Getting health from [{0}]", url)
     response = restutil.http_get(url, max_retry=1)
     return restutil.request_succeeded(response)
Exemplo n.º 7
0
 def download_ext_handler_pkg(self, uri, headers=None, use_proxy=True):
     pkg = None
     try:
         resp = restutil.http_get(uri, headers=headers, use_proxy=use_proxy)
         if restutil.request_succeeded(resp):
             pkg = resp.read()
     except Exception as e:
         logger.warn("Failed to download from: {0}".format(uri), e)
     return pkg
Exemplo n.º 8
0
 def get_health(self):
     """
     Call the /health endpoint
     :return: True if 200 received, False otherwise
     """
     url = URI_FORMAT_HEALTH.format(self.endpoint,
                                    HOST_PLUGIN_PORT)
     logger.verbose("HostGAPlugin: Getting health from [{0}]", url)
     response = restutil.http_get(url, max_retry=1)
     return restutil.request_succeeded(response)
Exemplo n.º 9
0
 def get_health(self):
     """
     Call the /health endpoint
     :return: True if 200 received, False otherwise
     """
     url = URI_FORMAT_HEALTH.format(self.endpoint, HOST_PLUGIN_PORT)
     logger.verbose("HostGAPlugin: Getting health from [{0}]", url)
     status_ok = False
     try:
         response = restutil.http_get(url, max_retry=1)
         status_ok = restutil.request_succeeded(response)
     except HttpError as e:
         logger.verbose("HostGAPlugin: Exception getting health", ustr(e))
     return status_ok
Exemplo n.º 10
0
 def download_ext_handler_pkg(self,
                              uri,
                              destination,
                              headers=None,
                              use_proxy=True):
     success = False
     try:
         resp = restutil.http_get(uri, headers=headers, use_proxy=use_proxy)
         if restutil.request_succeeded(resp):
             fileutil.write_file(destination,
                                 bytearray(resp.read()),
                                 asbin=True)
             success = True
     except Exception as e:
         logger.warn("Failed to download from: {0}".format(uri), e)
     return success
Exemplo n.º 11
0
    def _fetch(self, uri, headers=None, use_proxy=True):
        package = None
        try:
            resp = restutil.http_get(uri, use_proxy=use_proxy, headers=headers)
            if restutil.request_succeeded(resp):
                package = resp.read()
                fileutil.write_file(self.get_agent_pkg_path(),
                                    bytearray(package),
                                    asbin=True)
                logger.verbose(u"Agent {0} downloaded from {1}", self.name,
                               uri)
            else:
                logger.verbose("Fetch was unsuccessful [{0}]",
                               restutil.read_response_error(resp))
        except restutil.HttpError as http_error:
            if isinstance(http_error, ResourceGoneError):
                raise

            logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                           self.name, uri, http_error)

        return package is not None