/
machine_run.go
94 lines (76 loc) · 1.96 KB
/
machine_run.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
package cmd
import (
"encoding/json"
"os"
"sync"
"github.com/sirupsen/logrus"
"github.com/choria-io/go-choria/aagent/notifiers/console"
"github.com/choria-io/go-choria/aagent/machine"
"github.com/choria-io/go-choria/aagent/watchers"
)
type mRunCommand struct {
command
sourceDir string
factsFile string
dataFile string
}
func (r *mRunCommand) Setup() (err error) {
if machine, ok := cmdWithFullCommand("machine"); ok {
r.cmd = machine.Cmd().Command("run", "Runs an autonomous agent locally")
r.cmd.Arg("source", "Directory containing the machine definition").Required().ExistingDirVar(&r.sourceDir)
r.cmd.Flag("facts", "JSON format facts file to supply to the machine as run time facts").ExistingFileVar(&r.factsFile)
r.cmd.Flag("data", "JSON format data file to supply to the machine as run time data").ExistingFileVar(&r.dataFile)
}
return nil
}
func (r *mRunCommand) Configure() error {
return commonConfigure()
}
func (r *mRunCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
m, err := machine.FromDir(r.sourceDir, watchers.New(ctx))
if err != nil {
return err
}
m.SetIdentity("cli")
m.RegisterNotifier(&console.Notifier{})
m.SetMainCollective(cfg.MainCollective)
if r.factsFile != "" {
facts, err := os.ReadFile(r.factsFile)
if err != nil {
return err
}
m.SetFactSource(func() json.RawMessage { return facts })
}
if r.dataFile != "" {
dat := make(map[string]string)
df, err := os.ReadFile(r.dataFile)
if err != nil {
return err
}
err = json.Unmarshal(df, &dat)
if err != nil {
return err
}
for k, v := range dat {
err = m.DataPut(k, v)
if err != nil {
return err
}
}
}
conn, err := c.NewConnector(ctx, c.MiddlewareServers, "machine run", c.Logger("machine"))
if err != nil {
return err
}
m.SetConnection(conn)
<-m.Start(ctx, wg)
<-ctx.Done()
return nil
}
func init() {
cli.commands = append(cli.commands, &mRunCommand{})
}