From fe196275f3bcbfd9d30575207b2fdbe68d60a6d8 Mon Sep 17 00:00:00 2001 From: Cory Close <3588173+cclose@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:23:43 -0500 Subject: [PATCH] [feat] Makes Relaoding work! - Makes DNSMasq restart work - Few other bug fixes - Some test cases for logging config --- .github/workflows/test.yml | 8 ++++ cmd/dnsMasqAPI/cmd/server.go | 61 ++++++++++++++++++++++++++- scripts/install.sh | 31 +++++++++----- scripts/package.sh | 3 ++ scripts/uninstall.sh | 82 ++++++++++++++++++++++++++++++++++++ service/dns_masq.go | 2 +- 6 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 scripts/uninstall.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca76ee7..4d96cdc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,5 +1,7 @@ name: Test +on: [push, pull_request] + jobs: test: runs-on: ubuntu-24.04 @@ -13,6 +15,12 @@ jobs: with: go-version: '1.22' + - name: Install golangci-lint + run: | + GCIL_VER=$(curl -L --silent https://api.github.com/repos/golangci/golangci-lint/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin "$GCIL_VER" + golangci-lint --version + - name: Install dependencies run: go mod tidy && go mod download && go get -t diff --git a/cmd/dnsMasqAPI/cmd/server.go b/cmd/dnsMasqAPI/cmd/server.go index 3faf4cb..0ee4cc3 100644 --- a/cmd/dnsMasqAPI/cmd/server.go +++ b/cmd/dnsMasqAPI/cmd/server.go @@ -11,6 +11,8 @@ import ( "github.com/labstack/echo/v4/middleware" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "io" + "os" "strings" ) @@ -64,11 +66,66 @@ func startUpMessage(aConfig model.AppConfig, lAddr string) string { return msg } +// configureLogging Configures the logging provider based on the logging config +func configureLogging(lConfig model.LoggingConfig) (*logrus.Logger, error) { + logger := logrus.New() + // If we set a file path + if lConfig.FilePath != "" { + // try to open the file and set as output + logFile, err := os.OpenFile(lConfig.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, err + } + logger.SetOutput(logFile) + if lConfig.Output != "" && strings.ToLower(lConfig.Output) != "file" { + // We defer this warning so we can finish setting up + defer func() { + logger.Warnf("ignoring contradictory config log.output = %s because log.file_path was set", + lConfig.Output) + }() + } + + // file_path cancels out output + } else if lConfig.Output != "" { + // We're stupidly permissive here... lol + switch strings.ToLower(lConfig.Output) { + case "stdout", "standardout", "out": + logger.SetOutput(os.Stdout) + case "stderr", "standarderror", "standarderr", "stderrout", "err": + logger.SetOutput(os.Stderr) + case "syslog": + //logger.SetOutput() + return nil, fmt.Errorf("configuration log.output = syslog is not yet implemented. Use stdout") + case "file": + return nil, fmt.Errorf("invalid configuration log.output = file: set log.file_path instead") + default: + return nil, fmt.Errorf("unknown configuration log.output option '%s'", lConfig.Output) + } + } + + if lConfig.Level != "" { + loggingLevel, err := logrus.ParseLevel(lConfig.Level) + if err != nil { + return nil, err + } + logger.SetLevel(loggingLevel) + } + + return logger, nil +} + // startServer configures and boots the webservice func startServer(ctx context.Context, appConfig model.AppConfig) error { config := appConfig.Config - // TODO configure logging from config - logger := logrus.New() + logger, err := configureLogging(config.Logging) + // Make sure we close our log file when the server shuts down + // check this BEFORE err incase we opened a file but still returned an err + if logCloser, ok := logger.Out.(io.Closer); ok { + defer logCloser.Close() // we're just gunna eat this error since we're shtting down + } + if err != nil { + return err + } // Initialize Echo e := echo.New() diff --git a/scripts/install.sh b/scripts/install.sh index 25fb628..3026cb2 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -11,8 +11,8 @@ DMA_CONFIG="" DMA_GROUP=${DMA_GROUP:-dnsmasqapi} # DMA_USER The linux user that runs the API service. Default dnsmasqapi DMA_USER=${DMA_USER:-dnsmasqapi} -# DM_USER The linux user that runs dnsMasq. This is usually root -DM_USER=${DM_USER:-root} +# DM_USER The linux user that runs dnsMasq. This is usually dnsmasq +DM_USER=${DM_USER:-dnsmasq} # ARCH The architecture of this system ARCH=${ARCH-amd64} # PLATFORM The platform of this system @@ -75,9 +75,10 @@ if [ -z "${SKIP_USER_SETUP}" ]; then if [ -z "${SKIP_SUDOERS}" ] ; then echo " - installing sudoers file /etc/sudoers.d/dnsmasqapi" # Install sudoers file for DMA_USER to reload dnsMasq - tee /etc/sudoers.d/dnsmasqapi < /dev/null <