Skip to content

Commit

Permalink
[feat] Makes Relaoding work!
Browse files Browse the repository at this point in the history
- Makes DNSMasq restart work
- Few other bug fixes
- Some test cases for logging config
  • Loading branch information
cclose committed Aug 2, 2024
1 parent 888e11d commit fe19627
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-24.04
Expand All @@ -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

Expand Down
61 changes: 59 additions & 2 deletions cmd/dnsMasqAPI/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"os"
"strings"
)

Expand Down Expand Up @@ -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()
Expand Down
31 changes: 20 additions & 11 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <<EOF
# Allow DMA_USER to reload dnsmasq.service without a password
${DMA_USER} ALL=(ALL) NOPASSWD: /bin/systemctl reload dnsmasq.service
tee /etc/sudoers.d/dnsmasqapi > /dev/null <<EOF
# Allow DMA_USER to restart dnsmasq.service without a password
${DMA_USER} ALL=(ALL) NOPASSWD: /bin/systemctl restart dnsmasq.service
${DMA_USER} ALL=(ALL) NOPASSWD: /bin/systemctl status dnsmasq.service
EOF
fi
fi
Expand All @@ -102,20 +103,23 @@ curl -L --silent $ARCHIVE_URL -o "/var/cache/${ARCHIVE_NAME}" || { echo "Failed
echo "Extracting to /usr/local"
tar -xzf "/var/cache/${ARCHIVE_NAME}" -C /usr/local || { echo "Failed to extract ${ARCHIVE_NAME}"; exit 1; }

# Set the user and group int he Systemd unit
# Set the user and group in the Systemd unit
sed -i "s/nobody/${DMA_USER}/" /usr/local/lib/systemd/system/dnsMasqAPI.service
sed -i "s/nogroup/${DMA_GROUP}/" /usr/local/lib/systemd/system/dnsMasqAPI.service

# If they want a different config, remove the default and update the systemd unit
if [ -n "${DMA_CONFIG}" ] ; then
echo "Changing Systemd Unit Config to ${DMA_CONFIG}"
sed -i "s|/usr/local/etc/dnsMasqAPI/config.yaml|${DMA_CONFIG}|" /usr/local/lib/systemd/system/dnsMasqAPI.service
rm -f /usr/local/etc/dnsMasqAPI/config.yaml
# If using standard config but custom log path
else
if [ -n "${LOG_TO_JOURNAL}" ] ; then
echo "Setting Logging to Journald (STDOUT)"
sed -i "/log:/d" /usr/local/etc/dnsMasqAPI/config.yaml
sed -i "/dnsMasqAPI.log/d" /usr/local/etc/dnsMasqAPI/config.yaml
elif [ -n "${LOG_PATH}" ] ; then
echo "Setting log path to ${LOG_PATH}"
sed -i "s|/var/log/dnsMasqAPI.log|${LOG_PATH}|" /usr/local/etc/dnsMasqAPI/config.yaml
fi
fi
Expand All @@ -124,6 +128,7 @@ fi
sed -i "s/nobody/${DMA_USER}/" /usr/local/etc/dnsMasqAPI/logrotated.conf
sed -i "s/nogroup/${DMA_GROUP}/" /usr/local/etc/dnsMasqAPI/logrotated.conf
if [ -z "${SKIP_LOG_ROTATE}" ] && [ -z "${LOG_TO_JOURNAL}" ] ; then
echo "Deploying LogRotate"
# Adjust the path if using a custom path
if [ ! -z "${LOG_PATH}" ] ; then
sed -i "s|/var/log/dnsMasqAPI.log|${LOG_PATH}|" /usr/local/etc/dnsMasqAPI/logrotated.conf
Expand All @@ -139,17 +144,21 @@ if [ ! -f "${DMA_DM_CONFIG}" ] ; then
fi

# Create the /var/lib directory for default db file and pid file
echo "Creating /var/lib/dnsMasqAPI"
mkdir -p /var/lib/dnsMasqAPI
if [ "${DMA_USER}" != "root" ] ; then
chown -R "${DMA_USER}:${DMA_GROUP}" "${DMA_DM_CONFIG}"
chmod -R 550 /var/lib/dnsMasqAPI
if [ -z "${SKIP_USER_SETUP}" ] ; then
if [ "${DMA_USER}" != "root" ] ; then
echo "Chowning /var/lib/dnsMasqAPI to ${DMA_USER}:${DMA_GROUP}"
chown -R "${DMA_USER}:${DMA_GROUP}" /var/lib/dnsMasqAPI
chmod -R 770 /var/lib/dnsMasqAPI
fi
fi

if [ -z "${SKIP_USER_SETUP}" ] ; then
if [ "${DMA_USER}" != "root" ] ; then
echo " - setting permissions"
echo " - setting permissions on dnsMasq config"
chown "${DMA_USER}:${DMA_GROUP}" "${DMA_DM_CONFIG}"
chmod 550 "$DMA_DM_CONFIG"
chmod 770 "$DMA_DM_CONFIG"
fi
fi

Expand Down
3 changes: 3 additions & 0 deletions scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mkdir -p "$ARTIFACT_DIR"
echo "Bundling binary"
cp dnsMasqAPI dist/bundle/bin/dnsMasqAPI
chmod +x dist/bundle/bin/dnsMasqAPI
echo "Bundling uninstall.sh"
cp scripts/install.sh dist/bundle/bin/dnsMasqAPI-uninstall.sh
chmod +x dist/bundle/bin/dnsMasqAPI-uninstall.sh

# Move the systemd service into the dist
echo "Bundling Service Unit"
Expand Down
82 changes: 82 additions & 0 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

set -e

# Set default values for environment variables
DMA_DM_CONFIG="${DMA_DM_CONFIG-/etc/dnsmasq.d/api.conf}"
DMA_CONFIG="${DMA_CONFIG-/usr/local/etc/dnsMasqAPI/config.yaml}"
DMA_GROUP="${DMA_GROUP:-dnsmasqapi}"
DMA_USER="${DMA_USER:-dnsmasqapi}"
DM_USER="${DM_USER:-root}"
ARCH="${ARCH-amd64}"
PLATFORM="${PLATFORM-linux}"

# SKIP_USER_REMOVE - Don't remove the DM_USER and DM_GROUP form the linux system
# SKIP_DAEMON_RELOAD - Don't reload the system daemon
# PRESERVE_DM_CONFIG - Don't remove the dnsMasq config that was managed by the service
# PRESERVE_LOGS - Don't remove the log files
# PRESERVE_SELF - Don't self-delete this script

if [ -z "${PRESERVE_LOGS}" ]; then
echo "TODO: Removing Logs"
# TODO detect and remove logs
fi

# Remove logrotate configuration
if [ -L /etc/logrotate.d/dnsMasqAPI ]; then
echo "Removing logrotate configuration..."
rm /etc/logrotate.d/dnsMasqAPI
fi

# Remove systemd service and unit file
echo "Stopping and disabling systemd service..."
systemctl stop dnsMasqAPI.service || true
systemctl disable dnsMasqAPI.service || true
rm -f /usr/local/lib/systemd/system/dnsMasqAPI.service
if [ -z "${SKIP_DAEMON_RELOAD}" ]; then
systemctl daemon-reload
fi

# Remove installation files and directories
echo "Removing installed files and directories..."
rm -f /usr/local/bin/dnsMasqAPI
rm -rf /usr/local/etc/dnsMasqAPI
rm -rf /var/lib/dnsMasqAPI

# Remove config files if they exist
if [ -z "${PRESERVE_DM_CONFIG}" ]; then
if [ -f "${DMA_DM_CONFIG}" ]; then
echo "Removing dnsMasq config file ${DMA_DM_CONFIG}..."
rm -f "${DMA_DM_CONFIG}"
fi
fi

if [ -f "${DMA_CONFIG}" ]; then
if [ -f "${DMA_CONFIG}" ]; then
echo "Removing config file ${DMA_CONFIG}..."
rm -f "${DMA_CONFIG}"
fi
fi

# Optionally remove user and group
if [ -z "${SKIP_USER_REMOVE}" ]; then
if [ "${DMA_USER}" != "root" ]; then
echo "Removing user ${DMA_USER}..."
userdel "${DMA_USER}" || true
fi

if [ -n "${DMA_GROUP}" ]; then
echo "Removing group ${DMA_GROUP}..."
groupdel "${DMA_GROUP}" || true
fi
fi

# Schedule the script to delete itself after a short delay
if [ -z "${PRESERVE_SELF}" ] ; then
SELF=$(realpath "$0")
{
echo "sleep 1; rm -f '$SELF'"
} | at now
echo "Scheduled self-deletion of the script."
fi
echo "Uninstallation complete!"
2 changes: 1 addition & 1 deletion service/dns_masq.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (ds *DNSMasqService) BuildDatabase() error {
// ReloadDNSMasq Calls DNSMasq to reload it's config
func (ds *DNSMasqService) ReloadDNSMasq() error {
metrics.GetOrCreateCounter(MetricDNSReloads).Inc()
cmd := exec.Command("sudo", "systemctl", "reload", "dnsmasq")
cmd := exec.Command("sudo", "systemctl", "restart", "dnsmasq.service")
err := cmd.Run()
if err != nil {
return err
Expand Down

0 comments on commit fe19627

Please sign in to comment.