-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support limiting minimum instances to a time range #404
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,8 @@ public class SlaveTemplate implements Describable<SlaveTemplate> { | |
|
||
private int minimumNumberOfInstances; | ||
|
||
private MinimumNumberOfInstancesTimeRangeConfig minimumNumberOfInstancesTimeRangeConfig; | ||
|
||
public final boolean stopOnTerminate; | ||
|
||
private final List<EC2Tag> tags; | ||
|
@@ -508,6 +510,15 @@ public int getMinimumNumberOfInstances() { | |
return minimumNumberOfInstances; | ||
} | ||
|
||
public MinimumNumberOfInstancesTimeRangeConfig getMinimumNumberOfInstancesTimeRangeConfig() { | ||
return minimumNumberOfInstancesTimeRangeConfig; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMinimumNumberOfInstancesTimeRangeConfig(MinimumNumberOfInstancesTimeRangeConfig minimumNumberOfInstancesTimeRangeConfig) { | ||
this.minimumNumberOfInstancesTimeRangeConfig = minimumNumberOfInstancesTimeRangeConfig; | ||
} | ||
|
||
public int getInstanceCap() { | ||
return instanceCap; | ||
} | ||
|
@@ -1503,6 +1514,38 @@ public FormValidation doCheckMinimumNumberOfInstances(@QueryParameter String val | |
return FormValidation.error("Minimum number of instances must be a non-negative integer (or null)"); | ||
} | ||
|
||
public FormValidation doCheckMinimumNoInstancesActiveTimeRangeFrom(@QueryParameter String value) { | ||
try { | ||
MinimumNumberOfInstancesTimeRangeConfig.validateLocalTimeString(value); | ||
return FormValidation.ok(); | ||
} catch (IllegalArgumentException e) { | ||
return FormValidation.error("Please enter value in format 'h:mm a' or 'HH:mm'"); | ||
} | ||
} | ||
|
||
public FormValidation doCheckMinimumNoInstancesActiveTimeRangeTo(@QueryParameter String value) { | ||
try { | ||
MinimumNumberOfInstancesTimeRangeConfig.validateLocalTimeString(value); | ||
return FormValidation.ok(); | ||
} catch (IllegalArgumentException e) { | ||
return FormValidation.error("Please enter value in format 'h:mm a' or 'HH:mm'"); | ||
} | ||
} | ||
|
||
// For some reason, all days will validate against this method so no need to repeat for each day. | ||
public FormValidation doCheckMonday(@QueryParameter boolean monday, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a different name if it's valid for all days? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, yes, but it's some sort of Jelly magic based on the field names. |
||
@QueryParameter boolean tuesday, | ||
@QueryParameter boolean wednesday, | ||
@QueryParameter boolean thursday, | ||
@QueryParameter boolean friday, | ||
@QueryParameter boolean saturday, | ||
@QueryParameter boolean sunday) { | ||
if (!(monday || tuesday || wednesday || thursday || friday || saturday || sunday)) { | ||
return FormValidation.warning("At least one day should be checked or minimum number of instances won't be active"); | ||
} | ||
return FormValidation.ok(); | ||
} | ||
|
||
public FormValidation doCheckInstanceCapStr(@QueryParameter String value) { | ||
if (value == null || value.trim().isEmpty()) | ||
return FormValidation.ok(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package hudson.plugins.ec2.util; | ||
|
||
import net.sf.json.JSONObject; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class MinimumNumberOfInstancesTimeRangeConfig { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The field names are excessively long due to the class name prefix. Is this prefix required? |
||
private String minimumNoInstancesActiveTimeRangeFrom; | ||
private String minimumNoInstancesActiveTimeRangeTo; | ||
private Map<String, Boolean> minimumNoInstancesActiveTimeRangeDays; | ||
|
||
@DataBoundConstructor | ||
public MinimumNumberOfInstancesTimeRangeConfig() { | ||
} | ||
|
||
private static Map<String, Boolean> parseDays(JSONObject days) { | ||
Map<String, Boolean> map = new HashMap<>(); | ||
map.put("monday", days.getBoolean("monday")); | ||
map.put("tuesday", days.getBoolean("tuesday")); | ||
map.put("wednesday", days.getBoolean("wednesday")); | ||
map.put("thursday", days.getBoolean("thursday")); | ||
map.put("friday", days.getBoolean("friday")); | ||
map.put("saturday", days.getBoolean("saturday")); | ||
map.put("sunday", days.getBoolean("sunday")); | ||
return map; | ||
} | ||
|
||
private static LocalTime getLocalTime(String value) { | ||
try { | ||
return LocalTime.parse(value, DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH)); | ||
} catch (DateTimeParseException e) { | ||
try { | ||
return LocalTime.parse(value, DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH)); | ||
} catch (DateTimeParseException ignore) { | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static void validateLocalTimeString(String value) { | ||
if (getLocalTime(value) == null) { | ||
throw new IllegalArgumentException("Value " + value + " is not valid time format, ([h:mm a] or [HH:mm])"); | ||
} | ||
} | ||
|
||
public String getMinimumNoInstancesActiveTimeRangeFrom() { | ||
return minimumNoInstancesActiveTimeRangeFrom; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMinimumNoInstancesActiveTimeRangeFrom(String minimumNoInstancesActiveTimeRangeFrom) { | ||
validateLocalTimeString(minimumNoInstancesActiveTimeRangeFrom); | ||
this.minimumNoInstancesActiveTimeRangeFrom = minimumNoInstancesActiveTimeRangeFrom; | ||
} | ||
|
||
public LocalTime getMinimumNoInstancesActiveTimeRangeFromAsTime() { | ||
return getLocalTime(minimumNoInstancesActiveTimeRangeFrom); | ||
} | ||
|
||
public String getMinimumNoInstancesActiveTimeRangeTo() { | ||
return minimumNoInstancesActiveTimeRangeTo; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMinimumNoInstancesActiveTimeRangeTo(String minimumNoInstancesActiveTimeRangeTo) { | ||
validateLocalTimeString(minimumNoInstancesActiveTimeRangeTo); | ||
this.minimumNoInstancesActiveTimeRangeTo = minimumNoInstancesActiveTimeRangeTo; | ||
} | ||
|
||
public LocalTime getMinimumNoInstancesActiveTimeRangeToAsTime() { | ||
return getLocalTime(minimumNoInstancesActiveTimeRangeTo); | ||
} | ||
|
||
public Map<String, Boolean> getMinimumNoInstancesActiveTimeRangeDays() { | ||
return minimumNoInstancesActiveTimeRangeDays; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMinimumNoInstancesActiveTimeRangeDays(JSONObject minimumNoInstancesActiveTimeRangeDays) { | ||
this.minimumNoInstancesActiveTimeRangeDays = parseDays(minimumNoInstancesActiveTimeRangeDays); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,34 @@ THE SOFTWARE. | |
<f:textbox /> | ||
</f:entry> | ||
|
||
<f:optionalBlock name="minimumNumberOfInstancesTimeRangeConfig" | ||
title="${%Only apply minimum number of instances during specific time range}" checked="${instance.minimumNumberOfInstancesTimeRangeConfig != null}" | ||
help="/descriptor/hudson.plugins.ec2.SlaveTemplate/help/minimumNumberOfInstancesTimeRangeConfig" > | ||
<f:entry title="${%From/to}" description="${%''13:00'' or ''1:00 PM'' format supported}"> | ||
From: <f:textbox style="width: 25%;" field="minimumNoInstancesActiveTimeRangeFrom" value="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeFrom}" /> | ||
To: <f:textbox style="width: 25%;" field="minimumNoInstancesActiveTimeRangeTo" value="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeTo}" /> | ||
</f:entry> | ||
<f:rowSet name="minimumNoInstancesActiveTimeRangeDays"> | ||
<f:entry title="${%On days}" > | ||
<f:checkbox field="monday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.monday}"/> | ||
<label>${%Monday}</label> | ||
<f:checkbox field="tuesday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.tuesday}"/> | ||
<label>${%Tuesday}</label> | ||
<f:checkbox field="wednesday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.wednesday}"/> | ||
<label>${%Wednesday}</label> | ||
<f:checkbox field="thursday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.thursday}"/> | ||
<label>${%Thursday}</label> | ||
<f:checkbox field="friday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.friday}"/> | ||
<label>${%Friday}</label> | ||
<f:checkbox field="saturday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.saturday}"/> | ||
<label>${%Saturday}</label> | ||
<f:checkbox field="sunday" checked="${instance.minimumNumberOfInstancesTimeRangeConfig.minimumNoInstancesActiveTimeRangeDays.sunday}"/> | ||
<label>${%Sunday}</label> | ||
</f:entry> | ||
</f:rowSet> | ||
</f:optionalBlock> | ||
<f:entry /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this empty entry element required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not required, but it gets really cramped and ugly without it. |
||
|
||
<f:entry title="${%Instance Cap}" field="instanceCapStr"> | ||
<f:textbox /> | ||
</f:entry> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div> | ||
Choose the timespan and on which days a minimum number of instances should be kept alive. If the To-time is before the From-time, the time range will wrap around to the next day. | ||
|
||
E.g. From 23:00 to 06:00 with Monday selected would mean that the instance(s) would be kept up from 23:00 on Monday evening until 06:00 Tuesday morning. | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about old configs not having this property? Should you manage that in readResolve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC There is a nullcheck for its usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, there's a null check. And I did a quick check now and couldn't find anywhere that I had missed checking. :)