2021-03-26 00:17:52 +08:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"github.com/davecgh/go-spew/spew"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-03-28 00:50:27 +08:00
|
|
|
"path/filepath"
|
2021-03-26 00:17:52 +08:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
|
2021-04-08 21:04:21 +08:00
|
|
|
func FileExists(f string) bool {
|
2021-04-05 23:06:30 +08:00
|
|
|
info, err := os.Stat(f)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigureSystemD(network string) error {
|
2021-03-28 00:50:27 +08:00
|
|
|
/*
|
2021-03-26 00:17:52 +08:00
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-03-28 00:50:27 +08:00
|
|
|
*/
|
|
|
|
//binarypath := path + "/netclient"
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
binarypath := dir + "/netclient"
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-03-28 00:50:27 +08:00
|
|
|
fmt.Println("Installing Binary from Path: " + binarypath)
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-03-28 00:50:27 +08:00
|
|
|
_, err = os.Stat("/etc/netclient")
|
2021-03-26 10:29:36 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.Mkdir("/etc/netclient", 744)
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Println("couldnt find or create /etc/netclient")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:04:21 +08:00
|
|
|
if !FileExists("/usr/local/bin/netclient") {
|
|
|
|
os.Symlink("/etc/netclient/netclient","/usr/local/bin/netclient")
|
|
|
|
/*
|
2021-03-26 22:35:52 +08:00
|
|
|
_, err = copy(binarypath, "/usr/local/bin/netclient")
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-08 21:04:21 +08:00
|
|
|
*/
|
2021-04-05 23:06:30 +08:00
|
|
|
}
|
2021-04-08 21:04:21 +08:00
|
|
|
if !FileExists("/etc/netclient/netclient") {
|
2021-03-26 22:35:52 +08:00
|
|
|
_, err = copy(binarypath, "/etc/netclient/netclient")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
systemservice := `[Unit]
|
|
|
|
Description=Regularly checks for updates in peers and local config
|
2021-03-26 10:29:36 +08:00
|
|
|
Wants=netclient.timer
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
[Service]
|
2021-04-05 23:06:30 +08:00
|
|
|
Type=simple
|
|
|
|
ExecStart=/etc/netclient/netclient -c checkin -n %i
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
`
|
|
|
|
|
|
|
|
systemtimer := `[Unit]
|
2021-03-26 10:29:36 +08:00
|
|
|
Description=Calls the Netmaker Mesh Client Service
|
2021-04-05 23:06:30 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
systemtimer = systemtimer + "Requires=netclient@"+network+".service"
|
|
|
|
|
|
|
|
systemtimer = systemtimer +
|
|
|
|
`
|
2021-03-26 00:17:52 +08:00
|
|
|
|
|
|
|
[Timer]
|
2021-04-05 23:06:30 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
systemtimer = systemtimer + "Unit=netclient@"+network+".service"
|
|
|
|
|
|
|
|
systemtimer = systemtimer +
|
|
|
|
`
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
OnCalendar=*:*:0/30
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=timers.target
|
|
|
|
`
|
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
servicebytes := []byte(systemservice)
|
|
|
|
timerbytes := []byte(systemtimer)
|
|
|
|
|
2021-04-08 21:04:21 +08:00
|
|
|
if !FileExists("/etc/systemd/system/netclient@.service") {
|
2021-04-05 23:06:30 +08:00
|
|
|
err = ioutil.WriteFile("/etc/systemd/system/netclient@.service", servicebytes, 0644)
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-04-08 21:04:21 +08:00
|
|
|
if !FileExists("/etc/systemd/system/netclient-"+network+".timer") {
|
2021-04-05 23:06:30 +08:00
|
|
|
err = ioutil.WriteFile("/etc/systemd/system/netclient-"+network+".timer", timerbytes, 0644)
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
sysExec, err := exec.LookPath("systemctl")
|
|
|
|
|
|
|
|
cmdSysEnableService := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "enable", "netclient@.service" },
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
/*
|
2021-03-26 00:17:52 +08:00
|
|
|
cmdSysStartService := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "start", "netclient@.service"},
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
*/
|
2021-03-26 00:17:52 +08:00
|
|
|
cmdSysDaemonReload := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
|
|
|
Args: []string{ sysExec, "daemon-reload"},
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
cmdSysEnableTimer := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "enable", "netclient-"+network+".timer" },
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
cmdSysStartTimer := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "start", "netclient-"+network+".timer"},
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmdSysEnableService.Run()
|
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error enabling netclient@.service. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
err = cmdSysDaemonReload.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reloading system daemons. Please investigate.")
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
err = cmdSysEnableTimer.Run()
|
|
|
|
if err != nil {
|
2021-03-26 22:35:52 +08:00
|
|
|
fmt.Println("Error enabling netclient.timer. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
err = cmdSysStartTimer.Run()
|
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error starting netclient-"+network+".timer. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:10:58 +08:00
|
|
|
func isOnlyService(network string) (bool, error) {
|
|
|
|
isonly := false
|
|
|
|
files, err := filepath.Glob("/etc/netclient/netconfig-*")
|
|
|
|
if err != nil {
|
|
|
|
return isonly, err
|
|
|
|
}
|
|
|
|
count := len(files)
|
|
|
|
if count == 0 {
|
|
|
|
isonly = true
|
|
|
|
}
|
|
|
|
return isonly, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
func RemoveSystemDServices(network string) error {
|
2021-03-26 00:17:52 +08:00
|
|
|
sysExec, err := exec.LookPath("systemctl")
|
|
|
|
|
2021-04-06 00:10:58 +08:00
|
|
|
|
|
|
|
fullremove, err := isOnlyService(network)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2021-03-26 00:17:52 +08:00
|
|
|
cmdSysDisableService := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "disable", "netclient@.service"},
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
cmdSysDaemonReload := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
|
|
|
Args: []string{ sysExec, "daemon-reload"},
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
2021-03-26 22:35:52 +08:00
|
|
|
cmdSysResetFailed := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
|
|
|
Args: []string{ sysExec, "reset-failed"},
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
cmdSysStopTimer := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "stop", "netclient-"+network+".timer" },
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
cmdSysDisableTimer := &exec.Cmd {
|
|
|
|
Path: sysExec,
|
2021-04-05 23:06:30 +08:00
|
|
|
Args: []string{ sysExec, "disable", "netclient-"+network+".timer"},
|
2021-03-26 00:17:52 +08:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stdout,
|
|
|
|
}
|
|
|
|
|
2021-04-05 23:06:30 +08:00
|
|
|
//err = cmdSysStopService.Run()
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error stopping netclient@.service. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-04-06 00:10:58 +08:00
|
|
|
if fullremove {
|
2021-03-26 00:17:52 +08:00
|
|
|
err = cmdSysDisableService.Run()
|
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error disabling netclient@.service. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-04-06 00:10:58 +08:00
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
err = cmdSysStopTimer.Run()
|
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error stopping netclient-"+network+".timer. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
err = cmdSysDisableTimer.Run()
|
|
|
|
if err != nil {
|
2021-04-05 23:06:30 +08:00
|
|
|
fmt.Println("Error disabling netclient-"+network+".timer. Please investigate.")
|
2021-03-26 00:17:52 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-04-06 00:10:58 +08:00
|
|
|
if fullremove {
|
2021-04-05 23:06:30 +08:00
|
|
|
err = os.Remove("/etc/systemd/system/netclient@.service")
|
2021-04-06 00:10:58 +08:00
|
|
|
}
|
2021-04-05 23:06:30 +08:00
|
|
|
err = os.Remove("/etc/systemd/system/netclient-"+network+".timer")
|
2021-03-26 00:17:52 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error removing file. Please investigate.")
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
err = cmdSysDaemonReload.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reloading system daemons. Please investigate.")
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-03-26 22:35:52 +08:00
|
|
|
err = cmdSysResetFailed.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reseting failed system services. Please investigate.")
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func copy(src, dst string) (int64, error) {
|
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
return 0, fmt.Errorf("%s is not a regular file", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
nBytes, err := io.Copy(destination, source)
|
|
|
|
err = os.Chmod(dst, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|