Skip to content

Commit

Permalink
Avancement tags et parsong yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 21, 2021
1 parent 2883d6b commit e08c08d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
6 changes: 3 additions & 3 deletions internal/ansible/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

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

func (play *Play) Tags() []string {
Expand Down
46 changes: 26 additions & 20 deletions internal/ansible/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ansible
import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/klog/v2"
"path/filepath"
Expand All @@ -26,6 +26,29 @@ func (playbook *Playbook) RelativePath() string {
return strings.TrimPrefix(playbook.AbsolutePath, *playbook.RootPath+"/")
}

func ReadFromFile(osPathname string) (plays []Play) {
// Try to check playbook content
binData, err := ioutil.ReadFile(osPathname)
if err != nil {
klog.Error("Cannot read playbook", osPathname, ". Error: ", err.Error())
return
}
err = yaml.Unmarshal([]byte(binData), plays)
if err != nil {
klog.Error("Skip", osPathname, " not an inventory ")
return
}
if plays == nil || len(plays) == 0 {
klog.Info("No play found inside the playbook: ", osPathname)
return
}
if (plays[0]).Hosts == utils.EmptyString {
klog.V(8).Info("No play found inside the playbook: ", osPathname)
return
}
return
}

// Gather playbook files from a Parent directory
// Using a recursive scan. All non playbook files are ignored ( not .yaml or .yml file )
func readPlaybook(rootPath string) (result []*Playbook, err error) {
Expand All @@ -50,25 +73,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
}

// Try to check playbook content
var plays []Play
binData, err := ioutil.ReadFile(osPathname)
if err != nil {
klog.Error("Cannot read playbook", osPathname, ". Error: ", err.Error())
return nil
}
err = yaml.Unmarshal([]byte(binData), &plays)
if err != nil {
klog.Error("Skip", osPathname, " not an inventory ")
return nil
}
if plays == nil || len(plays) == 0 {
klog.Info("No play found inside the playbook: ", osPathname)
return nil
}
if plays[0].Hosts == utils.EmptyString {
klog.V(8).Info("No play found inside the playbook: ", osPathname)
return nil
}
plays := ReadFromFile(osPathname)

// Browse Role Tags
for _, play := range plays {
Expand All @@ -77,6 +82,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
fmt.Println("Play tags are: ", play.Tags())
for _, role := range play.Roles {
role.ReadRole(rootPath)
klog.Info(" Role info", role.Tags())
allTags.Concat(role.Tags())
}
}
Expand Down
40 changes: 40 additions & 0 deletions internal/ansible/playbook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ansible

import (
"github.com/ghodss/yaml"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
"testing"
)

const validPlaybook = `
- hosts: aws-node
gather_facts: no
roles:
- { role: add-aws-facts, tags: [ add-aws-facts ] }
post_tasks:
- setup:
tags: always,alwaystest
`

func TestReadFromFile(t *testing.T) {

t.Run("with a valid play and tags", func(t *testing.T) {
binData := []byte(validPlaybook)
var plays []Play
err := yaml.Unmarshal([]byte(binData), &plays)
assert.Nil(t, err)
assert.NotNil(t, plays)
assert.NotEmpty(t, plays)

deep.Equal(plays[0], Play{
Hosts: "aws-node",
Roles: []Role{
{Name: "add-aws-facts"},
},
RawTags: []string{"add-aws-facts"},
})

})

}
10 changes: 5 additions & 5 deletions internal/ansible/role.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ansible

import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/klog/v2"
"path/filepath"
Expand All @@ -13,9 +12,9 @@ import (

type Role struct {
AbsolutePath string
Name string `yaml:"role"`
Name string `json:"name" yaml:"role,flow"`
rawTags interface{} `json:"tags" yaml:"tags,flow"`
Tasks []Tasks
rawTags interface{} `yaml:"tags,omitempty"`
}

func (role *Role) Tags() []string {
Expand Down Expand Up @@ -50,10 +49,11 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {
var tasks []Task
err = yaml.Unmarshal([]byte(binData), &tasks)
for _, task := range tasks {
fmt.Println("task t:", task.Tags())
tags.Concat(task.Tags())
}

klog.Info("tags in role tags:", role.rawTags)

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

type Task struct {
Role string `yaml:"role"`
rawTags interface{} `yaml:"tags,omitempty"`
Role string `json:"role"`
rawTags interface{} `json:"tags,omitempty" yaml:"tags,omitempty"`
}

func (task *Task) Tags() []string {
Expand Down

0 comments on commit e08c08d

Please sign in to comment.