use of com.google.api.services.compute.model.Metadata in project java-docs-samples by GoogleCloudPlatform.
the class ComputeEngineSample method startInstance.
// [END list_instances]
// [START create_instances]
public static Operation startInstance(Compute compute, String instanceName) throws IOException {
System.out.println("================== Starting New Instance ==================");
// Create VM Instance object with the required properties.
Instance instance = new Instance();
instance.setName(instanceName);
instance.setMachineType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/" + ZONE_NAME + "/machineTypes/n1-standard-1");
// Add Network Interface to be used by VM Instance.
NetworkInterface ifc = new NetworkInterface();
ifc.setNetwork("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/global/networks/default");
List<AccessConfig> configs = new ArrayList<>();
AccessConfig config = new AccessConfig();
config.setType(NETWORK_INTERFACE_CONFIG);
config.setName(NETWORK_ACCESS_CONFIG);
configs.add(config);
ifc.setAccessConfigs(configs);
instance.setNetworkInterfaces(Collections.singletonList(ifc));
// Add attached Persistent Disk to be used by VM Instance.
AttachedDisk disk = new AttachedDisk();
disk.setBoot(true);
disk.setAutoDelete(true);
disk.setType("PERSISTENT");
AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
// Assign the Persistent Disk the same name as the VM Instance.
params.setDiskName(instanceName);
// Specify the source operating system machine image to be used by the VM Instance.
params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH);
// Specify the disk type as Standard Persistent Disk
params.setDiskType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/" + ZONE_NAME + "/diskTypes/pd-standard");
disk.setInitializeParams(params);
instance.setDisks(Collections.singletonList(disk));
// Initialize the service account to be used by the VM Instance and set the API access scopes.
ServiceAccount account = new ServiceAccount();
account.setEmail("default");
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
scopes.add("https://www.googleapis.com/auth/compute");
account.setScopes(scopes);
instance.setServiceAccounts(Collections.singletonList(account));
// Optional - Add a startup script to be used by the VM Instance.
Metadata meta = new Metadata();
Metadata.Items item = new Metadata.Items();
item.setKey("startup-script-url");
// If you put a script called "vm-startup.sh" in this Google Cloud Storage
// bucket, it will execute on VM startup. This assumes you've created a
// bucket named the same as your PROJECT_ID.
// For info on creating buckets see: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
item.setValue("gs://" + PROJECT_ID + "/vm-startup.sh");
meta.setItems(Collections.singletonList(item));
instance.setMetadata(meta);
System.out.println(instance.toPrettyString());
Compute.Instances.Insert insert = compute.instances().insert(PROJECT_ID, ZONE_NAME, instance);
return insert.execute();
}
use of com.google.api.services.compute.model.Metadata 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;
}
use of com.google.api.services.compute.model.Metadata 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.Metadata in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> customTags) throws Exception {
InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
String projectId = context.getProjectId();
Location location = context.getLocation();
boolean noPublicIp = context.getNoPublicIp();
Compute compute = context.getCompute();
List<CloudResource> computeResources = context.getComputeResources(privateId);
List<AttachedDisk> listOfDisks = new ArrayList<>();
listOfDisks.addAll(getBootDiskList(computeResources, projectId, location.getAvailabilityZone()));
listOfDisks.addAll(getAttachedDisks(computeResources, projectId, location.getAvailabilityZone()));
Instance instance = new Instance();
instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", projectId, location.getAvailabilityZone().value(), template.getFlavor()));
instance.setName(buildableResource.get(0).getName());
instance.setCanIpForward(Boolean.TRUE);
instance.setNetworkInterfaces(getNetworkInterface(context.getNetworkResources(), computeResources, location.getRegion(), group, compute, projectId, noPublicIp));
instance.setDisks(listOfDisks);
Scheduling scheduling = new Scheduling();
boolean preemptible = false;
if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
}
scheduling.setPreemptible(preemptible);
instance.setScheduling(scheduling);
Tags tags = new Tags();
List<String> tagList = new ArrayList<>();
Map<String, String> labels = new HashMap<>();
String groupname = group.getName().toLowerCase().replaceAll("[^A-Za-z0-9 ]", "");
tagList.add(groupname);
Map<String, String> instanceTag = defaultCostTaggingService.prepareInstanceTagging();
for (Entry<String, String> entry : instanceTag.entrySet()) {
tagList.add(String.format("%s-%s", entry.getKey(), entry.getValue()));
labels.put(entry.getKey(), entry.getValue());
}
tagList.add(GcpStackUtil.getClusterTag(auth.getCloudContext()));
tagList.add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
customTags.forEach((key, value) -> tagList.add(key + '-' + value));
labels.putAll(customTags);
tags.setItems(tagList);
instance.setTags(tags);
instance.setLabels(labels);
Metadata metadata = new Metadata();
metadata.setItems(new ArrayList<>());
Items sshMetaData = new Items();
sshMetaData.setKey("ssh-keys");
sshMetaData.setValue(group.getInstanceAuthentication().getLoginUserName() + ':' + group.getInstanceAuthentication().getPublicKey());
Items blockProjectWideSsh = new Items();
blockProjectWideSsh.setKey("block-project-ssh-keys");
blockProjectWideSsh.setValue("TRUE");
Items startupScript = new Items();
startupScript.setKey("startup-script");
startupScript.setValue(image.getUserDataByType(group.getType()));
metadata.getItems().add(sshMetaData);
metadata.getItems().add(startupScript);
metadata.getItems().add(blockProjectWideSsh);
instance.setMetadata(metadata);
Insert insert = compute.instances().insert(projectId, location.getAvailabilityZone().value(), instance);
insert.setPrettyPrint(Boolean.TRUE);
try {
Operation operation = insert.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.get(0).getName());
}
return Collections.singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
}
}
use of com.google.api.services.compute.model.Metadata in project platformlayer by platformlayer.
the class GoogleComputeClient method createInstance.
public Instance createInstance(GoogleCloud cloud, MachineCreationRequest request, PublicKey sshPublicKey) throws OpsException {
try {
Image foundImage = null;
{
DiskImageRecipe recipe = null;
if (request.recipeId != null) {
recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
}
OperatingSystemRecipe operatingSystem = null;
if (recipe != null) {
operatingSystem = recipe.getOperatingSystem();
}
log.info("Listing images to pick best image");
Iterable<Image> images = listImages(PROJECTID_GOOGLE);
// TODO: We need a better solution here!!
log.warn("Hard coding image names");
Set<String> imageNames = Sets.newHashSet("ubuntu-12-04-v20120621");
for (Image image : images) {
if (imageNames.contains(image.getName())) {
foundImage = image;
break;
}
}
if (foundImage == null) {
throw new IllegalArgumentException("Could not find image");
}
}
// GCE requires that the name comply with RFC1035, which I think means a valid DNS
// For now, just use a UUID, with a pl- prefix so it doesn't start with a number
// TODO: Fix this!
String instanceName = "pl-" + UUID.randomUUID().toString();
Operation createServerOperation;
{
Instance create = new Instance();
create.setName(instanceName);
create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));
{
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));
AccessConfig networkAccessConfig = new AccessConfig();
networkAccessConfig.setType("ONE_TO_ONE_NAT");
networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));
create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
}
Metadata metadata = new Metadata();
metadata.setItems(Lists.<Items>newArrayList());
create.setMetadata(metadata);
if (request.tags != null) {
for (Tag tag : request.tags) {
Metadata.Items meta = new Metadata.Items();
meta.setKey(tag.getKey());
meta.setValue(tag.getValue());
metadata.getItems().add(meta);
}
}
if (request.sshPublicKey != null) {
Metadata.Items meta = new Metadata.Items();
meta.setKey("sshKeys");
meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));
metadata.getItems().add(meta);
}
create.setImage(foundImage.getSelfLink());
MachineType flavor = getClosestInstanceType(request);
if (flavor == null) {
throw new OpsException("Cannot determine machine type for request");
}
create.setMachineType(flavor.getSelfLink());
if (request.securityGroups != null) {
// TODO: Reimplement if needed
throw new UnsupportedOperationException();
}
// if (createdSecurityGroup != null) {
// ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
// serverSecurityGroup.setName(createdSecurityGroup.getName());
// create.getSecurityGroups().add(serverSecurityGroup);
// }
// create.setConfigDrive(cloudBehaviours.useConfigDrive());
log.info("Launching new server: " + instanceName);
try {
createServerOperation = compute.instances().insert(projectId, create).execute();
} catch (IOException e) {
throw new OpsException("Error launching new instance", e);
}
}
log.info("Waiting for server to be ready");
createServerOperation = waitComplete(createServerOperation, 10, TimeUnit.MINUTES);
Instance created;
InstanceState state = null;
while (true) {
created = findInstanceByName(instanceName);
state = InstanceState.get(created);
log.info("Instance state: " + state);
if (state.isRunning()) {
break;
}
Thread.sleep(1000);
}
return created;
} catch (InterruptedException e) {
ExceptionUtils.handleInterrupted(e);
throw new OpsException("Error building server", e);
} catch (TimeoutException e) {
throw new OpsException("Timeout waiting for server build", e);
}
}
Aggregations