Skip to content

Commit

Permalink
add prune --stage flag. Closes #647
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 19, 2018
1 parent 86fffb7 commit 8c3ed95
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
42 changes: 42 additions & 0 deletions docs/06-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Commands:
logs Show log output.
metrics Show project metrics.
rollback Rollback to a previous deployment.
prune Prune old S3 deployments of a stage.
run Run a hook.
stack plan Plan configuration changes.
stack apply Apply configuration changes.
Expand Down Expand Up @@ -810,3 +811,44 @@ Upgrade to the specified version.
```
$ up upgrade -t 0.4.4
```


## Prune

Prune old S3 deployments of a stage.

```
Usage:
up prune [<flags>]
Flags:
-h, --help Output usage information.
-C, --chdir="." Change working directory.
-v, --verbose Enable verbose log output.
--format="text" Output formatter.
--version Show application version.
-s, --stage="staging" Target stage name.
-r, --retain=60 Number of versions to retain.
```

### Examples

Prune and retain the most recent 15 staging versions.

```
$ up prune
```

Prune and retain the most recent 60 production versions.

```
$ up prune -s production
```

Prune and retain the most recent 15 production versions.

```
$ up prune -s production -r 15
```
11 changes: 7 additions & 4 deletions internal/cli/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

func init() {
cmd := root.Command("prune", "Prune old S3 deployments.")
cmd := root.Command("prune", "Prune old S3 deployments of a stage.")

cmd.Example(`up prune`, "Prune and retain the most recent 60 versions.")
cmd.Example(`up prune --retain 15`, "Prune and retain the most recent 15 versions.")
cmd.Example(`up prune`, "Prune and retain the most recent 15 staging versions.")
cmd.Example(`up prune -s production`, "Prune and retain the most recent 60 production versions.")
cmd.Example(`up prune -s production -r 15`, "Prune and retain the most recent 15 production versions.")

stage := cmd.Flag("stage", "Target stage name.").Short('s').Default("staging").String()
versions := cmd.Flag("retain", "Number of versions to retain.").Short('r').Default("60").Int()

cmd.Action(func(_ *kingpin.ParseContext) error {
Expand All @@ -26,8 +28,9 @@ func init() {

stats.Track("Prune", map[string]interface{}{
"versions": *versions,
"stage": *stage,
})

return p.Prune(region, *versions)
return p.Prune(region, *stage, *versions)
})
}
2 changes: 1 addition & 1 deletion platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Platform interface {
// 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
Prune(region, stage string, versions int) error
}

// Runtime is the interface used by a platform to support
Expand Down
4 changes: 2 additions & 2 deletions platform/lambda/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// Prune implementation.
func (p *Platform) Prune(region string, versions int) error {
func (p *Platform) Prune(region, stage string, versions int) error {
p.events.Emit("prune", nil)

if err := p.createRole(); err != nil {
Expand All @@ -22,7 +22,7 @@ func (p *Platform) Prune(region string, versions int) error {

s := s3.New(session.New(aws.NewConfig().WithRegion(region)))
b := aws.String(p.getS3BucketName(region))
prefix := p.config.Name + "/"
prefix := p.config.Name + "/" + stage + "/"

params := &s3.ListObjectsInput{
Bucket: b,
Expand Down
4 changes: 2 additions & 2 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ func (p *Project) ApplyStack(region string) error {
}

// Prune implementation.
func (p *Project) Prune(region string, versions int) error {
func (p *Project) Prune(region, stage string, versions int) error {
pruner, ok := p.Platform.(Pruner)
if !ok {
return errors.Errorf("platform does not support pruning")
}

return pruner.Prune(region, versions)
return pruner.Prune(region, stage, versions)
}

0 comments on commit 8c3ed95

Please sign in to comment.