Skip to content

Commit

Permalink
Bugfix/regional regions cli (#38)
Browse files Browse the repository at this point in the history
* bugfix: regional regions cli flag now works when not passing in primary flag

* feat: ability to declare a dockerfile to have runiac build rather than runiac provided dockerfile

* skip: go mod tidy
  • Loading branch information
tiny-dancer authored Oct 18, 2021
1 parent 8d69b4c commit 0910549
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 148 deletions.
53 changes: 42 additions & 11 deletions cmd/cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ var Local bool
var Runner string
var PullRequest string
var StepWhitelist []string
var Dockerfile string

// the base container for runiac
// DefaultBaseContainer the base container for runiac
var DefaultBaseContainer = "runiac/deploy:latest-alpine-full"
var DefaultDockerfile = ".runiac/Dockerfile"

func init() {
deployCmd.Flags().StringVarP(&AppVersion, "version", "v", "", "Version of the iac code")
Expand All @@ -55,6 +57,7 @@ func init() {
deployCmd.Flags().StringVarP(&Runner, "runner", "", "terraform", "The deployment tool to use for deploying infrastructure")
deployCmd.Flags().StringSliceVarP(&StepWhitelist, "steps", "s", []string{}, "Only run the specified steps. To specify steps inside a track: -s {trackName}/{stepName}. To run multiple steps, separate with a comma. If empty, it will run all steps. To run no steps, specify a non-existent step.")
deployCmd.Flags().StringVar(&PullRequest, "pull-request", "", "Pre-configure settings to create an isolated configuration specific to a pull request, provide pull request identifier")
deployCmd.Flags().StringVarP(&Dockerfile, "dockerfile", "f", "",fmt.Sprintf("The dockerfile runiac builds to execute the deploy in, defaults to the autogenerated '%s' and must derive from runiac/deploy:{version}-alpine. Runiac official dockerfiles are here: https://github.com/runiac/docker", DefaultDockerfile))

rootCmd.AddCommand(deployCmd)
}
Expand All @@ -75,23 +78,45 @@ var deployCmd = &cobra.Command{
buildKit := "DOCKER_BUILDKIT=1"
containerTag := viper.GetString("project")

cmdd := exec.Command("docker", "build", "-t", containerTag, "-f", ".runiac/Dockerfile")
if Dockerfile != "" {
//Dockerfile = Dockerfile
} else if viper.GetString("dockerfile") != "" {
Dockerfile = viper.GetString("dockerfile")
} else {
Dockerfile = DockerfileTemplate
}

cmdd := exec.Command("docker", "build", "-t", containerTag, "-f", Dockerfile)

cmdd.Args = append(cmdd.Args, getBuildArguments()...)

logrus.Info(strings.Join(cmdd.Args, " "))

var stdoutBuf, stderrBuf bytes.Buffer

cmdd.Env = append(os.Environ(), buildKit)
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Suffix = " Building project container..."
s.Start()
b, err := cmdd.CombinedOutput()
if err != nil {

if Dockerfile != "" {
cmdd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmdd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)

err := cmdd.Run()
if err != nil {
log.Fatalf("Runiac failed to build %s", Dockerfile)
}
} else {
s.Start()
b, err := cmdd.CombinedOutput()
if err != nil {
s.Stop()
logrus.Error(string(b))
logrus.WithError(err).Fatalf("Building project container failed with %s\n", err)
}

s.Stop()
logrus.Error(string(b))
logrus.WithError(err).Fatalf("Building project container failed with %s\n", err)
}
s.Stop()

logrus.Info("Completed build, lets run!")

Expand Down Expand Up @@ -127,8 +152,8 @@ var deployCmd = &cobra.Command{
cmd2.Args = appendEIfSet(cmd2.Args, "PRIMARY_REGION", PrimaryRegions[0])
}

if len(PrimaryRegions) > 0 {
cmd2.Args = appendEIfSet(cmd2.Args, "REGIONAL_REGIONS", strings.Join(append(RegionalRegions, PrimaryRegions[0]), ","))
if len(RegionalRegions) > 0 {
cmd2.Args = appendEIfSet(cmd2.Args, "REGIONAL_REGIONS", strings.Join(RegionalRegions, ","))
}
cmd2.Args = appendEIfSet(cmd2.Args, "ACCOUNT_ID", Account)
cmd2.Args = appendEIfSet(cmd2.Args, "LOG_LEVEL", LogLevel)
Expand All @@ -137,7 +162,7 @@ var deployCmd = &cobra.Command{
cmd2.Args = append(cmd2.Args, "-it")
}

// TODO: how to allow consumer whitelist environment variables or simply pass all in?
// TODO: how best to allow consumer whitelist environment variables or simply pass all in?
for _, env := range cmd2.Env {
if strings.HasPrefix(env, "TF_VAR_") {
cmd2.Args = append(cmd2.Args, "-e", env)
Expand All @@ -150,6 +175,12 @@ var deployCmd = &cobra.Command{
}
}

for _, env := range cmd2.Env {
if strings.HasPrefix(env, "RUNIAC_") {
cmd2.Args = append(cmd2.Args, "-e", env)
}
}

// handle local volume maps
dir, err := os.Getwd()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ func TestGetBuildArguments_ShouldSetBuildArgContainerOnlyWhenValueExists(t *test
result := getBuildArguments()
require.Equal(t, expected, result)
}
}
}
4 changes: 2 additions & 2 deletions cmd/cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func InitAction() bool {
logrus.Debug("Creating .runiac directory")
_ = appFS.Mkdir(".runiac", 0755)

dockerfile := strings.ReplaceAll(Dockerfile, "${BASE_CONTAINER}", BaseContainer)
dockerfile := strings.ReplaceAll(DockerfileTemplate, "${BASE_CONTAINER}", BaseContainer)

logrus.Debug("Writing .runiac/Dockerfile")
err := afero.WriteFile(appFS, ".runiac/Dockerfile", []byte(dockerfile), 0644)
Expand All @@ -42,7 +42,7 @@ const DockerIgnore = `# do not edit --- autogenerated by runiac --- do not edit
.runiac/
`

const Dockerfile = `# do not edit --- autogenerated by runiac --- do not edit
const DockerfileTemplate = `# do not edit --- autogenerated by runiac --- do not edit
# syntax = docker/dockerfile:experimental
ARG RUNIAC_CONTAINER="runiac/deploy:latest-alpine-full"
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ require (
github.com/go-errors/errors v1.0.1
github.com/go-playground/validator/v10 v10.1.0
github.com/golang/mock v1.3.1
github.com/gookit/color v1.4.2 // indirect
github.com/gookit/color v1.4.2
github.com/gruntwork-io/gruntwork-cli v0.4.2
github.com/gruntwork-io/terratest v0.17.5
github.com/hashicorp/go-getter v1.5.2
github.com/mitchellh/mapstructure v1.1.2
github.com/otiai10/copy v1.4.2
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.0
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.1 // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
Expand Down
Loading

0 comments on commit 0910549

Please sign in to comment.