Created
October 26, 2015 03:44
-
-
Save xorl/b3ef97d0c37dffc88546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/MichaelTJones/walk" | |
flag "github.com/ogier/pflag" | |
"gopkg.in/yaml.v2" | |
"io/ioutil" | |
//"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"sync" | |
) | |
type Cfg struct { | |
Params map[string]map[string]map[string]map[string]string `yaml:"params"` | |
OpsParams map[string]string `yaml:"ops_params"` | |
HostName string `yaml:"hostname"` | |
Colo string `yaml:"colo"` | |
} | |
type Params struct { | |
Libvirt `yaml:"libvirt"` | |
} | |
type Libvirt struct { | |
Guests | |
} | |
type Guests struct { | |
Hostname map[string]string | |
} | |
type meta struct { | |
_meta map[string]string | |
hostvars map[string]string | |
} | |
var index map[string][]string = map[string][]string{} | |
var cache map[string]Cfg = map[string]Cfg{} | |
func visit(path string, fi os.FileInfo, err error) error { | |
var mutex = &sync.Mutex{} | |
fileName := fi.Name() | |
if !fi.IsDir() && strings.Contains(fileName, "vmhost") { | |
yamlFile, err := ioutil.ReadFile(path) | |
if err != nil { | |
//fmt.Printf(fileName) | |
//panic(err) | |
} | |
var cfg Cfg | |
cfgCache := Cfg{} | |
err = yaml.Unmarshal(yamlFile, &cfg) | |
if err != nil { | |
//fmt.Printf(fileName) | |
//panic(err) | |
} | |
err = yaml.Unmarshal(yamlFile, &cfgCache) | |
if err != nil { | |
//fmt.Printf(fileName) | |
//panic(err) | |
} | |
// vars | |
hostname := cfg.HostName | |
lifecycle := cfg.OpsParams["lifecycle"] | |
environment := cfg.OpsParams["environment"] | |
// write the cache map | |
mutex.Lock() | |
index[environment] = append(index[environment], hostname) | |
index[lifecycle] = append(index[lifecycle], hostname) | |
cache[hostname] = cfgCache | |
mutex.Unlock() | |
} | |
return nil | |
} | |
func errorTest(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
func main() { | |
// read our flags | |
hosts := flag.Bool("hosts", false, "display all hosts") | |
refreshCache := flag.Bool("refresh-cache", false, "refreshes cache") | |
//host := flag.String("host", "host", "Display a specific host") | |
flag.Parse() | |
if *hosts == true { | |
dat, err := ioutil.ReadFile("/tmp/ansible.cache") | |
errorTest(err) | |
fmt.Println("{\n \"_meta\" : {\n \"hostvars\" : {") | |
fmt.Print("\t" + string(dat)) | |
fmt.Println("\n\t}\n}") | |
} | |
if *refreshCache == true { | |
var fullPath string | |
root := "../../ops-config/hosts/" | |
fullPath, err := filepath.Abs(root) | |
errorTest(err) | |
data := walk.Walk(fullPath, visit) | |
if data != nil { | |
panic(data) | |
} | |
// write our index file | |
b, err := json.MarshalIndent(index, "", " ") | |
fo, err := os.Create("/tmp/ansible.index") | |
errorTest(err) | |
if _, err := fo.Write(b); err != nil { | |
panic(err) | |
} | |
// Write our cache | |
c, err := json.MarshalIndent(cache, "", " ") | |
foc, err := os.Create("/tmp/ansible.cache") | |
errorTest(err) | |
if _, err := foc.Write(c); err != nil { | |
panic(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment