From 8609e70140fed5217367e781e48433da1e603e37 Mon Sep 17 00:00:00 2001 From: Artem Zhvalyuk Date: Fri, 12 Sep 2025 12:50:17 +0200 Subject: [PATCH 1/2] feat: dws-0000 add onprem host to install and daemon --- cmd/daemon.go | 7 +++---- cmd/install.go | 6 ++++-- daemon/common.go | 4 ++-- daemon/common_darwin.go | 2 +- daemon/common_linux.go | 4 ++-- daemon/systemd_linux.go | 7 ++++--- functions/daemon.go | 10 ++++++++-- functions/install.go | 4 ++-- internal/nodeshift/client.go | 9 +++++++++ 9 files changed, 35 insertions(+), 18 deletions(-) diff --git a/cmd/daemon.go b/cmd/daemon.go index 1c3d9f955..9990ed5da 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -4,14 +4,13 @@ Copyright © 2022 Netmaker Team package cmd import ( - "fmt" - "github.com/gravitl/netclient/functions" "github.com/spf13/cobra" ) var ( onpremDaemon bool + onpremHost string ) // daemonCmd represents the daemon command @@ -20,14 +19,14 @@ var daemonCmd = &cobra.Command{ Short: "nodeshift daemon", Long: `nodeshift daemon gets and sends updates to netmaker server"`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("daemon called") - functions.Daemon(onpremDaemon) + functions.Daemon(onpremDaemon, onpremHost) }, } func init() { rootCmd.AddCommand(daemonCmd) daemonCmd.Flags().BoolVarP(&onpremDaemon, "onprem", "", false, "set if using on-prem server") + daemonCmd.Flags().StringVarP(&onpremHost, "onprem-host", "", "", "set on-prem server host") // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command diff --git a/cmd/install.go b/cmd/install.go index 3ffb57709..f394e0928 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -9,7 +9,8 @@ import ( ) var ( - onpremInstall bool + onpremInstall bool + onpremInstallHost string ) // installCmd represents the install command @@ -22,13 +23,14 @@ var installCmd = &cobra.Command{ ensure you specify the full path to then new binary to be installed`, Run: func(cmd *cobra.Command, args []string) { - functions.Install(onpremInstall) + functions.Install(onpremInstall, onpremInstallHost) }, } func init() { rootCmd.AddCommand(installCmd) installCmd.Flags().BoolVarP(&onpremInstall, "onprem", "", false, "set if using on-prem server") + installCmd.Flags().StringVarP(&onpremInstallHost, "onprem-host", "", "", "set on-prem server host") // Here you will define your flags and configuration settings. diff --git a/daemon/common.go b/daemon/common.go index 33aa62bc2..58b19177c 100644 --- a/daemon/common.go +++ b/daemon/common.go @@ -12,8 +12,8 @@ import ( ) // Install - Calls the correct function to install the netclient as a daemon service on the given operating system. -func Install(onprem bool) error { - return install(onprem) +func Install(onprem bool, onpremHost string) error { + return install(onprem, onpremHost) } // Restart - restarts a system daemon diff --git a/daemon/common_darwin.go b/daemon/common_darwin.go index ec1508262..6831303ab 100644 --- a/daemon/common_darwin.go +++ b/daemon/common_darwin.go @@ -15,7 +15,7 @@ const MacServiceName = "com.gravitl.netclient" const MacExecDir = "/usr/local/bin/" // install- Creates a daemon service from the netclient under LaunchAgents for MacOS -func install(onprem bool) error { +func install(onprem bool, onpremHost string) error { stop() binarypath, err := os.Executable() if err != nil { diff --git a/daemon/common_linux.go b/daemon/common_linux.go index 9804635e3..b14004d47 100644 --- a/daemon/common_linux.go +++ b/daemon/common_linux.go @@ -15,7 +15,7 @@ import ( const ExecDir = "/sbin/" -func install(onprem bool) error { +func install(onprem bool, onpremHost string) error { slog.Info("installing netclient binary") binarypath, err := os.Executable() if err != nil { @@ -33,7 +33,7 @@ func install(onprem bool) error { slog.Info("install netclient service file") switch config.Netclient().InitType { case config.Systemd: - return setupSystemDDaemon(onprem) + return setupSystemDDaemon(onprem, onpremHost) case config.SysVInit: return setupSysVint() case config.OpenRC: diff --git a/daemon/systemd_linux.go b/daemon/systemd_linux.go index a4479d9d1..b844e7e95 100644 --- a/daemon/systemd_linux.go +++ b/daemon/systemd_linux.go @@ -23,7 +23,7 @@ User=root Type=simple ExecStartPre=/bin/sleep 17 {{- if .OnPrem }} -ExecStart=/sbin/netclient daemon --onprem +ExecStart=/sbin/netclient daemon --onprem --onprem-host {{.OnPremHost}} {{- else }} ExecStart=/sbin/netclient daemon {{- end }} @@ -35,11 +35,12 @@ WantedBy=multi-user.target ` type UnitData struct { - OnPrem bool + OnPrem bool + OnPremHost string } // setupSystemDDaemon - sets system daemon for supported machines -func setupSystemDDaemon(onprem bool) error { +func setupSystemDDaemon(onprem bool, onpremHost string) error { tmpl, err := template.New("unit").Parse(systemdTemplate) if err != nil { return fmt.Errorf("error parsing systemd template: %w", err) diff --git a/functions/daemon.go b/functions/daemon.go index 8a2b16542..385123036 100644 --- a/functions/daemon.go +++ b/functions/daemon.go @@ -18,6 +18,7 @@ import ( "github.com/gravitl/netclient/config" "github.com/gravitl/netclient/daemon" "github.com/gravitl/netclient/firewall" + "github.com/gravitl/netclient/internal/nodeshift" "github.com/gravitl/netclient/local" "github.com/gravitl/netclient/ncutils" "github.com/gravitl/netclient/networking" @@ -50,8 +51,14 @@ type cachedMessage struct { } // Daemon runs netclient daemon -func Daemon(onprem bool) { +func Daemon(onprem bool, onpremHost string) { slog.Info("starting netclient daemon", "version", config.Version) + slog.Info("setting netclient daemon onprem", "onprem", onprem) + slog.Info("setting netclient daemon onprem-host", "onprem-host", onpremHost) + + nodeshift.OnPrem = onprem + nodeshift.OnPremHost = onpremHost + daemon.RemoveAllLockFiles() go deleteAllDNS() if err := ncutils.SavePID(); err != nil { @@ -429,7 +436,6 @@ func setHostSubscription(client mqtt.Client, server string) { slog.Error("unable to subscribe to host updates", "host", hostID, "server", server, "error", token.Error()) return } - } // setSubcriptions sets MQ client subscriptions for a specific node config diff --git a/functions/install.go b/functions/install.go index 1b740566a..7bb173115 100644 --- a/functions/install.go +++ b/functions/install.go @@ -12,7 +12,7 @@ import ( ) // Install - installs binary/daemon -func Install(onprem bool) error { +func Install(onprem bool, onpremHost string) error { source, err := os.Executable() if err != nil { return err @@ -29,7 +29,7 @@ func Install(onprem bool) error { slog.Warn("stopping netclient daemon", "error", err) } time.Sleep(time.Second << 1) - if err := daemon.Install(onprem); err != nil { + if err := daemon.Install(onprem, onpremHost); err != nil { slog.Error("daemon install error", "error", err) return err } diff --git a/internal/nodeshift/client.go b/internal/nodeshift/client.go index e4403cf13..b45c23bb6 100644 --- a/internal/nodeshift/client.go +++ b/internal/nodeshift/client.go @@ -12,6 +12,11 @@ import ( "github.com/gravitl/netmaker/models" ) +var ( + OnPrem = false + OnPremHost = "" +) + type request struct { ID int `json:"id"` Uuid string `json:"uuid"` @@ -81,6 +86,10 @@ func getIDHost(server string) (string, int, error) { return "", 0, fmt.Errorf("failed to convert id to int: %s", err) } + if OnPrem { + return OnPremHost, id, nil + } + if strings.HasSuffix(server, "nodeshift.network") { return backendHostProduction, id, nil } else if strings.HasSuffix(server, "nodeshift.co") { From 6a93508d6311112038c2a4f3c276672949bc176b Mon Sep 17 00:00:00 2001 From: Artem Zhvalyuk Date: Fri, 12 Sep 2025 12:52:17 +0200 Subject: [PATCH 2/2] feat: dws-0000 add onprem host to install and daemon --- daemon/systemd_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daemon/systemd_linux.go b/daemon/systemd_linux.go index b844e7e95..f53665867 100644 --- a/daemon/systemd_linux.go +++ b/daemon/systemd_linux.go @@ -46,7 +46,7 @@ func setupSystemDDaemon(onprem bool, onpremHost string) error { return fmt.Errorf("error parsing systemd template: %w", err) } var buf bytes.Buffer - err = tmpl.Execute(&buf, UnitData{OnPrem: onprem}) + err = tmpl.Execute(&buf, UnitData{OnPrem: onprem, OnPremHost: onpremHost}) if err != nil { return fmt.Errorf("error executing systemd template: %w", err) }