From 473706c3be289381693a7acc2c9970caffffda69 Mon Sep 17 00:00:00 2001 From: Jeff Roche <82384784+jeff-roche@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:17:10 -0400 Subject: [PATCH] feat: config command runner (#6) * feat: allow running of external commands like kubectx * fix: documentation and such --- README.md | 15 ++++++++++++++- example.biome.yaml | 9 +++++++-- go.mod | 5 ++++- go.sum | 16 ++++++++++++---- main.go | 9 ++------- src/lib/cmdr/cmdr.go | 20 ++++++++++++++++++++ src/lib/types/biome.go | 1 + src/repos/dragoman.go | 2 +- src/services/biome_configuration_service.go | 11 +++++++++++ 9 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 src/lib/cmdr/cmdr.go diff --git a/README.md b/README.md index 58d52d8..90e7357 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,18 @@ environment: ... ``` +### Commands +Additional commands can be run using the commands setting. Any commands specified will be run as the last steps prior to running the top level command specified when running biome. + +```yaml +# .biome.yaml +name: my-biome +commands: + - kubectx my-k8s-context +environment: + ... +``` + ## Usage The most common use case is for use with scripts that need context via environment variables. The need for this tool came about for CI/CD scripts that need AWS context as well as additional environment variables that change based on certain states. This tool will allow you to configure those different states and provide that context to your scripts and pipelines. @@ -65,5 +77,6 @@ $ onstaging ./bin/ci/deploy-service.sh - :white_check_mark: Implement some tests - Loading Environment variables from a .env file - :white_check_mark: Encrypted environment variables via [dragoman](https://github.com/meltwater/dragoman) -- Kubernetes context setting +- :white_check_mark: Kubernetes context setting + - *NOTE* this is done through commands - Potentially switching to [cobra](https://github.com/spf13/cobra) for the cli \ No newline at end of file diff --git a/example.biome.yaml b/example.biome.yaml index 963cce1..281509d 100644 --- a/example.biome.yaml +++ b/example.biome.yaml @@ -5,8 +5,13 @@ environment: # Additional environment vars to load in MY_USEFUL_ENV: "A value I need" MY_OTHER_ENV: "Another value I need" MY_AWS_SECRET_ENV: - secret_arn: "{{ARN}}" - secret_json_key: "my_super_secret_key" + secret_arn: "{{ARN}}" # Secrets manager ARN + secret_json_key: "my_super_secret_key" # JSON key in the secret + MY_DRAGOMAN_SECRET_ENV: + from_dragoman: "[ENC,...]" # Tells the biome to decrypt this secret +commands: # Any additional config steps needed, this is the last thing run + - kubectx my-k8s-context + - npm run someconfigscript --- diff --git a/go.mod b/go.mod index 377ae81..c4ce8c2 100644 --- a/go.mod +++ b/go.mod @@ -23,8 +23,11 @@ require ( github.com/aws/aws-sdk-go-v2/service/sso v1.11.7 // indirect github.com/aws/smithy-go v1.11.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.1.1 // indirect + github.com/stretchr/objx v0.2.0 // indirect golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index d79219f..aae46f9 100644 --- a/go.sum +++ b/go.sum @@ -37,23 +37,31 @@ github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/meltwater/dragoman v1.2.2 h1:ZdpdB2vkm9grPjqvp9/6eZEmGubCQtVPuMUi4NDgdoE= github.com/meltwater/dragoman v1.2.2/go.mod h1:waYPsylnXTj4F7xblDEeDEKqxNPZjkqcLYd/6DaUoOM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce h1:Roh6XWxHFKrPgC/EQhVubSAGQ6Ozk6IdxHSzt1mR0EI= golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 88531c3..04bfa27 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,8 @@ import ( "flag" "fmt" "log" - "os" - "os/exec" + "github.com/jeff-roche/biome/src/lib/cmdr" "github.com/jeff-roche/biome/src/services" ) @@ -51,11 +50,7 @@ func main() { } // Execute order 66 - cmd := exec.Command(cmds[0], cmds[1:]...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin - if err := cmd.Run(); err != nil { + if err := cmdr.Run(cmds[0], cmds[1:]...); err != nil { log.Fatal(err) } } diff --git a/src/lib/cmdr/cmdr.go b/src/lib/cmdr/cmdr.go new file mode 100644 index 0000000..7151480 --- /dev/null +++ b/src/lib/cmdr/cmdr.go @@ -0,0 +1,20 @@ +package cmdr + +import ( + "os" + "os/exec" +) + +func Run(cmdStr string, args ...string) error { + cmd := exec.Command(cmdStr, args...) + + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + + if err := cmd.Run(); err != nil { + return err + } + + return nil +} diff --git a/src/lib/types/biome.go b/src/lib/types/biome.go index 4b77b5a..99e6066 100644 --- a/src/lib/types/biome.go +++ b/src/lib/types/biome.go @@ -8,5 +8,6 @@ type Biome struct { type BiomeConfig struct { Name string AwsProfile string `yaml:"aws_profile"` + Commands []string `yaml:"commands"` Environment map[string]interface{} `yaml:"environment"` } diff --git a/src/repos/dragoman.go b/src/repos/dragoman.go index ce7c978..e937461 100644 --- a/src/repos/dragoman.go +++ b/src/repos/dragoman.go @@ -2,7 +2,7 @@ package repos import "github.com/meltwater/dragoman/cryptography" -// The interface for the Secrets Manager Repository +// The interface for the Dragoman Repository type DragomanRepoIfc interface { Decrypt(string) (string, error) } diff --git a/src/services/biome_configuration_service.go b/src/services/biome_configuration_service.go index b927574..5339a87 100644 --- a/src/services/biome_configuration_service.go +++ b/src/services/biome_configuration_service.go @@ -3,7 +3,9 @@ package services import ( "fmt" "path" + "strings" + "github.com/jeff-roche/biome/src/lib/cmdr" "github.com/jeff-roche/biome/src/lib/fileio" "github.com/jeff-roche/biome/src/lib/setters" "github.com/jeff-roche/biome/src/lib/types" @@ -98,5 +100,14 @@ func (svc *BiomeConfigurationService) ActivateBiome() error { } } + // Any other environment setup commands + if len(svc.ActiveBiome.Config.Commands) > 0 { + for _, cmd := range svc.ActiveBiome.Config.Commands { + parts := strings.Split(cmd, " ") + + cmdr.Run(parts[0], parts[1:]...) + } + } + return nil }