diff --git a/cmd/plural/up.go b/cmd/plural/up.go index cbdc2223..ece81ba4 100644 --- a/cmd/plural/up.go +++ b/cmd/plural/up.go @@ -32,8 +32,6 @@ func (p *Plural) handleUp(c *cli.Context) error { return err } - defer ctx.Cleanup() - if err := ctx.Backfill(); err != nil { return err } diff --git a/pkg/up/deploy.go b/pkg/up/deploy.go index 3b50eced..012fb7c4 100644 --- a/pkg/up/deploy.go +++ b/pkg/up/deploy.go @@ -23,13 +23,13 @@ func (ctx *Context) Deploy(commit func() error) error { } if err := runAll([]terraformCmd{ - {dir: "./clusters", cmd: "init", args: []string{"-upgrade"}}, - {dir: "./clusters", cmd: "apply", args: []string{"-auto-approve"}, retries: 1}, + {dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}}, + {dir: "./terraform/mgmt", cmd: "apply", args: []string{"-auto-approve"}, retries: 1}, }); err != nil { return err } - stateCmd := &terraformCmd{dir: "./clusters"} + stateCmd := &terraformCmd{dir: "./terraform/mgmt"} outs, err := stateCmd.outputs() if err != nil { return err @@ -56,16 +56,20 @@ func (ctx *Context) Deploy(commit func() error) error { utils.Highlight("\nSetting up gitops management...\n") - return runAll([]terraformCmd{ - {dir: "./apps/terraform", cmd: "init", args: []string{"-upgrade"}}, - {dir: "./apps/terraform", cmd: "apply", args: []string{"-auto-approve"}, retries: 1}, - }) + if err := runAll([]terraformCmd{ + {dir: "./terraform/apps", cmd: "init", args: []string{"-upgrade"}}, + {dir: "./terraform/apps", cmd: "apply", args: []string{"-auto-approve"}, retries: 1}, + }); err != nil { + return err + } + + return ctx.Prune() } func (ctx *Context) Destroy() error { return runAll([]terraformCmd{ - {dir: "./clusters", cmd: "init", args: []string{"-upgrade"}}, - {dir: "./clusters", cmd: "destroy", args: []string{"-auto-approve"}, retries: 2}, + {dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}}, + {dir: "./terraform/mgmt", cmd: "destroy", args: []string{"-auto-approve"}, retries: 2}, }) } diff --git a/pkg/up/generate.go b/pkg/up/generate.go index 275163f4..5c46b35d 100644 --- a/pkg/up/generate.go +++ b/pkg/up/generate.go @@ -21,7 +21,7 @@ func (ctx *Context) Cleanup() { func (ctx *Context) Generate() error { if !utils.Exists("./bootstrap") { - if err := git.BranchedSubmodule("https://github.com/pluralsh/bootstrap.git", "stacks-support"); err != nil { + if err := git.BranchedSubmodule("https://github.com/pluralsh/bootstrap.git", "refactored-up"); err != nil { return err } } @@ -31,11 +31,11 @@ func (ctx *Context) Generate() error { {from: "./bootstrap/charts/runtime/values.yaml.tpl", to: "./helm-values/runtime.yaml", overwrite: true}, {from: "./bootstrap/helm/certmanager.yaml", to: "./helm-values/certmanager.yaml", overwrite: true}, {from: "./bootstrap/helm/flux.yaml", to: "./helm-values/flux.yaml", overwrite: true}, - {from: fmt.Sprintf("./bootstrap/templates/providers/bootstrap/%s.tf", prov), to: "clusters/provider.tf"}, - {from: fmt.Sprintf("./bootstrap/templates/setup/providers/%s.tf", prov), to: "clusters/mgmt.tf"}, - {from: "./bootstrap/templates/setup/console.tf", to: "clusters/console.tf"}, - {from: fmt.Sprintf("./bootstrap/templates/providers/apps/%s.tf", prov), to: "apps/terraform/provider.tf"}, - {from: "./bootstrap/templates/setup/cd.tf", to: "apps/terraform/cd.tf"}, + {from: fmt.Sprintf("./bootstrap/templates/providers/bootstrap/%s.tf", prov), to: "terraform/mgmt/provider.tf"}, + {from: fmt.Sprintf("./bootstrap/templates/setup/providers/%s.tf", prov), to: "terraform/mgmt/mgmt.tf"}, + {from: "./bootstrap/templates/setup/console.tf", to: "terraform/mgmt/console.tf"}, + {from: fmt.Sprintf("./bootstrap/templates/providers/apps/%s.tf", prov), to: "terraform/apps/provider.tf"}, + {from: "./bootstrap/templates/setup/cd.tf", to: "terraform/apps/cd.tf"}, {from: "./bootstrap/README.md", to: "README.md", overwrite: true}, } @@ -52,7 +52,7 @@ func (ctx *Context) Generate() error { copies := []templatePair{ {from: "./bootstrap/terraform/modules/clusters", to: "terraform/modules/clusters"}, - {from: fmt.Sprintf("./bootstrap/terraform/clouds/%s", prov), to: "terraform/modules/mgmt"}, + {from: fmt.Sprintf("./bootstrap/terraform/clouds/%s", prov), to: "terraform/mgmt/cluster"}, {from: "./bootstrap/apps/repositories", to: "apps/repositories"}, {from: "./bootstrap/apps/services", to: "apps/services"}, {from: "./bootstrap/templates", to: "templates"}, diff --git a/pkg/up/prune.go b/pkg/up/prune.go new file mode 100644 index 00000000..1b2e873a --- /dev/null +++ b/pkg/up/prune.go @@ -0,0 +1,48 @@ +package up + +import ( + "os" + "os/exec" + + "github.com/pluralsh/plural-cli/pkg/utils" + "github.com/pluralsh/plural-cli/pkg/utils/git" +) + +func (ctx *Context) Prune() error { + utils.Highlight("\nCleaning up unneeded resources...\n\n") + repoRoot, err := git.Root() + if err != nil { + return err + } + + toRemove := []string{ + "null_resource.console", + "helm_release.certmanager", + "helm_release.flux", + "helm_release.runtime", + "helm_release.console", + } + + for _, field := range toRemove { + if err := stateRm("./terraform/mgmt", field); err != nil { + return err + } + } + + if err := os.Remove("./terraform/mgmt/console.tf"); err != nil { + return err + } + + _ = os.RemoveAll("./terraform/apps") + ctx.Cleanup() + + return git.Sync(repoRoot, "Post-setup resource cleanup", true) +} + +func stateRm(dir, field string) error { + cmd := exec.Command("terraform", "state", "rm", field) + cmd.Dir = dir + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +}