From 66aed0f0dae263b4255a46e49fed44f37cd1a990 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sat, 11 Mar 2023 20:42:21 +0100 Subject: [PATCH] Make sure the notifier can work inside Docker --- .drone.yml | 6 ++--- Dockerfile | 2 +- examples/docker-compose.yml | 9 +++++++ notifier/config/config.go | 44 +++++++++++++++++++++++++++++++++-- notifier/listener/listener.go | 3 +++ 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 examples/docker-compose.yml diff --git a/.drone.yml b/.drone.yml index 5e8a5b2..d8c3c2a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -21,7 +21,7 @@ steps: image: thegeeklab/drone-docker-buildx privileged: true settings: - repo: git.faercol.me/faercol/telegram-notifier + repo: git.faercol.me/notification/telegram-notifier tags: latest dry_run: true platforms: @@ -37,7 +37,7 @@ steps: image: thegeeklab/drone-docker-buildx privileged: true settings: - repo: git.faercol.me/faercol/telegram-notifier + repo: git.faercol.me/notification/telegram-notifier registry: git.faercol.me tags: latest username: @@ -73,7 +73,7 @@ steps: privileged: true settings: registry: git.faercol.me - repo: git.faercol.me/faercol/teelgram-notifier + repo: git.faercol.me/notification/telegram-notifier auto_tag: true platforms: - linux/amd64 diff --git a/Dockerfile b/Dockerfile index d1d1252..0858740 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ FROM --platform=$TARGETPLATFORM alpine:latest WORKDIR /root COPY --from=builder go/src/git.faercol.me/telegram-notifier/build/notifier ./ -VOLUME [ "/config" "/input" ] +VOLUME [ "/config", "/input" ] ENTRYPOINT [ "./notifier" ] CMD [ "-config", "/config/config.json" ] diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml new file mode 100644 index 0000000..9cec29f --- /dev/null +++ b/examples/docker-compose.yml @@ -0,0 +1,9 @@ +services: + telegram_exporter: + container_name: telegram_exporter + image: git.faercol.me/notification/telegram-notifier:latest + environment: + - SOCK_PATH=/input/input.sock + volumes: + - ./build/config:/config + - ./build/input:/input diff --git a/notifier/config/config.go b/notifier/config/config.go index 1ddbeb7..1fcc017 100644 --- a/notifier/config/config.go +++ b/notifier/config/config.go @@ -2,8 +2,11 @@ package config import ( "encoding/json" + "errors" "fmt" + "io/fs" "os" + "strconv" "github.com/sirupsen/logrus" ) @@ -46,20 +49,57 @@ func parseLevel(lvlStr string) logrus.Level { return logrus.InfoLevel } +var defaultConfig Config = Config{ + Telegram: TelegramConfig{ + ChannelID: 0, + Token: "", + }, + Log: LogConfig{ + Level: logrus.InfoLevel, + }, + Listener: ListenerConfig{ + SockPath: "input/notifier.sock", + }, +} + +func checkOverride(conf *Config) { + if val, ok := os.LookupEnv("LOG_LEVEL"); ok { + conf.Log.Level = parseLevel(val) + } + if val, ok := os.LookupEnv("SOCK_PATH"); ok { + conf.Listener.SockPath = val + } + if val, ok := os.LookupEnv("TELEGRAM_CHANNEL_ID"); ok { + if intVal, err := strconv.Atoi(val); err == nil { + conf.Telegram.ChannelID = int64(intVal) + } + } + if val, ok := os.LookupEnv("TELEGRAM_TOKEN"); ok { + conf.Telegram.Token = val + } +} + func New(filepath string) (*Config, error) { content, err := os.ReadFile(filepath) if err != nil { + if errors.Is(err, fs.ErrNotExist) { + conf := defaultConfig + checkOverride(&conf) + return &conf, nil + } return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err) } var jsonConf jsonConfig if err := json.Unmarshal(content, &jsonConf); err != nil { return nil, fmt.Errorf("failed to parse config file: %w", err) } - return &Config{ + conf := &Config{ Telegram: jsonConf.Telegram, Log: LogConfig{ Level: parseLevel(jsonConf.Log.Level), }, Listener: jsonConf.Listener, - }, nil + } + checkOverride(conf) + return conf, nil } diff --git a/notifier/listener/listener.go b/notifier/listener/listener.go index 42a359b..1f19e72 100644 --- a/notifier/listener/listener.go +++ b/notifier/listener/listener.go @@ -29,6 +29,9 @@ func New(sockPath string) (*Listener, error) { if err != nil { return nil, fmt.Errorf("failed to create unix socket: %w", err) } + if err := os.Chmod(sockPath, 0o777); err != nil { + return nil, fmt.Errorf("failed to set permissions to unix socket: %w", err) + } m := http.NewServeMux()