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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ launch.json
backup/*
dist/*
/.idea
/bin
/bin/*
8 changes: 6 additions & 2 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import (
"github.com/spf13/cobra"
)

var (
onpremDaemon bool
)

// daemonCmd represents the daemon command
var daemonCmd = &cobra.Command{
Use: "daemon",
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()
functions.Daemon(onpremDaemon)
},
}

func init() {
rootCmd.AddCommand(daemonCmd)

daemonCmd.Flags().BoolVarP(&onpremDaemon, "onprem", "", false, "set if using on-prem server")
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down
7 changes: 6 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/spf13/cobra"
)

var (
onpremInstall bool
)

// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Expand All @@ -18,12 +22,13 @@ 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()
functions.Install(onpremInstall)
},
}

func init() {
rootCmd.AddCommand(installCmd)
installCmd.Flags().BoolVarP(&onpremInstall, "onprem", "", false, "set if using on-prem server")

// Here you will define your flags and configuration settings.

Expand Down
19 changes: 17 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package cmd

import (
"crypto/rand"
"crypto/tls"
"errors"
"net/http"
"os"
"path/filepath"
"runtime"

"github.com/devilcove/httpclient"
"github.com/google/uuid"
"github.com/gravitl/netclient/config"
"github.com/gravitl/netclient/daemon"
Expand Down Expand Up @@ -55,6 +58,10 @@ func init() {
rootCmd.PersistentFlags().IntP("verbosity", "v", 0, "set logging verbosity 0-4")
viper.BindPFlags(rootCmd.Flags())

// nodehisft on-prem
httpclient.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Cobra also supports local flags, which will only run
// when this action is called directly.
}
Expand Down Expand Up @@ -96,7 +103,8 @@ func InitConfig(viper *viper.Viper) {
config.ReadNodeConfig()
config.ReadServerConf()
config.SetServerCtx()
checkConfig()

checkConfig(true)
//check netclient dirs exist
if _, err := os.Stat(config.GetNetclientPath()); err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -147,7 +155,7 @@ func setupLogging(flags *viper.Viper) {
}

// checkConfig - verifies and updates configuration settings
func checkConfig() {
func checkConfig(onprem bool) {
fail := false
saveRequired := false
netclient := config.Netclient()
Expand Down Expand Up @@ -227,9 +235,16 @@ func checkConfig() {
saveRequired = true
}
}
if onprem {
netclient.MTU = config.DefaultMTUOnPrem
}

if netclient.MTU == 0 {
logger.Log(0, "setting MTU")
netclient.MTU = config.DefaultMTU
if onprem {
netclient.MTU = config.DefaultMTUOnPrem
}
}

if len(netclient.TrafficKeyPrivate) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"gopkg.in/yaml.v3"
)

const (
var (
// LinuxAppDataPath - linux path
LinuxAppDataPath = "/etc/netclient/"
// MacAppDataPath - mac path
Expand All @@ -37,7 +37,8 @@ const (
// DefaultListenPort default port for wireguard
DefaultListenPort = 51821
// DefaultMTU default MTU for wireguard
DefaultMTU = 1420
DefaultMTU = 1420
DefaultMTUOnPrem = 1320
)

const (
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() error {
return install()
func Install(onprem bool) error {
return install(onprem)
}

// 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() error {
func install(onprem bool) 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() error {
func install(onprem bool) error {
slog.Info("installing netclient binary")
binarypath, err := os.Executable()
if err != nil {
Expand All @@ -33,7 +33,7 @@ func install() error {
slog.Info("install netclient service file")
switch config.Netclient().InitType {
case config.Systemd:
return setupSystemDDaemon()
return setupSystemDDaemon(onprem)
case config.SysVInit:
return setupSysVint()
case config.OpenRC:
Expand Down
31 changes: 26 additions & 5 deletions daemon/systemd_linux.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package daemon

import (
"bytes"
"errors"
"fmt"
"html/template"
"os"

"github.com/gravitl/netclient/ncutils"
"github.com/gravitl/netmaker/logger"
"golang.org/x/exp/slog"
)

// setupSystemDDaemon - sets system daemon for supported machines
func setupSystemDDaemon() error {
systemservice := `[Unit]
Description=Netclient Daemon
var systemdTemplate = `[Unit]
Description=Nodeshift Netclient Daemon
Documentation=https://docs.netmaker.org https://k8s.netmaker.org
After=network-online.target
Wants=network-online.target
Expand All @@ -21,15 +22,35 @@ Wants=network-online.target
User=root
Type=simple
ExecStartPre=/bin/sleep 17
{{- if .OnPrem }}
ExecStart=/sbin/netclient daemon --onprem
{{- else }}
ExecStart=/sbin/netclient daemon
{{- end }}
Restart=on-failure
RestartSec=15s

[Install]
WantedBy=multi-user.target
`

servicebytes := []byte(systemservice)
type UnitData struct {
OnPrem bool
}

// setupSystemDDaemon - sets system daemon for supported machines
func setupSystemDDaemon(onprem bool) 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})
if err != nil {
return fmt.Errorf("error executing systemd template: %w", err)
}

servicebytes := []byte(buf.Bytes())

if !ncutils.FileExists("/etc/systemd/system/netclient.service") {
err := os.WriteFile("/etc/systemd/system/netclient.service", servicebytes, 0644)
Expand Down
Loading
Loading