emissary/internal/command/agent/run.go

111 lines
3.1 KiB
Go
Raw Normal View History

2023-02-02 10:55:24 +01:00
package agent
import (
"time"
"forge.cadoles.com/Cadoles/emissary/internal/agent"
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app"
2023-02-02 10:55:24 +01:00
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/openwrt"
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/persistence"
2023-03-21 13:28:41 +01:00
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/proxy"
2023-02-02 10:55:24 +01:00
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/spec"
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata/collector/buildinfo"
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata/collector/shell"
2023-02-02 10:55:24 +01:00
"forge.cadoles.com/Cadoles/emissary/internal/command/common"
"forge.cadoles.com/Cadoles/emissary/internal/config"
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
"forge.cadoles.com/Cadoles/emissary/internal/machineid"
2023-02-02 10:55:24 +01:00
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/logger"
)
func RunCommand() *cli.Command {
flags := common.Flags()
return &cli.Command{
Name: "run",
Usage: "Run the emissary agent",
Flags: flags,
Action: func(ctx *cli.Context) error {
conf, err := common.LoadConfig(ctx)
if err != nil {
return errors.Wrap(err, "Could not load configuration")
}
logger.SetFormat(logger.Format(conf.Logger.Format))
logger.SetLevel(logger.Level(conf.Logger.Level))
controllers := make([]agent.Controller, 0)
ctrlConf := conf.Agent.Controllers
if ctrlConf.Persistence.Enabled {
controllers = append(controllers, persistence.NewController(string(ctrlConf.Persistence.StateFile)))
}
if ctrlConf.Spec.Enabled {
controllers = append(controllers, spec.NewController())
2023-02-02 10:55:24 +01:00
}
2023-03-21 13:28:41 +01:00
if ctrlConf.Proxy.Enabled {
controllers = append(controllers, proxy.NewController())
2023-02-02 10:55:24 +01:00
}
if ctrlConf.UCI.Enabled {
controllers = append(controllers, openwrt.NewUCIController(
string(ctrlConf.UCI.BinPath),
))
}
if ctrlConf.App.Enabled {
controllers = append(controllers, app.NewController(
app.WithDataDir(string(ctrlConf.App.DataDir)),
app.WithDownloadDir(string(ctrlConf.App.DownloadDir)),
))
}
key, err := jwk.LoadOrGenerate(string(conf.Agent.PrivateKeyPath), jwk.DefaultKeySize)
if err != nil {
return errors.WithStack(err)
}
thumbprint, err := machineid.Get()
if err != nil {
return errors.WithStack(err)
}
collectors := createShellCollectors(&conf.Agent)
collectors = append(collectors, buildinfo.NewCollector())
2023-02-02 10:55:24 +01:00
agent := agent.New(
string(conf.Agent.ServerURL),
key,
thumbprint,
2023-02-02 10:55:24 +01:00
agent.WithInterval(time.Duration(conf.Agent.ReconciliationInterval)*time.Second),
agent.WithControllers(controllers...),
agent.WithCollectors(collectors...),
2023-02-02 10:55:24 +01:00
)
if err := agent.Run(ctx.Context); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}
func createShellCollectors(conf *config.AgentConfig) []metadata.Collector {
collectors := make([]metadata.Collector, 0)
for _, c := range conf.Collectors {
collector := shell.NewCollector(string(c.Name), string(c.Command), c.Args...)
collectors = append(collectors, collector)
}
return collectors
}