Skip to content

Commit

Permalink
fix tags parser
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 21, 2021
1 parent 8f1cc0b commit 104fc52
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 48 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ require (
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
gopkg.in/yaml.v2 v2.2.8 // indirect
k8s.io/klog/v2 v2.4.0
)
18 changes: 5 additions & 13 deletions internal/ansible/play.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package ansible

import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
)

type Play struct {
Hosts string `yaml:"hosts"`
Roles []Role `yaml:"roles"`
Tags utils.Set `yaml:"tags"`
Hosts string `yaml:"hosts"`
Roles []Role `yaml:"roles"`
RawTags interface{} `yaml:"tags,omitempty"`
}

func (play *Play) AllTags() (tags *utils.Set) {
tags = utils.NewSet()
for _, role := range play.Roles {
tags = tags.Concat(role.AllTags().List())
fmt.Println("role loop tags list is: ", tags.List())
}
tags.Concat(play.Tags.List())
fmt.Println("play tags list is: ", tags.List())
return
func (play *Play) Tags() []string {
return utils.InferSlice(play.RawTags)
}
14 changes: 8 additions & 6 deletions internal/ansible/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package ansible
import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
"gopkg.in/yaml.v2"
log "github.com/sirupsen/logrus"
"io/ioutil"
"path/filepath"
"strings"
)

type Tasks struct {
Tags []string `yaml:"tags,omitempty" yaml:"tags,omitempty"`
Tags interface{} `yaml:"tags,omitempty"`
}

type Playbook struct {
Expand Down Expand Up @@ -41,7 +41,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {

err = godirwalk.Walk(absRoot, &godirwalk.Options{
Callback: func(osPathname string, de *godirwalk.Dirent) error {
if strings.Contains(osPathname, "vars") || strings.Contains(osPathname, "template") {
if strings.Contains(de.Name(), "vars") || de.Name() == "template" || de.Name() == "roles" {
return godirwalk.SkipThis
}

Expand All @@ -58,7 +58,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
}
err = yaml.Unmarshal([]byte(binData), &plays)
if err != nil {
log.Error("Cannot unmashal playbook data", osPathname, ". Error: ", err.Error())
log.Error("Skip", osPathname, " not an inventory ")
return nil
}
if plays == nil || len(plays) == 0 {
Expand All @@ -72,10 +72,12 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {

// Browse Role Tags
for _, play := range plays {
allTags.Concat(play.Tags)

allTags.Concat(play.Tags())
fmt.Println("Play tags are: ", play.Tags())
for _, role := range play.Roles {
role.ReadRole(rootPath)
allTags.Concat(role.Tags)
allTags.Concat(role.Tags())
}
}

Expand Down
22 changes: 14 additions & 8 deletions internal/ansible/role.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ansible

import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/klog/v2"
"path/filepath"
"strings"
)
Expand All @@ -14,7 +15,11 @@ type Role struct {
AbsolutePath string
Name string `yaml:"role"`
Tasks []Tasks
Tags []string `yaml:"tags,omitempty" yaml:"tags,omitempty"`
rawTags interface{} `yaml:"tags,omitempty"`
}

func (role *Role) Tags() []string {
return utils.InferSlice(role.rawTags)
}

// Gather inventory files from a Parent directory
Expand All @@ -24,7 +29,7 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {
absRoot, err := filepath.Abs(rootPath + "/roles/" + role.Name)

if err != nil {
log.Error("The role ", role.Name, "can't be read. Error:", err.Error())
klog.Error("The role ", role.Name, "can't be read. Error:", err.Error())
return
}

Expand All @@ -39,18 +44,19 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {

binData, err := ioutil.ReadFile(osPathname)
if err != nil {
log.Error("Cannot read file: ", osPathname, ". Error:", err.Error())
klog.Error("Cannot read file: ", osPathname, ". Error:", err.Error())
}

var tasks []Task
err = yaml.Unmarshal([]byte(binData), &tasks)
for _, task := range tasks {
tags.Concat(task.Tags)
fmt.Println("task t:", task.Tags())
tags.Concat(task.Tags())
}

tasks = append(tasks, Task{Tags: tags.List()})
tasks = append(tasks, Task{rawTags: tags.List()})
if len(tags.List()) > 0 {
log.Info("Task tags:", tags.List())
klog.Info("Task tags:", tags.List())
}
return nil
},
Expand Down
8 changes: 6 additions & 2 deletions internal/ansible/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package ansible
import "github.com/ca-gip/dploy/internal/utils"

type Task struct {
Name string `yaml:"name,omitempty"`
Tags utils.Set
Role string `yaml:"role"`
rawTags interface{} `yaml:"tags,omitempty"`
}

func (task *Task) Tags() []string {
return utils.InferSlice(task.rawTags)
}
29 changes: 29 additions & 0 deletions internal/utils/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import (
"fmt"
"reflect"
"strings"
)

func InferSlice(input interface{}) (slice []string) {
if input == nil {
return
}

value := reflect.ValueOf(input)
rt := reflect.TypeOf(input)
switch rt.Kind() {
case reflect.Slice:
slice = make([]string, value.Len())
for i := 0; i < value.Len(); i++ {
slice[i] = fmt.Sprintf("%v", value.Index(i).Interface())
}
return
case reflect.String:
slice = strings.Split(value.String(), ",")
default:
fmt.Println("cannot find type for", value)
}
return
}
38 changes: 19 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (

func main() {

home, _ := os.UserHomeDir()
path := fmt.Sprintf("%s/%s", home, "Projects/ansible-mock")
k8s := ansible.LoadFromPath(path)
fmt.Println("Filtering ", len(k8s.Inventories), "/", len(k8s.Inventories))

fmt.Println("Playbooks")

tpl := ansible.AnsibleCommandTpl{
Inventory: k8s.Inventories,
Playbook: k8s.Playbooks[0],
Tags: []string{"tag1", "tag2"},
Limit: []string{"limit1,limit2"},
SkipTags: []string{"testt"},
Check: true,
Diff: true,
VaultPasswordFile: "/path/to/passwordfile",
AskVaultPass: false,
}
tpl.GenerateCmd()
//home, _ := os.UserHomeDir()
//path := fmt.Sprintf("%s/%s", home, "Projects/ansible-kube")
//k8s := services.LoadFromPath(path)
//fmt.Println("Filtering ", len(k8s.Inventories), "/", len(k8s.Inventories))
//
//fmt.Println("Playbooks")
//
//tpl := services.AnsibleCommandTpl{
// Inventory: k8s.Inventories,
// Playbook: k8s.Playbooks[1],
// Tags: []string{"tag1", "tag2"},
// Limit: []string{"limit1,limit2"},
// SkipTags: []string{"testt"},
// Check: true,
// Diff: true,
// VaultPasswordFile: "/path/to/passwordfile",
// AskVaultPass: false,
//}
//tpl.GenerateCmd()

cmd.Execute()
}

0 comments on commit 104fc52

Please sign in to comment.