Пример #1
0
    def test_http_request_retries_ioerrors(self, _http_request, _sleep):
        ioerror = IOError()
        ioerror.errno = 42

        _http_request.side_effect = [ioerror, Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #2
0
    def test_http_request_retries_status_codes(self, _http_request, _sleep):
        _http_request.side_effect = [
            Mock(status=httpclient.SERVICE_UNAVAILABLE),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #3
0
    def test_http_request_retries_status_codes(self, _http_request, _sleep):
        _http_request.side_effect = [
            Mock(status=httpclient.SERVICE_UNAVAILABLE),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #4
0
    def test_http_request_retries_passed_status_codes(self, _http_request, _sleep):
        # Ensure the code is not part of the standard set
        self.assertFalse(httpclient.UNAUTHORIZED in restutil.RETRY_CODES)

        _http_request.side_effect = [
            Mock(status=httpclient.UNAUTHORIZED),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar", retry_codes=[httpclient.UNAUTHORIZED])
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #5
0
    def test_http_request_retries_ioerrors(self, _http_request, _sleep):
        ioerror = IOError()
        ioerror.errno = 42

        _http_request.side_effect = [
            ioerror,
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #6
0
    def test_http_request_retries_passed_status_codes(self, _http_request, _sleep):
        # Ensure the code is not part of the standard set
        self.assertFalse(httpclient.UNAUTHORIZED in restutil.RETRY_CODES)

        _http_request.side_effect = [
            Mock(status=httpclient.UNAUTHORIZED),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar", retry_codes=[httpclient.UNAUTHORIZED])
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #7
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)
Пример #8
0
    def get_api_versions(self):
        url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
                                                 HOST_PLUGIN_PORT)
        logger.verbose(
            "HostGAPlugin: Getting API versions at [{0}]".format(url))
        return_val = []
        error_response = ''
        is_healthy = False
        try:
            headers = {HEADER_CONTAINER_ID: self.container_id}
            response = restutil.http_get(url, headers)
            if restutil.request_failed(response):
                error_response = restutil.read_response_error(response)
                logger.error(
                    "HostGAPlugin: Failed Get API versions: {0}".format(
                        error_response))
            else:
                return_val = ustr(remove_bom(response.read()),
                                  encoding='utf-8')
                is_healthy = True
        except HttpError as e:
            logger.error(
                "HostGAPlugin: Exception Get API versions: {0}".format(e))

        self.health_service.report_host_plugin_versions(
            is_healthy=is_healthy, response=error_response)

        return return_val
Пример #9
0
 def download_ext_handler_pkg(self, uri):
     try:
         resp = restutil.http_get(uri, chk_proxy=True)
         if resp.status == restutil.httpclient.OK:
             return resp.read()
     except HttpError as e:
         raise ProtocolError("Failed to download from: {0}".format(uri), e)
Пример #10
0
    def _download(self):
        package = None

        for uri in self.pkg.uris:
            try:
                resp = restutil.http_get(uri.uri, chk_proxy=True)
                if resp.status == restutil.httpclient.OK:
                    package = resp.read()
                    fileutil.write_file(self.get_agent_pkg_path(),
                                        bytearray(package),
                                        asbin=True)
                    logger.info(u"Agent {0} downloaded from {1}", self.name,
                                uri.uri)
                    break
            except restutil.HttpError as e:
                logger.warn(u"Agent {0} download from {1} failed", self.name,
                            uri.uri)

        if not os.path.isfile(self.get_agent_pkg_path()):
            msg = u"Unable to download Agent {0} from any URI".format(
                self.name)
            add_event(AGENT_NAME,
                      op=WALAEventOperation.Download,
                      version=CURRENT_VERSION,
                      is_success=False,
                      message=msg)
            raise UpdateError(msg)
        return
Пример #11
0
 def download_ext_handler_pkg(self, uri, headers=None):
     try:
         resp = restutil.http_get(uri, chk_proxy=True, headers=headers)
         if resp.status == restutil.httpclient.OK:
             return resp.read()
     except Exception as e:
         logger.warn("Failed to download from: {0}".format(uri), e)
Пример #12
0
 def download_ext_handler_pkg(self, uri):
     try:
         resp = restutil.http_get(uri, chk_proxy=True)
         if resp.status == restutil.httpclient.OK:
             return resp.read()
     except HttpError as e:
         raise ProtocolError("Failed to download from: {0}".format(uri), e)
Пример #13
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
Пример #14
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
Пример #15
0
    def test_http_request_retries_exceptions(self, _http_request, _sleep):
        # Testing each exception is difficult because they have varying
        # signatures; for now, test one and ensure the set is unchanged
        recognized_exceptions = [
            httpclient.NotConnected, httpclient.IncompleteRead,
            httpclient.ImproperConnectionState, httpclient.BadStatusLine
        ]
        self.assertEqual(recognized_exceptions, restutil.RETRY_EXCEPTIONS)

        _http_request.side_effect = [
            httpclient.IncompleteRead(''),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #16
0
    def test_http_request_retries_for_safe_minimum_number_when_throttled(self, _http_request, _sleep):
        # Ensure the code is a throttle code
        self.assertTrue(httpclient.SERVICE_UNAVAILABLE in restutil.THROTTLE_CODES)

        _http_request.side_effect = [
                Mock(status=httpclient.SERVICE_UNAVAILABLE)
                    for i in range(restutil.THROTTLE_RETRIES-1) # pylint: disable=unused-variable
            ] + [Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar",
                            max_retry=1)

        self.assertEqual(restutil.THROTTLE_RETRIES, _http_request.call_count)
        self.assertEqual(restutil.THROTTLE_RETRIES-1, _sleep.call_count)
        self.assertEqual(
            [call(1) for i in range(restutil.THROTTLE_RETRIES-1)],
            _sleep.call_args_list)
Пример #17
0
    def test_http_request_retries_for_safe_minimum_number_when_throttled(self, _http_request, _sleep):
        # Ensure the code is a throttle code
        self.assertTrue(httpclient.SERVICE_UNAVAILABLE in restutil.THROTTLE_CODES)

        _http_request.side_effect = [
                Mock(status=httpclient.SERVICE_UNAVAILABLE)
                    for i in range(restutil.THROTTLE_RETRIES-1)
            ] + [Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar",
                            max_retry=1)

        self.assertEqual(restutil.THROTTLE_RETRIES, _http_request.call_count)
        self.assertEqual(restutil.THROTTLE_RETRIES-1, _sleep.call_count)
        self.assertEqual(
            [call(1) for i in range(restutil.THROTTLE_RETRIES-1)],
            _sleep.call_args_list)
Пример #18
0
    def test_http_request_retries_with_constant_delay_when_throttled(self, _http_request, _sleep):
        # Ensure the code is a throttle code
        self.assertTrue(httpclient.SERVICE_UNAVAILABLE in restutil.THROTTLE_CODES)

        _http_request.side_effect = [
                Mock(status=httpclient.SERVICE_UNAVAILABLE)
                    for i in range(restutil.DEFAULT_RETRIES)
            ] + [Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar",
                            max_retry=restutil.DEFAULT_RETRIES+1)

        self.assertEqual(restutil.DEFAULT_RETRIES+1, _http_request.call_count)
        self.assertEqual(restutil.DEFAULT_RETRIES, _sleep.call_count)
        self.assertEqual(
            [call(1) for i in range(restutil.DEFAULT_RETRIES)],
            _sleep.call_args_list)
Пример #19
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
Пример #20
0
    def test_http_request_proxy_with_no_proxy_check(self, _http_request, sleep,
                                                    mock_get_http_proxy):  # pylint: disable=unused-argument
        mock_http_resp = MagicMock()
        mock_http_resp.read = Mock(return_value="hehe")
        _http_request.return_value = mock_http_resp
        mock_get_http_proxy.return_value = "host", 1234  # Return a host/port combination

        no_proxy_list = ["foo.com", "www.google.com", "168.63.129.16"]
        with patch.dict(os.environ, {'no_proxy': ",".join(no_proxy_list)}):
            # Test http get
            resp = restutil.http_get("http://foo.com", use_proxy=True)
            self.assertEqual("hehe", resp.read())
            self.assertEqual(0, mock_get_http_proxy.call_count)

            # Test http get
            resp = restutil.http_get("http://bar.com", use_proxy=True)
            self.assertEqual("hehe", resp.read())
            self.assertEqual(1, mock_get_http_proxy.call_count)
Пример #21
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)
Пример #22
0
    def test_http_request_retries_exceptions(self, _http_request, _sleep):
        # Testing each exception is difficult because they have varying
        # signatures; for now, test one and ensure the set is unchanged
        recognized_exceptions = [
            httpclient.NotConnected,
            httpclient.IncompleteRead,
            httpclient.ImproperConnectionState,
            httpclient.BadStatusLine
        ]
        self.assertEqual(recognized_exceptions, restutil.RETRY_EXCEPTIONS)

        _http_request.side_effect = [
            httpclient.IncompleteRead(''),
            Mock(status=httpclient.OK)
        ]

        restutil.http_get("https://foo.bar")
        self.assertEqual(2, _http_request.call_count)
        self.assertEqual(1, _sleep.call_count)
Пример #23
0
    def test_http_request_retries_with_fibonacci_delay(self, _http_request, _sleep):
        # Ensure the code is not a throttle code
        self.assertFalse(httpclient.BAD_GATEWAY in restutil.THROTTLE_CODES)

        _http_request.side_effect = [
                Mock(status=httpclient.BAD_GATEWAY)
                    for i in range(restutil.DEFAULT_RETRIES)
            ] + [Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar",
                            max_retry=restutil.DEFAULT_RETRIES+1)

        self.assertEqual(restutil.DEFAULT_RETRIES+1, _http_request.call_count)
        self.assertEqual(restutil.DEFAULT_RETRIES, _sleep.call_count)
        self.assertEqual(
            [
                call(restutil._compute_delay(i+1, restutil.DELAY_IN_SECONDS))  # pylint: disable=protected-access
                    for i in range(restutil.DEFAULT_RETRIES)],
            _sleep.call_args_list)
Пример #24
0
    def test_http_request_retries_with_fibonacci_delay(self, _http_request, _sleep):
        # Ensure the code is not a throttle code
        self.assertFalse(httpclient.BAD_GATEWAY in restutil.THROTTLE_CODES)

        _http_request.side_effect = [
                Mock(status=httpclient.BAD_GATEWAY)
                    for i in range(restutil.DEFAULT_RETRIES)
            ] + [Mock(status=httpclient.OK)]

        restutil.http_get("https://foo.bar",
                            max_retry=restutil.DEFAULT_RETRIES+1)

        self.assertEqual(restutil.DEFAULT_RETRIES+1, _http_request.call_count)
        self.assertEqual(restutil.DEFAULT_RETRIES, _sleep.call_count)
        self.assertEqual(
            [
                call(restutil._compute_delay(i+1, restutil.DELAY_IN_SECONDS))
                    for i in range(restutil.DEFAULT_RETRIES)],
            _sleep.call_args_list)
Пример #25
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)
Пример #26
0
    def test_http_request_proxy_with_no_proxy_check(self, _http_request, sleep, mock_get_http_proxy):
        mock_http_resp = MagicMock()
        mock_http_resp.read = Mock(return_value="hehe")
        _http_request.return_value = mock_http_resp
        mock_get_http_proxy.return_value = "host", 1234 # Return a host/port combination

        no_proxy_list = ["foo.com", "www.google.com", "168.63.129.16"]
        with patch.dict(os.environ, {
            'no_proxy': ",".join(no_proxy_list)
        }):
            # Test http get
            resp = restutil.http_get("http://foo.com", use_proxy=True)
            self.assertEquals("hehe", resp.read())
            self.assertEquals(0, mock_get_http_proxy.call_count)

            # Test http get
            resp = restutil.http_get("http://bar.com", use_proxy=True)
            self.assertEquals("hehe", resp.read())
            self.assertEquals(1, mock_get_http_proxy.call_count)
Пример #27
0
    def test_http_request_with_retry(self, _http_request, sleep):
        mock_httpresp = MagicMock()
        mock_httpresp.read = Mock(return_value="hehe")
        _http_request.return_value = mock_httpresp

        #Test http get
        resp = restutil.http_get("http://foo.bar") 
        self.assertEquals("hehe", resp.read())

        #Test https get
        resp = restutil.http_get("https://foo.bar") 
        self.assertEquals("hehe", resp.read())
        
        #Test http failure
        _http_request.side_effect = httpclient.HTTPException("Http failure")
        self.assertRaises(restutil.HttpError, restutil.http_get, "http://foo.bar")

        #Test http failure
        _http_request.side_effect = IOError("IO failure")
        self.assertRaises(restutil.HttpError, restutil.http_get, "http://foo.bar")
Пример #28
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
Пример #29
0
 def _fetch(self, uri, headers=None):
     package = None
     try:
         resp = restutil.http_get(uri, chk_proxy=True, headers=headers)
         if resp.status == restutil.httpclient.OK:
             package = resp.read()
             fileutil.write_file(self.get_agent_pkg_path(),
                                 bytearray(package),
                                 asbin=True)
             logger.info(u"Agent {0} downloaded from {1}", self.name, uri)
     except restutil.HttpError as http_error:
         logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                        self.name, uri, http_error)
     return package is not None
Пример #30
0
    def _get_data(self, url, headers=None):
        try:
            resp = restutil.http_get(url, headers=headers)
        except HttpError as e:
            raise ProtocolError(ustr(e))

        if resp.status != httpclient.OK:
            raise ProtocolError("{0} - GET: {1}".format(resp.status, url))

        data = resp.read()
        etag = resp.getheader('ETag')
        if data is None:
            return None
        data = json.loads(ustr(data, encoding="utf-8"))
        return data, etag
Пример #31
0
    def _get_data(self, url, headers=None):
        try:
            resp = restutil.http_get(url, headers=headers)
        except HttpError as e:
            raise ProtocolError(ustr(e))

        if resp.status != httpclient.OK:
            raise ProtocolError("{0} - GET: {1}".format(resp.status, url))

        data = resp.read()
        etag = resp.getheader('ETag')
        if data is None:
            return None
        data = json.loads(ustr(data, encoding="utf-8"))
        return data, etag
Пример #32
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
Пример #33
0
 def _fetch(self, uri, headers=None):
     package = None
     try:
         resp = restutil.http_get(uri, chk_proxy=True, headers=headers)
         if resp.status == restutil.httpclient.OK:
             package = resp.read()
             fileutil.write_file(self.get_agent_pkg_path(),
                                 bytearray(package),
                                 asbin=True)
             logger.info(u"Agent {0} downloaded from {1}", self.name, uri)
     except restutil.HttpError as http_error:
         logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                     self.name,
                     uri,
                     http_error)
     return package is not None
Пример #34
0
    def validate(self):
        """
        Determines whether the metadata instance api returns 200, and the response
        is valid: compute should contain location, name, subscription id, and vm size
        and network should contain mac address and private ip address.
        :return: Tuple<is_healthy:bool, error_response:str>
            is_healthy: True when validation succeeds, False otherwise
            error_response: validation failure details to assist with debugging
        """

        # ensure we get a 200
        resp = restutil.http_get(self.instance_url,
                                 headers=self._health_headers)
        if restutil.request_failed(resp):
            return False, "{0}".format(restutil.read_response_error(resp))

        # ensure the response is valid json
        data = resp.read()
        try:
            json_data = json.loads(ustr(data, encoding="utf-8"))
        except Exception as e:
            return False, "JSON parsing failed: {0}".format(ustr(e))

        # ensure all expected fields are present and have a value
        try:
            self.check_field(json_data, 'compute')
            self.check_field(json_data['compute'], 'location')
            self.check_field(json_data['compute'], 'name')
            self.check_field(json_data['compute'], 'subscriptionId')
            self.check_field(json_data['compute'], 'vmSize')

            self.check_field(json_data, 'network')
            self.check_field(json_data['network'], 'interface')
            self.check_field(json_data['network']['interface'][0],
                             'macAddress')
            self.check_field(json_data['network']['interface'][0], 'ipv4')
            self.check_field(json_data['network']['interface'][0]['ipv4'],
                             'ipAddress')
            self.check_field(
                json_data['network']['interface'][0]['ipv4']['ipAddress'][0],
                'privateIpAddress')
        except ValueError as v:
            return False, ustr(v)

        return True, ''
Пример #35
0
 def _fetch(self, uri, headers=None, chk_proxy=True):
     package = None
     try:
         resp = restutil.http_get(uri, chk_proxy=chk_proxy, headers=headers)
         if resp.status == restutil.httpclient.OK:
             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}]",
                            HostPluginProtocol.read_response_error(resp))
     except restutil.HttpError as http_error:
         logger.verbose(u"Agent {0} download from {1} failed [{2}]",
                        self.name, uri, http_error)
     return package is not None
