From 0410e1d4fbc4207aecd8a7691ab2539c56855678 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Thu, 23 Jun 2022 14:06:26 -0400 Subject: [PATCH] install command --- netclient/cli_options/cmds.go | 9 +++++++++ netclient/command/commands.go | 5 +++++ netclient/daemon/common.go | 4 +--- netclient/daemon/freebsd.go | 14 +++++++------- netclient/daemon/macos.go | 13 +++++++------ netclient/daemon/systemd.go | 14 +++++++------- netclient/daemon/windows.go | 23 ++++++++++++----------- netclient/functions/install.go | 16 ++++++++++++++++ netclient/functions/join.go | 4 ++-- 9 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 netclient/functions/install.go diff --git a/netclient/cli_options/cmds.go b/netclient/cli_options/cmds.go index 50979612..e1dfa4a8 100644 --- a/netclient/cli_options/cmds.go +++ b/netclient/cli_options/cmds.go @@ -95,6 +95,15 @@ func GetCommands(cliFlags []cli.Flag) []*cli.Command { return err }, }, + { + Name: "install", + Usage: "install binary and daemon", + Flags: cliFlags, + Action: func(c *cli.Context) error { + parseVerbosity(c) + return command.Install() + }, + }, } } diff --git a/netclient/command/commands.go b/netclient/command/commands.go index 6f036ca3..66e9b3ad 100644 --- a/netclient/command/commands.go +++ b/netclient/command/commands.go @@ -137,3 +137,8 @@ func Daemon() error { err := functions.Daemon() return err } + +// Install - installs binary and daemon +func Install() error { + return functions.Install() +} diff --git a/netclient/daemon/common.go b/netclient/daemon/common.go index 9572f1c5..da24e268 100644 --- a/netclient/daemon/common.go +++ b/netclient/daemon/common.go @@ -4,12 +4,10 @@ import ( "errors" "runtime" "time" - - "github.com/gravitl/netmaker/netclient/config" ) // InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system. -func InstallDaemon(cfg *config.ClientConfig) error { +func InstallDaemon() error { os := runtime.GOOS var err error diff --git a/netclient/daemon/freebsd.go b/netclient/daemon/freebsd.go index 24694026..f7f00fb2 100644 --- a/netclient/daemon/freebsd.go +++ b/netclient/daemon/freebsd.go @@ -23,13 +23,13 @@ func SetupFreebsdDaemon() error { return err } //install binary - //should check if the existing binary is the corect version -- for now only copy if file doesn't exist - if !ncutils.FileExists(EXEC_DIR + "netclient") { - err = ncutils.Copy(binarypath, EXEC_DIR+"netclient") - if err != nil { - log.Println(err) - return err - } + if ncutils.FileExists(EXEC_DIR + "netclient") { + logger.Log(0, "updating netclient binary in ", EXEC_DIR) + } + err = ncutils.Copy(binarypath, EXEC_DIR+"netclient") + if err != nil { + log.Println(err) + return err } rcFile := `#!/bin/sh diff --git a/netclient/daemon/macos.go b/netclient/daemon/macos.go index 21322bf9..bf991ca2 100644 --- a/netclient/daemon/macos.go +++ b/netclient/daemon/macos.go @@ -20,12 +20,13 @@ func SetupMacDaemon() error { return err } - if !ncutils.FileExists(MAC_EXEC_DIR + "netclient") { - err = ncutils.Copy(binarypath, MAC_EXEC_DIR+"netclient") - if err != nil { - log.Println(err) - return err - } + if ncutils.FileExists(MAC_EXEC_DIR + "netclient") { + logger.Log(0, "updating netclient binary in", MAC_EXEC_DIR) + } + err = ncutils.Copy(binarypath, MAC_EXEC_DIR+"netclient") + if err != nil { + log.Println(err) + return err } _, errN := os.Stat("~/Library/LaunchAgents") diff --git a/netclient/daemon/systemd.go b/netclient/daemon/systemd.go index cc678d78..b073d4fe 100644 --- a/netclient/daemon/systemd.go +++ b/netclient/daemon/systemd.go @@ -33,13 +33,13 @@ func SetupSystemDDaemon() error { return err } //install binary - //should check if the existing binary is the corect version -- for now only copy if file doesn't exist - if !ncutils.FileExists(EXEC_DIR + "netclient") { - err = ncutils.Copy(binarypath, EXEC_DIR+"netclient") - if err != nil { - log.Println(err) - return err - } + if ncutils.FileExists(EXEC_DIR + "netclient") { + logger.Log(0, "updating netclient binary in", EXEC_DIR) + } + err = ncutils.Copy(binarypath, EXEC_DIR+"netclient") + if err != nil { + log.Println(err) + return err } systemservice := `[Unit] diff --git a/netclient/daemon/windows.go b/netclient/daemon/windows.go index a6c306db..97fba1ef 100644 --- a/netclient/daemon/windows.go +++ b/netclient/daemon/windows.go @@ -14,20 +14,21 @@ import ( // SetupWindowsDaemon - sets up the Windows daemon service func SetupWindowsDaemon() error { - if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") { - if err := writeServiceConfig(); err != nil { - return err - } + if ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") { + logger.Log(0, "updating netclient service") + } + if err := writeServiceConfig(); err != nil { + return err } - if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") { - logger.Log(0, "performing first time daemon setup") - err := ncutils.GetEmbedded() - if err != nil { - return err - } - logger.Log(0, "finished daemon setup") + if ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") { + logger.Log(0, "updating netclient binary") } + err := ncutils.GetEmbedded() + if err != nil { + return err + } + logger.Log(0, "finished daemon setup") //get exact formatted commands RunWinSWCMD("install") time.Sleep(time.Millisecond) diff --git a/netclient/functions/install.go b/netclient/functions/install.go new file mode 100644 index 00000000..bd8dec7f --- /dev/null +++ b/netclient/functions/install.go @@ -0,0 +1,16 @@ +package functions + +import ( + "github.com/gravitl/netmaker/logger" + "github.com/gravitl/netmaker/netclient/daemon" +) + +//Install - installs binary/daemon +func Install() error { + daemon.Stop() + if err := daemon.InstallDaemon(); err != nil { + logger.Log(0, "error installing daemon", err.Error()) + return err + } + return daemon.Restart() +} diff --git a/netclient/functions/join.go b/netclient/functions/join.go index 6dcfa15b..cb56ec3f 100644 --- a/netclient/functions/join.go +++ b/netclient/functions/join.go @@ -166,7 +166,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error { logger.Log(0, "Node is marked as PENDING.") logger.Log(0, "Awaiting approval from Admin before configuring WireGuard.") if cfg.Daemon != "off" { - return daemon.InstallDaemon(cfg) + return daemon.InstallDaemon() } } logger.Log(1, "node created on remote server...updating configs") @@ -200,7 +200,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error { } if cfg.Daemon == "install" || ncutils.IsFreeBSD() { - err = daemon.InstallDaemon(cfg) + err = daemon.InstallDaemon() if err != nil { return err }