Search in sources :

Example 6 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project solarnetwork-central by SolarNetwork.

the class AwsVirtualMachineBiz method changeVirtualMachinesState.

@Override
public void changeVirtualMachinesState(Set<String> machineIds, VirtualMachineState desiredState) {
    AmazonEC2 client = getEc2Client();
    List<InstanceStateChange> results = null;
    if (desiredState == VirtualMachineState.Running) {
        StartInstancesRequest req = new StartInstancesRequest().withInstanceIds(machineIds);
        StartInstancesResult res = client.startInstances(req);
        results = res.getStartingInstances();
    } else if (desiredState == VirtualMachineState.Stopped) {
        StopInstancesRequest req = new StopInstancesRequest().withInstanceIds(machineIds);
        StopInstancesResult res = client.stopInstances(req);
        results = res.getStoppingInstances();
    } else {
        throw new IllegalArgumentException("Desired state not supported: " + desiredState);
    }
    log.info("Changed EC2 instances {} desired state to {}: {}", machineIds, desiredState, results);
}
Also used : StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 7 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project gocd-ecs-elastic-agent by gocd.

the class StopOperationTest method shouldStopInstance.

@Test
void shouldStopInstance() {
    final ContainerInstance instanceToDeregister = containerInstance("i-abcde12", "container-instance-arn");
    ;
    final ArgumentCaptor<StopInstancesRequest> stopInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(StopInstancesRequest.class);
    when(ec2Client.stopInstances(stopInstancesRequestArgumentCaptor.capture())).thenReturn(new StopInstancesResult());
    new StopOperation().execute(pluginSettings, instanceToDeregister);
    final StopInstancesRequest stopInstancesRequest = stopInstancesRequestArgumentCaptor.getValue();
    assertThat(stopInstancesRequest.getInstanceIds()).hasSize(1).contains("i-abcde12");
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 8 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.

the class AmazonEC2ClientMock method stopInstances.

@Override
public StopInstancesResult stopInstances(StopInstancesRequest stopInstancesRequest) {
    StopInstancesResult result = new StopInstancesResult();
    if (stopInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
        Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
        InstanceStateChange sc = new InstanceStateChange();
        InstanceState previousState = new InstanceState();
        previousState.setCode(80);
        previousState.setName(InstanceStateName.Running);
        InstanceState newState = new InstanceState();
        newState.setCode(16);
        newState.setName(InstanceStateName.Stopped);
        sc.setPreviousState(previousState);
        sc.setCurrentState(newState);
        sc.setInstanceId("test-1");
        coll.add(sc);
        result.setStoppingInstances(coll);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 9 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project photon-model by vmware.

the class AWSPowerService method powerOff.

private void powerOff(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
    OperationContext opContext = OperationContext.getOperationContext();
    StopInstancesRequest request = new StopInstancesRequest();
    request.withInstanceIds(c.child.id);
    client.stopInstancesAsync(request, new AsyncHandler<StopInstancesRequest, StopInstancesResult>() {

        @Override
        public void onSuccess(StopInstancesRequest request, StopInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
                OperationContext.restoreOperationContext(opContext);
                if (e != null) {
                    onError(e);
                    return;
                }
                updateComputeState(pr, c);
            });
        }

        @Override
        public void onError(Exception e) {
            OperationContext.restoreOperationContext(opContext);
            c.taskManager.patchTaskToFailure(e);
        }
    });
}
Also used : OperationContext(com.vmware.xenon.common.OperationContext) ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) OperationContext(com.vmware.xenon.common.OperationContext) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OFF(com.vmware.photon.controller.model.resources.ComputeService.PowerState.OFF) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Example 10 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project photon-model by vmware.

the class AWSResetService method reset.

private void reset(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
    if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
        logWarning(() -> String.format("Cannot perform a reset on this EC2 instance. " + "The machine should be in powered on state"));
        c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
        return;
    }
    // The stop action for reset is a force stop. So we use the withForce method to set the force parameter to TRUE
    // This is similar to unplugging the machine from the power circuit.
    // The OS and the applications are forcefully stopped.
    StopInstancesRequest stopRequest = new StopInstancesRequest();
    stopRequest.withInstanceIds(c.child.id).withForce(Boolean.TRUE);
    client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {

        @Override
        protected void handleError(Exception e) {
            c.taskManager.patchTaskToFailure(e);
        }

        @Override
        protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
                if (e != null) {
                    onError(e);
                    return;
                }
                // Instances will be started only if they're successfully stopped
                startInstance(client, c);
            });
        }
    });
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) FactoryService(com.vmware.xenon.common.FactoryService) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) ResourceType(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceType) ResourceOperationUtils(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Aggregations

StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)13 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)10 ArrayList (java.util.ArrayList)6 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)5 StartInstancesResult (com.amazonaws.services.ec2.model.StartInstancesResult)5 Collection (java.util.Collection)4 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 Instance (com.amazonaws.services.ec2.model.Instance)2 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)2 InstanceState (com.amazonaws.services.ec2.model.InstanceState)2 Reservation (com.amazonaws.services.ec2.model.Reservation)2 Volume (com.amazonaws.services.ec2.model.Volume)2 AWSClientManager (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager)2 AWSClientManagerFactory (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory)2 BaseAdapterStage (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage)2