netmaker/netclient/daemon/windows.go

107 lines
2.8 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package daemon
import (
"fmt"
"log"
"os"
"strings"
2022-05-16 08:26:38 +08:00
"time"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/logger"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
2021-10-09 03:07:12 +08:00
// SetupWindowsDaemon - sets up the Windows daemon service
2021-09-20 02:03:47 +08:00
func SetupWindowsDaemon() error {
2022-06-24 02:06:26 +08:00
if ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
logger.Log(0, "updating netclient service")
}
if err := writeServiceConfig(); err != nil {
return err
2021-09-20 02:03:47 +08:00
}
2022-06-24 02:06:26 +08:00
if ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") {
logger.Log(0, "updating netclient binary")
}
err := ncutils.GetEmbedded()
if err != nil {
return err
2021-09-20 02:03:47 +08:00
}
2022-06-24 02:06:26 +08:00
logger.Log(0, "finished daemon setup")
2022-05-16 08:26:38 +08:00
//get exact formatted commands
RunWinSWCMD("install")
time.Sleep(time.Millisecond)
2022-05-16 08:26:38 +08:00
RunWinSWCMD("start")
2021-09-20 02:03:47 +08:00
return nil
}
2022-02-02 13:18:02 +08:00
// RestartWindowsDaemon - restarts windows service
2022-02-02 13:02:36 +08:00
func RestartWindowsDaemon() {
2022-05-16 08:26:38 +08:00
RunWinSWCMD("stop")
time.Sleep(time.Millisecond)
2022-05-16 08:26:38 +08:00
RunWinSWCMD("start")
2022-02-02 13:02:36 +08:00
}
2021-10-09 03:07:12 +08:00
// CleanupWindows - cleans up windows files
2021-09-20 02:03:47 +08:00
func CleanupWindows() {
if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.xml") {
writeServiceConfig()
}
2022-05-16 08:26:38 +08:00
RunWinSWCMD("stop")
RunWinSWCMD("uninstall")
2021-09-20 02:03:47 +08:00
os.RemoveAll(ncutils.GetNetclientPath())
log.Println("Netclient on Windows, uninstalled")
}
func writeServiceConfig() error {
serviceConfigPath := ncutils.GetNetclientPathSpecific() + "winsw.xml"
scriptString := fmt.Sprintf(`<service>
<id>netclient</id>
<name>Netclient</name>
<description>Connects Windows nodes to one or more Netmaker networks.</description>
<executable>%v</executable>
2022-02-02 13:18:02 +08:00
<arguments>daemon</arguments>
2021-09-20 02:03:47 +08:00
<log mode="roll"></log>
</service>
`, strings.Replace(ncutils.GetNetclientPathSpecific()+"netclient.exe", `\\`, `\`, -1))
if !ncutils.FileExists(serviceConfigPath) {
err := os.WriteFile(serviceConfigPath, []byte(scriptString), 0600)
2021-09-20 02:03:47 +08:00
if err != nil {
return err
}
logger.Log(0, "wrote the daemon config file to the Netclient directory")
2021-09-20 02:03:47 +08:00
}
return nil
}
2022-05-16 08:26:38 +08:00
// RunWinSWCMD - Run a command with the winsw.exe tool (start, stop, install, uninstall)
func RunWinSWCMD(command string) {
2021-10-09 03:07:12 +08:00
2022-05-16 08:26:38 +08:00
// check if command allowed
allowedCommands := map[string]bool{
"start": true,
"stop": true,
"install": true,
"uninstall": true,
}
if !allowedCommands[command] {
logger.Log(0, "command "+command+" unsupported by winsw")
return
}
2021-09-20 02:03:47 +08:00
2022-05-16 08:26:38 +08:00
// format command
dirPath := strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)
winCmd := fmt.Sprintf(`"%swinsw.exe" "%s"`, dirPath, command)
logger.Log(0, "running "+command+" of Windows Netclient daemon")
// run command and log for success/failure
out, err := ncutils.RunCmdFormatted(winCmd, true)
if err != nil {
logger.Log(0, "error with "+command+" of Windows Netclient daemon: "+err.Error()+" : "+out)
} else {
logger.Log(0, "successfully ran "+command+" of Windows Netclient daemon")
}
2021-09-20 02:03:47 +08:00
}