Skip to content

Commit

Permalink
Add lifecycle configuration, improve confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
daaru00 committed Mar 6, 2021
1 parent 4337102 commit c3dbcfe
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 58 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ using `--upload` flag the zip code will be uploaded to the source bucket instead
aws-canary deploy --upload
```

Source bucket name can be customized using `--source-bucket` parameter:
Source bucket name can be customized using `--sources-bucket` parameter:
```bash
aws-canary deploy --source-bucket my-source-bucket-name --upload
aws-canary deploy --sources-bucket my-sources-bucket-name --upload
```

## Start canaries (manually execution)
Expand Down Expand Up @@ -452,14 +452,14 @@ aws-canary remove
```
The related Lambda function and Layer Versions (with name that starts with "cwsyn-") are also cleaned

In order to also remove artifact and source bucket with canaries run the `remove` command with `--delete-artifact-bucket` or `--delete-source-bucket` flag:
In order to also remove artifact and source bucket with canaries run the `remove` command with `--delete-artifact-bucket` or `--delete-sources-bucket` flag:
```bash
aws-canary remove --delete-artifact-bucket --artifact-bucket my-bucket-bucket-name
# delete only artifact bucket
aws-canary remove --delete-source-bucket --source-bucket my-source-bucket-name
aws-canary remove --delete-sources-bucket --sources-bucket my-sources-bucket-name
# delete only source bucket
aws-canary remove --delete-source-bucket --delete-artifact-bucket --source-bucket my-source-bucket-name --artifact-bucket my-bucket-bucket-name
aws-canary remove --delete-sources-bucket --delete-artifact-bucket --sources-bucket my-sources-bucket-name --artifact-bucket my-bucket-bucket-name
# delete both
```
48 changes: 26 additions & 22 deletions cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func NewCommand(globalFlags []cli.Flag) *cli.Command {
EnvVars: []string{"CANARY_ARTIFACT_BUCKET", "CANARY_ARTIFACT_BUCKET_NAME"},
},
&cli.StringFlag{
Name: "source-bucket",
Name: "sources-bucket",
Usage: "Then source code bucket name",
EnvVars: []string{"CANARY_SOURCE_BUCKET", "CANARY_SOURCE_BUCKET_NAME"},
EnvVars: []string{"CANARY_SOURCES_BUCKET", "CANARY_SOURCES_BUCKET_NAME"},
},
&cli.BoolFlag{
Name: "yes",
Expand Down Expand Up @@ -72,18 +72,6 @@ func Action(c *cli.Context) error {
// Create AWS session
ses := aws.NewAwsSession(c)

// Get canaries
canaries, err := config.LoadCanaries(c, ses)
if err != nil {
return err
}

// Ask canaries selection
canaries, err = config.AskMultipleCanariesSelection(c, *canaries)
if err != nil {
return err
}

// Get caller infos
accountID := aws.GetCallerAccountID(ses)
region := aws.GetCallerRegion(ses)
Expand All @@ -100,11 +88,27 @@ func Action(c *cli.Context) error {
if err != nil {
return err
}
err = artifactBucket.DeployLifecycleConfigurationExpires(90)
if err != nil {
return err
}

// Get canaries
canaries, err := config.LoadCanaries(c, ses)
if err != nil {
return err
}

// Ask canaries selection
canaries, err = config.AskMultipleCanariesSelection(c, *canaries)
if err != nil {
return err
}

// Deploy source bucket
var sourceBucket *bucket.Bucket
if c.Bool("upload") {
sourceBucketName := c.String("source-bucket")
sourceBucketName := c.String("sources-bucket")
if len(sourceBucketName) == 0 {
sourceBucketName = fmt.Sprintf("cw-syn-sources-%s-%s", *accountID, *region)
}
Expand Down Expand Up @@ -170,7 +174,7 @@ func Action(c *cli.Context) error {
func deployBucket(ses *session.Session, bucketName *string) (*bucket.Bucket, error) {
fmt.Println(fmt.Sprintf("Checking bucket %s..", *bucketName))

// Check artifact bucket
// Check bucket
bucket := bucket.New(ses, bucketName)
if bucket.IsDeployed() == false {
// Ask for deploy
Expand All @@ -184,13 +188,13 @@ func deployBucket(ses *session.Session, bucketName *string) (*bucket.Bucket, err
if confirm == false {
return nil, fmt.Errorf("Bucket %s not found", *bucketName)
}
}

// Deploy artifact bucket
fmt.Println(fmt.Sprintf("Deploying bucket %s..", *bucketName))
err := bucket.Deploy()
if err != nil {
return bucket, err
}
// Deploy bucket
fmt.Println(fmt.Sprintf("Deploying bucket %s..", *bucketName))
err := bucket.Deploy()
if err != nil {
return bucket, err
}

return bucket, nil
Expand Down
91 changes: 60 additions & 31 deletions cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func NewCommand(globalFlags []cli.Flag) *cli.Command {
Usage: "Remove also artifact bucket",
},
&cli.StringFlag{
Name: "source-bucket",
Name: "sources-bucket",
Usage: "Then source code bucket name",
EnvVars: []string{"CANARY_SOURCE_BUCKET", "CANARY_SOURCE_BUCKET_NAME"},
EnvVars: []string{"CANARY_SOURCES_BUCKET", "CANARY_SOURCES_BUCKET_NAME"},
},
&cli.BoolFlag{
Name: "delete-source-bucket",
Name: "delete-sources-bucket",
Usage: "Remove also source bucket",
},
&cli.BoolFlag{
Expand All @@ -54,35 +54,11 @@ func NewCommand(globalFlags []cli.Flag) *cli.Command {

// Action contain the command flow
func Action(c *cli.Context) error {
var err error

// Create AWS session
ses := aws.NewAwsSession(c)

// Get canaries
canaries, err := config.LoadCanaries(c, ses)
if err != nil {
return err
}

// Ask canaries selection
canaries, err = config.AskMultipleCanariesSelection(c, *canaries)
if err != nil {
return err
}

// Ask confirmation
if c.Bool("yes") == false {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Are you sure you want to remove %d canaries?", len(*canaries)),
}
survey.AskOne(prompt, &confirm)

// Check respose
if confirm == false {
return errors.New("Not confirmed canaries remove, skip operation")
}
}

// Get caller infos
accountID := aws.GetCallerAccountID(ses)
region := aws.GetCallerRegion(ses)
Expand All @@ -96,24 +72,56 @@ func Action(c *cli.Context) error {
if len(artifactBucketName) == 0 {
artifactBucketName = fmt.Sprintf("canary-artifact-%s-%s", *accountID, *region)
}

// Ask confirmation
err = askConfirmation(c, fmt.Sprintf("Are you sure you want to remove artifact bucket %s?", artifactBucketName))
if err != nil {
return err
}

err = removeBucket(ses, &artifactBucketName)
if err != nil {
return err
}
}

// Remove source bucket
if c.Bool("delete-source-bucket") {
sourceBucketName := c.String("source-bucket")
if c.Bool("delete-sources-bucket") {
sourceBucketName := c.String("sources-bucket")
if len(sourceBucketName) == 0 {
sourceBucketName = fmt.Sprintf("cw-syn-sources-%s-%s", *accountID, *region)
}

// Ask confirmation
err = askConfirmation(c, fmt.Sprintf("Are you sure you want to remove sources bucket %s?", sourceBucketName))
if err != nil {
return err
}

err = removeBucket(ses, &sourceBucketName)
if err != nil {
return err
}
}

// Get canaries
canaries, err := config.LoadCanaries(c, ses)
if err != nil {
return err
}

// Ask canaries selection
canaries, err = config.AskMultipleCanariesSelection(c, *canaries)
if err != nil {
return err
}

// Ask confirmation
err = askConfirmation(c, fmt.Sprintf("Are you sure you want to remove %d canaries?", len(*canaries)))
if err != nil {
return err
}

// Setup wait group for async jobs
var waitGroup sync.WaitGroup

Expand Down Expand Up @@ -226,3 +234,24 @@ func removeSingleCanary(ses *session.Session, canary *canary.Canary, region *str
fmt.Println(fmt.Sprintf("[%s] Remove completed!", canary.Name))
return nil
}

func askConfirmation(c *cli.Context, message string) error {
// Check yes flag
if c.Bool("yes") {
return nil
}

// Ask confirmation
confirm := false
prompt := &survey.Confirm{
Message: message,
}
survey.AskOne(prompt, &confirm)

// Check respose
if confirm == false {
return errors.New("Not confirmed canaries remove, skip operation")
}

return nil
}
19 changes: 19 additions & 0 deletions internal/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func (b *Bucket) Deploy() error {
return err
}

// DeployLifecycleConfigurationExpires deploy lifecycle configuration for expiration
func (b *Bucket) DeployLifecycleConfigurationExpires(days int64) error {
_, err := b.clients.s3.PutBucketLifecycleConfiguration(&s3.PutBucketLifecycleConfigurationInput{
Bucket: b.Name,
LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
Rules: []*s3.LifecycleRule{
{
Prefix: aws.String(""),
Status: aws.String("Enabled"),
Expiration: &s3.LifecycleExpiration{
Days: aws.Int64(days),
},
},
},
},
})
return err
}

// Empty Bucket
func (b *Bucket) Empty() error {

Expand Down

0 comments on commit c3dbcfe

Please sign in to comment.