Skip to content

Commit

Permalink
[#3485] Add support to schedule the rack to shutdown and up
Browse files Browse the repository at this point in the history
User can pass a cron to shutdown the rack and another cron to set up the rack.

```
convox rack params set ScheduleRackScaleDown="0 18 * * 5" ScheduleRackScaleUp="0 9 * * 1"
```
  • Loading branch information
Twsouza committed Dec 21, 2021
1 parent 5077828 commit 200adbe
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/cli/rack.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func RackInstall(rack sdk.Interface, c *stdcli.Context) error {
opts.Parameters[parts[0]] = parts[1]
}

if err := validateParams(opts.Parameters); err != nil {
return err
}

p, err := pv.FromName(c.Arg(0))
if err != nil {
return err
Expand Down Expand Up @@ -254,6 +258,10 @@ func RackParamsSet(rack sdk.Interface, c *stdcli.Context) error {
opts.Parameters[parts[0]] = parts[1]
}

if err := validateParams(opts.Parameters); err != nil {
return err
}

c.Startf("Updating parameters")

if s.Version <= "20180708231844" {
Expand Down Expand Up @@ -429,3 +437,13 @@ func RackWait(rack sdk.Interface, c *stdcli.Context) error {

return c.OK()
}

// validateParams validate parameters for install and update rack
func validateParams(params map[string]string) error {
srdown, srup := params["ScheduleRackScaleDown"], params["ScheduleRackScaleUp"]
if (srdown == "" || srup == "") && (srdown != "" || srup != "") {
return fmt.Errorf("to configure ScheduleAction you need both ScheduleRackScaleDown and ScheduleRackScaleUp parameters")
}

return nil
}
59 changes: 59 additions & 0 deletions provider/aws/formation/rack.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
"RegionHasEFSAndThirdAvailabilityZoneAndHighAvailability": {
"Fn::And": [ { "Condition": "RegionHasEFS" }, { "Condition": "ThirdAvailabilityZone" }, { "Condition": "HighAvailability" } ]
},
"SetScheduleRackScale": { "Fn::Not" : [{
"Fn::And": [
{ "Fn::Equals": [ { "Ref": "ScheduleRackScaleDown" }, "" ] },
{ "Fn::Equals": [ { "Ref": "ScheduleRackScaleUp" }, "" ] }
]}
] },
"SpotInstances": { "Fn::Not": [ { "Fn::Equals": [ { "Ref": "SpotInstanceBid"}, "" ] } ] },
"SwapEnabled": { "Fn::Not": [ { "Fn::Equals": [ { "Ref": "SwapSize" }, "0" ] } ] },
"ThirdAvailabilityZone": { "Fn::And": [
Expand Down Expand Up @@ -726,6 +732,16 @@
"Description": "The security groups (comma delimited) to assign to the rack router.",
"Type": "CommaDelimitedList"
},
"ScheduleRackScaleDown": {
"Type": "String",
"Description": "The recurring schedule for when the rack will be shutdown. This format consists of five fields separated by white spaces: [Minute] [Hour] [Day_of_Month] [Month_of_Year] [Day_of_Week]",
"Default": ""
},
"ScheduleRackScaleUp": {
"Type": "String",
"Description": "The recurring schedule for when the rack will start. This format consists of five fields separated by white spaces: [Minute] [Hour] [Day_of_Month] [Month_of_Year] [Day_of_Week]",
"Default": ""
},
"SpotInstanceBid": {
"Default": "",
"Description": "Bid price for spot instances",
Expand Down Expand Up @@ -1722,6 +1738,49 @@
}
}
},
"InstancesScheduleRackScaleDown":{
"Type":"AWS::AutoScaling::ScheduledAction",
"Condition": "SetScheduleRackScale",
"Properties":{
"AutoScalingGroupName": { "Ref": "Instances" },
"MaxSize": "0",
"MinSize": "0",
"Recurrence": { "Ref": "ScheduleRackScaleDown" }
}
},
"InstancesScheduleRackScaleUp":{
"Type":"AWS::AutoScaling::ScheduledAction",
"Condition": "SetScheduleRackScale",
"Properties":{
"AutoScalingGroupName": { "Ref": "Instances" },
"DesiredCapacity" : { "Fn::If": [ "SpotInstances", { "Ref": "AWS::NoValue" }, { "Fn::If": [ "HighAvailability", { "Ref": "InstanceCount"}, "1" ] } ] },
"MinSize" : { "Fn::If": [ "SpotInstances", { "Ref": "OnDemandMinCount" }, { "Fn::If": [ "HighAvailability", { "Ref": "InstanceCount"}, "1" ] } ] },
"MaxSize" : { "Fn::If": [ "HighAvailability", "1000", "3" ] },
"Recurrence": { "Ref": "ScheduleRackScaleUp" }
}
},
"BuildScheduleRackScaleDown":{
"Type":"AWS::AutoScaling::ScheduledAction",
"Condition": "SetScheduleRackScale",
"Properties":{
"AutoScalingGroupName": { "Ref": "BuildInstances" },
"DesiredCapacity": "0",
"MaxSize": "0",
"MinSize": "0",
"Recurrence": { "Ref": "ScheduleRackScaleDown" }
}
},
"BuildScheduleRackScaleUp":{
"Type":"AWS::AutoScaling::ScheduledAction",
"Condition": "SetScheduleRackScale",
"Properties":{
"AutoScalingGroupName": { "Ref": "BuildInstances" },
"DesiredCapacity": "1",
"MaxSize":"2",
"MinSize":"1",
"Recurrence": { "Ref": "ScheduleRackScaleUp" }
}
},
"InstancesAutoscaler": {
"Type": "AWS::Lambda::Function",
"Condition": "Autoscale",
Expand Down

0 comments on commit 200adbe

Please sign in to comment.