Skip to content

Commit

Permalink
#1906 PrunesInstances
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 28, 2024
1 parent 2b9cb1c commit 37c858b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/rultor/agents/Agents.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.rultor.agents.aws.DescribesInstance;
import com.rultor.agents.aws.KillsInstance;
import com.rultor.agents.aws.PingsInstance;
import com.rultor.agents.aws.PrunesInstances;
import com.rultor.agents.aws.StartsInstance;
import com.rultor.agents.aws.TerminatesInstance;
import com.rultor.agents.daemons.ArchivesDaemon;
Expand Down Expand Up @@ -167,11 +168,16 @@ public Agents(final Github ghub, final Sttc stc) {
* @throws IOException If fails
*/
public SuperAgent starter() throws IOException {
final AwsEc2 aws = new AwsEc2(
Manifests.read("Rultor-EC2Key"),
Manifests.read("Rultor-EC2Secret")
);
return new SuperAgent.Iterative(
new Array<>(
new StartsTalks(this.github),
new Invitations(this.github),
new IndexesRequests(),
new SuperAgent.Quiet(new PrunesInstances(aws)),
new SuperAgent.Disabled(
new DockerExec(
new Ssh(
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/com/rultor/agents/aws/PrunesInstances.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import com.rultor.spi.SuperAgent;
import com.rultor.spi.Talks;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import lombok.ToString;

/**
* Terminates all instances that are very old.
*
* @since 1.77
*/
@Immutable
@ToString
public final class PrunesInstances implements SuperAgent {

/**
* AWS Client.
*/
private final transient AwsEc2 api;

/**
* Ctor.
* @param aws API
*/
public PrunesInstances(final AwsEc2 aws) {
this.api = aws;
}

@Override
public void execute(final Talks talks) throws IOException {
final DescribeInstancesResult res = this.api.aws().describeInstances(
new DescribeInstancesRequest()
.withFilters(new Filter().withName("rultor-talk"))
);
final long threshold = new Date().getTime() - TimeUnit.HOURS.toMillis(6L);
for (final Reservation rsrv : res.getReservations()) {
final Instance instance = rsrv.getInstances().get(0);
final Date time = instance.getLaunchTime();
if (time.getTime() > threshold) {
continue;
}
this.api.aws().terminateInstances(
new TerminateInstancesRequest()
.withInstanceIds(instance.getInstanceId())
);
Logger.warn(
this, "AWS instance %s is too old, terminated",
instance.getInstanceId()
);
}
}

}
35 changes: 35 additions & 0 deletions src/main/java/com/rultor/spi/SuperAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.jcabi.aspects.Immutable;
import com.jcabi.immutable.Array;
import com.jcabi.log.Logger;
import java.io.IOException;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down Expand Up @@ -108,4 +109,38 @@ public void execute(final Talks talks) throws IOException {
}
}

/**
* Quiet.
*
* @since 1.0
*/
@Immutable
@ToString
@EqualsAndHashCode(of = "agent")
final class Quiet implements SuperAgent {
/**
* Agent to disable.
*/
private final transient SuperAgent agent;

/**
* Ctor.
* @param agt The agent
*/
public Quiet(final SuperAgent agt) {
this.agent = agt;
}

@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public void execute(final Talks talks) throws IOException {
try {
this.agent.execute(talks);
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
Logger.error(this, "%[exception]s", ex);
}
}
}

}

0 comments on commit 37c858b

Please sign in to comment.