Skip to content

Commit

Permalink
Merge pull request #132 from shalb/new-generator
Browse files Browse the repository at this point in the history
New generator
  • Loading branch information
romanprog authored May 27, 2021
2 parents 4ff27a9 + faac998 commit 475e56c
Show file tree
Hide file tree
Showing 39 changed files with 322 additions and 454 deletions.
6 changes: 0 additions & 6 deletions examples/aws-eks/backend.yaml

This file was deleted.

37 changes: 0 additions & 37 deletions examples/aws-eks/infra.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions examples/aws-eks/project.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions examples/aws-k3s/backend.yaml

This file was deleted.

30 changes: 0 additions & 30 deletions examples/aws-k3s/infra.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions examples/aws-k3s/project.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions examples/do-k8s/backend.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions examples/do-k8s/infra.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions examples/do-k8s/project.yaml

This file was deleted.

29 changes: 15 additions & 14 deletions pkg/cmd/cdev/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cdev

import (
"fmt"
"strings"

"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/config"
Expand All @@ -22,8 +21,8 @@ func init() {
rootCmd.AddCommand(projectCmd)
projectCmd.AddCommand(projectLs)
projectCmd.AddCommand(projectCreate)
projectCreate.Flags().BoolVar(&config.Global.Interactive, "interactive", false, "Use intteractive mode to for project generation")
projectCreate.Flags().BoolVar(&listAllTemplates, "list-templates", false, "Show all available templates for project generator.")
projectCreate.Flags().BoolVar(&config.Global.Interactive, "interactive", false, "Use interactive mode for project generation")
projectCreate.Flags().BoolVar(&listAllTemplates, "list-templates", false, "Show all available templates for project generation")
}

// projectsCmd represents the plan command
Expand All @@ -39,27 +38,29 @@ var projectLs = &cobra.Command{
log.Info("Project info:")
p.PrintInfo()
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("requires a template git URL argument")
}
if len(args) > 2 {
return fmt.Errorf("too many arguments")
}
return nil
},
}

// projectsCmd represents the plan command
var projectCreate = &cobra.Command{
Use: "create",
Short: "Generate new project from template in curent dir. Directory must be empty",
Run: func(cmd *cobra.Command, args []string) {

if listAllTemplates {
list, err := ui.GetProjectTemplates()
if err != nil {
log.Fatalf("List project templates: %v", err.Error())
}
res := strings.Join(list, "\n")
fmt.Println(res)
return
}
if project.ProjectsFilesExists() {
log.Fatalf("project creating: some project's data (yaml files) found in current directory, use command in empty dir")
}
err := ui.CreteProject(config.Global.WorkingDir, args...)
if len(args) < 1 {
log.Fatal("project creating: ")
}
err := ui.CreteProject(config.Global.WorkingDir, args[0], args[1:]...)
if err != nil {
log.Fatalf("Create project: %v", err.Error())
}
Expand Down
26 changes: 24 additions & 2 deletions pkg/modules/terraform/common/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/hcltools"
Expand Down Expand Up @@ -113,10 +114,31 @@ func (m *Module) BuildCommon() error {
}

if m.preHook != nil {
m.filesList["pre_hook.sh"] = []byte(m.preHook.Command)
hookCmd, err := m.replaceRemoteStatesForBash(m.preHook.Command)
if err != nil {
return err
}
m.filesList["pre_hook.sh"] = []byte(hookCmd)
}
if m.postHook != nil {
m.filesList["post_hook.sh"] = []byte(m.postHook.Command)
hookCmd, err := m.replaceRemoteStatesForBash(m.postHook.Command)
if err != nil {
return err
}
m.filesList["post_hook.sh"] = []byte(hookCmd)
}
return nil
}

func (m *Module) replaceRemoteStatesForBash(cmd string) (res string, err error) {
res = cmd
for hash, mr := range m.Markers() {
marker, ok := mr.(*project.Dependency)
if !ok {
return "", fmt.Errorf("preparing module: internal error: incorrect remote state type")
}
refStr := DependencyToBashRemoteState(marker)
res = strings.ReplaceAll(res, hash, refStr)
}
return
}
23 changes: 20 additions & 3 deletions pkg/modules/terraform/common/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Module struct {
filesList map[string][]byte
providers interface{}
specRaw map[string]interface{}
markers map[string]string
markers map[string]interface{}
applyOutput []byte
requiredProviders map[string]RequiredProvider
}
Expand All @@ -61,7 +61,7 @@ func (m *Module) AddRequiredProvider(name, source, version string) {
}
}

