mirror of https://github.com/apache/jclouds.git
Better documentation and a bugfix for cloud-init
This commit is contained in:
parent
c368bae28c
commit
a38cbd5803
|
@ -109,6 +109,7 @@ public class NovaComputeServiceAdapter implements
|
|||
options.securityGroupNames(templateOptions.getSecurityGroupNames().get());
|
||||
options.userData(templateOptions.getUserData());
|
||||
options.diskConfig(templateOptions.getDiskConfig());
|
||||
options.configDrive(templateOptions.getConfigDrive());
|
||||
options.networks(templateOptions.getNetworks());
|
||||
|
||||
Optional<String> privateKey = Optional.absent();
|
||||
|
|
|
@ -69,11 +69,13 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
eTo.generateKeyPair(shouldGenerateKeyPair());
|
||||
eTo.keyPairName(getKeyPairName());
|
||||
if (getUserData() != null) {
|
||||
eTo.userData(getUserData());
|
||||
eTo.userData(getUserData());
|
||||
}
|
||||
if (getDiskConfig() != null) {
|
||||
eTo.diskConfig(getDiskConfig());
|
||||
}
|
||||
}
|
||||
|
||||
eTo.configDrive(getConfigDrive());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,6 +85,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
protected String keyPairName;
|
||||
protected byte[] userData;
|
||||
protected String diskConfig;
|
||||
protected boolean configDrive;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
@ -96,12 +99,13 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
&& equal(this.generateKeyPair, that.generateKeyPair)
|
||||
&& equal(this.keyPairName, that.keyPairName)
|
||||
&& Arrays.equals(this.userData, that.userData)
|
||||
&& equal(this.diskConfig, that.diskConfig);
|
||||
&& equal(this.diskConfig, that.diskConfig)
|
||||
&& equal(this.configDrive, that.configDrive);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(super.hashCode(), autoAssignFloatingIp, securityGroupNames, generateKeyPair, keyPairName, userData, diskConfig);
|
||||
return Objects.hashCode(super.hashCode(), autoAssignFloatingIp, securityGroupNames, generateKeyPair, keyPairName, userData, diskConfig, configDrive);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -116,6 +120,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
toString.add("keyPairName", keyPairName);
|
||||
toString.add("userData", userData);
|
||||
toString.add("diskConfig", diskConfig);
|
||||
toString.add("configDrive", configDrive);
|
||||
return toString;
|
||||
}
|
||||
|
||||
|
@ -206,15 +211,22 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
}
|
||||
|
||||
public byte[] getUserData() {
|
||||
return userData;
|
||||
}
|
||||
return userData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see CreateServerOptions#getDiskConfig()
|
||||
*/
|
||||
public String getDiskConfig() {
|
||||
return diskConfig;
|
||||
}
|
||||
return diskConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see CreateServerOptions#getConfigDrive()
|
||||
*/
|
||||
public boolean getConfigDrive() {
|
||||
return configDrive;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
|
@ -376,6 +388,14 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
NovaTemplateOptions options = new NovaTemplateOptions();
|
||||
return NovaTemplateOptions.class.cast(options.diskConfig(diskConfig));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.jclouds.openstack.nova.v2_0.options.CreateServerOptions#getConfigDrive()
|
||||
*/
|
||||
public static NovaTemplateOptions configDrive(boolean configDrive) {
|
||||
NovaTemplateOptions options = new NovaTemplateOptions();
|
||||
return NovaTemplateOptions.class.cast(options.configDrive(configDrive));
|
||||
}
|
||||
}
|
||||
|
||||
// methods that only facilitate returning the correct object type
|
||||
|
@ -528,11 +548,11 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
* User data as bytes (not base64-encoded)
|
||||
*/
|
||||
public NovaTemplateOptions userData(byte[] userData) {
|
||||
// This limit may not be needed for nova
|
||||
checkArgument(checkNotNull(userData, "userData").length <= 16 * 1024,
|
||||
"userData cannot be larger than 16kb");
|
||||
this.userData = userData;
|
||||
return this;
|
||||
// This limit may not be needed for nova
|
||||
checkArgument(checkNotNull(userData, "userData").length <= 16 * 1024,
|
||||
"userData cannot be larger than 16kb");
|
||||
this.userData = userData;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -541,5 +561,18 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
|
|||
public NovaTemplateOptions diskConfig(String diskConfig) {
|
||||
this.diskConfig = diskConfig;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenStack can be configured to write metadata to a special configuration drive that will be
|
||||
* attached to the instance when it boots. The instance can retrieve any information that would
|
||||
* normally be available through the metadata service by mounting this disk and reading files from it.
|
||||
* To enable the config drive, set this parameter to "true".
|
||||
* This has to be enabled for user data cases.
|
||||
* @see CreateServerOptions#getConfigDrive()
|
||||
*/
|
||||
public NovaTemplateOptions configDrive(boolean configDrive) {
|
||||
this.configDrive = configDrive;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,6 +107,7 @@ public class CreateServerOptions implements MapBinder {
|
|||
private String diskConfig;
|
||||
private Set<String> networks = ImmutableSet.of();
|
||||
private String availabilityZone;
|
||||
private boolean configDrive;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
|
@ -119,7 +120,8 @@ public class CreateServerOptions implements MapBinder {
|
|||
&& equal(metadata, other.metadata) && equal(personality, other.personality)
|
||||
&& equal(adminPass, other.adminPass) && equal(diskConfig, other.diskConfig)
|
||||
&& equal(adminPass, other.adminPass) && equal(networks, other.networks)
|
||||
&& equal(availabilityZone, other.availabilityZone);
|
||||
&& equal(availabilityZone, other.availabilityZone)
|
||||
&& equal(configDrive, other.configDrive);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -127,7 +129,7 @@ public class CreateServerOptions implements MapBinder {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(keyName, securityGroupNames, metadata, personality, adminPass, networks, availabilityZone);
|
||||
return Objects.hashCode(keyName, securityGroupNames, metadata, personality, adminPass, networks, availabilityZone, configDrive);
|
||||
}
|
||||
|
||||
protected ToStringHelper string() {
|
||||
|
@ -147,6 +149,7 @@ public class CreateServerOptions implements MapBinder {
|
|||
if (!networks.isEmpty())
|
||||
toString.add("networks", networks);
|
||||
toString.add("availability_zone", availabilityZone == null ? null : availabilityZone);
|
||||
toString.add("configDrive", configDrive);
|
||||
return toString;
|
||||
}
|
||||
|
||||
|
@ -171,6 +174,8 @@ public class CreateServerOptions implements MapBinder {
|
|||
@Named("OS-DCF:diskConfig")
|
||||
String diskConfig;
|
||||
Set<Map<String, String>> networks;
|
||||
@Named("config_drive")
|
||||
String configDrive;
|
||||
|
||||
private ServerRequest(String name, String imageRef, String flavorRef) {
|
||||
this.name = name;
|
||||
|
@ -195,6 +200,8 @@ public class CreateServerOptions implements MapBinder {
|
|||
server.availabilityZone = availabilityZone;
|
||||
if (userData != null)
|
||||
server.user_data = base64().encode(userData);
|
||||
if (configDrive == true)
|
||||
server.configDrive = "true";
|
||||
if (securityGroupNames.size() > 0) {
|
||||
server.securityGroupNames = Sets.newLinkedHashSet();
|
||||
for (String groupName : securityGroupNames) {
|
||||
|
@ -290,12 +297,24 @@ public class CreateServerOptions implements MapBinder {
|
|||
* Custom user-data can be also be supplied at launch time.
|
||||
* It is retrievable by the instance and is often used for launch-time configuration
|
||||
* by instance scripts.
|
||||
* Pass userData unencdoed, as the value will be base64 encoded automatically.
|
||||
*/
|
||||
public CreateServerOptions userData(byte[] userData) {
|
||||
this.userData = userData;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to use a config drive for metadata.
|
||||
* This is a separate configuration drive that can be used separately from the metadata service.
|
||||
* This needs to be set to "true" when trying to use user data for cloud-init.
|
||||
* @see http://docs.openstack.org/grizzly/openstack-compute/admin/content/config-drive.html
|
||||
*/
|
||||
public CreateServerOptions configDrive(boolean configDrive) {
|
||||
this.configDrive = configDrive;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* A keypair name can be defined when creating a server. This key will be
|
||||
* linked to the server and used to SSH connect to the machine
|
||||
|
|
|
@ -131,6 +131,40 @@ public class NovaComputeServiceAdapterExpectTest extends BaseNovaComputeServiceC
|
|||
assertEquals(server.getNode().getServer().getDiskConfig().orNull(), Server.DISK_CONFIG_AUTO);
|
||||
}
|
||||
|
||||
public void testCreateNodeWithGroupEncodedIntoNameWithConfigDrive() throws Exception {
|
||||
|
||||
HttpRequest createServer = HttpRequest
|
||||
.builder()
|
||||
.method("POST")
|
||||
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers")
|
||||
.addHeader("Accept", "application/json")
|
||||
.addHeader("X-Auth-Token", authToken)
|
||||
.payload(payloadFromStringWithContentType(
|
||||
"{\"server\":{\"name\":\"test-e92\",\"imageRef\":\"1241\",\"flavorRef\":\"100\",\"config_drive\":\"true\"}}","application/json"))
|
||||
.build();
|
||||
|
||||
HttpResponse createServerResponse = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted")
|
||||
.payload(payloadFromResourceWithContentType("/new_server_config_drive.json","application/json; charset=UTF-8")).build();
|
||||
|
||||
Map<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
|
||||
.put(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess)
|
||||
.put(extensionsOfNovaRequest, extensionsOfNovaResponse)
|
||||
.put(listDetail, listDetailResponse)
|
||||
.put(listFlavorsDetail, listFlavorsDetailResponse)
|
||||
.put(createServer, createServerResponse)
|
||||
.put(serverDetail, serverDetailResponse).build();
|
||||
|
||||
Injector forConfigDrive = requestsSendResponses(requestResponseMap);
|
||||
|
||||
Template template = forConfigDrive.getInstance(TemplateBuilder.class).build();
|
||||
template.getOptions().as(NovaTemplateOptions.class).configDrive(true);
|
||||
|
||||
NovaComputeServiceAdapter adapter = forConfigDrive.getInstance(NovaComputeServiceAdapter.class);
|
||||
|
||||
NodeAndInitialCredentials<ServerInZone> server = adapter.createNodeWithGroupEncodedIntoName("test", "test-e92", template);
|
||||
assertNotNull(server);
|
||||
}
|
||||
|
||||
public void testCreateNodeWithGroupEncodedIntoNameWhenSecurityGroupsArePresent() throws Exception {
|
||||
|
||||
HttpRequest createServer = HttpRequest
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
"server": {
|
||||
"status": "BUILD(scheduling)",
|
||||
"updated": "2012-03-19T06:21:13Z",
|
||||
"hostId": "",
|
||||
"user_id": "54297837463082",
|
||||
"name": "test-e92",
|
||||
"links": [{
|
||||
"href": "https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/37936628937291/servers/71752",
|
||||
"rel": "self"
|
||||
}, {
|
||||
"href": "https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/servers/71752",
|
||||
"rel": "bookmark"
|
||||
}],
|
||||
"addresses": {},
|
||||
"tenant_id": "37936628937291",
|
||||
"image": {
|
||||
"id": "1241",
|
||||
"links": [{
|
||||
"href": "https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/images/1241",
|
||||
"rel": "bookmark"
|
||||
}]
|
||||
},
|
||||
"created": "2012-03-19T06:21:13Z",
|
||||
"uuid": "47491020-6a78-4f63-9475-23195ac4515c",
|
||||
"accessIPv4": "",
|
||||
"accessIPv6": "",
|
||||
"key_name": null,
|
||||
"adminPass": "ZWuHcmTMQ7eXoHeM",
|
||||
"flavor": {
|
||||
"id": "100",
|
||||
"links": [{
|
||||
"href": "https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/flavors/100",
|
||||
"rel": "bookmark"
|
||||
}]
|
||||
},
|
||||
"config_drive": "true",
|
||||
"id": 71752,
|
||||
"metadata": {},
|
||||
"OS-DCF:diskConfig": "AUTO"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue