2022-02-09 11:06:41 +08:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-02-09 11:31:17 +08:00
|
|
|
"os"
|
2022-02-09 11:06:41 +08:00
|
|
|
|
2022-03-20 23:12:05 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2022-02-09 11:06:41 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetupFreebsdDaemon -- sets up daemon for freebsd
|
|
|
|
func SetupFreebsdDaemon() error {
|
2022-03-30 20:04:52 +08:00
|
|
|
binarypath, err := os.Executable()
|
2022-02-09 11:06:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat("/etc/netclient/config")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.MkdirAll("/etc/netclient/config", 0744)
|
|
|
|
} 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
|
|
|
|
if !ncutils.FileExists(EXEC_DIR + "netclient") {
|
|
|
|
err = ncutils.Copy(binarypath, EXEC_DIR+"netclient")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rcFile := `#!/bin/sh
|
|
|
|
#
|
|
|
|
# PROVIDE: netclient
|
|
|
|
# REQUIRE: LOGIN
|
|
|
|
# KEYWORD: shutdown
|
|
|
|
|
|
|
|
# Description:
|
|
|
|
# This script runs netclient as a service as root on boot
|
|
|
|
|
|
|
|
# How to use:
|
|
|
|
# Place this file in /usr/local/etc/rc.d/
|
|
|
|
# Add netclient="YES" to /etc/rc.config.d/netclient
|
|
|
|
# To pass args, add netclient_args="daemon" to /etc/rc.config.d/netclient
|
|
|
|
|
|
|
|
# Freebsd rc library
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
|
|
|
# General Info
|
|
|
|
name="netclient" # Safe name of program
|
|
|
|
program_name="netclient" # Name of exec
|
|
|
|
title="netclient" # Title to display in top/htop
|
|
|
|
|
|
|
|
# RC.config vars
|
|
|
|
load_rc_config $name # Loading rc config vars
|
|
|
|
: ${netclient_enable="YES"} # Default: enable netclient
|
|
|
|
: ${netclient_runAs="root"} # Default: Run Node-RED as root
|
|
|
|
|
|
|
|
# Freebsd Setup
|
|
|
|
rcvar=netclient_enable # Enables the rc.conf YES/NO flag
|
|
|
|
pidfile="/var/run/${program_name}.pid" # File that allows the system to keep track of node-red status
|
|
|
|
|
|
|
|
# Env Setup
|
|
|
|
#export HOME=$( getent passwd "$netclient_runAs" | cut -d: -f6 ) # Gets the home directory of the runAs user
|
|
|
|
|
|
|
|
# Command Setup
|
|
|
|
exec_path="/sbin/${program_name}" # Path to the netclient exec
|
|
|
|
output_file="/var/log/${program_name}.log" # Path to netclient logs
|
|
|
|
|
|
|
|
# Command
|
|
|
|
command="/usr/sbin/daemon"
|
|
|
|
command_args="-r -t ${title} -u ${netclient_runAs} -o ${output_file} -P ${pidfile} ${exec_path} ${netclient_args}"
|
|
|
|
|
|
|
|
# Loading Config
|
|
|
|
load_rc_config ${name}
|
|
|
|
run_rc_command "$1"
|
|
|
|
`
|
|
|
|
|
|
|
|
rcConfig := `netclient="YES"
|
|
|
|
netclient_args="daemon"`
|
|
|
|
|
|
|
|
rcbytes := []byte(rcFile)
|
|
|
|
if !ncutils.FileExists("/etc/rc.d/netclient") {
|
2022-02-09 11:31:17 +08:00
|
|
|
err := os.WriteFile("/etc/rc.d/netclient", rcbytes, 0744)
|
2022-02-09 11:06:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcConfigbytes := []byte(rcConfig)
|
|
|
|
if !ncutils.FileExists("/etc/rc.conf.d/netclient") {
|
|
|
|
err := os.WriteFile("/etc/rc.conf.d/netclient", rcConfigbytes, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
FreebsdDaemon("start")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 11:31:17 +08:00
|
|
|
return nil
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:10:59 +08:00
|
|
|
// FreebsdDaemon - accepts args to service netclient and applies
|
2022-02-09 11:06:41 +08:00
|
|
|
func FreebsdDaemon(command string) {
|
2022-02-09 12:30:50 +08:00
|
|
|
_, _ = ncutils.RunCmdFormatted("service netclient "+command, true)
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:10:59 +08:00
|
|
|
// CleanupFreebsd - removes config files and netclient binary
|
2022-02-09 11:31:17 +08:00
|
|
|
func CleanupFreebsd() {
|
2022-02-09 11:06:41 +08:00
|
|
|
if err := os.RemoveAll(ncutils.GetNetclientPath()); err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "Removing netclient configs: ", err.Error())
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
if err := os.Remove(EXEC_DIR + "netclient"); err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "Removing netclient binary: ", err.Error())
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:10:59 +08:00
|
|
|
// RemoveFreebsdDaemon - remove freebsd daemon
|
2022-02-09 11:06:41 +08:00
|
|
|
func RemoveFreebsdDaemon() {
|
|
|
|
if ncutils.FileExists("/etc/rc.d/netclient") {
|
2022-02-09 11:31:17 +08:00
|
|
|
err := os.Remove("/etc/rc.d/netclient")
|
2022-02-09 11:06:41 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "Error removing /etc/rc.d/netclient. Please investigate.")
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ncutils.FileExists("/etc/rc.conf.d/netclient") {
|
2022-02-09 11:31:17 +08:00
|
|
|
err := os.Remove("/etc/rc.conf.d/netclient")
|
2022-02-09 11:06:41 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "Error removing /etc/rc.conf.d/netclient. Please investigate.")
|
2022-02-09 11:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|