Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ Copyright © 2022 Netmaker Team <info@netmaker.io>
package cmd

import (
"fmt"

"github.com/gravitl/netclient/functions"
"github.com/spf13/cobra"
)

var (
onpremDaemon bool
onpremHost string
)

// daemonCmd represents the daemon command
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

var (
onpremInstall bool
onpremInstall bool
onpremInstallHost string
)

// installCmd represents the install command
Expand All @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions daemon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion daemon/common_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions daemon/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions daemon/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -35,17 +35,18 @@ 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)
}
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)
}
Expand Down
10 changes: 8 additions & 2 deletions functions/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions functions/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions internal/nodeshift/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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") {
Expand Down
Loading