netmaker/netclient/daemon/systemd.go

135 lines
3.5 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package daemon
import (
//"github.com/davecgh/go-spew/spew"
"log"
"os"
"path/filepath"
2022-02-02 13:45:22 +08:00
"time"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
2022-01-26 07:23:08 +08:00
const EXEC_DIR = "/sbin/"
2022-01-26 07:15:27 +08:00
2021-10-09 03:07:12 +08:00
// SetupSystemDDaemon - sets system daemon for supported machines
2022-02-19 04:18:50 +08:00
func SetupSystemDDaemon() error {
2021-10-09 03:07:12 +08:00
2021-09-20 02:03:47 +08:00
if ncutils.IsWindows() {
return nil
}
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
binarypath := dir + "/netclient"
_, err = os.Stat("/etc/netclient/config")
2021-09-20 02:03:47 +08:00
if os.IsNotExist(err) {
os.MkdirAll("/etc/netclient/config", 0744)
2021-09-20 02:03:47 +08:00
} else if err != nil {
log.Println("couldnt find or create /etc/netclient")
return err
}
//install binary
//should check if the existing binary is the corect version -- for now only copy if file doesn't exist
2022-01-26 07:15:27 +08:00
if !ncutils.FileExists(EXEC_DIR + "netclient") {
err = ncutils.Copy(binarypath, EXEC_DIR+"netclient")
2021-09-20 02:03:47 +08:00
if err != nil {
log.Println(err)
return err
}
}
systemservice := `[Unit]
2022-02-02 04:27:00 +08:00
Description=Netclient Daemon
Documentation=https://docs.netmaker.org https://k8s.netmaker.org
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
2021-09-20 02:03:47 +08:00
[Service]
2022-02-02 04:27:00 +08:00
User=root
2021-09-20 02:03:47 +08:00
Type=simple
2022-02-02 04:27:00 +08:00
ExecStart=/sbin/netclient daemon
Restart=on-failure
RestartSec=15s
2021-09-20 02:03:47 +08:00
[Install]
WantedBy=multi-user.target
`
servicebytes := []byte(systemservice)
if !ncutils.FileExists("/etc/systemd/system/netclient.service") {
2022-01-07 04:05:38 +08:00
err = os.WriteFile("/etc/systemd/system/netclient.service", servicebytes, 0644)
2021-09-20 02:03:47 +08:00
if err != nil {
log.Println(err)
return err
}
}
_, _ = ncutils.RunCmd("systemctl enable netclient.service", true)
2021-09-20 02:03:47 +08:00
_, _ = ncutils.RunCmd("systemctl daemon-reload", true)
2022-01-26 07:15:27 +08:00
_, _ = ncutils.RunCmd("systemctl start netclient.service", true)
2021-09-20 02:03:47 +08:00
return nil
}
// RestartSystemD - restarts systemd service
2022-02-02 13:02:36 +08:00
func RestartSystemD() {
2022-02-02 13:45:22 +08:00
ncutils.PrintLog("restarting netclient.service", 1)
time.Sleep(time.Second)
_, _ = ncutils.RunCmd("systemctl restart netclient.service", true)
2022-02-02 13:02:36 +08:00
}
// CleanupLinux - cleans up neclient configs
2021-10-20 03:53:01 +08:00
func CleanupLinux() {
if err := os.RemoveAll(ncutils.GetNetclientPath()); err != nil {
ncutils.PrintLog("Removing netclient configs: "+err.Error(), 1)
}
2022-01-26 07:15:27 +08:00
if err := os.Remove(EXEC_DIR + "netclient"); err != nil {
2021-10-20 03:53:01 +08:00
ncutils.PrintLog("Removing netclient binary: "+err.Error(), 1)
}
}
// StopSystemD - tells system to stop systemd
func StopSystemD() {
ncutils.RunCmd("systemctl stop netclient.service", false)
}
2021-10-09 03:07:12 +08:00
// RemoveSystemDServices - removes the systemd services on a machine
2021-10-18 03:31:37 +08:00
func RemoveSystemDServices() error {
2021-09-20 02:03:47 +08:00
//sysExec, err := exec.LookPath("systemctl")
2021-10-18 03:31:37 +08:00
var err error
if !ncutils.IsWindows() && isOnlyService() {
2021-09-20 02:03:47 +08:00
if err != nil {
log.Println(err)
}
2021-10-20 22:20:19 +08:00
ncutils.RunCmd("systemctl disable netclient.service", false)
ncutils.RunCmd("systemctl disable netclient.timer", false)
if ncutils.FileExists("/etc/systemd/system/netclient.service") {
err = os.Remove("/etc/systemd/system/netclient.service")
if err != nil {
ncutils.Log("Error removing /etc/systemd/system/netclient.service. Please investigate.")
}
}
if ncutils.FileExists("/etc/systemd/system/netclient.timer") {
err = os.Remove("/etc/systemd/system/netclient.timer")
if err != nil {
ncutils.Log("Error removing /etc/systemd/system/netclient.timer. Please investigate.")
}
2021-09-20 02:03:47 +08:00
}
2021-10-20 22:20:19 +08:00
ncutils.RunCmd("systemctl daemon-reload", false)
ncutils.RunCmd("systemctl reset-failed", false)
ncutils.Log("removed systemd remnants if any existed")
2021-09-20 02:03:47 +08:00
}
return nil
}
2021-10-18 03:31:37 +08:00
func isOnlyService() bool {
files, err := filepath.Glob("/etc/netclient/config/netconfig-*")
2021-09-20 02:03:47 +08:00
if err != nil {
2021-10-18 03:31:37 +08:00
return false
2021-09-20 02:03:47 +08:00
}
2021-10-18 03:31:37 +08:00
return len(files) == 0
2021-09-20 02:03:47 +08:00
}