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 caea74e commit 2883d6b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 30 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ require (
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351
github.com/mitchellh/go-homedir v1.1.0
github.com/relex/aini v1.2.1
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.1.1
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
)
14 changes: 11 additions & 3 deletions internal/ansible/play.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package ansible

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

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

func (play *Play) Tags() []string {
return utils.InferSlice(play.RawTags)
}
24 changes: 13 additions & 11 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"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/klog/v2"
"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 @@ -53,29 +53,31 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
var plays []Play
binData, err := ioutil.ReadFile(osPathname)
if err != nil {
log.Error("Cannot read playbook", osPathname, ". Error: ", err.Error())
klog.Error("Cannot read playbook", osPathname, ". Error: ", err.Error())
return nil
}
err = yaml.Unmarshal([]byte(binData), &plays)
if err != nil {
log.Error("Cannot unmashal playbook data", osPathname, ". Error: ", err.Error())
klog.Error("Skip", osPathname, " not an inventory ")
return nil
}
if plays == nil || len(plays) == 0 {
log.Debug("No play found inside the playbook: ", osPathname)
klog.Info("No play found inside the playbook: ", osPathname)
return nil
}
if plays[0].Hosts == utils.EmptyString {
log.Debug("No play found inside the playbook: ", osPathname)
klog.V(8).Info("No play found inside the playbook: ", osPathname)
return nil
}

// 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 All @@ -86,7 +88,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
AllTags: *allTags,
}
result = append(result, &playbook)
log.Debug("Available tags are :", playbook.AllTags)
klog.V(8).Info("Available tags are :", playbook.AllTags)
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
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
10 changes: 8 additions & 2 deletions internal/ansible/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package ansible

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

type Task struct {
Role string `yaml:"role"`
Tags []string `yaml:"tags,omitempty" yaml:"tags,omitempty"`
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
}
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package main

import (
"fmt"
"github.com/ca-gip/dploy/cmd"
"github.com/ca-gip/dploy/internal/ansible"
"os"
)

func main() {

//var TagsRe = regexp.MustCompile("([\\w-.\\/]+)([,]|)")
//
//for _, tag := range TagsRe.FindAllStringSubmatch("multipass-create-v_ms,os-metadata,os-provisionning-dns-add", -1) {
// fmt.Println(tag)
//}
home, _ := os.UserHomeDir()
path := fmt.Sprintf("%s/%s", home, "Projects/ansible-kube")
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()

cmd.Execute()
}

0 comments on commit 2883d6b

Please sign in to comment.