use of org.openstack4j.model.compute.ServerCreate in project airavata by apache.
the class OpenstackIntfImpl method createServer.
@Override
public Server createServer(String serverName, String imageId, String flavorId, String keyPairName) {
try {
Server newServer = null;
String networkId = null;
// Adhering to openstack format of subnet names 'subnet-<name>'.
String networkName = "subnet-" + properties.getProperty(Constants.OS_NETWORK_NAME);
for (Subnet net : os.networking().subnet().list()) {
if (net.getName().equals(networkName)) {
networkId = net.getNetworkId();
logger.info("Using network " + networkName + " with ID: " + networkId);
break;
}
}
if (networkId != null) {
List<String> srvNet = new LinkedList<String>();
srvNet.add(networkId);
ServerCreate sc = Builders.server().name(serverName).flavor(flavorId).image(imageId).networks(srvNet).keypairName(keyPairName).build();
// Boot the Server
newServer = os.compute().servers().boot(sc);
logger.info("New server created with ID: " + newServer.getId());
} else {
logger.error("Network with name " + networkName + " not found.");
}
return newServer;
} catch (Exception ex) {
ex.printStackTrace();
// TODO: Check with the team on how to handle exceptions.
logger.error("Failed to create server.");
return null;
}
}
use of org.openstack4j.model.compute.ServerCreate in project cloudbreak by hortonworks.
the class OpenStackInstanceBuilder method build.
@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
CloudResource resource = buildableResource.get(0);
try {
OSClient<?> osClient = createOSClient(auth);
InstanceTemplate template = getInstanceTemplate(group, privateId);
CloudResource port = getPort(context.getComputeResources(privateId));
KeystoneCredentialView osCredential = new KeystoneCredentialView(auth);
NovaInstanceView novaInstanceView = new NovaInstanceView(context.getName(), template, group.getType(), group.getLoginUserName());
String imageId = osClient.imagesV2().list(Collections.singletonMap("name", image.getImageName())).get(0).getId();
LOGGER.info("Selected image id: {}", imageId);
Map<String, String> metadata = mergeMetadata(novaInstanceView.getMetadataMap(), tags);
ServerCreateBuilder serverCreateBuilder = Builders.server().name(resource.getName()).image(imageId).flavor(getFlavorId(osClient, novaInstanceView.getFlavor())).keypairName(osCredential.getKeyPairName()).addMetadata(metadata).addNetworkPort(port.getStringParameter(OpenStackConstants.PORT_ID)).userData(new String(Base64.encodeBase64(image.getUserDataByType(group.getType()).getBytes())));
BlockDeviceMappingBuilder blockDeviceMappingBuilder = Builders.blockDeviceMapping().uuid(imageId).sourceType(BDMSourceType.IMAGE).deviceName("/dev/vda").bootIndex(0).deleteOnTermination(true).destinationType(BDMDestType.LOCAL);
serverCreateBuilder = serverCreateBuilder.blockDevice(blockDeviceMappingBuilder.build());
for (CloudResource computeResource : context.getComputeResources(privateId)) {
if (computeResource.getType() == ResourceType.OPENSTACK_ATTACHED_DISK) {
BlockDeviceMappingCreate blockDeviceMappingCreate = Builders.blockDeviceMapping().uuid(computeResource.getReference()).deviceName(computeResource.getStringParameter(OpenStackConstants.VOLUME_MOUNT_POINT)).sourceType(BDMSourceType.VOLUME).destinationType(BDMDestType.VOLUME).build();
serverCreateBuilder.blockDevice(blockDeviceMappingCreate);
}
}
ServerCreate serverCreate = serverCreateBuilder.build();
Server server = osClient.compute().servers().boot(serverCreate);
return Collections.singletonList(createPersistedResource(resource, group.getName(), server.getId(), Collections.singletonMap(OpenStackConstants.SERVER, server)));
} catch (OS4JException ex) {
LOGGER.error("Failed to create OpenStack instance with privateId: {}", privateId, ex);
throw new OpenStackResourceException("Instance creation failed", resourceType(), resource.getName(), ex);
}
}
use of org.openstack4j.model.compute.ServerCreate in project camel by apache.
the class ServerProducer method messageToServer.
private ServerCreate messageToServer(Message message) {
ServerCreate serverCreate = message.getBody(ServerCreate.class);
if (serverCreate == null) {
Map headers = message.getHeaders();
ServerCreateBuilder builder = Builders.server();
ObjectHelper.notEmpty(message.getHeader(OpenstackConstants.NAME, String.class), "Name");
builder.name(message.getHeader(OpenstackConstants.NAME, String.class));
if (headers.containsKey(NovaConstants.IMAGE_ID)) {
builder.image(message.getHeader(NovaConstants.IMAGE_ID, String.class));
}
if (headers.containsKey(NovaConstants.NETWORK)) {
builder.networks(message.getHeader(NovaConstants.NETWORK, List.class));
}
if (headers.containsKey(NovaConstants.FLAVOR_ID)) {
builder.flavor(message.getHeader(NovaConstants.FLAVOR_ID, String.class));
}
if (headers.containsKey(NovaConstants.KEYPAIR_NAME)) {
builder.keypairName(message.getHeader(NovaConstants.KEYPAIR_NAME, String.class));
}
if (headers.containsKey(NovaConstants.ADMIN_PASSWORD)) {
builder.addAdminPass(message.getHeader(NovaConstants.ADMIN_PASSWORD, String.class));
}
serverCreate = builder.build();
}
return serverCreate;
}
use of org.openstack4j.model.compute.ServerCreate in project camel by apache.
the class ServerProducer method doCreate.
private void doCreate(Exchange exchange) {
final ServerCreate in = messageToServer(exchange.getIn());
final Server out = os.compute().servers().boot(in);
exchange.getIn().setBody(out);
}
Aggregations