Skip to content

Commit

Permalink
Merge pull request #148 from shalb/shell-unit
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
romanprog authored Nov 15, 2021
2 parents 0759b32 + ee33ee1 commit 63a0348
Show file tree
Hide file tree
Showing 64 changed files with 2,716 additions and 2,195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ units:
inputs:
bucket_name: {{ remoteState "this.create-bucket.s3_bucket_id" }}
data: |
The data that will be saved in the S3 bucket after being processed by the template engine.
The data that will be saved in the s3 bucket after being processed by the template engine.
Organization: {{ .variables.organization }}
Name: {{ .variables.name }}
-
Expand Down
8 changes: 4 additions & 4 deletions cmd/cdev/includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
_ "github.com/shalb/cluster.dev/pkg/logging"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/common"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/kubectl"
_ "github.com/shalb/cluster.dev/pkg/modules/terraform/helm"
_ "github.com/shalb/cluster.dev/pkg/modules/terraform/kubernetes"
_ "github.com/shalb/cluster.dev/pkg/modules/terraform/printer"
_ "github.com/shalb/cluster.dev/pkg/modules/terraform/tf_module"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/terraform/helm"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/terraform/kubernetes"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/terraform/module"
_ "github.com/shalb/cluster.dev/pkg/modules/shell/terraform/printer"
_ "github.com/shalb/cluster.dev/pkg/project"
_ "github.com/shalb/cluster.dev/pkg/secrets/aws_secretmanager"
_ "github.com/shalb/cluster.dev/pkg/secrets/sops"
Expand Down
54 changes: 52 additions & 2 deletions pkg/cmd/cdev/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cdev

import (
"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/config"
"github.com/shalb/cluster.dev/pkg/project"
"github.com/spf13/cobra"
)
Expand All @@ -19,17 +20,66 @@ var stateUnlockCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: plan: %v", err.Error())
log.Fatalf("Fatal error: state unlock: %v", err.Error())
}
log.Info("Unlocking state...")
err = project.UnLockState()
if err != nil {
log.Fatalf("Fatal error: plan: %v", err.Error())
log.Fatalf("Fatal error: state unlock: %v", err.Error())
}
},
}

// planCmd represents the plan command
var stateUpdateCmd = &cobra.Command{
Use: "update",
Short: "Updates the state of the current project to version %v. Make sure that the state of the project is consistent (run cdev apply with the old version before)",
Run: func(cmd *cobra.Command, args []string) {
config.Global.NotLoadState = true
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: state update: %v", err.Error())
}
project.LockState()
defer project.UnLockState()

err = project.BackupState()
if err != nil {

log.Fatalf("Fatal error: state update: %v", err.Error())
}
log.Info("Updating state...")
err = project.SaveState()
if err != nil {
log.Fatalf("Fatal error: state update: %v", err.Error())
}
},
}

// planCmd represents the plan command
var statePullCmd = &cobra.Command{
Use: "pull",
Short: "Downloads the remote state",
Run: func(cmd *cobra.Command, args []string) {
config.Global.NotLoadState = true
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: state pull: %v", err.Error())
}
project.LockState()
defer project.UnLockState()

log.Info("Updating state...")
err = project.PullState()
if err != nil {
log.Fatalf("Fatal error: state pull: %v", err.Error())
}
},
}

