Skip to content

Commit

Permalink
[#3704] Feat: Add explicit time state maintain support
Browse files Browse the repository at this point in the history
  • Loading branch information
nightfury1204 committed Nov 6, 2023
2 parents ee3795c + 15e55b0 commit 106b740
Show file tree
Hide file tree
Showing 10 changed files with 7,699 additions and 9 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/RackSec/srslog v0.0.0-20170920152354-4d2c753a4ee1
github.com/adhocore/gronx v1.6.5
github.com/aws/aws-lambda-go v1.37.0
github.com/aws/aws-sdk-go v1.25.29
github.com/boltdb/bolt v1.3.1
Expand All @@ -22,6 +23,7 @@ require (
github.com/fsouza/go-dockerclient v0.0.0-20160427172547-1d4f4ae73768
github.com/gobuffalo/packr v1.22.0
github.com/gobwas/glob v0.2.3
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/gorilla/mux v1.7.0
github.com/gorilla/websocket v1.5.0
github.com/headzoo/surf v1.0.0
Expand All @@ -31,6 +33,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mweagle/Sparta v0.8.1-0.20171126182155-ead2872585dc
github.com/pkg/errors v0.8.1
github.com/satori/go.uuid v1.2.0
github.com/segmentio/analytics-go v2.0.1-0.20160426181448-2d840d861c32+incompatible
github.com/stretchr/testify v1.8.1
github.com/stvp/rollbar v0.5.1
Expand All @@ -45,10 +48,7 @@ require (
)

require (
github.com/adhocore/gronx v1.6.5 // indirect
github.com/adrg/xdg v0.2.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
11 changes: 11 additions & 0 deletions provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -69,6 +70,7 @@ type Provider struct {
ELBLogBucket string
LogBucket string
LogDriver string
MaintainTimerState bool
NotificationTopic string
OnDemandMinCount int
Password string
Expand Down Expand Up @@ -168,6 +170,7 @@ func (p *Provider) loadParams() error {
p.InternalOnly = labels["rack.InternalOnly"] == "Yes"
p.LogBucket = labels["rack.LogBucket"]
p.LogDriver = labels["rack.LogDriver"]
p.MaintainTimerState = labels["rack.MaintainTimerState"] == "Yes"
p.NotificationTopic = labels["rack.NotificationTopic"]
p.OnDemandMinCount = intParam(labels["rack.OnDemandMinCount"], 2)
p.Private = labels["rack.Private"] == "Yes"
Expand Down Expand Up @@ -376,6 +379,14 @@ func (p *Provider) ecs() *ecs.ECS {
return ecs.New(s, p.config())
}

func (p *Provider) eventbridge() *eventbridge.EventBridge {
s, err := helpers.NewSession()
if err != nil {
panic(errors.WithStack(err))
}
return eventbridge.New(s, p.config())
}

func (p *Provider) kms() *kms.KMS {
s, err := helpers.NewSession()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions provider/aws/formation/rack.json
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@
"Description": "Number of days to keep logs (blank for unlimited)",
"Type": "String"
},
"MaintainTimerState": {
"Type": "String",
"Description": "Explicitly maintain time state. If use disable the timer, it will keep it disbaled",
"Default": "No",
"AllowedValues": [ "Yes", "No" ]
},
"MaxAvailabilityZones": {
"Type": "Number",
"Default": "3",
Expand Down Expand Up @@ -3530,6 +3536,7 @@
"rack.Internal": { "Ref": "Internal" },
"rack.InternalOnly": { "Ref": "InternalOnly" },
"rack.LogBucket": { "Fn::If": [ "BlankLogBucket", { "Ref": "Logs" }, { "Ref": "LogBucket" } ] },
"rack.MaintainTimerState": { "Ref": "MaintainTimerState" },
"rack.NotificationTopic": { "Ref": "NotificationTopic" },
"rack.OnDemandMinCount": { "Ref": "OnDemandMinCount" },
"rack.Private": { "Ref": "Private" },
Expand Down
3 changes: 3 additions & 0 deletions provider/aws/formation/timer.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "cron({{.Cron}})",
{{ if $.TimeState }}
"State": "{{$.TimeState}}",
{{ end }}
"Targets": [ {
"Arn": { "Fn::If": [ "FargateEither",
{ "Ref": "Launcher" },
Expand Down
57 changes: 51 additions & 6 deletions provider/aws/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/convox/rack/pkg/crypt"
Expand Down Expand Up @@ -328,12 +330,20 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt

for _, t := range m.Timers {
ttp := map[string]interface{}{
"App": r.App,
"Build": tp["Build"],
"Manifest": tp["Manifest"],
"Password": p.Password,
"Release": tp["Release"],
"Timer": t,
"App": r.App,
"Build": tp["Build"],
"Manifest": tp["Manifest"],
"Password": p.Password,
"Release": tp["Release"],
"Timer": t,
"TimeState": "",
}

if p.MaintainTimerState {
ttp["TimeState"], err = p.getTimerState(app, t.Name)
if err != nil {
return err
}
}

data, err := formationTemplate("timer", ttp)
Expand Down Expand Up @@ -886,3 +896,38 @@ func (p *Provider) resolveLinks(a *structs.App, m *manifest1.Manifest, r *struct

return m, nil
}

func (p *Provider) getTimerState(app, timerName string) (string, error) {
ars, err := p.describeStackResources(&cloudformation.DescribeStackResourcesInput{
StackName: aws.String(p.rackStack(app)),
})
if err != nil {
return "", err
}

arsns := map[string]string{}

for _, ar := range ars.StackResources {
arsns[cs(ar.LogicalResourceId, "")] = cs(ar.PhysicalResourceId, "")
}

timerOut, err := p.describeStackResources(&cloudformation.DescribeStackResourcesInput{
StackName: aws.String(arsns[fmt.Sprintf("Timer%s", upperName(timerName))]),
})
if err != nil {
return "", err
}

for _, to := range timerOut.StackResources {
if cs(to.LogicalResourceId, "") == "Timer" {
resp, err := p.eventbridge().DescribeRule(&eventbridge.DescribeRuleInput{
Name: to.PhysicalResourceId,
})
if err != nil {
return "", err
}
return *resp.State, nil
}
}
return "", fmt.Errorf("Timer rule not found")
}
Loading

0 comments on commit 106b740

Please sign in to comment.