Skip to content
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

WIP: Remove docker #195

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ var applyCmd = &cobra.Command{
openGitOrExit(pwd)

config, err := readAndValidateConfig(fs, configFile, verbose)

e = mergeConfigValidationErrors(err)
if e != nil {
return e
Expand Down
162 changes: 162 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"path"
"runtime"

"github.com/chanzuckerberg/fogg/config"
"github.com/chanzuckerberg/fogg/errs"
"github.com/chanzuckerberg/fogg/plugins"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

func init() {
setupCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
setupCmd.Flags().BoolP("verbose", "v", false, "use this to turn on verbose output")
rootCmd.AddCommand(setupCmd)
}

var setupCmd = &cobra.Command{
Use: "setup",
Short: "Setup dependencies for this project",
Long: `This command will set up dependencies for this project.
These include things like tfenv, terraform, and custom plugins.`,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
setupDebug(debug)
pwd, err := os.Getwd()
if err != nil {
return errs.WrapInternal(err, "Could not Getwd")
}
fs := afero.NewBasePathFs(afero.NewOsFs(), pwd)

// handle flags
verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
return errs.WrapInternal(err, "Could not parse verbose flag")
}
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return errs.WrapInternal(err, "Could not parse config flag")
}

// parse config
config, err := readAndValidateConfig(fs, configFile, verbose)
err = mergeConfigValidationErrors(err)
if err != nil {
return err
}

// check that we are at root of initialized git repo
openGitOrExit(pwd)

// create the setup
setup := setup{
config: config,
pwd: pwd,
}
err = setup.tfEnv()
if err != nil {
return err
}
err = setup.terraform()
if err != nil {
return err
}
err = setup.customProviders()
if err != nil {
return err
}
return setup.customPlugins()
},
}

// is a namespace
type setup struct {
pwd string
config *config.Config
fs afero.Fs
}

func (s *setup) getOsArchPathComponent() string {
return fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
}

func (s *setup) getBinPath() string {
return path.Join(s.pwd, plugins.BinDir, s.getOsArchPathComponent())
}

func (s *setup) getTfenvPath() string {
return path.Join(s.getBinPath(), ".tfenv")
}

// tfEnv installs tfEnv
func (s *setup) tfEnv() error {
// some paths
tfenvPath := s.getTfenvPath()
tfenvExecPathSrc := path.Join(tfenvPath, "bin", "tfenv")
tfenvExecPathTarget := path.Join(s.getBinPath(), "tfenv")
terraformExecPathSrc := path.Join(tfenvPath, "bin", "terraform")
terraformExecPathTarget := path.Join(s.getBinPath(), "terraform")

_, err := os.Stat(tfenvPath)
if err == nil {
// if something here, remove it to start clean
// TODO: error handling?
os.RemoveAll(tfenvPath)
os.Remove(tfenvExecPathTarget)
os.Remove(terraformExecPathTarget)
}
if err != nil && !os.IsNotExist(err) {
return errs.WrapInternal(err, "Could not stat tfenv dir")
}
log.Debugf("Git clone to %s", tfenvPath)
// not exist error
cmd := exec.Command("git", "clone", "https://github.com/kamatama41/tfenv.git", tfenvPath)
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return errs.WrapInternal(err, "Could not clone tfenv")
}

// link tfenv and terraform
err = os.Symlink(tfenvExecPathSrc, tfenvExecPathTarget)
if err != nil {
return errs.WrapInternal(err, "Could not link tfenv")
}
err = os.Symlink(terraformExecPathSrc, terraformExecPathTarget)
if err != nil {
return errs.WrapInternal(err, "Could not link terraform")
}
return nil
}

// terraform installs terraform
// assume tfenv already present
func (s *setup) terraform() error {
pathEnv := fmt.Sprintf("PATH=%s:%s", os.Getenv("PATH"), s.getBinPath())
log.Debugf("path: %s", pathEnv)
cmd := exec.Command("tfenv", "install", s.config.Defaults.TerraformVersion)
// cmd := exec.Command("printenv")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = []string{pathEnv}

err := cmd.Run()
return errs.WrapInternal(err, "Could not install terraform")
}

// customProviders installs custom providers
func (s *setup) customProviders() error {
return nil
}

// customPlugins installs custom plugins
func (s *setup) customPlugins() error {
return nil
}
2 changes: 1 addition & 1 deletion plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Plugins struct {
func (p *Plugins) SetCustomPluginsPlan(customPlugins map[string]*plugins.CustomPlugin) {
p.CustomPlugins = customPlugins
for _, plugin := range p.CustomPlugins {
plugin.SetTargetPath(plugins.CustomPluginDir)
plugin.SetTargetPath(plugins.BinDir)
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const (
// We vendor providers here
// See https://www.terraform.io/docs/configuration/providers.html#third-party-plugins
TerraformCustomPluginCacheDir = "terraform.d/plugins/linux_amd64"
// CustomPluginDir where we place custom binaries
CustomPluginDir = ".bin"
// BinDir where we place custom binaries
BinDir = ".bin"
)
12 changes: 6 additions & 6 deletions plugins/custom_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCustomPluginTar(t *testing.T) {
URL: ts.URL,
Format: plugins.TypePluginFormatTar,
}
customPlugin.SetTargetPath(plugins.CustomPluginDir)
customPlugin.SetTargetPath(plugins.BinDir)
a.Nil(customPlugin.Install(fs, pluginName))

afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
Expand All @@ -49,7 +49,7 @@ func TestCustomPluginTar(t *testing.T) {
})

for _, file := range files {
filePath := path.Join(plugins.CustomPluginDir, file)
filePath := path.Join(plugins.BinDir, file)
fi, err := fs.Stat(filePath)
a.Nil(err)
a.False(fi.IsDir())
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestCustomPluginZip(t *testing.T) {
URL: ts.URL,
Format: plugins.TypePluginFormatZip,
}
customPlugin.SetTargetPath(plugins.CustomPluginDir)
customPlugin.SetTargetPath(plugins.BinDir)
a.Nil(customPlugin.Install(fs, pluginName))

afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
Expand All @@ -91,7 +91,7 @@ func TestCustomPluginZip(t *testing.T) {
})

for _, file := range files {
filePath := path.Join(plugins.CustomPluginDir, file)
filePath := path.Join(plugins.BinDir, file)
fi, err := fs.Stat(filePath)
a.Nil(err)
a.False(fi.IsDir())
Expand Down Expand Up @@ -120,15 +120,15 @@ func TestCustomPluginBin(t *testing.T) {
Format: plugins.TypePluginFormatBin,
}

customPlugin.SetTargetPath(plugins.CustomPluginDir)
customPlugin.SetTargetPath(plugins.BinDir)
a.Nil(customPlugin.Install(fs, pluginName))

afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
a.Nil(err)
return nil
})

customPluginPath := path.Join(plugins.CustomPluginDir, pluginName)
customPluginPath := path.Join(plugins.BinDir, pluginName)
f, err := fs.Open(customPluginPath)
a.Nil(err)

Expand Down
1 change: 1 addition & 0 deletions templates/repo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ terraform/**/terraform.d

# Pycharm folder
.idea
.bin
2 changes: 2 additions & 0 deletions templates/repo/scripts/dependencies.mk.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ ifeq ($(shell uname -m),aarch64)
ARCH ?= "arm"
endif

export PATH := $(REPO_ROOT)/.bin/$(OS)_$(ARCH):$(PATH)

TF_PRESENT := $(shell command -v terraform 2> /dev/null)
TERRAFORM_CURRENT_VERSION := $(shell terraform version | head -n1 | sed 's/Terraform v//g')

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading