2021-09-20 02:03:47 +08:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"runtime"
|
2022-02-02 13:02:36 +08:00
|
|
|
"time"
|
2021-09-20 02:03:47 +08:00
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
|
|
|
)
|
|
|
|
|
2022-01-16 08:57:07 +08:00
|
|
|
// InstallDaemon - Calls the correct function to install the netclient as a daemon service on the given operating system.
|
2022-03-21 12:43:17 +08:00
|
|
|
func InstallDaemon(cfg *config.ClientConfig) error {
|
2021-09-20 02:03:47 +08:00
|
|
|
os := runtime.GOOS
|
|
|
|
var err error
|
2021-10-03 00:28:17 +08:00
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
switch os {
|
|
|
|
case "windows":
|
|
|
|
err = SetupWindowsDaemon()
|
|
|
|
case "darwin":
|
2022-02-19 04:18:50 +08:00
|
|
|
err = SetupMacDaemon()
|
2021-09-20 02:03:47 +08:00
|
|
|
case "linux":
|
2022-02-19 04:18:50 +08:00
|
|
|
err = SetupSystemDDaemon()
|
2022-02-09 11:06:41 +08:00
|
|
|
case "freebsd":
|
|
|
|
err = SetupFreebsdDaemon()
|
2021-09-20 02:03:47 +08:00
|
|
|
default:
|
|
|
|
err = errors.New("this os is not yet supported for daemon mode. Run join cmd with flag '--daemon off'")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-02-02 13:02:36 +08:00
|
|
|
|
2022-02-08 23:43:46 +08:00
|
|
|
// Restart - restarts a system daemon
|
2022-02-02 13:02:36 +08:00
|
|
|
func Restart() error {
|
|
|
|
os := runtime.GOOS
|
|
|
|
var err error
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
switch os {
|
|
|
|
case "windows":
|
|
|
|
RestartWindowsDaemon()
|
|
|
|
case "darwin":
|
|
|
|
RestartLaunchD()
|
|
|
|
case "linux":
|
|
|
|
RestartSystemD()
|
2022-02-09 11:06:41 +08:00
|
|
|
case "freebsd":
|
|
|
|
FreebsdDaemon("restart")
|
2022-02-02 13:02:36 +08:00
|
|
|
default:
|
|
|
|
err = errors.New("this os is not yet supported for daemon mode. Run join cmd with flag '--daemon off'")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 23:43:46 +08:00
|
|
|
|
|
|
|
// Stop - stops a system daemon
|
|
|
|
func Stop() error {
|
|
|
|
os := runtime.GOOS
|
|
|
|
var err error
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
switch os {
|
|
|
|
case "windows":
|
2022-05-16 08:26:38 +08:00
|
|
|
RunWinSWCMD("stop")
|
2022-02-08 23:43:46 +08:00
|
|
|
case "darwin":
|
|
|
|
StopLaunchD()
|
|
|
|
case "linux":
|
|
|
|
StopSystemD()
|
2022-02-09 11:06:41 +08:00
|
|
|
case "freebsd":
|
|
|
|
FreebsdDaemon("stop")
|
2022-02-08 23:43:46 +08:00
|
|
|
default:
|
|
|
|
err = errors.New("no OS daemon to stop")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|