mirror of
https://github.com/netbirdio/netbird.git
synced 2025-05-22 01:11:01 +02:00
Move flags declaration to root (#329)
This allows for mgmtDataDir and mgmtConfig to be initialized properly use handleMigration function for copying files
This commit is contained in:
parent
4b5e6b93a6
commit
e5c429af1a
@ -30,10 +30,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
mgmtPort int
|
mgmtPort int
|
||||||
defaultMgmtDataDir string
|
|
||||||
defaultMgmtConfig string
|
|
||||||
mgmtDataDir string
|
|
||||||
mgmtConfig string
|
|
||||||
mgmtLetsencryptDomain string
|
mgmtLetsencryptDomain string
|
||||||
certFile string
|
certFile string
|
||||||
certKey string
|
certKey string
|
||||||
@ -60,33 +56,14 @@ var (
|
|||||||
log.Fatalf("failed initializing log %v", err)
|
log.Fatalf("failed initializing log %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mgmtDataDir == "" {
|
err = handleRebrand(cmd)
|
||||||
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)
|
|
||||||
if err != nil {
|
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) {
|
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
|
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 {
|
func cpFile(src, dst string) error {
|
||||||
var err error
|
var err error
|
||||||
var srcfd *os.File
|
var srcfd *os.File
|
||||||
@ -305,13 +314,3 @@ func migrateToNetbird(oldPath, newPath string) bool {
|
|||||||
|
|
||||||
return true
|
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
|
|
||||||
}
|
|
||||||
|
@ -2,11 +2,9 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -15,10 +13,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configPath string
|
defaultMgmtConfigDir string
|
||||||
defaultConfigPath string
|
defaultMgmtDataDir string
|
||||||
logLevel string
|
defaultMgmtConfig string
|
||||||
|
defaultLogDir string
|
||||||
defaultLogFile string
|
defaultLogFile string
|
||||||
|
oldDefaultMgmtConfigDir string
|
||||||
|
oldDefaultMgmtDataDir string
|
||||||
|
oldDefaultMgmtConfig string
|
||||||
|
oldDefaultLogDir string
|
||||||
|
oldDefaultLogFile string
|
||||||
|
mgmtDataDir string
|
||||||
|
mgmtConfig string
|
||||||
|
logLevel string
|
||||||
logFile string
|
logFile string
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
@ -40,16 +47,27 @@ func init() {
|
|||||||
stopCh = make(chan int)
|
stopCh = make(chan int)
|
||||||
|
|
||||||
defaultMgmtDataDir = "/var/lib/netbird/"
|
defaultMgmtDataDir = "/var/lib/netbird/"
|
||||||
defaultConfigPath = "/etc/netbird"
|
defaultMgmtConfigDir = "/etc/netbird"
|
||||||
defaultMgmtConfig = defaultConfigPath + "/management.json"
|
defaultLogDir = "/var/log/netbird"
|
||||||
defaultLogFile = "/var/log/netbird/management.log"
|
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
oldDefaultMgmtDataDir = "/var/lib/wiretrustee/"
|
||||||
defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Netbird\\" + "management.json"
|
oldDefaultMgmtConfigDir = "/etc/wiretrustee"
|
||||||
defaultLogFile = os.Getenv("PROGRAMDATA") + "\\Netbird\\" + "management.log"
|
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(&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.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)
|
rootCmd.AddCommand(mgmtCmd)
|
||||||
|
Loading…
Reference in New Issue
Block a user