From e5c429af1ae2f5b23582851a3f4d18f4fb1f082c Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Thu, 26 May 2022 12:55:39 +0200 Subject: [PATCH] Move flags declaration to root (#329) This allows for mgmtDataDir and mgmtConfig to be initialized properly use handleMigration function for copying files --- management/cmd/management.go | 79 ++++++++++++++++++------------------ management/cmd/root.go | 50 +++++++++++++++-------- 2 files changed, 73 insertions(+), 56 deletions(-) diff --git a/management/cmd/management.go b/management/cmd/management.go index 72c3a5d23..639ed86f6 100644 --- a/management/cmd/management.go +++ b/management/cmd/management.go @@ -30,10 +30,6 @@ import ( var ( mgmtPort int - defaultMgmtDataDir string - defaultMgmtConfig string - mgmtDataDir string - mgmtConfig string mgmtLetsencryptDomain string certFile string certKey string @@ -60,33 +56,14 @@ var ( log.Fatalf("failed initializing log %v", err) } - if mgmtDataDir == "" { - oldPath := "/var/lib/wiretrustee" - if migrateToNetbird(oldPath, defaultMgmtDataDir) { - if err := cpDir(oldPath, defaultMgmtDataDir); err != nil { - log.Fatal(err) - } - } - } - - actualMgmtConfigPath := mgmtConfig - if mgmtConfig == "" { - oldPath := "/etc/wiretrustee/management.json" - if migrateToNetbird(oldPath, defaultMgmtConfig) { - if err := cpDir("/etc/wiretrustee/", defaultConfigPath); err != nil { - log.Fatal(err) - } - - if err := cpFile(oldPath, defaultMgmtConfig); err != nil { - log.Fatal(err) - } - } - actualMgmtConfigPath = defaultMgmtConfig - } - - config, err := loadMgmtConfig(actualMgmtConfigPath) + err = handleRebrand(cmd) if err != nil { - log.Fatalf("failed reading provided config file: %s: %v", actualMgmtConfigPath, err) + log.Fatalf("failed to migrate files %v", err) + } + + config, err := loadMgmtConfig(mgmtConfig) + if err != nil { + log.Fatalf("failed reading provided config file: %s: %v", mgmtConfig, err) } if _, err = os.Stat(config.Datadir); os.IsNotExist(err) { @@ -219,6 +196,38 @@ func loadTLSConfig(certFile string, certKey string) (*tls.Config, error) { return config, nil } +func handleRebrand(cmd *cobra.Command) error { + var err error + if logFile == defaultLogFile { + if migrateToNetbird(oldDefaultLogFile, defaultLogFile) { + cmd.Printf("will copy Log dir %s and its content to %s\n", oldDefaultLogDir, defaultLogDir) + err = cpDir(oldDefaultLogDir, defaultLogDir) + if err != nil { + return err + } + } + } + if mgmtConfig == defaultMgmtConfig { + if migrateToNetbird(oldDefaultMgmtConfig, defaultMgmtConfig) { + cmd.Printf("will copy Config dir %s and its content to %s\n", oldDefaultMgmtConfigDir, defaultMgmtConfigDir) + err = cpDir(oldDefaultMgmtConfigDir, defaultMgmtConfigDir) + if err != nil { + return err + } + } + } + if mgmtDataDir == defaultMgmtDataDir { + if migrateToNetbird(oldDefaultMgmtDataDir, defaultMgmtDataDir) { + cmd.Printf("will copy Config dir %s and its content to %s\n", oldDefaultMgmtDataDir, defaultMgmtDataDir) + err = cpDir(oldDefaultMgmtDataDir, defaultMgmtDataDir) + if err != nil { + return err + } + } + } + return nil +} + func cpFile(src, dst string) error { var err error var srcfd *os.File @@ -305,13 +314,3 @@ func migrateToNetbird(oldPath, newPath string) bool { return true } - -func init() { - mgmtCmd.Flags().IntVar(&mgmtPort, "port", 33073, "server port to listen on") - mgmtCmd.Flags().StringVar(&mgmtDataDir, "datadir", defaultMgmtDataDir, "server data directory location") - mgmtCmd.Flags().StringVar(&mgmtConfig, "config", defaultMgmtConfig, "Netbird config file location. Config params specified via command line (e.g. datadir) have a precedence over configuration from this file") - mgmtCmd.Flags().StringVar(&mgmtLetsencryptDomain, "letsencrypt-domain", "", "a domain to issue Let's Encrypt certificate for. Enables TLS using Let's Encrypt. Will fetch and renew certificate, and run the server with TLS") - mgmtCmd.Flags().StringVar(&certFile, "cert-file", "", "Location of your SSL certificate. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect") - mgmtCmd.Flags().StringVar(&certKey, "cert-key", "", "Location of your SSL certificate private key. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect") - rootCmd.MarkFlagRequired("config") //nolint -} diff --git a/management/cmd/root.go b/management/cmd/root.go index 97a300fff..37ea347c1 100644 --- a/management/cmd/root.go +++ b/management/cmd/root.go @@ -2,11 +2,9 @@ package cmd import ( "fmt" + "github.com/spf13/cobra" "os" "os/signal" - "runtime" - - "github.com/spf13/cobra" ) const ( @@ -15,11 +13,20 @@ const ( ) var ( - configPath string - defaultConfigPath string - logLevel string - defaultLogFile string - logFile string + defaultMgmtConfigDir string + defaultMgmtDataDir string + defaultMgmtConfig string + defaultLogDir string + defaultLogFile string + oldDefaultMgmtConfigDir string + oldDefaultMgmtDataDir string + oldDefaultMgmtConfig string + oldDefaultLogDir string + oldDefaultLogFile string + mgmtDataDir string + mgmtConfig string + logLevel string + logFile string rootCmd = &cobra.Command{ Use: "netbird-mgmt", @@ -40,16 +47,27 @@ func init() { stopCh = make(chan int) defaultMgmtDataDir = "/var/lib/netbird/" - defaultConfigPath = "/etc/netbird" - defaultMgmtConfig = defaultConfigPath + "/management.json" - defaultLogFile = "/var/log/netbird/management.log" + defaultMgmtConfigDir = "/etc/netbird" + defaultLogDir = "/var/log/netbird" - if runtime.GOOS == "windows" { - defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Netbird\\" + "management.json" - defaultLogFile = os.Getenv("PROGRAMDATA") + "\\Netbird\\" + "management.log" - } + oldDefaultMgmtDataDir = "/var/lib/wiretrustee/" + oldDefaultMgmtConfigDir = "/etc/wiretrustee" + oldDefaultLogDir = "/var/log/wiretrustee" + + defaultMgmtConfig = defaultMgmtConfigDir + "/management.json" + defaultLogFile = defaultLogDir + "/management.log" + + oldDefaultMgmtConfig = oldDefaultMgmtConfigDir + "/management.json" + oldDefaultLogFile = oldDefaultLogDir + "/management.log" + + mgmtCmd.Flags().IntVar(&mgmtPort, "port", 33073, "server port to listen on") + mgmtCmd.Flags().StringVar(&mgmtDataDir, "datadir", defaultMgmtDataDir, "server data directory location") + mgmtCmd.Flags().StringVar(&mgmtConfig, "config", defaultMgmtConfig, "Netbird config file location. Config params specified via command line (e.g. datadir) have a precedence over configuration from this file") + mgmtCmd.Flags().StringVar(&mgmtLetsencryptDomain, "letsencrypt-domain", "", "a domain to issue Let's Encrypt certificate for. Enables TLS using Let's Encrypt. Will fetch and renew certificate, and run the server with TLS") + mgmtCmd.Flags().StringVar(&certFile, "cert-file", "", "Location of your SSL certificate. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect") + mgmtCmd.Flags().StringVar(&certKey, "cert-key", "", "Location of your SSL certificate private key. Can be used when you have an existing certificate and don't want a new certificate be generated automatically. If letsencrypt-domain is specified this property has no effect") + rootCmd.MarkFlagRequired("config") //nolint - rootCmd.PersistentFlags().StringVar(&configPath, "config", defaultConfigPath, "Netbird config file location to write new config to") rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "") rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the the log will be output to stdout") rootCmd.AddCommand(mgmtCmd)