func init() {
rootCmd.AddCommand(stateCmd)
stateCmd.AddCommand(stateUnlockCmd)
stateCmd.AddCommand(stateUpdateCmd)
stateCmd.AddCommand(statePullCmd)
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ConfSpec struct {
UseCache bool
OptFooTest bool
IgnoreState bool
NotLoadState bool
ShowTerraformPlan bool
StateLocalFileName string
StateLocalLockFile string
Expand Down Expand Up @@ -78,6 +79,7 @@ func InitConfig() {
Global.StateLocalFileName = filepath.Join(curPath, "cdev.state")
Global.StateLocalLockFile = filepath.Join(curPath, "cdev.state.lock")
Global.TemplatesCacheDir = filepath.Join(Global.TmpDir, "templates")
Global.NotLoadState = false
usr, err := user.Current()
if err != nil {
log.Fatal(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func formatFuncName(v fmtVerb, f string) string {
panic("unexpected func formatter")
}

func formatCallpath(calldepth int, depth int) string {
func FormatCallpath(calldepth int, depth int) string {
v := ""
callers := make([]uintptr, 64)
n := runtime.Callers(calldepth+2, callers)
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func logFormatter(e *log.Entry) string {
output += fmt.Sprintf("[%v]", e.Fields.Get(name))
}
if traceLog {
traceMsg := colors.Fmt(colors.LightWhite).Sprintf("<%s>", formatCallpath(6, 15))
traceMsg := colors.Fmt(colors.LightWhite).Sprintf("<%s>", FormatCallpath(6, 15))
output = fmt.Sprintf("%s %s", output, traceMsg)
}
output = fmt.Sprintf("%s %-25s", output, e.Message)
Expand Down
50 changes: 36 additions & 14 deletions pkg/modules/shell/common/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,61 @@ import (
type Factory struct {
}

// New creates new units driver factory.
func (f *Factory) New(spec map[string]interface{}, stack *project.Stack) (project.Unit, error) {
mod := Unit{
markers: make(map[string]interface{}),
applied: false,
const unitKind string = "shell"

// NewEmptyUnit creates new unit.
func NewEmptyUnit() Unit {
unit := Unit{
//UnitMarkers: make(map[string]interface{}),
Applied: false,
StatePtr: &Unit{},
UnitKind: unitKind,
CreateFiles: &FilesListT{},
}
mod.outputParsers = map[string]outputParser{
"json": mod.JSONOutputParser,
"regexp": mod.RegexOutputParser,
"separator": mod.SeparatorOutputParser,
unit.OutputParsers = map[string]OutputParser{
"json": unit.JSONOutputParser,
"regexp": unit.RegexOutputParser,
"separator": unit.SeparatorOutputParser,
}
err := mod.ReadConfig(spec, stack)
//unit.StatePtr.UnitMarkers = unit.UnitMarkers
return unit
}

// NewUnit creates new unit and load config.
func NewUnit(spec map[string]interface{}, stack *project.Stack) (*Unit, error) {
unit := NewEmptyUnit()
//unit.StatePtr.UnitMarkers = unit.UnitMarkers
err := unit.ReadConfig(spec, stack)
if err != nil {
log.Debug(err.Error())
return nil, err
}
return &mod, nil
unit.BackendName = stack.Backend.Name()
// /log.Fatalf("%v", unit.BackendName)
return &unit, nil
}

// New creates new units driver factory.
func (f *Factory) New(spec map[string]interface{}, stack *project.Stack) (project.Unit, error) {
return NewUnit(spec, stack)
}

// NewFromState creates new units from state data.
func (f *Factory) NewFromState(spec map[string]interface{}, modKey string, p *project.StateProject) (project.Unit, error) {
mod := Unit{}
mod := NewEmptyUnit()
err := mod.LoadState(spec, modKey, p)
if err != nil {
log.Debug(err.Error())
return nil, err
}

return &mod, nil
}

func init() {
modDrv := Factory{}
if err := project.RegisterUnitFactory(&modDrv, "shell"); err != nil {
log.Trace("Can't register unit driver 'shell'.")
log.Debugf("Registering unit driver '%v'", unitKind)
if err := project.RegisterUnitFactory(&modDrv, unitKind); err != nil {
log.Trace("Can't register unit driver '" + unitKind + "'.")
}
}
123 changes: 123 additions & 0 deletions pkg/modules/shell/common/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package common

import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
)

// CreateFileRepresentation describes the unit's file that will be saved in the unit's working directory when building.
type CreateFileRepresentation struct {
FileName string `yaml:"file"`
FileMode fs.FileMode `yaml:"file_mode,omitempty"`
Content string `yaml:"content"`
}

// FilesListT describes all unit's files will be write to the unit's working directory when building.
type FilesListT []*CreateFileRepresentation

// Find searchs file and returns a pointer to it or nil if not found.
func (l *FilesListT) Find(fileName string) int {
for i, f := range *l {
if f.FileName == fileName {
return i
}
}
return -1
}

func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}

func (l *FilesListT) SPrint() string {
var res string
for _, f := range *l {
res += fmt.Sprintf("%v: %v\n", f.FileName, f.FileMode)
}
return res
}

// Add insert the new file with name fileName, returns error if file with this name already exists.
func (l *FilesListT) Add(fileName string, content string, mode fs.FileMode) error {
if l.Find(fileName) >= 0 {
return fmt.Errorf("add file: file '%v' already exists", fileName)
}
*l = append(*l,
&CreateFileRepresentation{
FileName: fileName,
Content: content,
FileMode: mode,
})
return nil
}

// ReadDir recursively reads files in path, saving relative pathes from baseDir.
func (l *FilesListT) ReadDir(path, baseDir string) (err error) {
_, err = filepath.Rel(baseDir, path)
if err != nil {
return
}
err = filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
relPath, err := filepath.Rel(baseDir, path)
if err != nil {
return err
}
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = l.Add(relPath, string(content), info.Mode())
if err != nil {
return err
}
}
return nil
})
return
}

// WriteFiles write all files to path.
func (l *FilesListT) WriteFiles(path string) (err error) {
for _, file := range *l {
var fileName, fileDir, fileFullName string
splittedPath := strings.Split(file.FileName, "/")
if len(splittedPath) < 2 {
fileDir = path
fileName = file.FileName
} else {
fileDir = filepath.Join(path, filepath.Join(splittedPath[0:len(splittedPath)-1]...))
fileName = splittedPath[len(splittedPath)-1]
err = os.MkdirAll(fileDir, os.ModePerm)
if err != nil {
return err
}
}

fileFullName = filepath.Join(fileDir, fileName)
log.Debugf("Writing file: %v", fileFullName)
err = ioutil.WriteFile(fileFullName, []byte(file.Content), file.FileMode)
if err != nil {
return err
}
}
return
}

// Delete delete file with name fileName, do nothing if not found.
func (l *FilesListT) Delete(fileName string) {
if i := l.Find(fileName); i >= 0 {
*l = append((*l)[:i], (*l)[i+1:]...)
}
return
}
Loading

0 comments on commit 63a0348

Please sign in to comment.