use of com.amazonaws.services.ec2.model.InstanceStateChange in project photon-model by vmware.
the class AWSUtils method waitForTransitionCompletion.
public static void waitForTransitionCompletion(ServiceHost host, List<InstanceStateChange> stateChangeList, final String desiredState, AmazonEC2AsyncClient client, BiConsumer<InstanceState, Exception> callback) {
InstanceStateChange stateChange = stateChangeList.get(0);
try {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.withInstanceIds(stateChange.getInstanceId());
DescribeInstancesResult result = client.describeInstances(request);
Instance instance = result.getReservations().stream().flatMap(r -> r.getInstances().stream()).filter(i -> i.getInstanceId().equalsIgnoreCase(stateChange.getInstanceId())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s instance not found", stateChange.getInstanceId())));
String state = instance.getState().getName();
if (state.equals(desiredState)) {
callback.accept(instance.getState(), null);
} else {
host.schedule(() -> waitForTransitionCompletion(host, stateChangeList, desiredState, client, callback), 5, TimeUnit.SECONDS);
}
} catch (AmazonServiceException | IllegalArgumentException ase) {
callback.accept(null, ase);
}
}
use of com.amazonaws.services.ec2.model.InstanceStateChange in project onebusaway-application-modules by camsys.
the class BundleServerServiceImpl method stop.
@Override
public String stop(String instanceId) {
if (!_isAws) {
return LOCAL_HOST;
}
if (LOCAL_HOST.equalsIgnoreCase(instanceId)) {
return instanceId;
}
List<String> instances = new ArrayList<String>();
instances.add(instanceId);
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest(instances);
StopInstancesResult stopInstancesResult = _ec2.stopInstances(stopInstancesRequest);
InstanceStateChange change = null;
if (!stopInstancesResult.getStoppingInstances().isEmpty()) {
change = stopInstancesResult.getStoppingInstances().get(0);
_log.info("from state=" + change.getPreviousState() + " to state=" + change.getCurrentState());
return change.getInstanceId();
}
return null;
}
use of com.amazonaws.services.ec2.model.InstanceStateChange in project onebusaway-application-modules by camsys.
the class BundleServerServiceImpl method start.
@Override
public String start(String instanceId) {
if (!_isAws) {
return LOCAL_HOST;
}
if (LOCAL_HOST.equalsIgnoreCase(instanceId)) {
return instanceId;
}
List<String> instances = new ArrayList<String>();
instances.add(instanceId);
StartInstancesRequest startInstancesRequest = new StartInstancesRequest(instances);
StartInstancesResult startInstancesResult = _ec2.startInstances(startInstancesRequest);
InstanceStateChange change = null;
if (!startInstancesResult.getStartingInstances().isEmpty()) {
change = startInstancesResult.getStartingInstances().get(0);
_log.info("from state=" + change.getPreviousState() + " to state=" + change.getCurrentState());
return change.getInstanceId();
}
return null;
}
Aggregations