use of com.google.api.services.compute.model.InstanceGroupManager 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));
}
}
use of com.google.api.services.compute.model.InstanceGroupManager in project halyard by spinnaker.
the class GoogleDistributedService method deleteVersion.
@Override
default void deleteVersion(AccountDeploymentDetails<GoogleAccount> details, ServiceSettings settings, Integer version) {
String migName = getVersionedName(version);
String zone = settings.getLocation();
String project = details.getAccount().getProject();
Compute compute = GoogleProviderUtils.getCompute(details);
InstanceGroupManager mig;
try {
mig = compute.instanceGroupManagers().get(project, zone, migName).execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
return;
} else {
throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
}
try {
GoogleProviderUtils.waitOnZoneOperation(compute, project, zone, compute.instanceGroupManagers().delete(project, zone, migName).execute());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to delete mig " + migName + " in " + zone, e);
}
String instanceTemplateName = mig.getInstanceTemplate();
instanceTemplateName = instanceTemplateName.substring(instanceTemplateName.lastIndexOf('/') + 1);
try {
GoogleProviderUtils.waitOnGlobalOperation(compute, project, compute.instanceTemplates().delete(project, instanceTemplateName).execute());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to delete template " + instanceTemplateName + " in " + zone, e);
}
}
use of com.google.api.services.compute.model.InstanceGroupManager 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;
}
Aggregations