Search in sources :

Example 6 with RunningServiceDetails

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails in project halyard by spinnaker.

the class GoogleDistributedService method getServerGroupDescription.

@Override
default Map<String, Object> getServerGroupDescription(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings, List<ConfigSource> configSources) {
    GoogleAccount account = details.getAccount();
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    Integer version = runningServiceDetails.getLatestEnabledVersion();
    if (version == null) {
        version = 0;
    } else {
        version++;
    }
    Names name = Names.parseName(getServiceName());
    String app = name.getApp();
    String stack = name.getStack();
    String detail = name.getDetail();
    String network = GoogleProviderUtils.getNetworkName();
    Map<String, String> metadata = getMetadata(details, runtimeSettings, configSources, version).stream().reduce(new HashMap<String, String>(), (h1, item) -> {
        h1.put(item.getKey(), item.getValue());
        return h1;
    }, (h1, h2) -> {
        h1.putAll(h2);
        return h1;
    });
    String serviceAccountEmail = GoogleProviderUtils.defaultServiceAccount(details);
    List<String> scopes = getScopes();
    String accountName = account.getName();
    Map<String, Object> deployDescription = new HashMap<>();
    deployDescription.put("application", app);
    deployDescription.put("stack", stack);
    deployDescription.put("freeFormDetails", detail);
    deployDescription.put("network", network);
    deployDescription.put("instanceMetadata", metadata);
    deployDescription.put("serviceAccountEmail", serviceAccountEmail);
    deployDescription.put("authScopes", scopes);
    deployDescription.put("accountName", accountName);
    deployDescription.put("account", accountName);
    return deployDescription;
/* TODO(lwander): Google's credential class cannot be serialized as-is, making this type of construction impossible
    BasicGoogleDeployDescription deployDescription = new BasicGoogleDeployDescription();
    deployDescription.setApplication(app);
    deployDescription.setStack(stack);
    deployDescription.setFreeFormDetails(detail);

    deployDescription.setNetwork(network);
    deployDescription.setInstanceMetadata(metadata);
    deployDescription.setServiceAccountEmail(serviceAccountEmail);
    deployDescription.setAuthScopes(scopes);
    // Google's credentials constructor prevents us from neatly creating a deploy description with only a name supplied
    String jsonKey = null;
    if (!StringUtils.isEmpty(account.getJsonPath())) {
      try {
        jsonKey = IOUtils.toString(new FileInputStream(account.getJsonPath()));
      } catch (IOException e) {
        throw new RuntimeException("Unvalidated json path found during deployment: " + e.getMessage(), e);
      }
    }

    deployDescription.setCredentials(new GoogleNamedAccountCredentials.Builder()
        .name(account.getName())
        .jsonKey(jsonKey)
        .project(account.getProject())
        .build()
    );

    return new ObjectMapper().convertValue(deployDescription, new TypeReference<Map<String, Object>>() { });
    */
}
Also used : Names(com.netflix.frigga.Names) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) HashMap(java.util.HashMap) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)

Example 7 with RunningServiceDetails

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails in project halyard by spinnaker.

the class GoogleDistributedService method sshTunnelIntoService.

default <S> URI sshTunnelIntoService(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings, SpinnakerService<S> sidecar) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(sidecar);
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    Integer enabledVersion = runningServiceDetails.getLatestEnabledVersion();
    if (enabledVersion == null) {
        throw new HalException(FATAL, "Cannot connect to " + getServiceName() + " when no server groups have been deployed yet");
    }
    List<RunningServiceDetails.Instance> instances = runningServiceDetails.getInstances().get(enabledVersion);
    if (instances == null || instances.isEmpty()) {
        throw new HalException(FATAL, "Cannot connect to " + getServiceName() + " when no instances have been deployed yet");
    }
    try {
        return GoogleProviderUtils.openSshTunnel(details, instances.get(0).getId(), settings);
    } catch (InterruptedException e) {
        throw new DaemonTaskInterrupted(e);
    }
}
Also used : ManagedInstance(com.google.api.services.compute.model.ManagedInstance) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted)

Example 8 with RunningServiceDetails

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails in project halyard by spinnaker.

the class GoogleDistributedService method connectCommand.

