Ejemplo n.º 1
0
  def bintray_repo_delete_version(self, repo, package_name, repository,
                                  build_version=None):
    """Delete the given bintray repository version if it exsts."""
    try:
      bintray_url = self.__to_bintray_url(repo, package_name, repository,
                                          build_version)
      logging.debug('Checking for %s', bintray_url)
      request = urllib2.Request(url=bintray_url)
      request.get_method = lambda: 'DELETE'
      self.__add_bintray_auth_header(request)

      labels = {
          'repo': repo,
          'repository': repository.name,
          'artifact': 'debian'
      }
      self.__metrics.count_call(
          'DeleteArtifact', labels,
          'Attempts to delete versioned artifacts on bintray',
          urllib2.urlopen, request)
      return True
    except urllib2.HTTPError as ex:
      if ex.code == 404:
        return True
      raise_and_log_error(
          ResponseError('Bintray failure: {}'.format(ex),
                        server='bintray.delete'),
          'Failed on url=%s: %s' % (bintray_url, ex.message))
Ejemplo n.º 2
0
 def bintray_repo_has_version(self,
                              repo,
                              package_name,
                              repository,
                              build_version=None,
                              build_number=None):
     """See if the given bintray repository has the package version to build."""
     try:
         bintray_url = self.__to_bintray_url(repo,
                                             package_name,
                                             repository,
                                             build_version=build_version,
                                             build_number=build_number)
         logging.debug('Checking for %s', bintray_url)
         request = urllib2.Request(url=bintray_url)
         self.__add_bintray_auth_header(request)
         urllib2.urlopen(request)
         return True
     except urllib2.HTTPError as ex:
         if ex.code == 404:
             return False
         raise_and_log_error(
             ResponseError('Bintray failure: {}'.format(ex),
                           server='bintray.check'),
             'Failed on url=%s: %s' % (bintray_url, ex.message))
     except Exception as ex:
         raise
Ejemplo n.º 3
0
    def bintray_repo_delete_version(self,
                                    repo,
                                    package_name,
                                    repository,
                                    build_version=None):
        """Delete the given bintray repository version if it exsts."""
        try:
            bintray_url = self.__to_bintray_url(repo, package_name, repository,
                                                build_version)
            logging.debug("Checking for %s", bintray_url)
            request = Request(url=bintray_url)
            request.get_method = lambda: "DELETE"
            self.__add_bintray_auth_header(request)

            labels = {
                "repo": repo,
                "repository": repository.name,
                "artifact": "debian"
            }
            self.__metrics.count_call("DeleteArtifact", labels, urlopen,
                                      request)
            return True
        except HTTPError as ex:
            if ex.code == 404:
                return True
            raise_and_log_error(
                ResponseError("Bintray failure: {}".format(ex),
                              server="bintray.delete"),
                "Failed on url=%s: %s" %
                (bintray_url, exception_to_message(ex)),
            )
Ejemplo n.º 4
0
    def __validate_service_base_url(self, service_name, timeout=None):
        service_config = self.__public_service_configs[service_name]
        base_url = service_config["base_url"]

        timeout = timeout or self.options.test_service_startup_timeout
        end_time = time.time() + timeout
        logging.info('Validating base URL of "%s"...', service_name)

        try:
            url = "{base_url}/health".format(base_url=base_url)
            request = Request(url=url)
            if "bearer_auth_token" in service_config:
                request.add_header(
                    "Authorization",
                    "Bearer {}".format(service_config["bearer_auth_token"]),
                )
            context = None
            if self.options.test_ignore_ssl_cert_verification:
                context = ssl._create_unverified_context()
            urlopen(request, context=context)
            logging.info('"%s" is ready on service endpoint %s', service_name,
                         base_url)
            return
        except HTTPError as error:
            logging.error("%s service endpoint got %s.", service_name, error)
            raise_and_log_error(
                ResponseError(
                    "{0} service endpoint got {1}".format(service_name, error),
                    server=base_url,
                ))
        except Exception as error:
            raise_and_log_error(
                ResponseError(
                    "{0} service endpoint got {1}".format(service_name, error),
                    server=base_url,
                ))
        except URLError as error:
            if time.time() >= end_time:
                logging.error("Timing out waiting for %s", service_name)
                raise_and_log_error(
                    TimeoutError(service_name, cause=service_name))
            raise
Ejemplo n.º 5
0
    def __init__(self, options):
        self.__options = options
        self.__hal_path = options.hal_path

        logging.debug('Retrieving halyard runtime configuration.')
        url = 'http://' + options.halyard_daemon + '/resolvedEnv'
        response = urllib2.urlopen(url)
        if response.getcode() >= 300:
            raise_and_log_error(
                ResponseError('{url}: {code}\n{body}'.format(
                    url=url, code=response.getcode(), body=response.read()),
                              server='halyard'))
        self.__halyard_runtime_config = yaml.load(response)
Ejemplo n.º 6
0
    def __init__(self, options):
        self.__options = options
        self.__hal_path = options.hal_path

        logging.debug('Retrieving halyard runtime configuration.')
        url = 'http://' + options.halyard_daemon + '/resolvedEnv'
        try:
            response = urlopen(url)
        except HTTPError as error:
            raise_and_log_error(
                ResponseError('{url}: {code}\n{body}'.format(
                    url=url, code=error.code, body=response.read()),
                              server='halyard'))
        self.__halyard_runtime_config = yaml.safe_load(response)
