From eb6a3fbc7093a479acb31bdf55b93eedee782280 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Wed, 11 Apr 2018 11:04:57 -0700 Subject: [PATCH] add prune command to remove old releases from S3. Closes #322 --- cmd/up/main.go | 1 + platform.go | 6 ++++++ platform/lambda/lambda.go | 4 +++- reporter/text/text.go | 9 +++++++++ up.go | 10 ++++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cmd/up/main.go b/cmd/up/main.go index 194eb1f0..c5be0163 100644 --- a/cmd/up/main.go +++ b/cmd/up/main.go @@ -17,6 +17,7 @@ import ( _ "github.com/apex/up/internal/cli/domains" _ "github.com/apex/up/internal/cli/logs" _ "github.com/apex/up/internal/cli/metrics" + _ "github.com/apex/up/internal/cli/prune" _ "github.com/apex/up/internal/cli/run" _ "github.com/apex/up/internal/cli/stack" _ "github.com/apex/up/internal/cli/start" diff --git a/platform.go b/platform.go index b6914046..31a20702 100644 --- a/platform.go +++ b/platform.go @@ -83,6 +83,12 @@ type Platform interface { ShowMetrics(region, stage string, start time.Time) error } +// Pruner is the interface used to prune old versions and +// the artifacts associated such as S3 zip files for Lambda. +type Pruner interface { + Prune(region string, versions int) error +} + // Runtime is the interface used by a platform to support // runtime operations such as initializing environment // variables from remote storage. diff --git a/platform/lambda/lambda.go b/platform/lambda/lambda.go index 645e043a..5e0dc4c4 100644 --- a/platform/lambda/lambda.go +++ b/platform/lambda/lambda.go @@ -846,7 +846,9 @@ func (p *Platform) removeProxy() error { // getS3Key returns a randomized s3 key. func (p *Platform) getS3Key(stage string) string { - return fmt.Sprintf("%s/%s/%s.zip", p.config.Name, stage, uniuri.New()) + ts := time.Now().Unix() + uid := uniuri.New() + return fmt.Sprintf("%s/%s/%d-%s.zip", p.config.Name, stage, ts, uid) } // getS3BucketName returns the s3 bucket name. diff --git a/reporter/text/text.go b/reporter/text/text.go index f440f39a..dde5569c 100644 --- a/reporter/text/text.go +++ b/reporter/text/text.go @@ -239,6 +239,15 @@ func (r *reporter) Start() { default: r.log(n, humanize.Comma(int64(e.Int("value")))) } + case "prune": + fmt.Printf("\n") + r.pending("prune", "removing old releases") + case "prune.complete": + n := e.Int("count") + b := e.Int64("size") + s := fmt.Sprintf("%d old files removed from S3 (%s)", n, humanize.Bytes(uint64(b))) + r.complete("prune", s, e.Duration("duration")) + fmt.Printf("\n") } r.prevTime = time.Now() diff --git a/up.go b/up.go index 156456d6..e34333f1 100644 --- a/up.go +++ b/up.go @@ -222,3 +222,13 @@ func (p *Project) ApplyStack(region string) error { return p.Platform.ApplyStack(region) } + +// Prune implementation. +func (p *Project) Prune(region string, versions int) error { + pruner, ok := p.Platform.(Pruner) + if !ok { + return errors.Errorf("platform does not support pruning") + } + + return pruner.Prune(region, versions) +}