Skip to content

Commit

Permalink
Better systemd support
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 20, 2023
1 parent 3874743 commit 144538e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions common/updown-badge-server.knf
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
[updown]

# updown.io read-only API key
api-key:
api-key:

[badge]

# Path to font for badge (we recommend using Verdana)
font:
font: /usr/share/fonts/Verdana.ttf

# Badge style (plastic, flat, flat-square)
style: flat
Expand Down
4 changes: 3 additions & 1 deletion common/updown-badge-server.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
ExecStart=/usr/bin/updown-badge-server --no-color
User=updownbs
ExecStart=/usr/bin/updown-badge-server
ExecReload=kill -HUP $MAINPID
StandardError=syslog
SyslogIdentifier=updown-badge-server

[Install]
Expand Down
8 changes: 5 additions & 3 deletions common/updown-badge-server.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

%define debug_package %{nil}

%define user_name updownbs

################################################################################

Summary: Service for generating badges for updown.io checks
Expand Down Expand Up @@ -76,16 +78,16 @@ install -pDm 644 %{name}/common/%{name}.service \
rm -rf %{buildroot}

%pre
getent group %{name} >/dev/null || groupadd -r %{name}
getent passwd %{name} >/dev/null || useradd -r -M -g %{name} -s /sbin/nologin %{name}
getent group %{user_name} >/dev/null || groupadd -r %{user_name}
getent passwd %{user_name} >/dev/null || useradd -r -M -g %{user_name} -s /sbin/nologin %{user_name}
exit 0

################################################################################

%files
%defattr(-,root,root,-)
%doc LICENSE
%attr(-,%{name},%{name}) %dir %{_localstatedir}/log/%{name}
%attr(-,%{user_name},%{user_name}) %dir %{_localstatedir}/log/%{name}
%config(noreplace) %{_sysconfdir}/%{name}.knf
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%{_unitdir}/%{name}.service
Expand Down
38 changes: 20 additions & 18 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ var redirectURL string
// ////////////////////////////////////////////////////////////////////////////////// //

func Init() {
preConfigureUI()

_, errs := options.Parse(optMap)

if len(errs) != 0 {
for _, err := range errs {
printError(err.Error())
log.Crit(err.Error())
}

os.Exit(1)
Expand Down Expand Up @@ -133,6 +135,16 @@ func Init() {
start()
}

// preConfigureUI preconfigures user interface
func preConfigureUI() {
switch {
case os.Getenv("INVOCATION_ID") != "",
os.Getenv("SYSTEMCTL_IGNORE_DEPENDENCIES") != "",
os.Getenv("NO_COLOR") != "":
fmtc.DisableColors = true
}
}

// configureUI configures user interface
func configureUI() {
if options.GetB(OPT_NO_COLOR) {
Expand All @@ -145,7 +157,8 @@ func loadConfig() {
err := knf.Global(options.GetS(OPT_CONFIG))

if err != nil {
printErrorAndExit(err.Error())
log.Crit(err.Error())
os.Exit(1)
}
}

Expand Down Expand Up @@ -188,10 +201,8 @@ func validateConfig() {
})

if len(errs) != 0 {
printError("Error while configuration file validation:")

for _, err := range errs {
printError(" %v", err)
log.Crit(err.Error())
}

os.Exit(1)
Expand Down Expand Up @@ -221,13 +232,15 @@ func setupLogger() {
err := log.Set(knf.GetS(LOG_FILE), knf.GetM(LOG_PERMS, 0644))

if err != nil {
printErrorAndExit(err.Error())
log.Crit(err.Error())
os.Exit(1)
}

err = log.MinLevel(knf.GetS(LOG_LEVEL))

if err != nil {
printErrorAndExit(err.Error())
log.Crit(err.Error())
os.Exit(1)
}
}

Expand Down Expand Up @@ -277,17 +290,6 @@ func hupSignalHandler() {
log.Info("Log reopened by HUP signal")
}

// printError prints error message to console
func printError(f string, a ...interface{}) {
fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...)
}

// printErrorAndExit print error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
os.Exit(1)
}

// shutdown stops daemon
func shutdown(code int) {
if server != nil {
Expand Down

0 comments on commit 144538e

Please sign in to comment.