Repository: jclouds-labs Updated Branches: refs/heads/fix/AzureTemplateBuilderLiveTest 8d0cd4d85 -> 5b832bfc6
wip Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/5b832bfc Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/5b832bfc Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/5b832bfc Branch: refs/heads/fix/AzureTemplateBuilderLiveTest Commit: 5b832bfc663aef9c4f4cf42c4a6839ac36f79bf9 Parents: 8d0cd4d Author: Andrea Turli <[email protected]> Authored: Wed Oct 5 12:45:06 2016 +0200 Committer: Andrea Turli <[email protected]> Committed: Wed Oct 5 12:45:19 2016 +0200 ---------------------------------------------------------------------- .../arm/AzureComputeProviderMetadata.java | 8 +- .../arm/compute/AzureComputeServiceAdapter.java | 154 ++++++++++--------- .../CreateResourceGroupThenCreateNodes.java | 23 ++- .../compute/AzureComputeServiceLiveTest.java | 14 +- 4 files changed, 102 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/5b832bfc/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadata.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadata.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadata.java index 9eeaac9..5224410 100644 --- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadata.java +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadata.java @@ -29,6 +29,9 @@ import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.RESOURC import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.STORAGE_API_VERSION; import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_FORMAT; import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_REGEXP; +import static org.jclouds.compute.config.ComputeServiceProperties.IMAGE_AUTHENTICATE_SUDO; +import static org.jclouds.compute.config.ComputeServiceProperties.IMAGE_LOGIN_USER; +import static org.jclouds.compute.config.ComputeServiceProperties.TEMPLATE; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED; import static org.jclouds.oauth.v2.config.CredentialType.CLIENT_CREDENTIALS_SECRET; import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE; @@ -91,8 +94,9 @@ public class AzureComputeProviderMetadata extends BaseProviderMetadata { properties.put(DEFAULT_DATADISKSIZE, "100"); properties.put(IMAGE_PUBLISHERS, "Canonical,RedHat"); // Default credentials for all images - properties.put("jclouds.image.login-user", "jclouds:Password1!"); - properties.put("jclouds.image.authenticate-sudo", "true"); + properties.put(TEMPLATE, "osFamily=UBUNTU,os64Bit=true,osVersionMatches=16.04.0-LTS"); + properties.put(IMAGE_LOGIN_USER, "jclouds:Password12345!"); + properties.put(IMAGE_AUTHENTICATE_SUDO, "true"); properties.put(TIMEOUT_NODE_TERMINATED, 60 * 10 * 1000); // Api versions used in each API properties.put(API_VERSION_PREFIX + DeploymentApi.class.getSimpleName(), "2016-02-01"); http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/5b832bfc/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java index 5ee484d..b50e5f0 100644 --- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceAdapter.java @@ -16,7 +16,6 @@ */ package org.jclouds.azurecompute.arm.compute; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Set; @@ -27,6 +26,7 @@ import javax.inject.Named; import javax.inject.Singleton; import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; import org.jclouds.azurecompute.arm.compute.config.AzureComputeServiceContextModule.AzureComputeConstants; import org.jclouds.azurecompute.arm.compute.functions.VMImageToImage; import org.jclouds.azurecompute.arm.compute.options.AzureTemplateOptions; @@ -82,6 +82,9 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.Iterables.contains; +import static com.google.common.collect.Iterables.filter; +import static org.jclouds.compute.config.ComputeServiceProperties.IMAGE_LOGIN_USER; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; import static org.jclouds.util.Predicates2.retry; @@ -132,7 +135,7 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual NetworkInterfaceCard nic = createNetworkInterfaceCard(subnetId, name, locationName); StorageProfile storageProfile = createStorageProfile(name, template.getImage(), templateOptions.getBlob()); HardwareProfile hardwareProfile = HardwareProfile.builder().vmSize(template.getHardware().getId()).build(); - OSProfile osProfile = createOsProfile(name, templateOptions.getLoginUser(), templateOptions.getPublicKey()); + OSProfile osProfile = createOsProfile(name, templateOptions); NetworkProfile networkProfile = NetworkProfile.builder().networkInterfaces(ImmutableList.of(IdReference.create(nic.id()))).build(); VirtualMachineProperties virtualMachineProperties = VirtualMachineProperties.builder() .licenseType(null) // TODO @@ -152,67 +155,6 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual return new NodeAndInitialCredentials<VirtualMachine>(virtualMachine, name, null); } - private OSProfile createOsProfile(String computerName, String loginUser, String publicKey) { - String adminUsername = Objects.firstNonNull(loginUser, "jclouds"); - OSProfile.Builder builder = OSProfile.builder().adminUsername(adminUsername).computerName(computerName); - if (publicKey !=null) { - OSProfile.LinuxConfiguration linuxConfiguration = OSProfile.LinuxConfiguration.create("true", - OSProfile.LinuxConfiguration.SSH.create(Arrays.asList( - OSProfile.LinuxConfiguration.SSH.SSHPublicKey.create( - String.format("/home/%s/.ssh/authorized_keys", adminUsername), - publicKey) - )) - ); - builder.linuxConfiguration(linuxConfiguration); - } - return builder.build(); - } - - private NetworkInterfaceCard createNetworkInterfaceCard(String subnetId, String name, String locationName) { - final PublicIPAddressApi ipApi = api.getPublicIPAddressApi(azureGroup); - - PublicIPAddressProperties properties = - PublicIPAddressProperties.builder() - .publicIPAllocationMethod("Static") - .idleTimeoutInMinutes(4) - .build(); - - String publicIpAddressName = "public-address-" + name; - PublicIPAddress ip = ipApi.createOrUpdate(publicIpAddressName, locationName, ImmutableMap.of("testkey", "testvalue"), properties); - retry(new Predicate<String>() { - @Override public boolean apply(String name) { - return api.getPublicIPAddressApi(azureGroup).get(name).properties().provisioningState().equals("Succeeded"); - } - }, 10 * 1000).apply(publicIpAddressName); - - final NetworkInterfaceCardProperties networkInterfaceCardProperties = - NetworkInterfaceCardProperties.builder() - .ipConfigurations(ImmutableList.of( - IpConfiguration.builder() - .name("ipConfig-" + name) - .properties(IpConfigurationProperties.builder() - .privateIPAllocationMethod("Dynamic") - .publicIPAddress(IdReference.create(ip.id())) - .subnet(IdReference.create(subnetId)) - .build()) - .build())) - .build(); - - String networkInterfaceCardName = "jc-nic-" + name; - return api.getNetworkInterfaceCardApi(azureGroup).createOrUpdate(networkInterfaceCardName, locationName, networkInterfaceCardProperties, ImmutableMap.of("jclouds", "livetest")); - } - - private StorageProfile createStorageProfile(String name, Image image, String blob) { - ImageReference imageReference = ImageReference.builder() - .publisher(image.getProviderId()) - .offer(image.getName()) - .sku(image.getVersion()) - .version("latest") - .build(); - VHD vhd = VHD.create(blob + "vhds/" + name + ".vhd"); - OSDisk osDisk = OSDisk.create(null, name, vhd, "ReadWrite", "FromImage", null); - return StorageProfile.create(imageReference, osDisk, ImmutableList.<DataDisk>of()); } - @Override public Iterable<VMHardware> listHardwareProfiles() { @@ -295,10 +237,13 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual List<StorageService> storages = api.getStorageAccountApi(azureGroup).list(); for (StorageService storage : storages) { String name = storage.name(); - String key = api.getStorageAccountApi(azureGroup).getKeys(name).key1(); + StorageService storageService = api.getStorageAccountApi(azureGroup).get(name); + if (storageService != null && storageService.storageServiceProperties().provisioningState().equals("Succeded")) { + String key = api.getStorageAccountApi(azureGroup).getKeys(name).key1(); List<VMImage> images = BlobHelper.getImages("jclouds", azureGroup, storage.name(), key, - "custom", storage.location()); + "custom", storage.location()); osImages.addAll(images); + } } return osImages; } @@ -394,14 +339,81 @@ public class AzureComputeServiceAdapter implements ComputeServiceAdapter<Virtual @Override public Iterable<VirtualMachine> listNodesByIds(final Iterable<String> ids) { - return null; // TODO - /* - return Iterables.filter(listNodes(), new Predicate<VMDeployment>() { + return filter(listNodes(), new Predicate<VirtualMachine>() { @Override - public boolean apply(final VMDeployment input) { - return Iterables.contains(ids, input.virtualMachine().name()); + public boolean apply(VirtualMachine virtualMachine) { + return contains(ids, virtualMachine.id()); } }); - */ + } + + + private OSProfile createOsProfile(String computerName, AzureTemplateOptions templateOptions) { + Iterable<String> splittedImageLoginUser = Splitter.on(":").split(AzureComputeProviderMetadata.defaultProperties().getProperty(IMAGE_LOGIN_USER)); + String defaultLoginUser = Iterables.get(splittedImageLoginUser, 0); + String defaultLoginPassword = Iterables.get(splittedImageLoginUser, 1); + String adminUsername = Objects.firstNonNull(templateOptions.getLoginUser(), defaultLoginUser); + String adminPassword = Objects.firstNonNull(templateOptions.getLoginPassword(), defaultLoginPassword); + OSProfile.Builder builder = OSProfile.builder().adminUsername(adminUsername).computerName(computerName); + // prefer public key over password + if (templateOptions.getPublicKey() != null) { + OSProfile.LinuxConfiguration linuxConfiguration = OSProfile.LinuxConfiguration.create("true", + OSProfile.LinuxConfiguration.SSH.create(ImmutableList.of( + OSProfile.LinuxConfiguration.SSH.SSHPublicKey.create( + String.format("/home/%s/.ssh/authorized_keys", adminUsername), + templateOptions.getPublicKey()) + )) + ); + builder.linuxConfiguration(linuxConfiguration); + } else { + builder.adminPassword(adminPassword); + } + return builder.build(); + } + + private NetworkInterfaceCard createNetworkInterfaceCard(String subnetId, String name, String locationName) { + final PublicIPAddressApi ipApi = api.getPublicIPAddressApi(azureGroup); + + PublicIPAddressProperties properties = + PublicIPAddressProperties.builder() + .publicIPAllocationMethod("Static") + .idleTimeoutInMinutes(4) + .build(); + + String publicIpAddressName = "public-address-" + name; + PublicIPAddress ip = ipApi.createOrUpdate(publicIpAddressName, locationName, ImmutableMap.of("jclouds", name), properties); + retry(new Predicate<String>() { + @Override public boolean apply(String name) { + return api.getPublicIPAddressApi(azureGroup).get(name).properties().provisioningState().equals("Succeeded"); + } + }, 10 * 1000).apply(publicIpAddressName); + + final NetworkInterfaceCardProperties networkInterfaceCardProperties = + NetworkInterfaceCardProperties.builder() + .ipConfigurations(ImmutableList.of( + IpConfiguration.builder() + .name("ipConfig-" + name) + .properties(IpConfigurationProperties.builder() + .privateIPAllocationMethod("Dynamic") + .publicIPAddress(IdReference.create(ip.id())) + .subnet(IdReference.create(subnetId)) + .build()) + .build())) + .build(); + + String networkInterfaceCardName = "jc-nic-" + name; + return api.getNetworkInterfaceCardApi(azureGroup).createOrUpdate(networkInterfaceCardName, locationName, networkInterfaceCardProperties, ImmutableMap.of("jclouds", name)); + } + + private StorageProfile createStorageProfile(String name, Image image, String blob) { + ImageReference imageReference = ImageReference.builder() + .publisher(image.getProviderId()) + .offer(image.getName()) + .sku(image.getVersion()) + .version("latest") + .build(); + VHD vhd = VHD.create(blob + "vhds/" + name + ".vhd"); + OSDisk osDisk = OSDisk.create(null, name, vhd, "ReadWrite", "FromImage", null); + return StorageProfile.create(imageReference, osDisk, ImmutableList.<DataDisk>of()); } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/5b832bfc/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourceGroupThenCreateNodes.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourceGroupThenCreateNodes.java b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourceGroupThenCreateNodes.java index a7900ec..0840689 100644 --- a/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourceGroupThenCreateNodes.java +++ b/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourceGroupThenCreateNodes.java @@ -175,14 +175,15 @@ public class CreateResourceGroupThenCreateNodes extends CreateNodesWithGroupEnco StorageService storageService = api.getStorageAccountApi(resourceGroupName).get(storageAccountName); if (storageService != null) return storageService; - URI uri = api.getStorageAccountApi(resourceGroupName).create(storageAccountName, locationName, ImmutableMap.of("property_name", - "property_value"), ImmutableMap.of("accountType", StorageService.AccountType.Standard_LRS.toString())); + URI uri = api.getStorageAccountApi(resourceGroupName).create(storageAccountName, locationName, ImmutableMap.of("jclouds", + name), ImmutableMap.of("accountType", StorageService.AccountType.Standard_LRS.toString())); retry(new Predicate<URI>() { @Override public boolean apply(URI uri) { return ParseJobStatus.JobStatus.DONE == api.getJobApi().jobStatus(uri); } - }, 60 * 1 * 1000 /* 1 minute timeout */).apply(uri); + }, 60 * 2 * 1000 /* 2 minutes timeout */).apply(uri); + // TODO check provisioning state of the primary storageService = api.getStorageAccountApi(resourceGroupName).get(storageAccountName); return storageService; } @@ -199,20 +200,18 @@ public class CreateResourceGroupThenCreateNodes extends CreateNodesWithGroupEnco * If sanitized name is more than 24 characters, storage account is first 10 chars of sanitized name plus 4 random chars plus last 10 chars of sanitized name. */ public static String generateStorageAccountName(String name) { - String storageAccountName = name.replaceAll("[^a-z0-9]", ""); - int nameLength = storageAccountName.length(); + String random = UUID.randomUUID().toString().substring(0, 4); + String storageAccountName = new StringBuilder().append(name).append(random).toString(); + String sanitizedStorageAccountName = storageAccountName.replaceAll("[^a-z0-9]", ""); + int nameLength = sanitizedStorageAccountName.length(); if (nameLength >= 3 && nameLength <= 24) { - return storageAccountName; + return sanitizedStorageAccountName; } - String random = UUID.randomUUID().toString().replaceAll("[^a-z0-9]", "").substring(0, 4); - if (nameLength < 3) { - storageAccountName = new StringBuilder().append(storageAccountName).append(random).toString(); - } if (nameLength > 24) { - storageAccountName = shorten(storageAccountName, random); + sanitizedStorageAccountName = shorten(storageAccountName, random); } - return storageAccountName; + return sanitizedStorageAccountName; } private static String shorten(String storageAccountName, String random) { http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/5b832bfc/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceLiveTest.java index eaa9ac2..81edddb 100644 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceLiveTest.java +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/AzureComputeServiceLiveTest.java @@ -22,14 +22,12 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; -import org.jclouds.azurecompute.arm.compute.options.AzureTemplateOptions; import org.jclouds.azurecompute.arm.internal.AzureLiveTestUtils; import org.jclouds.compute.RunScriptOnNodesException; import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.Template; -import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.internal.BaseComputeServiceLiveTest; import org.jclouds.compute.predicates.NodePredicates; import org.jclouds.domain.LoginCredentials; @@ -93,8 +91,8 @@ public class AzureComputeServiceLiveTest extends BaseComputeServiceLiveTest { properties.setProperty(TIMEOUT_PORT_OPEN, scriptTimeout + ""); properties.setProperty(TIMEOUT_NODE_TERMINATED, scriptTimeout + ""); properties.setProperty(TIMEOUT_NODE_SUSPENDED, scriptTimeout + ""); - properties.put(RESOURCE_GROUP_NAME, "a4"); - properties.put(TEMPLATE, "locationId=northeurope"); + properties.put(RESOURCE_GROUP_NAME, "j5s"); + properties.put(TEMPLATE, "osFamily=UBUNTU,os64Bit=true,osVersionMatches=16.04.0-LTS,locationId=northeurope"); properties.put(IMAGE_PUBLISHERS, "Canonical"); AzureLiveTestUtils.defaultProperties(properties); @@ -104,14 +102,6 @@ public class AzureComputeServiceLiveTest extends BaseComputeServiceLiveTest { } @Override - protected Template buildTemplate(TemplateBuilder templateBuilder) { - Template template = templateBuilder.imageVersionMatches("16.04.0-LTS").build(); - AzureTemplateOptions options = template.getOptions().as(AzureTemplateOptions.class); - options.authorizePublicKey("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNnvPZ25wZK19grrbal6R5JP1hLRBUuNh86KxZVMAFkGd5ouVSgO9dFajHZ45Q4mbaTkdOiqf7otMLDzkaztEa7oLK7Jso0Y0LOi+nT4gf38rvbEF5mq069G9b9XqlNleaGnpLuTN54iEK8c4TWZxIJqgelHEHhjp7V1asmilBbpZDmwA5cTt9vGJIhqA/BptKH3folZKeAQjRa1ZRSwSQUnk9rBKn4PKSRDojVa9A9jKt4qboJh5q7ZjtE8z+665F/4TgLzElTXUA8+uUFGpuynMSmQEt301e18dXAl+vBr8fMiThcoVVbdVdqdjXsw75fMXPAgqhjrw8k3+0/4P9 [email protected]"); - return template; - } - - @Override protected Template refreshTemplate() { return this.template = addRunScriptToTemplate(this.buildTemplate(this.client.templateBuilder())); }