Пример #36
0
    def get_compute(self):
        """
        Fetch compute information.

        :return: instance of a ComputeInfo
        :rtype: ComputeInfo
        """

        resp = restutil.http_get(self.compute_url, headers=self._headers)

        if restutil.request_failed(resp):
            raise HttpError("{0} - GET: {1}".format(resp.status, self.compute_url))

        data = resp.read()
        data = json.loads(ustr(data, encoding="utf-8"))

        compute_info = ComputeInfo()
        set_properties('compute', compute_info, data)

        return compute_info
Пример #37
0
    def get_compute(self):
        """
        Fetch compute information.

        :return: instance of a ComputeInfo
        :rtype: ComputeInfo
        """

        resp = restutil.http_get(self.compute_url, headers=self._headers)

        if restutil.request_failed(resp):
            raise HttpError("{0} - GET: {1}".format(resp.status, self.compute_url))

        data = resp.read()
        data = json.loads(ustr(data, encoding="utf-8"))

        compute_info = ComputeInfo()
        set_properties('compute', compute_info, data)

        return compute_info
Пример #38
0
    def get_api_versions(self):
        url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
                                                 HOST_PLUGIN_PORT)
        logger.verbose("HostGAPlugin: Getting API versions at [{0}]".format(
            url))
        return_val = []
        try:
            headers = {HEADER_CONTAINER_ID: self.container_id}
            response = restutil.http_get(url, headers)
            if response.status != httpclient.OK:
                logger.error(
                    "HostGAPlugin: Failed Get API versions: {0}".format(
                        self.read_response_error(response)))
            else:
                return_val = ustr(remove_bom(response.read()), encoding='utf-8')

        except HttpError as e:
            logger.error("HostGAPlugin: Exception Get API versions: {0}".format(e))

        return return_val
