Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CLI / Config with Cobra / Viper #1043

Merged
merged 32 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
37796a4
receptor running without cmdline tool
resoluteCoder May 8, 2024
902d630
another one
resoluteCoder May 15, 2024
cb64d8f
another one
resoluteCoder May 16, 2024
79719c3
added config version switch and defaults
resoluteCoder May 23, 2024
7ab6215
added support for env vars for v2 yml
resoluteCoder May 30, 2024
d9ad77d
added conditional for init fn for v2 config
resoluteCoder May 30, 2024
e0e89e7
another one
resoluteCoder Jun 25, 2024
5628b33
certs are a working
resoluteCoder Jun 27, 2024
39bfe48
added defaults to config the right way
resoluteCoder Jul 3, 2024
e396968
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 3, 2024
93de190
added --legacy flag to cli tests
resoluteCoder Jul 3, 2024
25a34db
linted, fixed issues
resoluteCoder Jul 5, 2024
b4eae42
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 5, 2024
9617e85
containerstuff
resoluteCoder Jul 5, 2024
b18f6dc
Merge branch 'ekans-arbok' of github.com:resoluteCoder/receptor into …
resoluteCoder Jul 5, 2024
6030c59
fixed default location
resoluteCoder Jul 8, 2024
353d768
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 8, 2024
4e95b47
added redial default and legacy to python test
resoluteCoder Jul 8, 2024
8553b93
added legacy option for python tests
resoluteCoder Jul 9, 2024
26dbb7a
fixed --help
resoluteCoder Jul 11, 2024
5d879fa
changed docs from v1 to v2
resoluteCoder Jul 11, 2024
9de74cb
removed comments
resoluteCoder Jul 11, 2024
2c94344
removed comments
resoluteCoder Jul 11, 2024
cb38a4d
Merge branch 'config-v2-docs' into ekans-arbok
resoluteCoder Jul 16, 2024
e693a16
added receptor cli usage for docs, added back sphinxext ext
resoluteCoder Jul 16, 2024
6ad3ef8
removed use of slices to support go 1.20
resoluteCoder Jul 17, 2024
50b45a6
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 17, 2024
3364a2c
updated docs for better readability, change docker container to use v…
resoluteCoder Jul 29, 2024
72139fb
changed gh wf on pr test.cfg to v2
resoluteCoder Jul 30, 2024
a6721a9
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 30, 2024
642d56a
updated container file and gh wf pr to rightfully use v2 yaml file in…
resoluteCoder Jul 30, 2024
10745fe
Merge branch 'devel' into ekans-arbok
resoluteCoder Jul 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package cmd

import (
"fmt"
"os"
"reflect"

"github.com/ansible/receptor/pkg/backends"
"github.com/ansible/receptor/pkg/certificates"
"github.com/ansible/receptor/pkg/controlsvc"
"github.com/ansible/receptor/pkg/logger"
"github.com/ansible/receptor/pkg/netceptor"
"github.com/ansible/receptor/pkg/services"
"github.com/ansible/receptor/pkg/types"
"github.com/ansible/receptor/pkg/workceptor"
"github.com/ghjm/cmdline"
"github.com/spf13/viper"
)

type Initer interface {
Init() error
}

type Preparer interface {
Prepare() error
}

type Runer interface {
Run() error
}