@Override
default String connectCommand(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    Integer version = runningServiceDetails.getLatestEnabledVersion();
    if (version == null) {
        throw new HalException(FATAL, "No version of " + getServiceName() + " to connect to.");
    }
    List<RunningServiceDetails.Instance> instances = runningServiceDetails.getInstances().get(version);
    if (instances.isEmpty()) {
        throw new HalException(FATAL, "Version " + version + " of " + getServiceName() + " has no instances to connect to");
    }
    RunningServiceDetails.Instance instance = instances.get(0);
    String instanceName = instance.getId();
    String zone = instance.getLocation();
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    int port = settings.getPort();
    return String.format("gcloud compute ssh %s --zone %s -- -L %d:localhost:%d -N", instanceName, zone, port, port);
}
Also used : ManagedInstance(com.google.api.services.compute.model.ManagedInstance) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)

Example 9 with RunningServiceDetails

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails in project halyard by spinnaker.

the class GoogleDistributedService method getMetadata.

default List<Metadata.Items> getMetadata(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings, List<ConfigSource> configSources, Integer version) {
    List<Metadata.Items> metadataItems = new ArrayList<>();
    String deploymentName = details.getDeploymentName();
    Metadata.Items items = new Metadata.Items().setKey("startup-script").setValue(getStartupScript());
    metadataItems.add(items);
    items = new Metadata.Items().setKey("ssh-keys").setValue(GoogleProviderUtils.getSshPublicKey());
    metadataItems.add(items);
    if (!configSources.isEmpty()) {
        DaemonTaskHandler.message("Mounting config in vault server");
        GoogleVaultServerService vaultService = getVaultServerService();
        VaultServerService.Vault vault = vaultService.connectToPrimaryService(details, runtimeSettings);
        String secretName = secretName("config-mounts", version);
        VaultConfigMountSet mountSet = VaultConfigMountSet.fromConfigSources(configSources);
        secretName = vaultService.writeVaultConfigMountSet(deploymentName, vault, secretName, mountSet);
        VaultConnectionDetails connectionDetails = buildConnectionDetails(details, runtimeSettings, secretName);
        DaemonTaskHandler.message("Placing vault connection details into instance metadata");
        items = new Metadata.Items().setKey("vault_address").setValue(connectionDetails.getAddress());
        metadataItems.add(items);
        items = new Metadata.Items().setKey("vault_token").setValue(connectionDetails.getToken());
        metadataItems.add(items);
        items = new Metadata.Items().setKey("vault_secret").setValue(connectionDetails.getSecret());
        metadataItems.add(items);
    }
    GoogleConsulServerService consulServerService = getConsulServerService();
    RunningServiceDetails consulServerDetails = consulServerService.getRunningServiceDetails(details, runtimeSettings);
    Integer latestConsulVersion = consulServerDetails.getLatestEnabledVersion();
    if (latestConsulVersion != null) {
        List<RunningServiceDetails.Instance> instances = consulServerDetails.getInstances().get(latestConsulVersion);
        String instancesValue = String.join(" ", instances.stream().map(RunningServiceDetails.Instance::getId).collect(Collectors.toList()));
        items = new Metadata.Items().setKey(// TODO(lwander) change to consul_members for consistency w/ vault
        "consul-members").setValue(instancesValue);
        DaemonTaskHandler.message("Placing consul connection details into instance metadata");
        metadataItems.add(items);
    }
    return metadataItems;
}
Also used : ManagedInstance(com.google.api.services.compute.model.ManagedInstance) ArrayList(java.util.ArrayList) Metadata(com.google.api.services.compute.model.Metadata) VaultConfigMountSet(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMountSet) VaultConnectionDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConnectionDetails) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) VaultServerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.VaultServerService)

Example 10 with RunningServiceDetails

use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails in project halyard by spinnaker.

the class GoogleDistributedService method ensureRunning.

