-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR 1 for fogg #1
Merged
Merged
Changes from 68 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
7c6d523
basic config impl
ryanking 9df8fe6
wire up init to main()
ryanking 1862c13
add test for default config
ryanking 36ac5f6
wip
ryanking 45ceded
basic account plan resolution
ryanking 66a956c
resolving other accounts
ryanking 0a3268b
wip
ryanking 04fb86a
add owner and account ids to example
ryanking 26c6b51
remove out of date comment
ryanking 209d222
test resolveStringArray
ryanking e86868b
a basic test for plan.Plan
ryanking 39e5a31
remove unused function
ryanking a976184
remove shared-infra stuff for now
ryanking 155618a
fix comment
ryanking 72e8161
init basically working
ryanking e1b9bf5
move init to a module
ryanking b5a87d9
version variable
ryanking 6d6910c
refactor to use cobra for commands and flags
ryanking 58a3afc
add ldflags for install
ryanking a85ac1d
add flag to override config file
ryanking 96db5cf
print out "other accounts" better
ryanking 6e200e1
add terraform version
ryanking 67975e2
get infra bucket working for accounts
ryanking 552221f
add owner to account
ryanking a55f7d7
add project to account
ryanking 3e3e3e5
fix plan for provider profile
ryanking 991d551
add version to plan
ryanking 70807c5
move to czi github
ryanking 04fae34
add plan for modules
ryanking 6a7cc4b
add modules to plan.Print
ryanking 578f413
minimal error handling for plan command
ryanking 5af52d5
wip env plan
ryanking f0ad55a
wip env plan
ryanking b60f38c
print envs plan
ryanking 95b17fb
test components in plan
ryanking 0ecc579
gofmt -s -w
ryanking 3eada62
add `make lint`
ryanking e14b2f2
rename plan.Plan to plan.Eval
ryanking a7d42da
very basis repo-level stuff
ryanking 0b061ea
remove extension properly
ryanking 431fb1a
add scripts to repo
ryanking 928810d
remove rm functionality for now
ryanking 8dcde23
simplify plan data type
ryanking 735afe8
rename ssh_config script
ryanking 4c228a6
only touch if it doesn't exist
ryanking 485f372
add .create
ryanking ec4f93e
basic templating + ssh_config template
ryanking 0b9907f
templating working with ssh_config envs
ryanking f0c68d0
working top-level makefile template
ryanking 5c12c06
move test helper to util
ryanking f1574d8
cleanup
ryanking ba81959
small refactoring
ryanking bf39717
add makefile to accounts
ryanking 5201aa8
sicc.tf working in accounts
ryanking eb77e9d
env templating working
ryanking 989154f
cleanup some debug prints
ryanking 08868d7
apply for components
ryanking 5953c28
remove debug output
ryanking e058123
configure gometalinter to avoid comment notes
ryanking cffe6aa
more error handling
ryanking 535f6c2
fix version in tests
ryanking 3f061e1
naming for AccountID
ryanking 44fc433
naming for AccountID
ryanking 001b181
remove unused joinEnvs
ryanking ae42585
lint cleanup
ryanking cf14b1f
also ignore vars
ryanking 9cc291d
remove unused resolveOptional
ryanking ecd7384
another pass at lint stuff
ryanking 0aca0bb
remove unneccessary panic
ryanking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
fogg | ||
fogg | ||
coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"Exclude": [ | ||
"exported (\\w+) (\\w+) should have comment or be unexported" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
SHA=$(shell git rev-parse --short HEAD) | ||
VERSION=$(shell cat VERSION) | ||
DIRTY=$(shell if `git diff-index --quiet HEAD --`; then echo false; else echo true; fi) | ||
# TODO add release flag | ||
LDFLAGS=-ldflags "-w -s -X github.com/chanzuckerberg/fogg/util.GitSha=${SHA} -X github.com/chanzuckerberg/fogg/util.Version=${VERSION} -X github.com/chanzuckerberg/fogg/util.Dirty=${DIRTY}" | ||
|
||
all: test install | ||
|
||
lint: | ||
gometalinter --fast ./... | ||
|
||
lint-slow: | ||
gometalinter ./... | ||
|
||
build: | ||
go build . | ||
go build ${LDFLAGS} . | ||
|
||
coverage: | ||
go tool cover -html=coverage.out | ||
|
||
test: | ||
go test -cover ./... | ||
|
||
install: | ||
go install ${LDFLAGS} . | ||
|
||
.PHONY: build coverage test install lint lint-slow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package apply | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/chanzuckerberg/fogg/plan" | ||
"github.com/chanzuckerberg/fogg/templates" | ||
"github.com/chanzuckerberg/fogg/util" | ||
"github.com/gobuffalo/packr" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
const rootPath = "terraform" | ||
|
||
func Apply(fs afero.Fs, configFile string, tmp *templates.T) error { | ||
p, err := plan.Eval(fs, configFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e := applyRepo(fs, p, &tmp.Repo) | ||
if e != nil { | ||
return e | ||
} | ||
|
||
e = applyAccounts(fs, p, &tmp.Account) | ||
if e != nil { | ||
return e | ||
} | ||
|
||
e = applyEnvs(fs, p, &tmp.Env, &tmp.Component) | ||
if e != nil { | ||
return e | ||
} | ||
|
||
// TODO global | ||
|
||
return nil | ||
} | ||
|
||
func applyRepo(fs afero.Fs, p *plan.Plan, repoBox *packr.Box) error { | ||
return applyTree(repoBox, fs, p) | ||
} | ||
|
||
func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) { | ||
for account, accountPlan := range p.Accounts { | ||
path := fmt.Sprintf("%s/accounts/%s", rootPath, account) | ||
e = fs.MkdirAll(path, 0755) | ||
if e != nil { | ||
return e | ||
} | ||
e = applyTree(accountBox, afero.NewBasePathFs(fs, path), accountPlan) | ||
if e != nil { | ||
return e | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr.Box) (e error) { | ||
for env, envPlan := range p.Envs { | ||
path := fmt.Sprintf("%s/envs/%s", rootPath, env) | ||
e = fs.MkdirAll(path, 0755) | ||
if e != nil { | ||
return e | ||
} | ||
e := applyTree(envBox, afero.NewBasePathFs(fs, path), envPlan) | ||
if e != nil { | ||
return e | ||
} | ||
for component, componentPlan := range envPlan.Components { | ||
path := fmt.Sprintf("%s/envs/%s/%s", rootPath, env, component) | ||
e = fs.MkdirAll(path, 0755) | ||
if e != nil { | ||
return e | ||
} | ||
e := applyTree(componentBox, afero.NewBasePathFs(fs, path), componentPlan) | ||
if e != nil { | ||
return e | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) { | ||
return source.Walk(func(path string, sourceFile packr.File) error { | ||
extension := filepath.Ext(path) | ||
if extension == ".tmpl" { | ||
|
||
err := applyTemplate(sourceFile, dest, path, subst) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// if dest.endswith('.tf'): | ||
// subprocess.call(['terraform', 'fmt', dest]) | ||
} else if extension == ".touch" { | ||
d := removeExtension(path) | ||
_, err := dest.Stat(d) | ||
if err != nil { // TODO we might not want to do this for all errors | ||
log.Printf("touching %s", d) | ||
_, e = dest.Create(d) | ||
if e != nil { | ||
return e | ||
} | ||
} else { | ||
log.Printf("skipping touch on existing file %s", d) | ||
} | ||
// if dest.endswith('.tf'): | ||
// subprocess.call(['terraform', 'fmt', dest]) | ||
|
||
} else if extension == ".create" { | ||
d := removeExtension(path) | ||
_, err := dest.Stat(d) | ||
if err != nil { // TODO we might not want to do this for all errors | ||
log.Printf("creating %s", d) | ||
e = afero.WriteReader(dest, path, sourceFile) | ||
if e != nil { | ||
return e | ||
} | ||
} else { | ||
log.Printf("skipping create on existing file %s", d) | ||
} | ||
// if dest.endswith('.tf'): | ||
// subprocess.call(['terraform', 'fmt', dest]) | ||
|
||
} else { | ||
log.Printf("copying %s", path) | ||
e = afero.WriteReader(dest, path, sourceFile) | ||
if e != nil { | ||
return e | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
} | ||
|
||
func removeExtension(path string) string { | ||
return strings.TrimSuffix(path, filepath.Ext(path)) | ||
} | ||
|
||
func applyTemplate(sourceFile io.Reader, dest afero.Fs, path string, overrides interface{}) error { | ||
d := removeExtension(path) | ||
log.Printf("templating %s", d) | ||
writer, err := dest.OpenFile(d, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) | ||
if err != nil { | ||
panic(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why panic vs return the error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question – I don't know why I did this, I will change it. |
||
} | ||
t := util.OpenTemplate(sourceFile) | ||
return t.Execute(writer, overrides) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package apply | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveExtension(t *testing.T) { | ||
x := removeExtension("foo") | ||
assert.Equal(t, "foo", x) | ||
|
||
x = removeExtension("foo.txt") | ||
assert.Equal(t, "foo", x) | ||
|
||
x = removeExtension("foo.txt.asdf") | ||
assert.Equal(t, "foo.txt", x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/chanzuckerberg/fogg/apply" | ||
"github.com/chanzuckerberg/fogg/templates" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
applyCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.") | ||
rootCmd.AddCommand(applyCmd) | ||
} | ||
|
||
var applyCmd = &cobra.Command{ | ||
Use: "apply", | ||
Short: "Run an apply", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var e error | ||
pwd, e := os.Getwd() | ||
if e != nil { | ||
panic(e) | ||
} | ||
fs := afero.NewBasePathFs(afero.NewOsFs(), pwd) | ||
configFile, e := cmd.Flags().GetString("config") | ||
if e != nil { | ||
panic(e) | ||
} | ||
|
||
e = apply.Apply(fs, configFile, templates.Templates) | ||
if e != nil { | ||
panic(e) | ||
} | ||
// apply.Print(p) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
fogg_init "github.com/chanzuckerberg/fogg/init" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(initCmd) | ||
} | ||
|
||
var initCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "Run init", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var e error | ||
pwd, e := os.Getwd() | ||
if e != nil { | ||
panic(e) | ||
} | ||
fs := afero.NewBasePathFs(afero.NewOsFs(), pwd) | ||
|
||
e = fogg_init.Init(fs) | ||
if e != nil { | ||
panic(e) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/chanzuckerberg/fogg/plan" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
planCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.") | ||
rootCmd.AddCommand(planCmd) | ||
} | ||
|
||
var planCmd = &cobra.Command{ | ||
Use: "plan", | ||
Short: "Run a plan", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var e error | ||
pwd, e := os.Getwd() | ||
if e != nil { | ||
panic(e) | ||
} | ||
fs := afero.NewBasePathFs(afero.NewOsFs(), pwd) | ||
configFile, e := cmd.Flags().GetString("config") | ||
if e != nil { | ||
panic(e) | ||
} | ||
|
||
p, e := plan.Eval(fs, configFile) | ||
if e != nil { | ||
panic(e) | ||
} | ||
e = plan.Print(p) | ||
if e != nil { | ||
panic(e) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "fogg", | ||
Short: "", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Do Stuff Here | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/chanzuckerberg/fogg/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number of fogg", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
v, e := util.VersionString() | ||
if e != nil { | ||
panic(e) | ||
} | ||
fmt.Println(v) | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be worth
errors.Wrap
these to capture stacks/potentially easier debugging?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good idea. This is a large diff already, so I will add a follow-up task to handle that https://app.asana.com/0/335732894489412/723850669292560
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good idea. This is a large diff already, so I will add a follow-up task to handle that https://app.asana.com/0/335732894489412/723850669292560