-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to shutdown idle agent just before billing hour completes
Implements #30
- Loading branch information
Showing
15 changed files
with
307 additions
and
19 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
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
31 changes: 31 additions & 0 deletions
31
src/main/java/cloud/dnation/jenkins/plugins/hetzner/shutdown/AbstractShutdownPolicy.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,31 @@ | ||
/* | ||
* Copyright 2022 https://dnation.cloud | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package cloud.dnation.jenkins.plugins.hetzner.shutdown; | ||
|
||
import hudson.model.AbstractDescribableImpl; | ||
import hudson.slaves.AbstractCloudComputer; | ||
import hudson.slaves.RetentionStrategy; | ||
import lombok.Getter; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public abstract class AbstractShutdownPolicy extends AbstractDescribableImpl<AbstractShutdownPolicy> { | ||
@Getter | ||
protected final transient RetentionStrategy<AbstractCloudComputer> retentionStrategy; | ||
|
||
protected AbstractShutdownPolicy(RetentionStrategy<AbstractCloudComputer> strategy) { | ||
this.retentionStrategy = strategy; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/cloud/dnation/jenkins/plugins/hetzner/shutdown/BeforeHourWrapsPolicy.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,80 @@ | ||
/* | ||
* Copyright 2022 https://dnation.cloud | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package cloud.dnation.jenkins.plugins.hetzner.shutdown; | ||
|
||
import cloud.dnation.jenkins.plugins.hetzner.Helper; | ||
import cloud.dnation.jenkins.plugins.hetzner.HetznerServerAgent; | ||
import cloud.dnation.jenkins.plugins.hetzner.Messages; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.model.Descriptor; | ||
import hudson.slaves.AbstractCloudComputer; | ||
import hudson.slaves.RetentionStrategy; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.jcip.annotations.GuardedBy; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
|
||
@Slf4j | ||
public class BeforeHourWrapsPolicy extends AbstractShutdownPolicy { | ||
@SuppressWarnings("rawtypes") | ||
private static final RetentionStrategy<AbstractCloudComputer> STRATEGY_SINGLETON = new RetentionStrategyImpl(); | ||
|
||
@DataBoundConstructor | ||
public BeforeHourWrapsPolicy() { | ||
super(STRATEGY_SINGLETON); | ||
} | ||
|
||
|
||
@SuppressWarnings("rawtypes") | ||
private static class RetentionStrategyImpl extends RetentionStrategy<AbstractCloudComputer> { | ||
@Override | ||
public void start(AbstractCloudComputer c) { | ||
c.connect(false); | ||
} | ||
|
||
@Override | ||
@GuardedBy("hudson.model.Queue.lock") | ||
public long check(final AbstractCloudComputer c) { | ||
final HetznerServerAgent agent = (HetznerServerAgent) c.getNode(); | ||
if (c.isIdle() && agent != null && agent.getServerInstance() != null) { | ||
if (Helper.canShutdownServer(agent.getServerInstance().getServerDetail().getCreated(), | ||
LocalDateTime.now().getMinute())) { | ||
log.info("Disconnecting {}", c.getName()); | ||
try { | ||
agent.terminate(); | ||
} catch (InterruptedException | IOException e) { | ||
log.warn("Failed to terminate {}", c.getName(), e); | ||
} | ||
} | ||
} | ||
return 1; | ||
} | ||
} | ||
|
||
@Extension | ||
@Symbol("hour-wrap") | ||
public static final class DescriptorImpl extends Descriptor<AbstractShutdownPolicy> { | ||
@NonNull | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.policy_shutdown_beforeHourWrap(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/cloud/dnation/jenkins/plugins/hetzner/shutdown/IdlePeriodPolicy.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,46 @@ | ||
/* | ||
* Copyright 2022 https://dnation.cloud | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package cloud.dnation.jenkins.plugins.hetzner.shutdown; | ||
|
||
import cloud.dnation.jenkins.plugins.hetzner.Messages; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.model.Descriptor; | ||
import hudson.slaves.CloudRetentionStrategy; | ||
import lombok.Getter; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
public class IdlePeriodPolicy extends AbstractShutdownPolicy { | ||
@Getter | ||
private final int idleMinutes; | ||
|
||
@DataBoundConstructor | ||
public IdlePeriodPolicy(int idleMinutes) { | ||
super(new CloudRetentionStrategy(idleMinutes)); | ||
this.idleMinutes = idleMinutes; | ||
} | ||
|
||
@Extension | ||
@Symbol("idle") | ||
public static final class DescriptorImpl extends Descriptor<AbstractShutdownPolicy> { | ||
@NonNull | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.policy_shutdown_idle(); | ||
} | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
.../resources/cloud/dnation/jenkins/plugins/hetzner/shutdown/BeforeHourWrapsPolicy/helm.html
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,19 @@ | ||
<!-- | ||
Copyright 2022 https://dnation.cloud | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<div> | ||
Server will be kept alive until current hour of billing cycle completes. | ||
Make sure that Jenkins controller's clock are configured correctly as skew may lead to 1 hour over-billing. | ||
</div> |
Oops, something went wrong.