Skip to content

Commit

Permalink
#1877 one ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 27, 2024
1 parent 366f5de commit 8d70e40
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 57 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/rultor/agents/aws/AwsEc2Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.rultor.agents.aws;

import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
Expand Down Expand Up @@ -127,8 +128,8 @@ public AwsEc2Instance run() {
);
final RunInstancesResult response =
this.api.aws().runInstances(request);
final String iid = response.getReservation().getInstances().get(0).getInstanceId();
Logger.info(this, "AWS instance %s launched", iid);
return new AwsEc2Instance(this.api, iid);
final Instance instance = response.getReservation().getInstances().get(0);
Logger.info(this, "AWS instance %s launched", instance.getInstanceId());
return new AwsEc2Instance(this.api, instance);
}
}
46 changes: 0 additions & 46 deletions src/main/java/com/rultor/agents/aws/AwsEc2Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@
*/
package com.rultor.agents.aws;

import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.CreateTagsRequest;
import com.amazonaws.services.ec2.model.DryRunResult;
import com.amazonaws.services.ec2.model.DryRunSupportedRequest;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import lombok.ToString;

/**
Expand Down Expand Up @@ -84,47 +79,6 @@ public AwsEc2Instance(final AwsEc2 api, final Instance inst) {
this.id = this.instance.getInstanceId();
}

/**
* Ctor.
* @param api AwsEc2 api client
* @param id Instance id
*/
public AwsEc2Instance(final AwsEc2 api, final String id) {
if (id.isEmpty()) {
throw new IllegalArgumentException(
"Instance id is mandatory"
);
}
this.api = api;
this.id = id;
this.instance = new Instance().withInstanceId(id);
}

/**
* Stop instance.
*/
public void stop() {
final DryRunSupportedRequest<StopInstancesRequest> draft = () -> {
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(this.instance.getInstanceId());
return request.getDryRunRequest();
};
final AmazonEC2 client = this.api.aws();
final DryRunResult<StopInstancesRequest> response =
client.dryRun(draft);
if (!response.isSuccessful()) {
Logger.error(
this,
"Failed dry run to stop instance %s", this.id
);
throw response.getDryRunResponse();
}
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(this.id);
client.stopInstances(request);
Logger.info("Successfully stopped instance %s", this.id);
}

/**
* Add a tag for instance.
* @param key Tag name
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/rultor/agents/aws/StartsInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ public Iterable<Directive> process(final XML xml) throws IOException {
"SSH key is empty, it's a mistake"
);
}
final AwsEc2Instance inst = this.image.run();
final AwsEc2Instance instance = this.image.run();
if (!this.tag.isEmpty()) {
inst.tag("Name", this.tag);
instance.tag("Name", this.tag);
}
Logger.info(
this, "EC2 instance %s on %s started in %s",
inst.id(), inst.address(),
instance.id(), instance.address(),
xml.xpath("/talk/@name").get(0)
);
dirs.xpath("/talk").add("ec2")
.attr("id", inst.id());
.attr("id", instance.id());
dirs.xpath("/talk").add("shell")
.attr("id", hash)
.add("host").set(inst.address()).up()
.add("host").set(instance.address()).up()
.add("port").set(Integer.toString(this.shell.port())).up()
.add("login").set(login).up()
.add("key").set(key);
Expand All @@ -151,4 +151,5 @@ public Iterable<Directive> process(final XML xml) throws IOException {
}
return dirs;
}

}
35 changes: 32 additions & 3 deletions src/main/java/com/rultor/agents/aws/StopsInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
package com.rultor.agents.aws;

import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DryRunResult;
import com.amazonaws.services.ec2.model.DryRunSupportedRequest;
import com.amazonaws.services.ec2.model.StopInstancesRequest;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
Expand Down Expand Up @@ -66,9 +70,34 @@ public StopsInstance(final AwsEc2 api) {

@Override
public Iterable<Directive> process(final XML xml) throws IOException {
final String inst = xml.xpath("/talk/ec2/@id").get(0);
new AwsEc2Instance(this.api, inst).stop();
Logger.info(this, "AWS instance %s stopped", inst);
final String instance = xml.xpath("/talk/ec2/@id").get(0);
this.stop(instance);
return new Directives().xpath("/talk/ec2").strict(1).remove();
}

/**
* Stop instance.
* @param instance Instance ID
*/
private void stop(final String instance) {
final DryRunSupportedRequest<StopInstancesRequest> draft = () -> {
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(instance);
return request.getDryRunRequest();
};
final AmazonEC2 client = this.api.aws();
final DryRunResult<StopInstancesRequest> response =
client.dryRun(draft);
if (!response.isSuccessful()) {
Logger.error(
this,
"Failed dry run to stop instance %s", instance
);
throw response.getDryRunResponse();
}
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(instance);
client.stopInstances(request);
Logger.info("Successfully stopped instance %s", instance);
}
}

0 comments on commit 8d70e40

Please sign in to comment.