Skip to content

Commit

Permalink
Cleanup pools on dlite termination (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham149 authored Aug 9, 2022
1 parent f48cdad commit 1f9d17c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
16 changes: 4 additions & 12 deletions command/harness/delegate/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,15 @@ func (c *delegateCommand) run(*kingpin.ParseContext) error {
return runnerServer.ListenAndServe(ctx)
})

g.Go(func() error {
return harness.Cleanup(ctx, &c.env, c.poolManager)
})

waitErr := g.Wait()
if waitErr != nil {
logrus.WithError(waitErr).
Errorln("shutting down the server")
}

// lets remove any old instances.
if !env.Settings.ReusePool {
cleanErr := c.poolManager.CleanPools(context.Background(), true, true)
if cleanErr != nil {
logrus.WithError(cleanErr).
Errorln("delegate: unable to clean pools")
} else {
logrus.Infoln("delegate: pools cleaned")
}
}

return waitErr
}

Expand Down
40 changes: 23 additions & 17 deletions command/harness/dlite/dlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/drone-runners/drone-runner-aws/store/database"
loghistory "github.com/drone/runner-go/logger/history"
"github.com/drone/signal"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/wings-software/dlite/delegate"
"github.com/wings-software/dlite/poller"
"github.com/wings-software/dlite/router"

"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -103,22 +105,26 @@ func (c *dliteCommand) run(*kingpin.ParseContext) error {
hook := loghistory.New()
logrus.AddHook(hook)

// TODO (Vistaar): Add support for tags based on available pools
err = c.startPoller(ctx, []string{})
if err != nil {
logrus.WithError(err).Error("could not start poller")
return err
}

// lets remove any old instances.
if !env.Settings.ReusePool {
cleanErr := c.poolManager.CleanPools(context.Background(), true, true)
if cleanErr != nil {
logrus.WithError(cleanErr).Errorln("dlite: unable to clean pools")
} else {
logrus.Infoln("dlite: pools cleaned")
var g errgroup.Group
g.Go(func() error {
// TODO (Vistaar): Add support for tags based on available pools
err = c.startPoller(ctx, []string{})
if err != nil {
logrus.WithError(err).Error("could not start poller")
return err
}
}
return nil
})

g.Go(func() error {
return harness.Cleanup(ctx, &c.env, c.poolManager)
})

waitErr := g.Wait()
if waitErr != nil {
logrus.WithError(waitErr).
Errorln("shutting down dlite")
return waitErr
}
return nil
}
18 changes: 18 additions & 0 deletions command/harness/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ func SetupPool(ctx context.Context, env *config.EnvConfig, poolManager *drivers.
logrus.Infoln("pool created")
return nil
}

func Cleanup(ctx context.Context, env *config.EnvConfig, poolManager *drivers.Manager) error {
if env.Settings.ReusePool {
return nil
}

<-ctx.Done()
// clean up pool on termination
cleanErr := poolManager.CleanPools(context.Background(), true, true)

if cleanErr != nil {
logrus.WithError(cleanErr).
Errorln("unable to clean pools")
} else {
logrus.Infoln("pools cleaned")
}
return cleanErr
}
4 changes: 2 additions & 2 deletions internal/drivers/amazon/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func checkIngressRules(ctx context.Context, client *ec2.EC2, groupID string) err
}
}
if !found {
return fmt.Errorf("security group %s does not have the correct ingress rules There is no rule for port %d", *securityGroup.GroupName, lehelper.LiteEnginePort)
return fmt.Errorf("security group %s does not have the correct ingress rules. There is no rule for port %d", *securityGroup.GroupName, lehelper.LiteEnginePort)
}
return nil
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func (p *config) Create(ctx context.Context, opts *types.InstanceCreateOpts) (in
WithField("time", fmt.Sprintf("%.2fs", time.Since(startTime).Seconds())).
Debugln("amazon: [provision] complete")

return // nolint:nakedret
return //nolint:nakedret
}

// Destroy destroys the server AWS EC2 instances.
Expand Down
2 changes: 1 addition & 1 deletion internal/drivers/digitalocean/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func newClient(ctx context.Context, pat string) *godo.Client {
)
}

// take a slice of ssh keys and return a slice of godo.DropletCreateSSHKey
// take a slice of ssh keys and return a slice of godo.DropletCreateSSHKey
func createSSHKeys(sshKeys []string) []godo.DropletCreateSSHKey {
var keys []godo.DropletCreateSSHKey
for _, key := range sshKeys {
Expand Down

0 comments on commit 1f9d17c

Please sign in to comment.