Skip to content

Commit

Permalink
healthcheck: check if drbd reactor reload units differ
Browse files Browse the repository at this point in the history
The bundled drbd-reactor-reload.{path,service} recently got updated, so
we need to check if the user actually has the most recent version.
  • Loading branch information
chrboe committed Apr 4, 2024
1 parent 668a1c6 commit 2c40ff0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var bold = color.New(color.Bold).SprintfFunc()
var faint = color.New(color.Faint).SprintfFunc()
var errNotFound = errors.New("not found")

type checker interface {
Expand Down
36 changes: 35 additions & 1 deletion pkg/healthcheck/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ import (
"fmt"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
)

type checkReactorAutoReload struct {
}

func fileContentsEqual(filenameA, filenameB string) (bool, error) {
a, err := os.ReadFile(filenameA)
if err != nil {
return false, fmt.Errorf("could not read %s: %w", filenameA, err)
}

b, err := os.ReadFile(filenameB)
if err != nil {
return false, fmt.Errorf("could not read %s: %w", filenameB, err)
}

return string(a) == string(b), nil
}

func (c *checkReactorAutoReload) check(prevError bool) error {
if prevError {
// reactor not installed, no need to check
Expand All @@ -23,6 +38,24 @@ func (c *checkReactorAutoReload) check(prevError bool) error {
if status.ActiveState != "active" {
return fmt.Errorf("service drbd-reactor-reload.path is not started")
}

dir := guessReactorReloadDir()
sourcePath := filepath.Join(dir, "drbd-reactor-reload.path")
sourceService := filepath.Join(dir, "drbd-reactor-reload.service")

destPath := "/etc/systemd/system/drbd-reactor-reload.path"
destService := "/etc/systemd/system/drbd-reactor-reload.service"

for _, path := range [][]string{{sourcePath, destPath}, {sourceService, destService}} {
equal, err := fileContentsEqual(path[0], path[1])
if err != nil {
return fmt.Errorf("could not compare %s and %s: %w", path[0], path[1], err)
}
if !equal {
return fmt.Errorf("%s differs from %s", path[0], path[1])
}
}

return nil
}

Expand All @@ -47,10 +80,11 @@ func guessReactorReloadDir() string {
return ""
}

func (c *checkReactorAutoReload) format(_ error) string {
func (c *checkReactorAutoReload) format(err error) string {
dir := guessReactorReloadDir()
var b strings.Builder
fmt.Fprintf(&b, " %s drbd-reactor is not configured to automatically reload\n", color.RedString("✗"))
fmt.Fprintf(&b, " %s\n", faint("→ %s", err.Error()))
if dir != "" {
path := filepath.Join(dir, "drbd-reactor-reload.{path,service}")
fmt.Fprintf(&b, " Please execute:\n")
Expand Down

0 comments on commit 2c40ff0

Please sign in to comment.