use of com.sequenceiq.cloudbreak.cloud.model.CloudResource 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 com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.
the class OpenStackContextBuilder method contextInit.
@Override
public OpenStackContext contextInit(CloudContext cloudContext, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
OpenStackContext openStackContext = new OpenStackContext(utils.getStackName(auth), cloudContext.getLocation(), PARALLEL_RESOURCE_REQUEST, build);
if (openStackClient.isV2Keystone(auth)) {
String v2TenantId = openStackClient.getV2TenantId(auth);
openStackContext.putParameter(OpenStackConstants.TENANT_ID, v2TenantId);
} else {
throw new CloudConnectorException("In case on native openstack api only V2 keystone is supported");
}
if (resources != null) {
for (CloudResource resource : resources) {
switch(resource.getType()) {
case OPENSTACK_SUBNET:
openStackContext.putParameter(OpenStackConstants.SUBNET_ID, resource.getReference());
break;
case OPENSTACK_NETWORK:
openStackContext.putParameter(OpenStackConstants.NETWORK_ID, resource.getReference());
break;
case OPENSTACK_SECURITY_GROUP:
openStackContext.addGroupResources(resource.getGroup(), Collections.singletonList(resource));
break;
default:
LOGGER.debug("Resource is not used during context build: {}", resource);
}
}
}
openStackContext.putParameter(OpenStackConstants.FLOATING_IP_IDS, Collections.synchronizedList(new ArrayList<String>()));
if (network != null) {
openStackContext.putParameter(OpenStackConstants.PUBLIC_NET_ID, network.getStringParameter(OpenStackConstants.PUBLIC_NET_ID));
}
return openStackContext;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.
the class OpenStackResourceConnector method launch.
@SuppressWarnings("unchecked")
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier notifier, AdjustmentType adjustmentType, Long threshold) {
String stackName = utils.getStackName(authenticatedContext);
NeutronNetworkView neutronNetworkView = new NeutronNetworkView(stack.getNetwork());
boolean existingNetwork = neutronNetworkView.isExistingNetwork();
String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
ModelContext modelContext = new ModelContext();
modelContext.withExistingNetwork(existingNetwork);
modelContext.withExistingSubnet(existingSubnetCidr != null);
modelContext.withGroups(stack.getGroups());
modelContext.withInstanceUserData(stack.getImage());
modelContext.withLocation(authenticatedContext.getCloudContext().getLocation());
modelContext.withStackName(stackName);
modelContext.withNeutronNetworkView(neutronNetworkView);
modelContext.withTemplateString(stack.getTemplate());
modelContext.withTags(stack.getTags());
String heatTemplate = heatTemplateBuilder.build(modelContext);
Map<String, String> parameters = heatTemplateBuilder.buildParameters(authenticatedContext, stack, existingNetwork, existingSubnetCidr);
OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
List<CloudResourceStatus> resources;
Stack existingStack = client.heat().stacks().getStackByName(stackName);
if (existingStack == null) {
if (stack.getInstanceAuthentication().getPublicKeyId() == null) {
createKeyPair(authenticatedContext, stack, client);
}
Stack heatStack = client.heat().stacks().create(Builders.stack().name(stackName).template(heatTemplate).disableRollback(false).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build());
List<CloudResource> cloudResources = collectResources(authenticatedContext, notifier, heatStack, stack, neutronNetworkView);
resources = check(authenticatedContext, cloudResources);
} else {
LOGGER.info("Heat stack already exists: {}", existingStack.getName());
CloudResource cloudResource = new Builder().type(ResourceType.HEAT_STACK).name(existingStack.getId()).build();
resources = Collections.singletonList(new CloudResourceStatus(cloudResource, ResourceStatus.CREATED));
}
LOGGER.debug("Launched resources: {}", resources);
return resources;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.
the class OpenStackResourceConnector method updateHeatStack.
private List<CloudResourceStatus> updateHeatStack(AuthenticatedContext authenticatedContext, List<CloudResource> resources, String heatTemplate, Map<String, String> parameters) {
CloudResource resource = utils.getHeatResource(resources);
String stackName = utils.getStackName(authenticatedContext);
String heatStackId = resource.getName();
OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
StackUpdate updateRequest = Builders.stackUpdate().template(heatTemplate).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build();
client.heat().stacks().update(stackName, heatStackId, updateRequest);
LOGGER.info("Heat stack update request sent with stack name: '{}' for Heat stack: '{}'", stackName, heatStackId);
return check(authenticatedContext, resources);
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.
the class AbstractOpenStackResourceBuilder method checkResources.
protected List<CloudResourceStatus> checkResources(ResourceType type, OpenStackContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
List<CloudResourceStatus> result = new ArrayList<>();
for (CloudResource resource : resources) {
LOGGER.info("Check {} resource: {}", type, resource);
try {
boolean finished = checkStatus(context, auth, resource);
ResourceStatus successStatus = context.isBuild() ? ResourceStatus.CREATED : ResourceStatus.DELETED;
result.add(new CloudResourceStatus(resource, finished ? successStatus : ResourceStatus.IN_PROGRESS));
if (finished) {
if (successStatus == ResourceStatus.CREATED) {
LOGGER.info("Creation of {} was successful", resource);
} else {
LOGGER.info("Deletion of {} was successful", resource);
}
}
} catch (OS4JException ex) {
throw new OpenStackResourceException("Error during status check", type, resource.getName(), ex);
}
}
return result;
}
Aggregations