Exemplo n.º 1
0
    def new_gce_instance_from_bindings(cls, name, status_factory, bindings,
                                       port):
        """Create a new Spinnaker HttpAgent talking to the specified server port.

    Args:
      name: [string] The name of agent we are creating for reporting only.
      status_factory: [SpinnakerStatus (SpinnakerAgent, HttpResponseType)]
         Factory method for creating specialized SpinnakerStatus instances.
      bindings: [dict] List of bindings to configure the endpoint
          GCE_PROJECT: The GCE project ID that the endpoint is in.
          GCE_ZONE: The GCE zone that the endpoint is in.
          GCE_INSTANCE: The GCE instance that the endpoint is in.
          GCE_SSH_PASSPHRASE_FILE: If not empty, the SSH passphrase key
              for tunneling if needed to connect through a GCE firewall.
          GCE_SERVICE_ACCOUNT: If not empty, the GCE service account to use
              when interacting with the GCE instance.
      port: [int] The port of the endpoint we want to connect to.
    Returns:
      A SpinnakerAgent connected to the specified instance port.
    """
        project = bindings['GCE_PROJECT']
        zone = bindings['GCE_ZONE']
        instance = bindings['GCE_INSTANCE']
        ssh_passphrase_file = bindings.get('GCE_SSH_PASSPHRASE_FILE', None)
        service_account = bindings.get('GCE_SERVICE_ACCOUNT', None)

        logger = logging.getLogger(__name__)
        JournalLogger.begin_context('Locating {0}...'.format(name))
        context_relation = 'ERROR'
        try:
            gcloud = gcp.GCloudAgent(project=project,
                                     zone=zone,
                                     service_account=service_account,
                                     ssh_passphrase_file=ssh_passphrase_file)
            netloc = gce_util.establish_network_connectivity(gcloud=gcloud,
                                                             instance=instance,
                                                             target_port=port)
            if not netloc:
                error = 'Could not locate {0}.'.format(name)
                logger.error(error)
                context_relation = 'INVALID'
                raise RuntimeError(error)

            protocol = bindings['NETWORK_PROTOCOL']
            base_url = '{protocol}://{netloc}'.format(protocol=protocol,
                                                      netloc=netloc)
            logger.info('%s is available at %s. Using %s', name, netloc,
                        base_url)
            deployed_config = scrape_spring_config(
                os.path.join(base_url, 'resolvedEnv'))
            JournalLogger.journal_or_log_detail(
                '{0} configuration'.format(name), deployed_config)
            spinnaker_agent = cls(base_url, status_factory)
            spinnaker_agent.__deployed_config = deployed_config
            context_relation = 'VALID'
        except:
            logger.exception('Failed to create spinnaker agent.')
            raise
        finally:
            JournalLogger.end_context(relation=context_relation)

        return spinnaker_agent
Exemplo n.º 2
0
    def new_gce_instance_from_bindings(cls, name, status_factory, bindings, port):
        """Create a new Spinnaker HttpAgent talking to the specified server port.

        Args:
          name: [string] The name of agent we are creating for reporting only.
          status_factory: [SpinnakerStatus (SpinnakerAgent, HttpResponseType)]
             Factory method for creating specialized SpinnakerStatus instances.
          bindings: [dict] List of bindings to configure the endpoint
              GCE_PROJECT: The GCE project ID that the endpoint is in.
              GCE_ZONE: The GCE zone that the endpoint is in.
              GCE_INSTANCE: The GCE instance that the endpoint is in.
              GCE_SSH_PASSPHRASE_FILE: If not empty, the SSH passphrase key
                  for tunneling if needed to connect through a GCE firewall.
              GCE_SERVICE_ACCOUNT: If not empty, the GCE service account to use
                  when interacting with the GCE instance.
              IGNORE_SSL_CERT_VERIFICATION: If True, ignores SSL certificate
                  verification when scraping spring config.
          port: [int] The port of the endpoint we want to connect to.
        Returns:
          A SpinnakerAgent connected to the specified instance port.
        """
        project = bindings["GCE_PROJECT"]
        zone = bindings["GCE_ZONE"]
        instance = bindings["GCE_INSTANCE"]
        ssh_passphrase_file = bindings.get("GCE_SSH_PASSPHRASE_FILE", None)
        service_account = bindings.get("GCE_SERVICE_ACCOUNT", None)
        ignore_ssl_cert_verification = bindings["IGNORE_SSL_CERT_VERIFICATION"]

        logger = logging.getLogger(__name__)
        JournalLogger.begin_context("Locating {0}...".format(name))
        context_relation = "ERROR"
        try:
            gcloud = gcp.GCloudAgent(
                project=project,
                zone=zone,
                service_account=service_account,
                ssh_passphrase_file=ssh_passphrase_file,
            )
            netloc = gce_util.establish_network_connectivity(
                gcloud=gcloud, instance=instance, target_port=port
            )
            if not netloc:
                error = "Could not locate {0}.".format(name)
                logger.error(error)
                context_relation = "INVALID"
                raise RuntimeError(error)

            protocol = bindings["NETWORK_PROTOCOL"]
            base_url = "{protocol}://{netloc}".format(protocol=protocol, netloc=netloc)
            logger.info("%s is available at %s. Using %s", name, netloc, base_url)
            deployed_config = scrape_spring_config(
                os.path.join(base_url, "resolvedEnv"),
                ignore_ssl_cert_verification=ignore_ssl_cert_verification,
            )
            JournalLogger.journal_or_log_detail(
                "{0} configuration".format(name), deployed_config
            )
            spinnaker_agent = cls(base_url, status_factory)
            spinnaker_agent.__deployed_config = deployed_config
            context_relation = "VALID"
        except:
            logger.exception("Failed to create spinnaker agent.")
            raise
        finally:
            JournalLogger.end_context(relation=context_relation)

        return spinnaker_agent