Search in sources :

Example 1 with GoogleAccount

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount in project halyard by spinnaker.

the class GoogleProviderUtils method defaultServiceAccount.

static String defaultServiceAccount(AccountDeploymentDetails<GoogleAccount> details) {
    GoogleAccount account = details.getAccount();
    String project = account.getProject();
    Compute compute = getCompute(details);
    try {
        return compute.projects().get(project).execute().getDefaultServiceAccount();
    } catch (IOException e) {
        throw new HalException(FATAL, "Unable to get default compute service account");
    }
}
Also used : GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) Compute(com.google.api.services.compute.Compute) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Example 2 with GoogleAccount

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount in project halyard by spinnaker.

the class GoogleAddAccountCommand method buildAccount.

@Override
protected Account buildAccount(String accountName) {
    GoogleAccount account = (GoogleAccount) new GoogleAccount().setName(accountName);
    account = (GoogleAccount) account.setJsonPath(jsonPath).setProject(project);
    account.setAlphaListed(alphaListed).setImageProjects(imageProjects).setUserDataFile(userDataFile).setRegions(regions);
    return account;
}
Also used : GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount)

Example 3 with GoogleAccount

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount 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 4 with GoogleAccount

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount 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)

Example 5 with GoogleAccount

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount in project halyard by spinnaker.

the class GoogleDistributedService method getRunningServiceDetails.

@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    RunningServiceDetails result = new RunningServiceDetails();
    // All GCE load balancing is done via consul
    result.setLoadBalancer(new RunningServiceDetails.LoadBalancer().setExists(true));
    Compute compute = GoogleProviderUtils.getCompute(details);
    GoogleAccount account = details.getAccount();
    List<InstanceGroupManager> migs;
    try {
        migs = compute.instanceGroupManagers().list(account.getProject(), settings.getLocation()).execute().getItems();
        if (migs == null) {
            migs = Collections.emptyList();
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to load MIGS: " + e.getMessage(), e);
    }
    boolean consulEnabled = getSidecars(runtimeSettings).stream().anyMatch(s -> s.getService().getType().equals(SpinnakerService.Type.CONSUL_CLIENT));
    Set<String> healthyConsulInstances = consulEnabled ? getConsulServerService().connectToPrimaryService(details, runtimeSettings).serviceHealth(getService().getCanonicalName(), true).stream().map(s -> s != null && s.getNode() != null ? s.getNode().getNodeName() : null).filter(Objects::nonNull).collect(Collectors.toSet()) : new HashSet<>();
    String serviceName = getService().getServiceName();
    migs = migs.stream().filter(ig -> ig.getName().startsWith(serviceName + "-v")).collect(Collectors.toList());
    Map<Integer, List<RunningServiceDetails.Instance>> instances = migs.stream().reduce(new HashMap<>(), (map, mig) -> {
        Names names = Names.parseName(mig.getName());
        Integer version = names.getSequence();
        List<RunningServiceDetails.Instance> computeInstances;
        try {
            List<ManagedInstance> managedInstances = compute.instanceGroupManagers().listManagedInstances(account.getProject(), settings.getLocation(), mig.getName()).execute().getManagedInstances();
            if (managedInstances == null) {
                managedInstances = new ArrayList<>();
            }
            computeInstances = managedInstances.stream().map(i -> {
                String instanceUrl = i.getInstance();
                String instanceStatus = i.getInstanceStatus();
                boolean running = instanceStatus != null && instanceStatus.equalsIgnoreCase("running");
                String instanceName = instanceUrl.substring(instanceUrl.lastIndexOf('/') + 1, instanceUrl.length());
                return new RunningServiceDetails.Instance().setId(instanceName).setLocation(settings.getLocation()).setRunning(running).setHealthy(!consulEnabled || healthyConsulInstances.contains(instanceName));
            }).collect(Collectors.toList());
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to load target pools for " + serviceName, e);
        }
        map.put(version, computeInstances);
        return map;
    }, (m1, m2) -> {
        m1.putAll(m2);
        return m1;
    });
    result.setInstances(instances);
    return result;
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) ServiceAccount(com.google.api.services.compute.model.ServiceAccount) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Names(com.netflix.frigga.Names) Map(java.util.Map) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) URI(java.net.URI) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Path(java.nio.file.Path) InstanceTemplate(com.google.api.services.compute.model.InstanceTemplate) ServiceInterfaceFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceInterfaceFactory) VaultConnectionDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConnectionDetails) URIBuilder(org.apache.http.client.utils.URIBuilder) Set(java.util.Set) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Operation(com.google.api.services.compute.model.Operation) Collectors(java.util.stream.Collectors) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) Objects(java.util.Objects) List(java.util.List) InstanceProperties(com.google.api.services.compute.model.InstanceProperties) VaultConfigMount(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMount) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) GoogleDiskType(com.netflix.spinnaker.clouddriver.google.model.GoogleDiskType) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Compute(com.google.api.services.compute.Compute) Metadata(com.google.api.services.compute.model.Metadata) VaultConfigMountSet(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.VaultConfigMountSet) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) AccessConfig(com.google.api.services.compute.model.AccessConfig) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) ResolvedConfiguration(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService.ResolvedConfiguration) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) VaultServerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.VaultServerService) TimeUnit(java.util.concurrent.TimeUnit) GCEUtil(com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) DistributedService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Collections(java.util.Collections) GoogleAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Names(com.netflix.frigga.Names) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) List(java.util.List) ArrayList(java.util.ArrayList) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) IOException(java.io.IOException) Compute(com.google.api.services.compute.Compute) Objects(java.util.Objects) ManagedInstance(com.google.api.services.compute.model.ManagedInstance)

Aggregations

GoogleAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount)6 Compute (com.google.api.services.compute.Compute)3 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)3 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)3 AccessConfig (com.google.api.services.compute.model.AccessConfig)2 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)2 AttachedDiskInitializeParams (com.google.api.services.compute.model.AttachedDiskInitializeParams)2 InstanceGroupManager (com.google.api.services.compute.model.InstanceGroupManager)2 InstanceProperties (com.google.api.services.compute.model.InstanceProperties)2 InstanceTemplate (com.google.api.services.compute.model.InstanceTemplate)2 Metadata (com.google.api.services.compute.model.Metadata)2 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)2 Operation (com.google.api.services.compute.model.Operation)2 ServiceAccount (com.google.api.services.compute.model.ServiceAccount)2 Names (com.netflix.frigga.Names)2 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)2 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)2 IOException (java.io.IOException)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)1