Skip to content

Commit

Permalink
Avant scan playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 6, 2021
1 parent ce44f2f commit a70f224
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 28 deletions.
30 changes: 15 additions & 15 deletions internal/services/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ type Inventory struct {
Data *aini.InventoryData
}

func (path *Inventory) make() {
if path == nil {
return
}

if strings.Contains(filepath.Base(path.FilePath), ".ini") {
if file, err := os.Open(path.FilePath); err == nil {
reader := bufio.NewReader(file)
if data, err := aini.Parse(reader); err == nil {
path.Data = data
}
}
}
}

// Gather inventory files from a Parent directory
// Using a recursive scan. All non inventory files are ignored ( not .ini file )
// All sub parent directory added like label in the inventory
Expand Down Expand Up @@ -50,18 +65,3 @@ func readInventories(rootPath string, pathTags ...string) (result []*Inventory,
})
return
}

func (path *Inventory) make() {
if path == nil {
return
}

if strings.Contains(filepath.Base(path.FilePath), ".ini") {
if file, err := os.Open(path.FilePath); err == nil {
reader := bufio.NewReader(file)
if data, err := aini.Parse(reader); err == nil {
path.Data = data
}
}
}
}
80 changes: 80 additions & 0 deletions internal/services/playbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package services

import (
"fmt"
"github.com/karrick/godirwalk"
"gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"
"strings"
)

type Role struct {
Role string `yaml:"role"`
Tags interface{} `yaml:"tags,omitempty" yaml:"tags,omitempty"`
}

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

type Playbook struct {
Name string
Plays []Play
}

// 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) {
absRoot, err := filepath.Abs(rootPath)

if err != nil {
fmt.Println(err)
return
}

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

if !strings.Contains(filepath.Base(osPathname), ".yaml") && !strings.Contains(filepath.Base(osPathname), ".yml") {
return nil
}

// Try to check playbook content
var plays []Play
binData, err := ioutil.ReadFile(osPathname)
if err != nil {
// TODO add debug for read
return nil
}
err = yaml.Unmarshal([]byte(binData), &plays)
if err != nil {
// TODO add debug for unmarshaling
return nil
}

if plays == nil || len(plays) == 0 {
// TODO Log debug no play found
return nil
}

if plays[0].Hosts == "" {
// TODO Log debug do not seems to be a playbook
return nil
}

result = append(result, &Playbook{Name: osPathname, Plays: plays})
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Unsorted: true,
})
return
}
25 changes: 15 additions & 10 deletions internal/services/project.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package services

import (
"fmt"
"log"
)

type Playbook struct {
Name string
}

type Project struct {
Name string
Inventories []*Inventory
Expand All @@ -32,17 +29,25 @@ func (project *Project) FilterByVarsOr(filters map[string]string) (filtered []*I
}

// TODO: Add assert on file system ( readable, permissions ...)
func LoadFromPath(rootPath string) (project Project) {
func LoadFromPath(inventoryPath string, playbookPath string) (project Project) {
project = Project{
Name: rootPath,
Name: inventoryPath,
Playbooks: nil,
}
fmt.Println(playbookPath)
playbooks, errPlaybooks := readPlaybook(playbookPath)
inventories, errInventories := readInventories(inventoryPath)
project.Playbooks = playbooks
project.Inventories = inventories

if errPlaybooks != nil {
log.Fatalln("Cannot parse directory for playbooks: ", errPlaybooks.Error())
}
inventories, err := readInventories(rootPath)
if err != nil {
log.Fatalln("Cannot parse directory: ", err.Error())
if errInventories != nil {
log.Fatalln("Cannot parse directory for inventories: ", errInventories.Error())
}
for _, inventory := range inventories {
inventory.make()
}
project.Inventories = inventories
return
}
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
func main() {

home, _ := os.UserHomeDir()
path := fmt.Sprintf("%s/%s", home, "Projects/ansible-kube/inventories/")

k8s := services.LoadFromPath(path)
path := fmt.Sprintf("%s/%s", home, "Projects/ansible-kube/inventories")
k8s := services.LoadFromPath(path, path+"/..")

filter := map[string]string{"customer": "cagip"}
filteredInventories := k8s.FilterByVarsOr(filter)
Expand All @@ -21,5 +20,14 @@ func main() {
fmt.Println(i.FilePath)
}

fmt.Println("Playbooks")

for _, i := range k8s.Playbooks {
fmt.Println(i.Name, i.Plays)
for _, t := range i.Plays {
fmt.Printf("\ntags:%v", t.Tags)
}
}

cmd.Execute()
}

0 comments on commit a70f224

Please sign in to comment.