Make sure the notifier can work inside Docker
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Melora Hugues 2023-03-11 20:42:21 +01:00
parent e2301892bf
commit 66aed0f0da
5 changed files with 58 additions and 6 deletions

View file

@ -21,7 +21,7 @@ steps:
image: thegeeklab/drone-docker-buildx image: thegeeklab/drone-docker-buildx
privileged: true privileged: true
settings: settings:
repo: git.faercol.me/faercol/telegram-notifier repo: git.faercol.me/notification/telegram-notifier
tags: latest tags: latest
dry_run: true dry_run: true
platforms: platforms:
@ -37,7 +37,7 @@ steps:
image: thegeeklab/drone-docker-buildx image: thegeeklab/drone-docker-buildx
privileged: true privileged: true
settings: settings:
repo: git.faercol.me/faercol/telegram-notifier repo: git.faercol.me/notification/telegram-notifier
registry: git.faercol.me registry: git.faercol.me
tags: latest tags: latest
username: username:
@ -73,7 +73,7 @@ steps:
privileged: true privileged: true
settings: settings:
registry: git.faercol.me registry: git.faercol.me
repo: git.faercol.me/faercol/teelgram-notifier repo: git.faercol.me/notification/telegram-notifier
auto_tag: true auto_tag: true
platforms: platforms:
- linux/amd64 - linux/amd64

View file

@ -10,7 +10,7 @@ FROM --platform=$TARGETPLATFORM alpine:latest
WORKDIR /root WORKDIR /root
COPY --from=builder go/src/git.faercol.me/telegram-notifier/build/notifier ./ COPY --from=builder go/src/git.faercol.me/telegram-notifier/build/notifier ./
VOLUME [ "/config" "/input" ] VOLUME [ "/config", "/input" ]
ENTRYPOINT [ "./notifier" ] ENTRYPOINT [ "./notifier" ]
CMD [ "-config", "/config/config.json" ] CMD [ "-config", "/config/config.json" ]

View file

@ -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

View file

@ -2,8 +2,11 @@ package config
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"strconv"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -46,20 +49,57 @@ func parseLevel(lvlStr string) logrus.Level {
return logrus.InfoLevel 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) { func New(filepath string) (*Config, error) {
content, err := os.ReadFile(filepath) content, err := os.ReadFile(filepath)
if err != nil { 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) return nil, fmt.Errorf("failed to read config file %q: %w", filepath, err)
} }
var jsonConf jsonConfig var jsonConf jsonConfig
if err := json.Unmarshal(content, &jsonConf); err != nil { if err := json.Unmarshal(content, &jsonConf); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err) return nil, fmt.Errorf("failed to parse config file: %w", err)
} }
return &Config{ conf := &Config{
Telegram: jsonConf.Telegram, Telegram: jsonConf.Telegram,
Log: LogConfig{ Log: LogConfig{
Level: parseLevel(jsonConf.Log.Level), Level: parseLevel(jsonConf.Log.Level),
}, },
Listener: jsonConf.Listener, Listener: jsonConf.Listener,
}, nil }
checkOverride(conf)
return conf, nil
} }

View file

@ -29,6 +29,9 @@ func New(sockPath string) (*Listener, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create unix socket: %w", err) 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() m := http.NewServeMux()