feat: initial commit

This commit is contained in:
2023-02-02 10:55:24 +01:00
commit a567e47421
92 changed files with 7300 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package openwrt
import (
"forge.cadoles.com/Cadoles/emissary/internal/command/agent/openwrt/uci"
"github.com/urfave/cli/v2"
)
func Root() *cli.Command {
return &cli.Command{
Name: "openwrt",
Usage: "OpenWRT related commands",
Subcommands: []*cli.Command{
uci.Root(),
},
}
}

View File

@ -0,0 +1,15 @@
package uci
import (
"github.com/urfave/cli/v2"
)
func Root() *cli.Command {
return &cli.Command{
Name: "uci",
Usage: "UCI related commands",
Subcommands: []*cli.Command{
TransformCommand(),
},
}
}

View File

@ -0,0 +1,107 @@
package uci
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"forge.cadoles.com/Cadoles/emissary/internal/command/common"
"forge.cadoles.com/Cadoles/emissary/internal/openwrt/uci"
"github.com/pkg/errors"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/urfave/cli/v2"
)
const (
FormatJSON = "json"
FormatUCI = "uci"
)
func TransformCommand() *cli.Command {
flags := common.Flags()
flags = append(flags,
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "Define the source format ('uci' or 'json')",
Value: "uci",
},
&cli.StringFlag{
Name: "input",
Usage: "File to use as input (or '-' for STDIN)",
Aliases: []string{"i"},
TakesFile: true,
Value: "-",
},
)
return &cli.Command{
Name: "transform",
Usage: "Transform UCI configuration from/to JSON",
Flags: flags,
Action: func(ctx *cli.Context) error {
input := ctx.String("input")
format := ctx.String("format")
var reader io.Reader
switch input {
case "-":
reader = os.Stdin
default:
file, err := os.Open(input)
if err != nil {
return errors.WithStack(err)
}
defer func() {
if err := file.Close(); err != nil {
panic(errors.WithStack(err))
}
}()
reader = file
}
var conf *uci.UCI
switch format {
case FormatJSON:
decoder := json.NewDecoder(reader)
conf = uci.NewUCI()
if err := decoder.Decode(conf); err != nil {
return errors.WithStack(err)
}
fmt.Print(conf.Export())
case FormatUCI:
data, err := ioutil.ReadAll(reader)
if err != nil {
return errors.WithStack(err)
}
conf, err = uci.Parse(data)
if err != nil {
return errors.WithStack(err)
}
jsonData, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return errors.WithStack(err)
}
fmt.Print(string(jsonData))
default:
return errors.Errorf("unexpected format '%s'", format)
}
return nil
},
}
}

View File

@ -0,0 +1,17 @@
package agent
import (
"forge.cadoles.com/Cadoles/emissary/internal/command/agent/openwrt"
"github.com/urfave/cli/v2"
)
func Root() *cli.Command {
return &cli.Command{
Name: "agent",
Usage: "Agent related commands",
Subcommands: []*cli.Command{
openwrt.Root(),
RunCommand(),
},
}
}

View File

@ -0,0 +1,68 @@
package agent
import (
"time"
"forge.cadoles.com/Cadoles/emissary/internal/agent"
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/gateway"
"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/spec"
"forge.cadoles.com/Cadoles/emissary/internal/command/common"
"github.com/pkg/errors"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"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(string(ctrlConf.Spec.ServerURL)))
}
if ctrlConf.Gateway.Enabled {
controllers = append(controllers, gateway.NewController())
}
if ctrlConf.UCI.Enabled {
controllers = append(controllers, openwrt.NewUCIController(
string(ctrlConf.UCI.BinPath),
))
}
agent := agent.New(
agent.WithInterval(time.Duration(conf.Agent.ReconciliationInterval)*time.Second),
agent.WithControllers(controllers...),
)
if err := agent.Run(ctx.Context); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}