mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-09 13:26:52 +08:00
- Avoid referencing conditions we know are false/true - Avoid using name of imported package as variable - Avoid broken (see list item 1) if else statement in `ipservice.go` by refactoring to switch statement - When assigning a pointer value to a variable along with an error, check that error before referencing that pointer. Thus avoiding de-referencing a nil and causing a panic. *** This item is the most important *** - Standard gofmt package sorting + linting; This includes fixing comment starts for go doc - Explicit non-handling of unhandled errors where appropriate (assigning errs to _ to reduce linter screaming) - Export ErrExpired in `netcache` package so that we can properly reference it using `errors.Is` instead of using `strings.Contains` against an `error.Error()` value
94 lines
1.8 KiB
Go
94 lines
1.8 KiB
Go
package daemon
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
)
|
|
|
|
// InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system.
|
|
func InstallDaemon() error {
|
|
|
|
runtimeOS := runtime.GOOS
|
|
var err error
|
|
|
|
switch runtimeOS {
|
|
case "windows":
|
|
err = SetupWindowsDaemon()
|
|
case "darwin":
|
|
err = SetupMacDaemon()
|
|
case "linux":
|
|
err = SetupSystemDDaemon()
|
|
case "freebsd":
|
|
err = SetupFreebsdDaemon()
|
|
default:
|
|
err = errors.New("this os is not yet supported for daemon mode. Run join cmd with flag '--daemon off'")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Restart - restarts a system daemon
|
|
func Restart() error {
|
|
if ncutils.IsWindows() {
|
|
RestartWindowsDaemon()
|
|
return nil
|
|
}
|
|
pid, err := ncutils.ReadPID()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to find pid %w", err)
|
|
}
|
|
p, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to find running process for pid %d -- %w", pid, err)
|
|
}
|
|
if err := p.Signal(syscall.SIGHUP); err != nil {
|
|
return fmt.Errorf("SIGHUP failed -- %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Start - starts system daemon
|
|
func Start() error {
|
|
runtimeOS := runtime.GOOS
|
|
var err error
|
|
switch runtimeOS {
|
|
case "windows":
|
|
RestartWindowsDaemon()
|
|
case "darwin":
|
|
RestartLaunchD()
|
|
case "linux":
|
|
RestartSystemD()
|
|
case "freebsd":
|
|
FreebsdDaemon("restart")
|
|
default:
|
|
err = errors.New("this os is not yet supported for daemon mode. Run join cmd with flag '--daemon off'")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Stop - stops a system daemon
|
|
func Stop() error {
|
|
runtimeOS := runtime.GOOS
|
|
var err error
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
switch runtimeOS {
|
|
case "windows":
|
|
RunWinSWCMD("stop")
|
|
case "darwin":
|
|
StopLaunchD()
|
|
case "linux":
|
|
StopSystemD()
|
|
case "freebsd":
|
|
FreebsdDaemon("stop")
|
|
default:
|
|
err = errors.New("no OS daemon to stop")
|
|
}
|
|
return err
|
|
}
|