-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmachines.go
102 lines (83 loc) · 2.36 KB
/
machines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
)
type StateMachine struct {
Name string
Path string
Usage string
States []string
}
var globalStateMachines []StateMachine
func initMachines() {
machines, err := ioutil.ReadDir(globalConfig.StateMachinePath)
if err != nil {
fmt.Printf("error listing StateMachinePath %s directory: %s\n", globalConfig.StateMachinePath, err)
os.Exit(1)
}
for _, machine := range machines {
machinePath := path.Join(globalConfig.StateMachinePath, machine.Name())
machineInfo, err := os.Stat(machinePath)
if err != nil {
fmt.Printf("error stat'ing %s: %s\n", machinePath, err)
}
if machineInfo.IsDir() {
machineStruct := StateMachine{Name: machine.Name(), Path: machinePath}
states, err := ioutil.ReadDir(machinePath)
if err != nil {
fmt.Printf("error listing %s directory: %s\n", machine.Name(), err)
os.Exit(1)
}
hasStart := false
for _, stateInfo := range states {
if stateInfo.Mode().Perm()&0111 > 0 {
machineStruct.States = append(machineStruct.States, stateInfo.Name())
if stateInfo.Name() == "start" {
hasStart = true
}
}
}
if !hasStart {
fmt.Printf("state machine directory for %s has no start state\n", machineStruct.Name)
os.Exit(1)
}
cmd := exec.Command(machinePath+"/start", "--help")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("error executing '%s/start --help' to get usage for %s: %s\n", machinePath, machineStruct.Name, err)
os.Exit(1)
} else {
machineStruct.Usage = string(output)
}
globalStateMachines = append(globalStateMachines, machineStruct)
}
}
}
func machineGet(name string) *StateMachine {
for _, machine := range globalStateMachines {
if machine.Name == name {
return &machine
}
}
return nil
}
type ExecuteResponse struct {
Id uint64
Message string
}
func machineExecute(name string, input string) (int, string, *ExecuteResponse) {
machine := machineGet(name)
if machine == nil {
return 404, "State machine not found", nil
}
id, err := globalScheduler.ScheduleMachine(name, machine.Path, input)
if err != nil {
return 500, fmt.Sprintf("Error scheduling execution of %s: %s", name, err), nil
} else {
return -1, "", &ExecuteResponse{Id: id, Message: fmt.Sprintf("The state machine %s was scheduled for execution successfully.", name)}
}
}