Skip to content

Commit

Permalink
[yegor256#1877] StopsInstanse and StartsInstance are created
Browse files Browse the repository at this point in the history
  • Loading branch information
pnatashap committed Feb 15, 2024
1 parent 02200d4 commit 10dff61
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.658</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ec2</artifactId>
<version>1.12.658</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.12.658</version>
</dependency>
<dependency>
<groupId>com.jcabi.incubator</groupId>
<artifactId>xembly</artifactId>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/rultor/agents/Agents.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import com.jcabi.s3.Region;
import com.jcabi.s3.retry.ReRegion;
import com.jcabi.ssh.Ssh;
import com.rultor.agents.aws.AwsEc2;
import com.rultor.agents.aws.AwsEc2Image;
import com.rultor.agents.aws.StartsInstance;
import com.rultor.agents.aws.StopsInstance;
import com.rultor.agents.daemons.ArchivesDaemon;
import com.rultor.agents.daemons.DismountDaemon;
import com.rultor.agents.daemons.DropsDaemon;
Expand Down Expand Up @@ -260,6 +264,15 @@ public Agent agent(final Talk talk, final Profile profile)
new QnSafe(question)
),
new StartsRequest(profile),
new StartsInstance(
new AwsEc2Image(
new AwsEc2(
Manifests.read("Rultor-EC2Key"),
Manifests.read("Rultor-EC2Secret")
),
Manifests.read("Rultor-EC2Image")
)
),
new RegistersShell(
profile,
Agents.HOST, Agents.PORT, Agents.LOGIN,
Expand Down Expand Up @@ -290,6 +303,12 @@ public Agent agent(final Talk talk, final Profile profile)
new ReleaseBinaries(this.github, profile),
new Dephantomizes(this.github),
new Reports(this.github),
new StopsInstance(
new AwsEc2(
Manifests.read("Rultor-EC2Key"),
Manifests.read("Rultor-EC2Secret")
)
),
new RemovesShell(),
new ArchivesDaemon(
new ReRegion(
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/com/rultor/agents/aws/AwsEc2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2009-2024 Yegor Bugayenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.aws;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.jcabi.aspects.Immutable;
import lombok.ToString;

/**
* Amazon EC2 client.
*
* @since 1.77
*/
@Immutable
@ToString
public final class AwsEc2 {
/**
* Builder to get client.
*/
private final transient AmazonEC2ClientBuilder client;

/**
* Ctor.
* @param key Key to ise api
* @param secret Secret to use api
*/
public AwsEc2(final String key, final String secret) {
this(key, secret, "us-east-1");
}

/**
* Ctor.
* @param key Key to use api
* @param secret Secret to use api
* @param region Region for instance run
*/
public AwsEc2(final String key, final String secret, final String region) {
this.client = AmazonEC2ClientBuilder.standard()
.withRegion(region)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(key, secret)
)
);
}

/**
* AWS EC2 client instance.
* @return AWS EC2 client
*/
public AmazonEC2 aws() {
return this.client.build();
}
}
111 changes: 111 additions & 0 deletions src/main/java/com/rultor/agents/aws/AwsEc2Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2009-2024 Yegor Bugayenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.aws;

import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.jcabi.aspects.Immutable;
import lombok.ToString;

/**
* Amazon EC2 instance image to run.
*
* @since 1.77
*/
@Immutable
@ToString
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public final class AwsEc2Image {
/**
* AWS Client.
*/
private final transient AwsEc2 api;

/**
* Amazon machine image id.
*/
private final String image;

/**
* AWS Instance type.
*/
private final transient InstanceType type;

/**
* Ctor.
* @param api AwsEc2 api client
* @param image Ec2 instance ami_id
*/
public AwsEc2Image(final AwsEc2 api, final String image) {
this(api, image, "t2.nano");
}

/**
* Ctor.
* @param api AwsEc2 api client
* @param image Ec2 instance ami_id
* @param type Instance type
*/
public AwsEc2Image(final AwsEc2 api, final String image,
final String type) {
if (image.isEmpty()) {
throw new IllegalArgumentException(
"Machine image id is mandatory"
);
}
if (type.isEmpty()) {
throw new IllegalArgumentException(
"Machine type is mandatory"
);
}
this.api = api;
this.image = image;
this.type = InstanceType.fromValue(type);
}

/**
* Run image.
* @return Instance_id
*/
public AwsEc2Instance run() {
final RunInstancesRequest request = new RunInstancesRequest()
.withImageId(this.image)
.withInstanceType(this.type)
.withMaxCount(1)
.withMinCount(1);
final RunInstancesResult response =
this.api.aws().runInstances(request);
return new AwsEc2Instance(
this.api,
response.getReservation().getInstances().get(0).getInstanceId()
);
}
}
129 changes: 129 additions & 0 deletions src/main/java/com/rultor/agents/aws/AwsEc2Instance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2009-2024 Yegor Bugayenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
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.StopInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import lombok.ToString;

/**
* Amazon EC2 instance.
*
* @since 1.77
*/
@Immutable
@ToString
@SuppressWarnings({"PMD.ShortMethodName",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors",
"PMD.AvoidFieldNameMatchingMethodName"
})
public final class AwsEc2Instance {
/**
* AWS Client.
*/
private final transient AwsEc2 api;

/**
* AWS Instance id.
*/
private final String id;

/**
* 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;
}

/**
* Stop instance.
*/
public void stop() {
final DryRunSupportedRequest<StopInstancesRequest> draft = () -> {
final StopInstancesRequest request = new StopInstancesRequest()
.withInstanceIds(this.id);
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 stop instance %s", this.id);
}

/**
* Add a tag for instance.
* @param key Tag name
* @param tag Tag value
* @return This instance
*/
public AwsEc2Instance tag(final String key, final String tag) {
final Tag awstag = new Tag()
.withKey(key)
.withValue(tag);
final CreateTagsRequest request = new CreateTagsRequest()
.withResources(this.id)
.withTags(awstag);
this.api.aws().createTags(request);
return this;
}

/**
* Instance id.
* @return Instance Id
* @checkstyle MethodNameCheck (3 lines)
*/
public String id() {
return this.id;
}
}
Loading

0 comments on commit 10dff61

Please sign in to comment.