type ReceptorConfig struct {
// Used pointer structs to apply defaults to config
Node *types.NodeCfg
Trace logger.TraceCfg
LocalOnly backends.NullBackendCfg `mapstructure:"local-only"`
LogLevel *logger.LoglevelCfg `mapstructure:"log-level"`
ControlServices []*controlsvc.CmdlineConfigUnix `mapstructure:"control-services"`
TCPPeers []*backends.TCPDialerCfg `mapstructure:"tcp-peers"`
UDPPeers []*backends.UDPDialerCfg `mapstructure:"udp-peers"`
WSPeers []*backends.WebsocketDialerCfg `mapstructure:"ws-peers"`
TCPListeners []*backends.TCPListenerCfg `mapstructure:"tcp-listeners"`
UDPListeners []*backends.UDPListenerCfg `mapstructure:"udp-listeners"`
WSListeners []*backends.WebsocketListenerCfg `mapstructure:"ws-listeners"`
TLSClients []netceptor.TLSClientConfig `mapstructure:"tls-clients"`
TLSServer []netceptor.TLSServerConfig `mapstructure:"tls-servers"`
WorkCommands []workceptor.CommandWorkerCfg `mapstructure:"work-commands"`
WorkKubernetes []*workceptor.KubeWorkerCfg `mapstructure:"work-kubernetes"`
// WorkPython []workceptor.WorkPythonCfg `mapstructure:"work-python"`
resoluteCoder marked this conversation as resolved.
Show resolved Hide resolved
WorkSigning workceptor.SigningKeyPrivateCfg `mapstructure:"work-signing"`
WorkVerification workceptor.VerifyingKeyPublicCfg `mapstructure:"work-verification"`
IpRouters []services.IpRouterCfg `mapstructure:"ip-routers"`

Check failure on line 52 in cmd/config.go

View workflow job for this annotation

GitHub Actions / go test coverage

undefined: services.IpRouterCfg

Check failure on line 52 in cmd/config.go

View workflow job for this annotation

GitHub Actions / receptor (Go 1.20)

undefined: services.IpRouterCfg

Check failure on line 52 in cmd/config.go

View workflow job for this annotation

GitHub Actions / receptor (Go 1.21)

undefined: services.IpRouterCfg
TCPClients []services.TCPProxyOutboundCfg `mapstructure:"tcp-clients"`
TCPServers []services.TCPProxyInboundCfg `mapstructure:"tcp-servers"`
UDPClients []services.TCPProxyInboundCfg `mapstructure:"udp-clients"`
UDPServers []services.TCPProxyInboundCfg `mapstructure:"udp-servers"`
UnixSocketClients []services.UnixProxyOutboundCfg `mapstructure:"unix-socket-clients"`
UnixSocketServers []services.UnixProxyInboundCfg `mapstructure:"unix-socket-servers"`
}

type CertificatesConfig struct {
InitCA certificates.InitCAConfig `mapstructure:"cert-init"`
MakeReq []certificates.MakeReqConfig `mapstructure:"cert-makereq"`
SignReq []certificates.SignReqConfig `mapstructure:"cert-signreq"`
}

func PrintPhaseErrorMessage(configName string, phase string, err error) {
fmt.Printf("ERROR: %s for %s on %s phase\n", err, configName, phase)
}

func ParseConfigs(configFile string) (*ReceptorConfig, *CertificatesConfig, error) {
if configFile == "" {
fmt.Fprintln(os.Stderr, "Could not locate config file (default is $HOME/.receptor.yaml)")
resoluteCoder marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}
var receptorConfig ReceptorConfig
var certifcatesConfig CertificatesConfig
err := viper.Unmarshal(&receptorConfig)
if err != nil {
return nil, nil, err

}

err = viper.Unmarshal(&certifcatesConfig)
if err != nil {
return nil, nil, err

}
return &receptorConfig, &certifcatesConfig, nil
}

func isConfigEmpty(v reflect.Value) bool {
isEmpty := true
for i := 0; i < v.NumField(); i++ {
if reflect.Value.IsZero(v.Field(i)) {
continue
}
isEmpty = false
}
return isEmpty
}

func RunConfigV2(v reflect.Value) {
phases := []string{"Init", "Prepare", "Run"}

for _, phase := range phases {
for i := 0; i < v.NumField(); i++ {
if reflect.Value.IsZero(v.Field(i)) {
continue
}

switch v.Field(i).Kind() {
case reflect.Slice:
for j := 0; j < v.Field(i).Len(); j++ {
RunPhases(phase, v.Field(i).Index(j))
}
default:
RunPhases(phase, v.Field(i))
}

}
}
}

// RunPhases runs the appropriate function (Init, Prepare, Run) on a command

Check failure on line 125 in cmd/config.go

View workflow job for this annotation

GitHub Actions / lint-receptor

Comment should end in a period (godot)
func RunPhases(phase string, v reflect.Value) {
resoluteCoder marked this conversation as resolved.
Show resolved Hide resolved
cmd := v.Interface()
var err error

if phase == "Init" {
switch c := cmd.(type) {

Check failure on line 131 in cmd/config.go

View workflow job for this annotation

GitHub Actions / lint-receptor

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case Initer:
err = c.Init()
if err != nil {
PrintPhaseErrorMessage(v.Type().Name(), phase, err)
}
}
}
if phase == "Prepare" {
switch c := cmd.(type) {

Check failure on line 140 in cmd/config.go

View workflow job for this annotation

GitHub Actions / lint-receptor

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case Preparer:
err = c.Prepare()
if err != nil {
PrintPhaseErrorMessage(v.Type().Name(), phase, err)
}
}
}
if phase == "Run" {
switch c := cmd.(type) {

Check failure on line 149 in cmd/config.go

View workflow job for this annotation

GitHub Actions / lint-receptor

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case Runer:
err = c.Run()
if err != nil {
PrintPhaseErrorMessage(v.Type().Name(), phase, err)
}
}
}
}

