2021-06-20 23:01:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-03-25 13:21:04 +01:00
|
|
|
"context"
|
2022-11-26 13:29:50 +01:00
|
|
|
"os"
|
2022-11-23 13:39:42 +01:00
|
|
|
"path/filepath"
|
2021-06-20 23:01:44 +02:00
|
|
|
"runtime"
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2021-06-20 23:01:44 +02:00
|
|
|
)
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
var installCmd = &cobra.Command{
|
|
|
|
Use: "install",
|
2022-05-22 18:53:47 +02:00
|
|
|
Short: "installs Netbird service",
|
2022-03-08 14:47:55 +01:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-01-17 19:16:50 +01:00
|
|
|
SetFlagsFromEnvVars(rootCmd)
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-05-25 19:41:03 +02:00
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
|
|
|
|
2022-05-22 18:53:47 +02:00
|
|
|
err := handleRebrand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
svcConfig := newSVCConfig()
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
svcConfig.Arguments = []string{
|
|
|
|
"service",
|
|
|
|
"run",
|
|
|
|
"--config",
|
|
|
|
configPath,
|
|
|
|
"--log-level",
|
|
|
|
logLevel,
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-05-12 11:17:24 +02:00
|
|
|
if managementURL != "" {
|
2022-11-23 13:39:42 +01:00
|
|
|
svcConfig.Arguments = append(svcConfig.Arguments, "--management-url", managementURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if logFile != "console" {
|
|
|
|
svcConfig.Arguments = append(svcConfig.Arguments, "--log-file", logFile)
|
2022-05-12 11:17:24 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
// Respected only by systemd systems
|
|
|
|
svcConfig.Dependencies = []string{"After=network.target syslog.target"}
|
2022-11-26 13:29:50 +01:00
|
|
|
|
|
|
|
if logFile != "console" {
|
|
|
|
setStdLogPath := true
|
|
|
|
dir := filepath.Dir(logFile)
|
|
|
|
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
err = os.MkdirAll(dir, 0750)
|
|
|
|
if err != nil {
|
|
|
|
setStdLogPath = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if setStdLogPath {
|
|
|
|
svcConfig.Option["LogOutput"] = true
|
|
|
|
svcConfig.Option["LogDirectory"] = dir
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-03-25 13:21:04 +01:00
|
|
|
ctx, cancel := context.WithCancel(cmd.Context())
|
|
|
|
|
|
|
|
s, err := newSVC(newProgram(ctx, cancel), svcConfig)
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
err = s.Install()
|
|
|
|
if err != nil {
|
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return err
|
|
|
|
}
|
2022-05-22 18:53:47 +02:00
|
|
|
cmd.Println("Netbird service has been installed")
|
2022-03-08 14:47:55 +01:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
var uninstallCmd = &cobra.Command{
|
|
|
|
Use: "uninstall",
|
2022-05-22 18:53:47 +02:00
|
|
|
Short: "uninstalls Netbird service from system",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-01-17 19:16:50 +01:00
|
|
|
SetFlagsFromEnvVars(rootCmd)
|
2021-06-20 23:01:44 +02:00
|
|
|
|
2022-05-25 19:41:03 +02:00
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
|
|
|
|
2022-05-22 18:53:47 +02:00
|
|
|
err := handleRebrand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:21:04 +01:00
|
|
|
ctx, cancel := context.WithCancel(cmd.Context())
|
|
|
|
|
|
|
|
s, err := newSVC(newProgram(ctx, cancel), newSVCConfig())
|
2022-03-08 14:47:55 +01:00
|
|
|
if err != nil {
|
2022-05-22 18:53:47 +02:00
|
|
|
return err
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Uninstall()
|
|
|
|
if err != nil {
|
2022-05-22 18:53:47 +02:00
|
|
|
return err
|
2022-03-08 14:47:55 +01:00
|
|
|
}
|
2022-05-22 18:53:47 +02:00
|
|
|
cmd.Println("Netbird has been uninstalled")
|
|
|
|
return nil
|
2022-03-08 14:47:55 +01:00
|
|
|
},
|
|
|
|
}
|