package agent import ( "time" "forge.cadoles.com/Cadoles/emissary/internal/agent" "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app" "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/openwrt" "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/persistence" "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/proxy" "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" "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" "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()) } if ctrlConf.Proxy.Enabled { controllers = append(controllers, proxy.NewController()) } 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()) agent := agent.New( string(conf.Agent.ServerURL), key, thumbprint, agent.WithInterval(time.Duration(conf.Agent.ReconciliationInterval)*time.Second), agent.WithControllers(controllers...), agent.WithCollectors(collectors...), ) 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 }