From 4ebe2fb5f48be8e7608a7acc41e0b956ee838703 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 13 Feb 2025 00:46:56 +0700 Subject: [PATCH] all: ensure ctrld started after mongodb on Ubios Because ctrld needs to query custom client mapping from it. While at it, also make the error message clearer when initializing ubios discover failed, by attaching the command output to returned error. --- cmd/cli/prog_linux.go | 4 ++++ internal/clientinfo/ubios.go | 5 +++-- internal/router/router.go | 14 ++++++++++++++ internal/router/service_ubios.go | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/cli/prog_linux.go b/cmd/cli/prog_linux.go index b987ed3..cc0046b 100644 --- a/cmd/cli/prog_linux.go +++ b/cmd/cli/prog_linux.go @@ -13,6 +13,7 @@ import ( "tailscale.com/health" "github.com/Control-D-Inc/ctrld/internal/dns" + "github.com/Control-D-Inc/ctrld/internal/router" ) func init() { @@ -39,6 +40,9 @@ func setDependencies(svc *service.Config) { svc.Dependencies = append(svc.Dependencies, "Wants=systemd-networkd-wait-online.service") } } + if routerDeps := router.ServiceDependencies(); len(routerDeps) > 0 { + svc.Dependencies = append(svc.Dependencies, routerDeps...) + } } func setWorkingDirectory(svc *service.Config, dir string) { diff --git a/internal/clientinfo/ubios.go b/internal/clientinfo/ubios.go index 1a60de0..0ffd6e5 100644 --- a/internal/clientinfo/ubios.go +++ b/internal/clientinfo/ubios.go @@ -3,6 +3,7 @@ package clientinfo import ( "bytes" "encoding/json" + "fmt" "io" "os/exec" "strings" @@ -44,9 +45,9 @@ func (u *ubiosDiscover) refreshDevices() error { cmd := exec.Command("/usr/bin/mongo", "localhost:27117/ace", "--quiet", "--eval", ` DBQuery.shellBatchSize = 256; db.user.find({name: {$exists: true, $ne: ""}}, {_id:0, mac:1, name:1});`) - b, err := cmd.Output() + b, err := cmd.CombinedOutput() if err != nil { - return err + return fmt.Errorf("out: %s, err: %w", string(b), err) } return u.storeDevices(bytes.NewReader(b)) } diff --git a/internal/router/router.go b/internal/router/router.go index 4b335a6..2d8c462 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -215,6 +215,20 @@ func LeaseFilesDir() string { return "" } +// ServiceDependencies returns list of dependencies that ctrld services needs on this router. +// See https://pkg.go.dev/github.com/kardianos/service#Config for list format. +func ServiceDependencies() []string { + if Name() == ubios.Name { + // On Ubios, ctrld needs to start after unifi-mongodb, + // so it can query custom client info mapping. + return []string{ + "Wants=unifi-mongodb.service", + "After=unifi-mongodb.service", + } + } + return nil +} + func distroName() string { switch { case bytes.HasPrefix(unameO(), []byte("DD-WRT")): diff --git a/internal/router/service_ubios.go b/internal/router/service_ubios.go index 0b49cd2..8077c07 100644 --- a/internal/router/service_ubios.go +++ b/internal/router/service_ubios.go @@ -219,6 +219,8 @@ const ubiosBootSystemdService = `[Unit] Description=Run ctrld On Startup UDM Wants=network-online.target After=network-online.target +Wants=unifi-mongodb +After=unifi-mongodb StartLimitIntervalSec=500 StartLimitBurst=5