emissary/internal/command/agent/run.go

153 lines
4.5 KiB
Go
Raw Permalink 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-04-04 20:26:19 +02:00
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/mdns"
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"
2024-02-29 17:17:21 +01:00
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/status"
"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
}
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)),
))
}
if ctrlConf.Proxy.Enabled {
controllers = append(controllers, proxy.NewController())
}
2023-04-04 20:26:19 +02:00
if ctrlConf.MDNS.Enabled {
controllers = append(controllers, mdns.NewController())
}
if ctrlConf.SysUpgrade.Enabled {
sysUpgradeArgs := make([]string, 0)
if len(ctrlConf.SysUpgrade.SysUpgradeCommand) > 1 {
sysUpgradeArgs = ctrlConf.SysUpgrade.SysUpgradeCommand[1:]
}
firmwareVersionArgs := make([]string, 0)
if len(ctrlConf.SysUpgrade.FirmwareVersionCommand) > 1 {
firmwareVersionArgs = ctrlConf.SysUpgrade.FirmwareVersionCommand[1:]
}
controllers = append(controllers, openwrt.NewSysUpgradeController(
openwrt.WithSysUpgradeCommand(
ctrlConf.SysUpgrade.SysUpgradeCommand[0],
sysUpgradeArgs...,
),
openwrt.WithSysUpgradeShellFirmwareVersion(
ctrlConf.SysUpgrade.FirmwareVersionCommand[0],
firmwareVersionArgs...,
),
))
}
2024-02-29 17:17:21 +01:00
if ctrlConf.Status.Enabled {
controllers = append(controllers, status.NewController(
string(ctrlConf.Status.Address),
string(ctrlConf.Status.ClaimURL),
string(ctrlConf.Status.AgentURL),
string(ctx.String("projectVersion")),
))
}
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)
}
logger.SetLevel(logger.LevelInfo)
logger.Info(ctx.Context, "agent thumbprint", logger.F("thumbprint", thumbprint))
logger.SetLevel(logger.Level(conf.Logger.Level))
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
}