Пример #39
0
    def validate(self):
        """
        Determines whether the metadata instance api returns 200, and the response
        is valid: compute should contain location, name, subscription id, and vm size
        and network should contain mac address and private ip address.
        :return: Tuple<is_healthy:bool, error_response:str>
            is_healthy: True when validation succeeds, False otherwise
            error_response: validation failure details to assist with debugging
        """

        # ensure we get a 200
        resp = restutil.http_get(self.instance_url, headers=self._health_headers)
        if restutil.request_failed(resp):
            return False, "{0}".format(restutil.read_response_error(resp))

        # ensure the response is valid json
        data = resp.read()
        try:
            json_data = json.loads(ustr(data, encoding="utf-8"))
        except Exception as e:
            return False, "JSON parsing failed: {0}".format(ustr(e))

        # ensure all expected fields are present and have a value
        try:
            self.check_field(json_data, 'compute')
            self.check_field(json_data['compute'], 'location')
            self.check_field(json_data['compute'], 'name')
            self.check_field(json_data['compute'], 'subscriptionId')
            self.check_field(json_data['compute'], 'vmSize')

            self.check_field(json_data, 'network')
            self.check_field(json_data['network'], 'interface')
            self.check_field(json_data['network']['interface'][0], 'macAddress')
            self.check_field(json_data['network']['interface'][0], 'ipv4')
            self.check_field(json_data['network']['interface'][0]['ipv4'], 'ipAddress')
            self.check_field(json_data['network']['interface'][0]['ipv4']['ipAddress'][0], 'privateIpAddress')
        except ValueError as v:
            return False, ustr(v)

        return True, ''
Пример #40
0
    def get_api_versions(self):
        url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
                                                 HOST_PLUGIN_PORT)
        logger.verbose(
            "HostGAPlugin: Getting API versions at [{0}]".format(url))
        return_val = []
        try:
            headers = {HEADER_CONTAINER_ID: self.container_id}
            response = restutil.http_get(url, headers)
            if response.status != httpclient.OK:
                logger.error(
                    "HostGAPlugin: Failed Get API versions: {0}".format(
                        self.read_response_error(response)))
            else:
                return_val = ustr(remove_bom(response.read()),
                                  encoding='utf-8')

        except HttpError as e:
            logger.error(
                "HostGAPlugin: Exception Get API versions: {0}".format(e))

        return return_val
Пример #41
0
    def _get_data(self, url, headers=None):
        try:
            resp = restutil.http_get(url, headers=headers)
        except HttpError as e:
            raise ProtocolError(ustr(e))

        # NOT_MODIFIED (304) response means the call was successful, so allow that to proceed.
        is_not_modified = restutil.request_not_modified(resp)
        if restutil.request_failed(resp) and not is_not_modified:
            raise ProtocolError("{0} - GET: {1}".format(resp.status, url))

        data = resp.read()
        etag = resp.getheader('ETag')

        # If the response was 304, then explicilty set data to None
        if is_not_modified:
            data = None

        if data is not None:
            data = json.loads(ustr(data, encoding="utf-8"))

        return data, etag