func RunConfigV1() {

cl := cmdline.NewCmdline()
cl.AddConfigType("node", "Specifies the node configuration of this instance", types.NodeCfg{}, cmdline.Required, cmdline.Singleton)
cl.AddConfigType("local-only", "Runs a self-contained node with no backend", backends.NullBackendCfg{}, cmdline.Singleton)

// Add registered config types from imported modules
for _, appName := range []string{
"receptor-version",
"receptor-logging",
"receptor-tls",
"receptor-certificates",
"receptor-control-service",
"receptor-command-service",
"receptor-ip-router",
"receptor-proxies",
"receptor-backends",
"receptor-workers",
} {
cl.AddRegisteredConfigTypes(appName)
}

osArgs := os.Args[1:]

err := cl.ParseAndRun(osArgs, []string{"Init", "Prepare", "Run"}, cmdline.ShowHelpIfNoArgs)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if cl.WhatRan() != "" {
// We ran an exclusive command, so we aren't starting any back-ends
os.Exit(0)
}

configPath := ""
for i, arg := range osArgs {
if arg == "--config" || arg == "-c" {
if len(osArgs) > i+1 {
configPath = osArgs[i+1]
}

break
}
}

// only allow reloading if a configuration file was provided. If ReloadCL is
// not set, then the control service reload command will fail
if configPath != "" {
// create closure with the passed in args to be ran during a reload
reloadParseAndRun := func(toRun []string) error {
return cl.ParseAndRun(osArgs, toRun)
}
err = controlsvc.InitReload(configPath, reloadParseAndRun)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}
}
110 changes: 110 additions & 0 deletions cmd/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

func SetTCPListenerDefaults(config *ReceptorConfig) {
for _, listener := range config.TCPListeners {
if listener.Cost == 0 {
listener.Cost = 1.0
}
if listener.BindAddr == "" {
listener.BindAddr = "0.0.0.0"
}
}
}

func SetUDPListenerDefaults(config *ReceptorConfig) {
for _, listener := range config.UDPListeners {
if listener.Cost == 0 {
listener.Cost = 1.0
}
if listener.BindAddr == "" {
listener.BindAddr = "0.0.0.0"
}
}
}

func SetWSListenerDefaults(config *ReceptorConfig) {
for _, listener := range config.WSListeners {
if listener.Cost == 0 {
listener.Cost = 1.0
}
if listener.BindAddr == "" {
listener.BindAddr = "0.0.0.0"
}
if listener.Path == "" {
listener.BindAddr = "/"
}
}
}

func SetUDPPeerDefaults(config *ReceptorConfig) {
for _, peer := range config.TCPPeers {
if peer.Cost == 0 {
peer.Cost = 1.0
}
}
}

func SetTCPPeerDefaults(config *ReceptorConfig) {
for _, peer := range config.TCPPeers {
if peer.Cost == 0 {
peer.Cost = 1.0
}
}
}

func SetWSPeerDefaults(config *ReceptorConfig) {
for _, peer := range config.WSPeers {
if peer.Cost == 0 {
peer.Cost = 1.0
}
}
}

func SetCmdlineUnixDefaults(config *ReceptorConfig) {
for _, service := range config.ControlServices {
if service.Permissions == 0 {
service.Permissions = 0600
}

if service.Service == "" {
service.Service = "control"
}
}
}

func SetLogLevelDefaults(config *ReceptorConfig) {
if config.LogLevel.Level == "" {
config.LogLevel.Level = "error"
}
}

func SetNodeDefaults(config *ReceptorConfig) {
if config.Node.DataDir == "" {
config.Node.DataDir = "/tmp/receptor"
}
}

func SetKubeWorkerDefaults(config *ReceptorConfig) {
for _, worker := range config.WorkKubernetes {
if worker.AuthMethod == "" {
worker.AuthMethod = "incluster"
}

if worker.StreamMethod == "" {
worker.StreamMethod = "logger"
}
}
}

func SetConfigDefaults(config *ReceptorConfig) {
SetTCPListenerDefaults(config)
SetUDPListenerDefaults(config)
SetWSListenerDefaults(config)
SetTCPPeerDefaults(config)
SetUDPPeerDefaults(config)
SetWSPeerDefaults(config)
SetCmdlineUnixDefaults(config)
SetLogLevelDefaults(config)
SetNodeDefaults(config)
SetKubeWorkerDefaults(config)
}
Loading
Loading