View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file2
path: "/tmp/mgmt/f2"
@@ -15,8 +15,8 @@ types:
edges:
- name: e2
from:
- type: file
+ res: file
name: file2
to:
- type: file
+ res: file
name: file3
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
noop:
- name: noop1
file:
@@ -9,22 +9,22 @@ types:
content: |
i am f1
state: exists
- service:
+ svc:
- name: purpleidea
state: running
startup: enabled
edges:
- name: e1
from:
- type: noop
+ res: noop
name: noop1
to:
- type: file
+ res: file
name: file1
- name: e2
from:
- type: file
+ res: file
name: file1
to:
- type: service
+ res: svc
name: purpleidea
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1a
path: "/tmp/mgmtA/f1a"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host A
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmtA/"
edges: []
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1b
path: "/tmp/mgmtB/f1b"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host B
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmtB/"
edges: []
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1c
path: "/tmp/mgmtC/f1c"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host C
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmtC/"
edges: []
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1
path: "/tmp/mgmt/f1"
@@ -13,6 +13,6 @@ types:
i am f3, exported from host A
state: exists
collect:
-- type: file
+- res: file
pattern: ''
edges:
View
@@ -1,13 +1,13 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1
path: "/tmp/mgmt/f1"
content: |
i am f1
state: exists
collect:
-- type: file
+- res: file
pattern: ''
edges:
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
noop:
- name: noop1
edges:
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
noop:
- name: noop1
exec:
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -45,15 +45,15 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec2
- name: e2
from:
- type: exec
+ res: exec
name: exec2
to:
- type: exec
+ res: exec
name: exec3
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -25,8 +25,8 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec2
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -25,8 +25,8 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec2
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
exec:
- name: exec1
cmd: echo hello from exec1
@@ -25,8 +25,8 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec2
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
exec:
- name: exec1
cmd: echo hello from exec1
View
@@ -1,7 +1,7 @@
---
graph: mygraph
comment: simple exec fan in example to demonstrate optimization
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -56,22 +56,22 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec5
- name: e2
from:
- type: exec
+ res: exec
name: exec2
to:
- type: exec
+ res: exec
name: exec5
- name: e3
from:
- type: exec
+ res: exec
name: exec3
to:
- type: exec
+ res: exec
name: exec5
View
78 exec.go
@@ -25,8 +25,8 @@ import (
"strings"
)
-type ExecType struct {
- BaseType `yaml:",inline"`
+type ExecRes struct {
+ BaseRes `yaml:",inline"`
State string `yaml:"state"` // state: exists/present?, absent, (undefined?)
Cmd string `yaml:"cmd"` // the command to run
Shell string `yaml:"shell"` // the (optional) shell to use to run the cmd
@@ -38,10 +38,10 @@ type ExecType struct {
PollInt int `yaml:"pollint"` // the poll interval for the ifcmd
}
-func NewExecType(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecType {
+func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecRes {
// FIXME if path = nil, path = name ...
- return &ExecType{
- BaseType: BaseType{
+ return &ExecRes{
+ BaseRes: BaseRes{
Name: name,
events: make(chan Event),
vertex: nil,
@@ -58,13 +58,13 @@ func NewExecType(name, cmd, shell string, timeout int, watchcmd, watchshell, ifc
}
}
-func (obj *ExecType) GetType() string {
+func (obj *ExecRes) GetRes() string {
return "Exec"
}
// validate if the params passed in are valid data
// FIXME: where should this get called ?
-func (obj *ExecType) Validate() bool {
+func (obj *ExecRes) Validate() bool {
if obj.Cmd == "" { // this is the only thing that is really required
return false
}
@@ -78,7 +78,7 @@ func (obj *ExecType) Validate() bool {
}
// wraps the scanner output in a channel
-func (obj *ExecType) BufioChanScanner(scanner *bufio.Scanner) (chan string, chan error) {
+func (obj *ExecRes) BufioChanScanner(scanner *bufio.Scanner) (chan string, chan error) {
ch, errch := make(chan string), make(chan error)
go func() {
for scanner.Scan() {
@@ -96,7 +96,7 @@ func (obj *ExecType) BufioChanScanner(scanner *bufio.Scanner) (chan string, chan
}
// Exec watcher
-func (obj *ExecType) Watch() {
+func (obj *ExecRes) Watch() {
if obj.IsWatching() {
return
}
@@ -128,7 +128,7 @@ func (obj *ExecType) Watch() {
cmdReader, err := cmd.StdoutPipe()
if err != nil {
- log.Printf("%v[%v]: Error creating StdoutPipe for Cmd: %v", obj.GetType(), obj.GetName(), err)
+ log.Printf("%v[%v]: Error creating StdoutPipe for Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Fatal(err) // XXX: how should we handle errors?
}
scanner := bufio.NewScanner(cmdReader)
@@ -140,44 +140,44 @@ func (obj *ExecType) Watch() {
cmd.Process.Kill() // TODO: is this necessary?
}()
if err := cmd.Start(); err != nil {
- log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetType(), obj.GetName(), err)
+ log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Fatal(err) // XXX: how should we handle errors?
}
bufioch, errch = obj.BufioChanScanner(scanner)
}
for {
- obj.SetState(typeWatching) // reset
+ obj.SetState(resStateWatching) // reset
select {
case text := <-bufioch:
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
// each time we get a line of output, we loop!
- log.Printf("%v[%v]: Watch output: %s", obj.GetType(), obj.GetName(), text)
+ log.Printf("%v[%v]: Watch output: %s", obj.GetRes(), obj.GetName(), text)
if text != "" {
send = true
}
case err := <-errch:
- obj.SetConvergedState(typeConvergedNil) // XXX ?
- if err == nil { // EOF
+ obj.SetConvergedState(resConvergedNil) // XXX ?
+ if err == nil { // EOF
// FIXME: add an "if watch command ends/crashes"
// restart or generate error option
- log.Printf("%v[%v]: Reached EOF", obj.GetType(), obj.GetName())
+ log.Printf("%v[%v]: Reached EOF", obj.GetRes(), obj.GetName())
return
}
- log.Printf("%v[%v]: Error reading input?: %v", obj.GetType(), obj.GetName(), err)
+ log.Printf("%v[%v]: Error reading input?: %v", obj.GetRes(), obj.GetName(), err)
log.Fatal(err)
// XXX: how should we handle errors?
case event := <-obj.events:
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
if exit, send = obj.ReadEvent(&event); exit {
return // exit
}
case _ = <-TimeAfterOrBlock(obj.ctimeout):
- obj.SetConvergedState(typeConvergedTimeout)
+ obj.SetConvergedState(resConvergedTimeout)
obj.converged <- true
continue
}
@@ -193,7 +193,7 @@ func (obj *ExecType) Watch() {
}
// TODO: expand the IfCmd to be a list of commands
-func (obj *ExecType) StateOK() bool {
+func (obj *ExecRes) StateOK() bool {
// if there is a watch command, but no if command, run based on state
if b := obj.isStateOK; obj.WatchCmd != "" && obj.IfCmd == "" {
@@ -241,8 +241,8 @@ func (obj *ExecType) StateOK() bool {
}
}
-func (obj *ExecType) Apply() bool {
- log.Printf("%v[%v]: Apply", obj.GetType(), obj.GetName())
+func (obj *ExecRes) Apply() bool {
+ log.Printf("%v[%v]: Apply", obj.GetRes(), obj.GetName())
var cmdName string
var cmdArgs []string
if obj.Shell == "" {
@@ -264,7 +264,7 @@ func (obj *ExecType) Apply() bool {
cmd.Stdout = &out
if err := cmd.Start(); err != nil {
- log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetType(), obj.GetName(), err)
+ log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetRes(), obj.GetName(), err)
return false
}
@@ -278,12 +278,12 @@ func (obj *ExecType) Apply() bool {
select {
case err := <-done:
if err != nil {
- log.Printf("%v[%v]: Error waiting for Cmd: %v", obj.GetType(), obj.GetName(), err)
+ log.Printf("%v[%v]: Error waiting for Cmd: %v", obj.GetRes(), obj.GetName(), err)
return false
}
case <-TimeAfterOrBlock(timeout):
- log.Printf("%v[%v]: Timeout waiting for Cmd", obj.GetType(), obj.GetName())
+ log.Printf("%v[%v]: Timeout waiting for Cmd", obj.GetRes(), obj.GetName())
//cmd.Process.Kill() // TODO: is this necessary?
return false
}
@@ -301,35 +301,35 @@ func (obj *ExecType) Apply() bool {
return true
}
-func (obj *ExecType) Compare(typ Type) bool {
- switch typ.(type) {
- case *ExecType:
- typ := typ.(*ExecType)
- if obj.Name != typ.Name {
+func (obj *ExecRes) Compare(res Res) bool {
+ switch res.(type) {
+ case *ExecRes:
+ res := res.(*ExecRes)
+ if obj.Name != res.Name {
return false
}
- if obj.Cmd != typ.Cmd {
+ if obj.Cmd != res.Cmd {
return false
}
- if obj.Shell != typ.Shell {
+ if obj.Shell != res.Shell {
return false
}
- if obj.Timeout != typ.Timeout {
+ if obj.Timeout != res.Timeout {
return false
}
- if obj.WatchCmd != typ.WatchCmd {
+ if obj.WatchCmd != res.WatchCmd {
return false
}
- if obj.WatchShell != typ.WatchShell {
+ if obj.WatchShell != res.WatchShell {
return false
}
- if obj.IfCmd != typ.IfCmd {
+ if obj.IfCmd != res.IfCmd {
return false
}
- if obj.PollInt != typ.PollInt {
+ if obj.PollInt != res.PollInt {
return false
}
- if obj.State != typ.State {
+ if obj.State != res.State {
return false
}
default:
View
68 file.go
@@ -31,8 +31,8 @@ import (
"syscall"
)
-type FileType struct {
- BaseType `yaml:",inline"`
+type FileRes struct {
+ BaseRes `yaml:",inline"`
Path string `yaml:"path"` // path variable (should default to name)
Dirname string `yaml:"dirname"`
Basename string `yaml:"basename"`
@@ -41,10 +41,10 @@ type FileType struct {
sha256sum string
}
-func NewFileType(name, path, dirname, basename, content, state string) *FileType {
+func NewFileRes(name, path, dirname, basename, content, state string) *FileRes {
// FIXME if path = nil, path = name ...
- return &FileType{
- BaseType: BaseType{
+ return &FileRes{
+ BaseRes: BaseRes{
Name: name,
events: make(chan Event),
vertex: nil,
@@ -58,12 +58,12 @@ func NewFileType(name, path, dirname, basename, content, state string) *FileType
}
}
-func (obj *FileType) GetType() string {
+func (obj *FileRes) GetRes() string {
return "File"
}
// validate if the params passed in are valid data
-func (obj *FileType) Validate() bool {
+func (obj *FileRes) Validate() bool {
if obj.Dirname != "" {
// must end with /
if obj.Dirname[len(obj.Dirname)-1:] != "/" {
@@ -79,7 +79,7 @@ func (obj *FileType) Validate() bool {
return true
}
-func (obj *FileType) GetPath() string {
+func (obj *FileRes) GetPath() string {
d := Dirname(obj.Path)
b := Basename(obj.Path)
if !obj.Validate() || (obj.Dirname == "" && obj.Basename == "") {
@@ -96,7 +96,7 @@ func (obj *FileType) GetPath() string {
// File watcher for files and directories
// Modify with caution, probably important to write some test cases first!
// obj.GetPath(): file or directory
-func (obj *FileType) Watch() {
+func (obj *FileRes) Watch() {
if obj.IsWatching() {
return
}
@@ -152,13 +152,13 @@ func (obj *FileType) Watch() {
continue
}
- obj.SetState(typeWatching) // reset
+ obj.SetState(resStateWatching) // reset
select {
case event := <-watcher.Events:
if DEBUG {
log.Printf("File[%v]: Watch(%v), Event(%v): %v", obj.GetName(), current, event.Name, event.Op)
}
- obj.SetConvergedState(typeConvergedNil) // XXX: technically i can detect if the event is erroneous or not first
+ obj.SetConvergedState(resConvergedNil) // XXX: technically i can detect if the event is erroneous or not first
// the deeper you go, the bigger the deltaDepth is...
// this is the difference between what we're watching,
// and the event... doesn't mean we can't watch deeper
@@ -228,20 +228,20 @@ func (obj *FileType) Watch() {
}
case err := <-watcher.Errors:
- obj.SetConvergedState(typeConvergedNil) // XXX ?
+ obj.SetConvergedState(resConvergedNil) // XXX ?
log.Println("error:", err)
log.Fatal(err)
//obj.events <- fmt.Sprintf("file: %v", "error") // XXX: how should we handle errors?
case event := <-obj.events:
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
if exit, send = obj.ReadEvent(&event); exit {
return // exit
}
//dirty = false // these events don't invalidate state
case _ = <-TimeAfterOrBlock(obj.ctimeout):
- obj.SetConvergedState(typeConvergedTimeout)
+ obj.SetConvergedState(resConvergedTimeout)
obj.converged <- true
continue
}
@@ -259,7 +259,7 @@ func (obj *FileType) Watch() {
}
}
-func (obj *FileType) HashSHA256fromContent() string {
+func (obj *FileRes) HashSHA256fromContent() string {
if obj.sha256sum != "" { // return if already computed
return obj.sha256sum
}
@@ -271,7 +271,7 @@ func (obj *FileType) HashSHA256fromContent() string {
}
// FIXME: add the obj.CleanState() calls all over the true returns!
-func (obj *FileType) StateOK() bool {
+func (obj *FileRes) StateOK() bool {
if obj.isStateOK { // cache the state
return true
}
@@ -295,9 +295,9 @@ func (obj *FileType) StateOK() bool {
}
}
-func (obj *FileType) StateOKFile() bool {
+func (obj *FileRes) StateOKFile() bool {
if PathIsDir(obj.GetPath()) {
- log.Fatal("This should only be called on a File type.")
+ log.Fatal("This should only be called on a File resource.")
}
// run a diff, and return true if needs changing
@@ -326,18 +326,18 @@ func (obj *FileType) StateOKFile() bool {
return false
}
-func (obj *FileType) StateOKDir() bool {
+func (obj *FileRes) StateOKDir() bool {
if !PathIsDir(obj.GetPath()) {
- log.Fatal("This should only be called on a Dir type.")
+ log.Fatal("This should only be called on a Dir resource.")
}
// XXX: not implemented
log.Fatal("Not implemented!")
return false
}
-func (obj *FileType) Apply() bool {
- log.Printf("%v[%v]: Apply", obj.GetType(), obj.GetName())
+func (obj *FileRes) Apply() bool {
+ log.Printf("%v[%v]: Apply", obj.GetRes(), obj.GetName())
if PathIsDir(obj.GetPath()) {
return obj.ApplyDir()
@@ -346,10 +346,10 @@ func (obj *FileType) Apply() bool {
}
}
-func (obj *FileType) ApplyFile() bool {
+func (obj *FileRes) ApplyFile() bool {
if PathIsDir(obj.GetPath()) {
- log.Fatal("This should only be called on a File type.")
+ log.Fatal("This should only be called on a File resource.")
}
if obj.State == "absent" {
@@ -378,30 +378,30 @@ func (obj *FileType) ApplyFile() bool {
return true
}
-func (obj *FileType) ApplyDir() bool {
+func (obj *FileRes) ApplyDir() bool {
if !PathIsDir(obj.GetPath()) {
- log.Fatal("This should only be called on a Dir type.")
+ log.Fatal("This should only be called on a Dir resource.")
}
// XXX: not implemented
log.Fatal("Not implemented!")
return true
}
-func (obj *FileType) Compare(typ Type) bool {
- switch typ.(type) {
- case *FileType:
- typ := typ.(*FileType)
- if obj.Name != typ.Name {
+func (obj *FileRes) Compare(res Res) bool {
+ switch res.(type) {
+ case *FileRes:
+ res := res.(*FileRes)
+ if obj.Name != res.Name {
return false
}
- if obj.GetPath() != typ.Path {
+ if obj.GetPath() != res.Path {
return false
}
- if obj.Content != typ.Content {
+ if obj.Content != res.Content {
return false
}
- if obj.State != typ.State {
+ if obj.State != res.State {
return false
}
default:
View
@@ -177,7 +177,7 @@ func run(c *cli.Context) {
continue
}
for v := range G.GetVerticesChan() {
- if v.Type.GetConvergedState() != typeConvergedTimeout {
+ if v.Res.GetConvergedState() != resConvergedTimeout {
continue ConvergedLoop
}
}
View
@@ -152,7 +152,7 @@ func TestMiscT6(t *testing.T) {
type foo struct {
Name string `yaml:"name"`
- Type string `yaml:"type"`
+ Res string `yaml:"res"`
Value int `yaml:"value"`
}
@@ -175,7 +175,7 @@ func TestMiscT7(t *testing.T) {
type Foo struct {
Name string `yaml:"name"`
- Type string `yaml:"type"`
+ Res string `yaml:"res"`
Value int `yaml:"value"`
}
View
@@ -57,7 +57,7 @@ type Graph struct {
type Vertex struct {
graph *Graph // store a pointer to the graph it's on
- Type // anonymous field
+ Res // anonymous field
data map[string]string // XXX: currently unused i think, remove?
}
@@ -73,9 +73,9 @@ func NewGraph(name string) *Graph {
}
}
-func NewVertex(t Type) *Vertex {
+func NewVertex(r Res) *Vertex {
return &Vertex{
- Type: t,
+ Res: r,
data: make(map[string]string),
}
}
@@ -111,10 +111,10 @@ func (g *Graph) SetState(state graphState) graphState {
return prev
}
-// store a pointer in the type to it's parent vertex
+// store a pointer in the resource to it's parent vertex
func (g *Graph) SetVertex() {
for v := range g.GetVerticesChan() {
- v.Type.SetVertex(v)
+ v.Res.SetVertex(v)
}
}
@@ -160,7 +160,7 @@ func (g *Graph) GetVertex(name string) chan *Vertex {
return ch
}
-func (g *Graph) GetVertexMatch(obj Type) *Vertex {
+func (g *Graph) GetVertexMatch(obj Res) *Vertex {
for k := range g.Adjacency {
if k.Compare(obj) { // XXX test
return k
@@ -241,7 +241,7 @@ func (g *Graph) Graphviz() (out string) {
//out += "\tnode [shape=box];\n"
str := ""
for i := range g.Adjacency { // reverse paths
- out += fmt.Sprintf("\t%v [label=\"%v[%v]\"];\n", i.GetName(), i.GetType(), i.GetName())
+ out += fmt.Sprintf("\t%v [label=\"%v[%v]\"];\n", i.GetName(), i.GetRes(), i.GetName())
for j := range g.Adjacency[i] {
k := g.Adjacency[i][j]
// use str for clearer output ordering
@@ -549,14 +549,14 @@ func (g *Graph) Start(wg *sync.WaitGroup, first bool) { // start or continue
indegree := g.InDegree() // compute all of the indegree's
for _, v := range Reverse(t) {
- if !v.Type.IsWatching() { // if Watch() is not running...
+ if !v.Res.IsWatching() { // if Watch() is not running...
wg.Add(1)
// must pass in value to avoid races...
// see: https://ttboj.wordpress.com/2015/07/27/golang-parallelism-issues-causing-too-many-open-files-error/
go func(vv *Vertex) {
defer wg.Done()
- vv.Type.Watch()
- log.Printf("%v[%v]: Exited", vv.GetType(), vv.GetName())
+ vv.Res.Watch()
+ log.Printf("%v[%v]: Exited", vv.GetRes(), vv.GetName())
}(v)
}
@@ -574,10 +574,10 @@ func (g *Graph) Start(wg *sync.WaitGroup, first bool) { // start or continue
// and not just selectively the subset with no indegree.
if (!first) || indegree[v] == 0 {
// ensure state is started before continuing on to next vertex
- for !v.Type.SendEvent(eventStart, true, false) {
+ for !v.Res.SendEvent(eventStart, true, false) {
if DEBUG {
// if SendEvent fails, we aren't up yet
- log.Printf("%v[%v]: Retrying SendEvent(Start)", v.GetType(), v.GetName())
+ log.Printf("%v[%v]: Retrying SendEvent(Start)", v.GetRes(), v.GetName())
// sleep here briefly or otherwise cause
// a different goroutine to be scheduled
time.Sleep(1 * time.Millisecond)
@@ -590,7 +590,7 @@ func (g *Graph) Start(wg *sync.WaitGroup, first bool) { // start or continue
func (g *Graph) Pause() {
t, _ := g.TopologicalSort()
for _, v := range t { // squeeze out the events...
- v.Type.SendEvent(eventPause, true, false)
+ v.Res.SendEvent(eventPause, true, false)
}
}
@@ -602,13 +602,13 @@ func (g *Graph) Exit() {
// when we hit the 'default' in the select statement!
// XXX: we can do this to quiesce, but it's not necessary now
- v.Type.SendEvent(eventExit, true, false)
+ v.Res.SendEvent(eventExit, true, false)
}
}
func (g *Graph) SetConvergedCallback(ctimeout int, converged chan bool) {
for v := range g.GetVerticesChan() {
- v.Type.SetConvegedCallback(ctimeout, converged)
+ v.Res.SetConvergedCallback(ctimeout, converged)
}
}
View
@@ -36,8 +36,8 @@ func TestPgraphT1(t *testing.T) {
t.Errorf("Should have 0 edges instead of: %d.", i)
}
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
e1 := NewEdge("e1")
G.AddEdge(v1, v2, e1)
@@ -53,12 +53,12 @@ func TestPgraphT1(t *testing.T) {
func TestPgraphT2(t *testing.T) {
G := NewGraph("g2")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -80,12 +80,12 @@ func TestPgraphT2(t *testing.T) {
func TestPgraphT3(t *testing.T) {
G := NewGraph("g3")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -121,9 +121,9 @@ func TestPgraphT3(t *testing.T) {
func TestPgraphT4(t *testing.T) {
G := NewGraph("g4")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -143,12 +143,12 @@ func TestPgraphT4(t *testing.T) {
func TestPgraphT5(t *testing.T) {
G := NewGraph("g5")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -172,12 +172,12 @@ func TestPgraphT5(t *testing.T) {
func TestPgraphT6(t *testing.T) {
G := NewGraph("g6")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -210,9 +210,9 @@ func TestPgraphT6(t *testing.T) {
func TestPgraphT7(t *testing.T) {
G := NewGraph("g7")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -251,28 +251,28 @@ func TestPgraphT7(t *testing.T) {
func TestPgraphT8(t *testing.T) {
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
if HasVertex(v1, []*Vertex{v1, v2, v3}) != true {
t.Errorf("Should be true instead of false.")
}
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
if HasVertex(v4, []*Vertex{v5, v6}) != false {
t.Errorf("Should be false instead of true.")
}
- v7 := NewVertex(NewNoopType("v7"))
- v8 := NewVertex(NewNoopType("v8"))
- v9 := NewVertex(NewNoopType("v9"))
+ v7 := NewVertex(NewNoopRes("v7"))
+ v8 := NewVertex(NewNoopRes("v8"))
+ v9 := NewVertex(NewNoopRes("v9"))
if HasVertex(v8, []*Vertex{v7, v8, v9}) != true {
t.Errorf("Should be true instead of false.")
}
- v1b := NewVertex(NewNoopType("v1")) // same value, different objects
+ v1b := NewVertex(NewNoopRes("v1")) // same value, different objects
if HasVertex(v1b, []*Vertex{v1, v2, v3}) != false {
t.Errorf("Should be false instead of true.")
}
@@ -281,12 +281,12 @@ func TestPgraphT8(t *testing.T) {
func TestPgraphT9(t *testing.T) {
G := NewGraph("g9")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -348,7 +348,7 @@ func TestPgraphT9(t *testing.T) {
t.Errorf("Topological sort failed, status: %v.", ok)
str := "Found:"
for _, v := range s {
- str += " " + v.Type.GetName()
+ str += " " + v.Res.GetName()
}
t.Errorf(str)
}
@@ -357,12 +357,12 @@ func TestPgraphT9(t *testing.T) {
func TestPgraphT10(t *testing.T) {
G := NewGraph("g10")
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
e1 := NewEdge("e1")
e2 := NewEdge("e2")
e3 := NewEdge("e3")
@@ -382,12 +382,12 @@ func TestPgraphT10(t *testing.T) {
}
func TestPgraphT11(t *testing.T) {
- v1 := NewVertex(NewNoopType("v1"))
- v2 := NewVertex(NewNoopType("v2"))
- v3 := NewVertex(NewNoopType("v3"))
- v4 := NewVertex(NewNoopType("v4"))
- v5 := NewVertex(NewNoopType("v5"))
- v6 := NewVertex(NewNoopType("v6"))
+ v1 := NewVertex(NewNoopRes("v1"))
+ v2 := NewVertex(NewNoopRes("v2"))
+ v3 := NewVertex(NewNoopRes("v3"))
+ v4 := NewVertex(NewNoopRes("v4"))
+ v5 := NewVertex(NewNoopRes("v5"))
+ v6 := NewVertex(NewNoopRes("v6"))
if rev := Reverse([]*Vertex{}); !reflect.DeepEqual(rev, []*Vertex{}) {
t.Errorf("Reverse of vertex slice failed.")
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -27,15 +27,15 @@ import (
"log"
)
-type ServiceType struct {
- BaseType `yaml:",inline"`
- State string `yaml:"state"` // state: running, stopped
- Startup string `yaml:"startup"` // enabled, disabled, undefined
+type SvcRes struct {
+ BaseRes `yaml:",inline"`
+ State string `yaml:"state"` // state: running, stopped
+ Startup string `yaml:"startup"` // enabled, disabled, undefined
}
-func NewServiceType(name, state, startup string) *ServiceType {
- return &ServiceType{
- BaseType: BaseType{
+func NewSvcRes(name, state, startup string) *SvcRes {
+ return &SvcRes{
+ BaseRes: BaseRes{
Name: name,
events: make(chan Event),
vertex: nil,
@@ -45,19 +45,19 @@ func NewServiceType(name, state, startup string) *ServiceType {
}
}
-func (obj *ServiceType) GetType() string {
- return "Service"
+func (obj *SvcRes) GetRes() string {
+ return "Svc"
}
// Service watcher
-func (obj *ServiceType) Watch() {
+func (obj *SvcRes) Watch() {
if obj.IsWatching() {
return
}
obj.SetWatching(true)
defer obj.SetWatching(false)
- // obj.Name: service name
+ // obj.Name: svc name
//vertex := obj.GetVertex() // stored with SetVertex
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
@@ -80,11 +80,11 @@ func (obj *ServiceType) Watch() {
buschan := make(chan *dbus.Signal, 10)
bus.Signal(buschan)
- var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
- var send = false // send event?
+ var svc = fmt.Sprintf("%v.service", obj.Name) // systemd name
+ var send = false // send event?
var exit = false
var dirty = false
- var invalid = false // does the service exist or not?
+ var invalid = false // does the svc exist or not?
var previous bool // previous invalid value
set := conn.NewSubscriptionSet() // no error should be returned
subChannel, subErrors := set.Subscribe()
@@ -97,8 +97,8 @@ func (obj *ServiceType) Watch() {
previous = invalid
invalid = false
- // firstly, does service even exist or not?
- loadstate, err := conn.GetUnitProperty(service, "LoadState")
+ // firstly, does svc even exist or not?
+ loadstate, err := conn.GetUnitProperty(svc, "LoadState")
if err != nil {
log.Printf("Failed to get property: %v", err)
invalid = true
@@ -107,7 +107,7 @@ func (obj *ServiceType) Watch() {
if !invalid {
var notFound = (loadstate.Value == dbus.MakeVariant("not-found"))
if notFound { // XXX: in the loop we'll handle changes better...
- log.Printf("Failed to find service: %v", service)
+ log.Printf("Failed to find svc: %v", svc)
invalid = true // XXX ?
}
}
@@ -118,21 +118,21 @@ func (obj *ServiceType) Watch() {
}
if invalid {
- log.Printf("Waiting for: %v", service) // waiting for service to appear...
+ log.Printf("Waiting for: %v", svc) // waiting for svc to appear...
if activeSet {
activeSet = false
- set.Remove(service) // no return value should ever occur
+ set.Remove(svc) // no return value should ever occur
}
- obj.SetState(typeWatching) // reset
+ obj.SetState(resStateWatching) // reset
select {
case _ = <-buschan: // XXX wait for new units event to unstick
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
// loop so that we can see the changed invalid signal
- log.Printf("Service[%v]->DaemonReload()", service)
+ log.Printf("Svc[%v]->DaemonReload()", svc)
case event := <-obj.events:
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
if exit, send = obj.ReadEvent(&event); exit {
return // exit
}
@@ -141,47 +141,47 @@ func (obj *ServiceType) Watch() {
}
case _ = <-TimeAfterOrBlock(obj.ctimeout):
- obj.SetConvergedState(typeConvergedTimeout)
+ obj.SetConvergedState(resConvergedTimeout)
obj.converged <- true
continue
}
} else {
if !activeSet {
activeSet = true
- set.Add(service) // no return value should ever occur
+ set.Add(svc) // no return value should ever occur
}
- log.Printf("Watching: %v", service) // attempting to watch...
- obj.SetState(typeWatching) // reset
+ log.Printf("Watching: %v", svc) // attempting to watch...
+ obj.SetState(resStateWatching) // reset
select {
case event := <-subChannel:
- log.Printf("Service event: %+v", event)
+ log.Printf("Svc event: %+v", event)
// NOTE: the value returned is a map for some reason...
- if event[service] != nil {
- // event[service].ActiveState is not nil
- if event[service].ActiveState == "active" {
- log.Printf("Service[%v]->Started()", service)
- } else if event[service].ActiveState == "inactive" {
- log.Printf("Service[%v]->Stopped!()", service)
+ if event[svc] != nil {
+ // event[svc].ActiveState is not nil
+ if event[svc].ActiveState == "active" {
+ log.Printf("Svc[%v]->Started()", svc)
+ } else if event[svc].ActiveState == "inactive" {
+ log.Printf("Svc[%v]->Stopped!()", svc)
} else {
- log.Fatal("Unknown service state: ", event[service].ActiveState)
+ log.Fatal("Unknown svc state: ", event[svc].ActiveState)
}
} else {
- // service stopped (and ActiveState is nil...)
- log.Printf("Service[%v]->Stopped", service)
+ // svc stopped (and ActiveState is nil...)
+ log.Printf("Svc[%v]->Stopped", svc)
}
send = true
dirty = true
case err := <-subErrors:
- obj.SetConvergedState(typeConvergedNil) // XXX ?
+ obj.SetConvergedState(resConvergedNil) // XXX ?
log.Println("error:", err)
log.Fatal(err)
- //vertex.events <- fmt.Sprintf("service: %v", "error") // XXX: how should we handle errors?
+ //vertex.events <- fmt.Sprintf("svc: %v", "error") // XXX: how should we handle errors?
case event := <-obj.events:
- obj.SetConvergedState(typeConvergedNil)
+ obj.SetConvergedState(resConvergedNil)
if exit, send = obj.ReadEvent(&event); exit {
return // exit
}
@@ -203,7 +203,7 @@ func (obj *ServiceType) Watch() {
}
}
-func (obj *ServiceType) StateOK() bool {
+func (obj *SvcRes) StateOK() bool {
if obj.isStateOK { // cache the state
return true
}
@@ -218,9 +218,9 @@ func (obj *ServiceType) StateOK() bool {
}
defer conn.Close()
- var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
+ var svc = fmt.Sprintf("%v.service", obj.Name) // systemd name
- loadstate, err := conn.GetUnitProperty(service, "LoadState")
+ loadstate, err := conn.GetUnitProperty(svc, "LoadState")
if err != nil {
log.Printf("Failed to get load state: %v", err)
return false
@@ -229,14 +229,14 @@ func (obj *ServiceType) StateOK() bool {
// NOTE: we have to compare variants with other variants, they are really strings...
var notFound = (loadstate.Value == dbus.MakeVariant("not-found"))
if notFound {
- log.Printf("Failed to find service: %v", service)
+ log.Printf("Failed to find svc: %v", svc)
return false
}
- // XXX: check service "enabled at boot" or not status...
+ // XXX: check svc "enabled at boot" or not status...
- //conn.GetUnitProperties(service)
- activestate, err := conn.GetUnitProperty(service, "ActiveState")
+ //conn.GetUnitProperties(svc)
+ activestate, err := conn.GetUnitProperty(svc, "ActiveState")
if err != nil {
log.Fatal("Failed to get active state: ", err)
}
@@ -258,8 +258,8 @@ func (obj *ServiceType) StateOK() bool {
return true // all is good, no state change needed
}
-func (obj *ServiceType) Apply() bool {
- log.Printf("%v[%v]: Apply", obj.GetType(), obj.GetName())
+func (obj *SvcRes) Apply() bool {
+ log.Printf("%v[%v]: Apply", obj.GetRes(), obj.GetName())
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
@@ -271,8 +271,8 @@ func (obj *ServiceType) Apply() bool {
}
defer conn.Close()
- var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
- var files = []string{service} // the service represented in a list
+ var svc = fmt.Sprintf("%v.service", obj.Name) // systemd name
+ var files = []string{svc} // the svc represented in a list
if obj.Startup == "enabled" {
_, _, err = conn.EnableUnitFiles(files, false, true)
@@ -289,13 +289,13 @@ func (obj *ServiceType) Apply() bool {
result := make(chan string, 1) // catch result information
if obj.State == "running" {
- _, err := conn.StartUnit(service, "fail", result)
+ _, err := conn.StartUnit(svc, "fail", result)
if err != nil {
log.Fatal("Failed to start unit: ", err)
return false
}
} else if obj.State == "stopped" {
- _, err = conn.StopUnit(service, "fail", result)
+ _, err = conn.StopUnit(svc, "fail", result)
if err != nil {
log.Fatal("Failed to stop unit: ", err)
return false
@@ -319,17 +319,17 @@ func (obj *ServiceType) Apply() bool {
return true
}
-func (obj *ServiceType) Compare(typ Type) bool {
- switch typ.(type) {
- case *ServiceType:
- typ := typ.(*ServiceType)
- if obj.Name != typ.Name {
+func (obj *SvcRes) Compare(res Res) bool {
+ switch res.(type) {
+ case *SvcRes:
+ res := res.(*SvcRes)
+ if obj.Name != res.Name {
return false
}
- if obj.State != typ.State {
+ if obj.State != res.State {
return false
}
- if obj.Startup != typ.Startup {
+ if obj.Startup != res.Startup {
return false
}
default:
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
noop:
- name: noop1
file:
@@ -27,15 +27,15 @@ types:
edges:
- name: e1
from:
- type: file
+ res: file
name: file1
to:
- type: file
+ res: file
name: file2
- name: e2
from:
- type: file
+ res: file
name: file2
to:
- type: file
+ res: file
name: file3
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1a
path: "/tmp/mgmt/mgmtA/f1a"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host A
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmt/mgmtA/"
edges: []
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1b
path: "/tmp/mgmt/mgmtB/f1b"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host B
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmt/mgmtB/"
edges: []
View
@@ -1,6 +1,6 @@
---
graph: mygraph
-types:
+resources:
file:
- name: file1c
path: "/tmp/mgmt/mgmtC/f1c"
@@ -23,6 +23,6 @@ types:
i am f4, exported from host C
state: exists
collect:
-- type: file
+- res: file
pattern: "/tmp/mgmt/mgmtC/"
edges: []
View
@@ -1,7 +1,7 @@
---
graph: mygraph
comment: simple exec fan in example to demonstrate optimization)
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -56,22 +56,22 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec5
- name: e2
from:
- type: exec
+ res: exec
name: exec2
to:
- type: exec
+ res: exec
name: exec5
- name: e3
from:
- type: exec
+ res: exec
name: exec3
to:
- type: exec
+ res: exec
name: exec5
View
@@ -1,7 +1,7 @@
---
graph: mygraph
comment: simple exec fan in to fan out example to demonstrate optimization
-types:
+resources:
exec:
- name: exec1
cmd: sleep 10s
@@ -86,43 +86,43 @@ types:
edges:
- name: e1
from:
- type: exec
+ res: exec
name: exec1
to:
- type: exec
+ res: exec
name: exec4
- name: e2
from:
- type: exec
+ res: exec
name: exec2
to:
- type: exec
+ res: exec
name: exec4
- name: e3
from:
- type: exec
+ res: exec
name: exec3
to:
- type: exec
+ res: exec
name: exec4
- name: e4
from:
- type: exec
+ res: exec
name: exec4
to:
- type: exec
+ res: exec
name: exec5
- name: e5
from:
- type: exec
+ res: exec
name: exec4
to:
- type: exec
+ res: exec
name: exec6
- name: e6
from:
- type: exec
+ res: exec
name: exec4
to:
- type: exec
+ res: exec
name: exec7