Пример #42
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
Пример #43
0
    def get_api_versions(self):
        url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
                                                 HOST_PLUGIN_PORT)
        logger.verbose("HostGAPlugin: Getting API versions at [{0}]"
                       .format(url))
        return_val = []
        error_response = ''
        is_healthy = False
        try:
            headers = {HEADER_CONTAINER_ID: self.container_id}
            response = restutil.http_get(url, headers)
            if restutil.request_failed(response):
                error_response = restutil.read_response_error(response)
                logger.error("HostGAPlugin: Failed Get API versions: {0}".format(error_response))
                is_healthy = not restutil.request_failed_at_hostplugin(response)
            else:
                return_val = ustr(remove_bom(response.read()), encoding='utf-8')
                is_healthy = True
        except HttpError as e:
            logger.error("HostGAPlugin: Exception Get API versions: {0}".format(e))

        self.health_service.report_host_plugin_versions(is_healthy=is_healthy, response=error_response)

        return return_val
Пример #44
0
    def _download(self):
        package = None

        for uri in self.pkg.uris:
            try:
                resp = restutil.http_get(uri.uri, chk_proxy=True)
                if resp.status == restutil.httpclient.OK:
                    package = resp.read()
                    fileutil.write_file(self.get_agent_pkg_path(), bytearray(package), asbin=True)
                    logger.info(u"Agent {0} downloaded from {1}", self.name, uri.uri)
                    break
            except restutil.HttpError as e:
                logger.warn(u"Agent {0} download from {1} failed", self.name, uri.uri)

        if not os.path.isfile(self.get_agent_pkg_path()):
            msg = u"Unable to download Agent {0} from any URI".format(self.name)
            add_event(
                AGENT_NAME,
                op=WALAEventOperation.Download,
                version=CURRENT_VERSION,
                is_success=False,
                message=msg)
            raise UpdateError(msg)
        return
Пример #45
0
 def _http_get(self, endpoint, resource_path, headers):
     url = self._get_metadata_url(endpoint, resource_path)
     return restutil.http_get(url, headers=headers, use_proxy=False)