diff --git a/orchestrator/graph.go b/orchestrator/graph.go index 6645eaf..ff27c37 100644 --- a/orchestrator/graph.go +++ b/orchestrator/graph.go @@ -32,6 +32,11 @@ type Graph struct { Nodes []Node `json:"nodes"` } +func (v *Graph) getNodeFromName(n string) (Node, error) { + var nn Node + return nn, nil +} + // Run executes the Graph structure func (v *Graph) Run(stop <-chan time.Time) { v.State = Running diff --git a/orchestrator/graph_test.go b/orchestrator/graph_test.go index ba49e3d..f9b02eb 100644 --- a/orchestrator/graph_test.go +++ b/orchestrator/graph_test.go @@ -25,6 +25,33 @@ import ( "testing" ) +func TestRun(t *testing.T) { + e := valid.Check() + if e.Code != 0 { + t.Errorf("Struct should be valid, error is: %v", e.Error()) + } + e = notValid.Check() + if e.Code == 0 { + t.Errorf("Struct should not be valid, error is: %v", e.Error()) + } + + var wg sync.WaitGroup + count := 1 + vs := make([]Graph, count) + wg.Add(count) + for i := 0; i < count; i++ { + vs[i] = valid + vs[i].Name = fmt.Sprintf("%v", i) + go func(v Graph, wg *sync.WaitGroup) { + v.Run(nil) + wg.Done() + }(vs[i], &wg) + } + wg.Wait() + for i := 0; i < count; i++ { + t.Log(vs[i]) + } +} func BenchmarkRun(b *testing.B) { e := valid.Check() if e.Code != 0 { diff --git a/orchestrator/node.go b/orchestrator/node.go index 5467098..a8bf8e5 100644 --- a/orchestrator/node.go +++ b/orchestrator/node.go @@ -20,7 +20,9 @@ along with this program. If not, see . package orchestrator import ( + "fmt" "math/rand" + "regexp" "time" ) @@ -39,6 +41,8 @@ type Node struct { func (n *Node) Run() <-chan Message { c := make(chan Message) waitForIt := make(chan Graph) // Shared between all messages. + var ga = regexp.MustCompile(`^get_attribute (.+):(.+)$`) + go func() { n.State = ToRun for n.State <= ToRun { @@ -60,6 +64,16 @@ func (n *Node) Run() <-chan Message { c <- Message{n.ID, n.State, waitForIt} } if n.State == Running { + // Check and find the arguments + for i, arg := range n.Args { + // If argument is a get_attribute node:attribute + // Then substitute it to its actual value + subargs := ga.FindStringSubmatch(arg) + if len(subargs) == 4 { + nn, _ := g.getNodeFromName(subargs[2]) + n.Args[i] = nn.Outputs[subargs[3]] + } + } c <- Message{n.ID, n.State, waitForIt} switch n.Engine { case "nil": @@ -69,6 +83,7 @@ func (n *Node) Run() <-chan Message { time.Sleep(time.Duration(rand.Intn(1e4)) * time.Millisecond) rand.Seed(time.Now().Unix()) n.State = Success + n.Outputs["result"] = fmt.Sprintf("%v_%v", n.Name, time.Now().Unix()) default: // Send the message to the appropriate backend n.State = Success diff --git a/orchestrator/structure_test.go b/orchestrator/structure_test.go index 7b7cb82..d6c2cd8 100644 --- a/orchestrator/structure_test.go +++ b/orchestrator/structure_test.go @@ -42,14 +42,14 @@ func init() { }, []Node{ {0, 0, "a", "nil", "myplaybook.yml", nil, nil}, - {1, 0, "b", "shell", "myscript.sh", nil, + {1, 0, "b", "sleep", "myscript.sh", nil, map[string]string{ - "output1": "", + "result": "", }, }, {2, 0, "c", "shell", "myscript2.sh", []string{ - "-e", "get_attribute 1:output1", + "-e", "get_attribute b:result", }, nil}, {3, 0, "d", "nil", "myplaybook3.yml", nil, nil}, {4, 0, "e", "nil", "myplaybook4.yml", nil, nil},