Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ your stack in a different region.

## Additional Flags

* `--stackit-s3-suffix "random"`
If the s3 bucket stackit creates requires a unique S3 bucket name.

* `--stackit-tags "key=val,key2=val2"`
If the s3 bucket stackit creates requires tags, you can add them with this flag.

TODO: Document these properly

* `--service-role VAL`
Expand Down
21 changes: 12 additions & 9 deletions cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
Expand All @@ -27,17 +34,11 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

func packageTemplate(ctx context.Context, sess *session.Session, prefix string, templateReader packager.TemplateReader, writer io.Writer) (*string, error) {
func packageTemplate(ctx context.Context, sess *session.Session, prefix string, s3suffix string, s3Tags string, templateReader packager.TemplateReader, writer io.Writer) (*string, error) {
s3api := s3.New(sess)
pkger := packager.New(s3api, sts.New(sess), *s3api.Config.Region)
pkger := packager.New(s3api, sts.New(sess), *s3api.Config.Region, s3suffix, s3Tags)
packagedTemplate, err := pkger.Package(ctx, prefix, templateReader, writer)
if err != nil {
return nil, errors.Wrap(err, "packaging template")
Expand Down Expand Up @@ -112,6 +113,8 @@ package will:
profile, _ := cmd.PersistentFlags().GetString("profile")
templatePath, _ := cmd.PersistentFlags().GetString("template")
prefix, _ := cmd.PersistentFlags().GetString("prefix")
s3Suffix, _ := cmd.PersistentFlags().GetString("s3Suffix")
s3Tags, _ := cmd.PersistentFlags().GetString("s3Tags")

template, err := pathToTemplate(templatePath)
if err != nil {
Expand All @@ -123,7 +126,7 @@ package will:
defer end()

sess := awsSession(profile, region)
packagedTemplate, err := packageTemplate(ctx, sess, prefix, template, cmd.OutOrStderr())
packagedTemplate, err := packageTemplate(ctx, sess, prefix, s3Suffix, s3Tags, template, cmd.OutOrStderr())
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), "%+v\n", err)
return
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func init() {
RootCmd.PersistentFlags().String("region", "", "")
RootCmd.PersistentFlags().String("profile", "", "")
RootCmd.PersistentFlags().String("stack-name", "", "")
RootCmd.PersistentFlags().String("stackit-s3-suffix", "", "")
RootCmd.PersistentFlags().String("stackit-tags", "", "")

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
Expand Down
19 changes: 15 additions & 4 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ package cmd
import (
"context"
"fmt"
"io"
"os"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/sts"
Expand All @@ -25,9 +29,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"os"
"strings"
)

func printUntilDone(ctx context.Context, events <-chan stackit.TailStackEvent, w io.Writer) {
Expand Down Expand Up @@ -65,6 +66,8 @@ func parseCLIInput(cmd *cobra.Command, args []string) stackit.StackitUpInput {
template, _ := cmd.PersistentFlags().GetString("template")
tags, _ := cmd.PersistentFlags().GetStringSlice("tag")
notificationArns, _ := cmd.PersistentFlags().GetStringSlice("notification-arn")
s3Suffix, _ := RootCmd.PersistentFlags().GetString("stackit-s3-suffix")
s3Tags, _ := RootCmd.PersistentFlags().GetString("stackit-tags")

input := stackit.StackitUpInput{
StackName: stackName,
Expand Down Expand Up @@ -101,6 +104,14 @@ func parseCLIInput(cmd *cobra.Command, args []string) stackit.StackitUpInput {
input.Tags = keyvalSliceToMap(tags)
}

if s3Suffix != "" {
input.S3Suffix = fmt.Sprintf("-%s", s3Suffix)
}

if s3Tags != "" {
input.S3Tags = s3Tags
}

return input
}

Expand All @@ -121,7 +132,7 @@ func up(cmd *cobra.Command, args []string) error {
defer printerCancel()

if templateFile, ok := input.Template.(*templateReader); ok && templateFile != nil {
template, err := packageTemplate(ctx, sess, input.StackName, templateFile, cmd.OutOrStderr())
template, err := packageTemplate(ctx, sess, input.StackName, input.S3Suffix, input.S3Tags, templateFile, cmd.OutOrStderr())
if err != nil {
return errors.Wrap(err, "packaging template")
}
Expand Down
39 changes: 38 additions & 1 deletion pkg/stackit/packager/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package packager

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
Expand All @@ -20,7 +22,8 @@ func (p *Packager) s3BucketName() (string, error) {
}

accountId := *getAccountResp.Account
bucketName := fmt.Sprintf("stackit-%s-%s", p.region, accountId)
// s3Suffix comes with a "-" if its set in params
bucketName := fmt.Sprintf("stackit-%s-%s%s", p.region, accountId, p.s3Suffix)

enableVersioning := func() error {
_, err := p.s3.PutBucketVersioning(&s3.PutBucketVersioningInput{
Expand All @@ -47,6 +50,19 @@ func (p *Packager) s3BucketName() (string, error) {
return "", err
}

if p.s3Tags != "" {
tags := getStackitTags(p.s3Tags)
_, err := p.s3.PutBucketTagging(&s3.PutBucketTaggingInput{
Bucket: &bucketName,
Tagging: &s3.Tagging{
TagSet: tags,
},
})
if err != nil {
return "", errors.Wrap(err, "Adding tags on bucket ")
}
}

p.cachedBucketName = bucketName
return bucketName, nil
}
Expand All @@ -64,3 +80,24 @@ func (p *Packager) s3BucketName() (string, error) {
p.cachedBucketName = bucketName
return bucketName, nil
}

func getStackitTags(tags string) []*s3.Tag {

tagList := strings.Split(tags, ",")
tagMap := make(map[string]string)
for _, pair := range tagList {
dict := strings.Split(pair, "=")
tagMap[dict[0]] = dict[1]
}

result := make([]*s3.Tag, 0, len(tagMap))
for k, v := range tagMap {
var t = &s3.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
result = append(result, t)
}

return result
}
27 changes: 16 additions & 11 deletions pkg/stackit/packager/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/glassechidna/stackit/pkg/stackit/cfnyaml"
"github.com/glassechidna/stackit/pkg/zipper"
"github.com/pkg/errors"
"io"
"os"
"path/filepath"
"strings"
)

type Packager struct {
s3 s3iface.S3API
sts stsiface.STSAPI
region string
s3 s3iface.S3API
sts stsiface.STSAPI
region string
s3Suffix string
s3Tags string

cachedBucketName string
}

func New(s3 s3iface.S3API, sts stsiface.STSAPI, region string) *Packager {
func New(s3 s3iface.S3API, sts stsiface.STSAPI, region string, s3Suffix string, s3Tags string) *Packager {
return &Packager{
s3: s3,
sts: sts,
region: region,
s3: s3,
sts: sts,
region: region,
s3Suffix: s3Suffix,
s3Tags: s3Tags,
}
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/stackit/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package stackit
import (
"context"
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/davecgh/go-spew/spew"
"github.com/glassechidna/stackit/pkg/stackit/changeset"
"github.com/pkg/errors"
"strings"
"time"
)

type Template interface {
Expand All @@ -28,6 +29,8 @@ type StackitUpInput struct {
Tags map[string]string
NotificationARNs []string
PopulateMissing bool
S3Suffix string
S3Tags string
}

func (s *Stackit) populateMissing(ctx context.Context, input *StackitUpInput) error {
Expand Down