@Override
default void ensureRunning(AccountDeploymentDetails<GoogleAccount> details, ResolvedConfiguration resolvedConfiguration, List<ConfigSource> configSources, boolean recreate) {
    DaemonTaskHandler.newStage("Deploying " + getServiceName() + " via GCE API");
    Integer version = 0;
    ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
    SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    GoogleAccount account = details.getAccount();
    Compute compute = GoogleProviderUtils.getCompute(details);
    String project = account.getProject();
    String zone = settings.getLocation();
    boolean exists = runningServiceDetails.getInstances().containsKey(version);
    if (!recreate && exists) {
        DaemonTaskHandler.message("Service " + getServiceName() + " is already deployed and not safe to restart");
        return;
    } else if (exists) {
        DaemonTaskHandler.message("Recreating existing " + getServiceName() + "...");
        deleteVersion(details, settings, version);
    }
    InstanceGroupManager manager = new InstanceGroupManager();
    InstanceTemplate template = new InstanceTemplate().setName(getServiceName() + "-hal-" + System.currentTimeMillis()).setDescription("Halyard-generated instance template for deploying Spinnaker");
    Metadata metadata = new Metadata().setItems(getMetadata(details, runtimeSettings, configSources, version));
    AccessConfig accessConfig = new AccessConfig().setName("External NAT").setType("ONE_TO_ONE_NAT");
    NetworkInterface networkInterface = new NetworkInterface().setNetwork(GoogleProviderUtils.ensureSpinnakerNetworkExists(details)).setAccessConfigs(Collections.singletonList(accessConfig));
    ServiceAccount sa = new ServiceAccount().setEmail(GoogleProviderUtils.defaultServiceAccount(details)).setScopes(getScopes());
    InstanceProperties properties = new InstanceProperties().setMachineType(getDefaultInstanceType()).setMetadata(metadata).setServiceAccounts(Collections.singletonList(sa)).setNetworkInterfaces(Collections.singletonList(networkInterface));
    AttachedDisk disk = new AttachedDisk().setBoot(true).setAutoDelete(true).setType("PERSISTENT");
    AttachedDiskInitializeParams diskParams = new AttachedDiskInitializeParams().setDiskSizeGb(20L).setDiskStorageType(GCEUtil.buildDiskTypeUrl(project, zone, GoogleDiskType.PD_SSD)).setSourceImage(getArtifactId(details.getDeploymentName()));
    disk.setInitializeParams(diskParams);
    List<AttachedDisk> disks = new ArrayList<>();
    disks.add(disk);
    properties.setDisks(disks);
    template.setProperties(properties);
    String instanceTemplateUrl;
    Operation operation;
    try {
        DaemonTaskHandler.message("Creating an instance template");
        operation = compute.instanceTemplates().insert(project, template).execute();
        instanceTemplateUrl = operation.getTargetLink();
        GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to create instance template for " + settings.getArtifactId() + ": " + e.getMessage(), e);
    }
    String migName = getVersionedName(version);
    manager.setInstanceTemplate(instanceTemplateUrl);
    manager.setBaseInstanceName(migName);
    manager.setTargetSize(settings.getTargetSize());
    manager.setName(migName);
    try {
        DaemonTaskHandler.message("Deploying the instance group manager");
        operation = compute.instanceGroupManagers().insert(project, settings.getLocation(), manager).execute();
        GoogleProviderUtils.waitOnZoneOperation(compute, project, settings.getLocation(), operation);
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to create instance group to run artifact " + settings.getArtifactId() + ": " + e.getMessage(), e);
    }
    boolean ready = false;
    DaemonTaskHandler.message("Waiting for all instances to become healthy.");
    while (!ready) {
        ready = getRunningServiceDetails(details, runtimeSettings).getLatestEnabledVersion() == version;
        DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(2));
    }
}
Also used : GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) ServiceAccount(com.google.api.services.compute.model.ServiceAccount) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) InstanceProperties(com.google.api.services.compute.model.InstanceProperties) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Metadata(com.google.api.services.compute.model.Metadata) ArrayList(java.util.ArrayList) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) Compute(com.google.api.services.compute.Compute) InstanceTemplate(com.google.api.services.compute.model.InstanceTemplate)

Aggregations

RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)19 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)14 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)13 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)9 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)8 SpinnakerService (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService)6 DistributedService (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService)6 List (java.util.List)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)4 Names (com.netflix.frigga.Names)4 DaemonResponse (com.netflix.spinnaker.halyard.core.DaemonResponse)4 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)4 DaemonTaskHandler (com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler)4 AccountDeploymentDetails (com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails)4 ConfigSource (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource)4 Collections (java.util.Collections)4 Set (java.util.Set)4