forked from yegor256/rultor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[yegor256#1877] StopsInstanse and StartsInstance are created
- Loading branch information
Showing
9 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
129
src/main/java/com/rultor/agents/aws/AwsEc2Instance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.