Ejemplo n.º 7
0
    def __validate_service_base_url(self, service_name, timeout=None):
        service_config = self.__public_service_configs[service_name]
        base_url = service_config['base_url']

        timeout = timeout or self.options.test_service_startup_timeout
        end_time = time.time() + timeout
        logging.info('Validating base URL of "%s..."', service_name)

        try:
            if 'bearer_auth_token' in service_config:
                _make_get_request_with_bearer_auth_token(
                    '{base_url}/health'.format(base_url=base_url),
                    service_config['bearer_auth_token'])
            # Add more authentication cases here.
            else:
                urlopen('{base_url}/health'.format(base_url=base_url))
            logging.info('"%s" is ready on service endpoint %s', service_name,
                         base_url)
            return
        except HTTPError as error:
            logging.error('%s service endpoint got %s.', service_name, error)
            raise_and_log_error(
                ResponseError('{0} service endpoint got {1}'.format(
                    service_name, error),
                              server=base_url))
        except Exception as error:
            raise_and_log_error(
                ResponseError('{0} service endpoint got {1}'.format(
                    service_name, error),
                              server=base_url))
        except URLError as error:
            if time.time() >= end_time:
                logging.error('Timing out waiting for %s', service_name)
                raise_and_log_error(
                    TimeoutError(service_name, cause=service_name))
            raise
Ejemplo n.º 8
0
 def fetch_bintray_url(self, bintray_url):
   request = Request(bintray_url)
   if self.__basic_auth:
     request.add_header('Authorization', self.__basic_auth)
   try:
     response = urlopen(request)
     headers = response.info()
     payload = response.read()
     content = json.JSONDecoder(encoding='utf-8').decode(payload)
   except HTTPError as ex:
     raise_and_log_error(
         ResponseError('Bintray failure: {}'.format(ex),
                       server='bintray.api'),
         'Failed on url=%s: %s' % (bintray_url, exception_to_message(ex)))
   except Exception as ex:
     raise
   return headers, content
Ejemplo n.º 9
0
    def __init__(self, options):
        self.__options = options
        self.__hal_path = options.hal_path

        logging.debug("Retrieving halyard runtime configuration.")
        url = "http://" + options.halyard_daemon + "/resolvedEnv"
        try:
            response = urlopen(url)
        except HTTPError as error:
            raise_and_log_error(
                ResponseError(
                    "{url}: {code}\n{body}".format(url=url,
                                                   code=error.code,
                                                   body=error.read()),
                    server="halyard",
                ))
        self.__halyard_runtime_config = yaml.safe_load(response)
Ejemplo n.º 10
0
 def bintray_repo_has_version(self, repo, package_name, repository,
                              build_version):
     """See if the given bintray repository has the package version to build."""
     try:
         bintray_url = self.__to_bintray_url(repo, package_name, repository,
                                             build_version)
         logging.debug("Checking for %s", bintray_url)
         request = Request(url=bintray_url)
         self.__add_bintray_auth_header(request)
         urlopen(request)
         return True
     except HTTPError as ex:
         if ex.code == 404:
             return False
         raise_and_log_error(
             ResponseError("Bintray failure: {}".format(ex),
                           server="bintray.check"),
             "Failed on url=%s: %s" %
             (bintray_url, exception_to_message(ex)),
         )
     except Exception as ex:
         raise
Ejemplo n.º 11
0
    def wait_on_service(self, service_name, port=None, timeout=None):
        """Wait for the given service to be available on the specified port.

    Args:
      service_name: [string] The service name we we are waiting on.
      port: [int] The remote port the service is at.
      timeout: [int] How much time to wait before giving up.

    Returns:
      The ForwardedPort entry for this service.
    """
        try:
            with self.__lock:
                forwarding = self.__forwarded_ports.get(service_name)
                if forwarding is None:
                    forwarding = self.__forward_port_to_service(service_name)
                self.__forwarded_ports[service_name] = forwarding
        except Exception:
            logging.exception(
                'Exception while attempting to forward ports to "%s"',
                service_name)
            raise

        timeout = timeout or self.options.test_service_startup_timeout
        end_time = time.time() + timeout
        logging.info('Waiting on "%s..."', service_name)
        if port is None:
            port = self.__service_port_map[service_name]

        # It seems we have a race condition in the poll
        # where it thinks the jobs have terminated.
        # I've only seen this happen once.
        time.sleep(1)

        threadid = hex(threading.current_thread().ident)
        logging.info('WaitOn polling %s from thread %s', service_name,
                     threadid)
        while forwarding.child.poll() is None:
            try:
                # localhost is hardcoded here because we are port forwarding.
                # timeout=20 is to appease kubectl port forwarding, which will close
                #            if left idle for 30s
                urlopen('http://localhost:{port}/health'.format(
                    port=forwarding.port),
                        timeout=20)
                logging.info('"%s" is ready on port %d | %s', service_name,
                             forwarding.port, threadid)
                return forwarding
            except HTTPError as error:
                logging.warning('%s got %s. Ignoring that for now.',
                                service_name, error)
                return forwarding

            except (URLError, Exception) as error:
                if time.time() >= end_time:
                    logging.error('Timing out waiting for %s | %s',
                                  service_name, threadid)
                    raise_and_log_error(
                        TimeoutError(service_name, cause=service_name))
                time.sleep(2.0)

        logging.error(
            'It appears %s is no longer available.'
            ' Perhaps the tunnel closed.', service_name)
        raise_and_log_error(
            ResponseError('It appears that {0} failed'.format(service_name),
                          server='tunnel'))