use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class InstanceMetadataService method saveInstanceRequests.
public void saveInstanceRequests(Stack stack, Iterable<Group> groups) {
Set<InstanceGroup> instanceGroups = stack.getInstanceGroups();
for (Group group : groups) {
InstanceGroup instanceGroup = getInstanceGroup(instanceGroups, group.getName());
List<InstanceMetaData> existingInGroup = instanceMetaDataRepository.findAllByInstanceGroupAndInstanceStatus(instanceGroup, com.sequenceiq.cloudbreak.api.model.InstanceStatus.REQUESTED);
for (CloudInstance cloudInstance : group.getInstances()) {
InstanceTemplate instanceTemplate = cloudInstance.getTemplate();
boolean exists = existingInGroup.stream().anyMatch(i -> i.getPrivateId().equals(instanceTemplate.getPrivateId()));
if (InstanceStatus.CREATE_REQUESTED == instanceTemplate.getStatus() && !exists) {
InstanceMetaData instanceMetaData = new InstanceMetaData();
instanceMetaData.setPrivateId(instanceTemplate.getPrivateId());
instanceMetaData.setInstanceStatus(com.sequenceiq.cloudbreak.api.model.InstanceStatus.REQUESTED);
instanceMetaData.setInstanceGroup(instanceGroup);
instanceMetaDataRepository.save(instanceMetaData);
}
}
}
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class ServiceProviderMetadataAdapter method collectMetadata.
public List<CloudVmMetaDataStatus> collectMetadata(Stack stack) {
Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone()));
CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getOwner(), stack.getPlatformVariant(), location);
CloudCredential cloudCredential = credentialConverter.convert(stack.getCredential());
List<CloudInstance> cloudInstances = cloudStackConverter.buildInstances(stack);
List<CloudResource> cloudResources = cloudResourceConverter.convert(stack.getResources());
CollectMetadataRequest cmr = new CollectMetadataRequest(cloudContext, cloudCredential, cloudResources, cloudInstances);
LOGGER.info("Triggering event: {}", cmr);
eventBus.notify(CloudPlatformRequest.selector(CollectMetadataRequest.class), eventFactory.createEvent(cmr));
try {
CollectMetadataResult res = cmr.await();
LOGGER.info("Result: {}", res);
if (res.getErrorDetails() != null) {
LOGGER.error("Failed to collect metadata", res.getErrorDetails());
return Collections.emptyList();
}
return res.getResults();
} catch (InterruptedException e) {
LOGGER.error(format("Error while executing collectMetadata, stack: %s", cloudContext), e);
throw new OperationException(e);
}
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsResourceConnector method downscale.
@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Object resourcesToRemove) {
Collection<String> instanceIds = new ArrayList<>();
for (CloudInstance vm : vms) {
instanceIds.add(vm.getInstanceId());
}
String asGroupName = cfStackUtil.getAutoscalingGroupName(auth, vms.get(0).getTemplate().getGroupName(), auth.getCloudContext().getLocation().getRegion().value());
DetachInstancesRequest detachInstancesRequest = new DetachInstancesRequest().withAutoScalingGroupName(asGroupName).withInstanceIds(instanceIds).withShouldDecrementDesiredCapacity(true);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonASClient.detachInstances(detachInstancesRequest);
} catch (AmazonServiceException e) {
if (!"ValidationError".equals(e.getErrorCode()) || !e.getErrorMessage().contains("not part of Auto Scaling") || instanceIds.stream().anyMatch(id -> !e.getErrorMessage().contains(id))) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
} catch (AmazonServiceException e) {
if (!"InvalidInstanceID.NotFound".equals(e.getErrorCode())) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
LOGGER.info("Terminated instances in stack '{}': '{}'", auth.getCloudContext().getId(), instanceIds);
return check(auth, resources);
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsInstanceConnector method stop.
@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (String group : getGroups(vms)) {
Collection<String> instances = new ArrayList<>();
Collection<CloudInstance> cloudInstances = new ArrayList<>();
for (CloudInstance vm : vms) {
if (vm.getTemplate().getGroupName().equals(group)) {
instances.add(vm.getInstanceId());
cloudInstances.add(vm);
}
}
try {
instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Stopped");
if (!instances.isEmpty()) {
amazonEC2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instances));
}
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
}
} catch (RuntimeException e) {
LOGGER.error("Stop instances failed on AWS", e);
String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
}
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsInstanceConnector method start.
@Override
public List<CloudVmInstanceStatus> start(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (String group : getGroups(vms)) {
Collection<String> instances = new ArrayList<>();
Collection<CloudInstance> cloudInstances = new ArrayList<>();
for (CloudInstance vm : vms) {
if (vm.getTemplate().getGroupName().equals(group)) {
instances.add(vm.getInstanceId());
cloudInstances.add(vm);
}
}
try {
instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Running");
if (!instances.isEmpty()) {
amazonEC2Client.startInstances(new StartInstancesRequest().withInstanceIds(instances));
}
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
}
} catch (RuntimeException e) {
LOGGER.error("Start instances failed on AWS", e);
String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
}
}
}
return statuses;
}
Aggregations