func (m *Module) Markers() map[string]string {
func (m *Module) Markers() map[string]interface{} {
return m.markers
}

Expand All @@ -86,7 +86,7 @@ func (m *Module) ReadConfigCommon(spec map[string]interface{}, infra *project.In
m.expectedOutputs = make(map[string]bool)
m.filesList = make(map[string][]byte)
m.specRaw = spec
m.markers = make(map[string]string)
m.markers = make(map[string]interface{})

// Process dependencies.
var modDeps []*project.Dependency
Expand Down Expand Up @@ -347,3 +347,20 @@ func (m *Module) CodeDir() string {
func (m *Module) UpdateProjectRuntimeDataCommon(p *project.Project) error {
return nil
}

// ReplaceMarkers replace all templated markers with values.
func (m *Module) ReplaceMarkersCommon(inheritedModule project.Module) error {
if m.preHook != nil {
err := project.ScanMarkers(&m.preHook.Command, m.RemoteStatesScanner, inheritedModule)
if err != nil {
return err
}
}
if m.postHook != nil {
err := project.ScanMarkers(&m.postHook.Command, m.RemoteStatesScanner, inheritedModule)
if err != nil {
return err
}
}
return nil
}
10 changes: 7 additions & 3 deletions pkg/modules/terraform/common/scanners.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (

// RemoteStatesScanner - project scanner function, witch process dependencies markers in module data setted by AddRemoteStateMarker template function.
func (m *Module) RemoteStatesScanner(data reflect.Value, module project.Module) (reflect.Value, error) {
subVal := reflect.ValueOf(data.Interface())
var subVal = data
if data.Kind() != reflect.String {
subVal = reflect.ValueOf(data.Interface())

}

resString := subVal.String()
depMarkers, ok := module.ProjectPtr().Markers[RemoteStateMarkerCatName]
if !ok {
Expand All @@ -38,8 +43,7 @@ func (m *Module) RemoteStatesScanner(data reflect.Value, module project.Module)
log.Fatalf("Depend module does not exists. Src: '%s.%s', depend: '%s'", module.InfraName(), module.Name(), modKey)
}
*module.Dependencies() = append(*module.Dependencies(), marker)
remoteStateRef := fmt.Sprintf("data.terraform_remote_state.%s-%s.outputs.%s", marker.InfraName, marker.ModuleName, marker.Output)
m.markers[key] = remoteStateRef
m.markers[key] = marker
depModule.ExpectedOutputs()[marker.Output] = true
return reflect.ValueOf(resString), nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/modules/terraform/common/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type StateSpecCommon struct {
PreHook *hookSpec `json:"pre_hook,omitempty"`
PostHook *hookSpec `json:"post_hook,omitempty"`
Providers interface{} `json:"providers,omitempty"`
Markers map[string]string `json:"markers,omitempty"`
Markers map[string]interface{} `json:"markers,omitempty"`
Dependencies []StateDep `json:"dependencies,omitempty"`
RequiredProvider map[string]RequiredProvider `json:"required_providers,omitempty"`
Outputs map[string]interface{} `json:"outputs,omitempty"`
Expand Down Expand Up @@ -114,10 +114,10 @@ func (m *Module) LoadStateCommon(spec StateCommon, modKey string, p *project.Sta
m.projectPtr = &p.Project
m.dependencies = modDeps
m.backendPtr = bPtr
m.expectedOutputs = map[string]bool{}
m.filesList = map[string][]byte{}
m.specRaw = map[string]interface{}{}
m.markers = map[string]string{}
m.expectedOutputs = make(map[string]bool)
m.filesList = make(map[string][]byte)
m.specRaw = make(map[string]interface{})
m.markers = make(map[string]interface{})
m.preHook = mState.PreHook
m.postHook = mState.PostHook
m.providers = mState.Providers
Expand Down
9 changes: 9 additions & 0 deletions pkg/modules/terraform/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ func readHook(hookData interface{}, hookType string) (*hookSpec, error) {
return &ScriptData, nil

}

func DependencyToRemoteStateRef(dep *project.Dependency) (remoteStateRef string) {
remoteStateRef = fmt.Sprintf("data.terraform_remote_state.%s-%s.outputs.%s", dep.InfraName, dep.ModuleName, dep.Output)
return
}
func DependencyToBashRemoteState(dep *project.Dependency) (remoteStateRef string) {
remoteStateRef = fmt.Sprintf("\"$(terraform -chdir=../%v.%v/ output -raw %v)\"", dep.InfraName, dep.ModuleName, dep.Output)
return
}
6 changes: 5 additions & 1 deletion pkg/modules/terraform/helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func (m *Module) ReadConfig(spec map[string]interface{}, infra *project.Infrastr

// ReplaceMarkers replace all templated markers with values.
func (m *Module) ReplaceMarkers() error {
err := project.ScanMarkers(m.helmOpts, m.YamlBlockMarkerScanner, m)
err := m.ReplaceMarkersCommon(m)
if err != nil {
return err
}
err = project.ScanMarkers(m.helmOpts, m.YamlBlockMarkerScanner, m)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 475e56c

Please sign in to comment.