From 9e4aa4f1f1294adc3163da2a2783a675b8d6c85c Mon Sep 17 00:00:00 2001 From: andpar83 Date: Sat, 24 Jul 2021 07:14:29 -0700 Subject: [PATCH] Move management server to a separate directory (#67) * Move management server to a separate directory --- cmd/root.go | 6 +- {cmd => management/cmd}/management.go | 12 ++-- management/cmd/root.go | 70 +++++++++++++++++++ management/main.go | 12 ++++ .../{ => server}/management_suite_test.go | 2 +- management/{ => server}/management_test.go | 13 ++-- management/{ => server}/server.go | 12 ++-- management/{ => server}/testdata/store.json | 0 management/{ => store}/file_store.go | 2 +- management/{ => store}/store.go | 2 +- 10 files changed, 109 insertions(+), 22 deletions(-) rename {cmd => management/cmd}/management.go (95%) create mode 100644 management/cmd/root.go create mode 100644 management/main.go rename management/{ => server}/management_suite_test.go (88%) rename management/{ => server}/management_test.go (98%) rename management/{ => server}/server.go (98%) rename management/{ => server}/testdata/store.json (100%) rename management/{ => store}/file_store.go (99%) rename management/{ => store}/store.go (97%) diff --git a/cmd/root.go b/cmd/root.go index 698f1a576..fcc8d16de 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,11 +2,12 @@ package cmd import ( "fmt" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" "os" "os/signal" "runtime" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) const ( @@ -47,7 +48,6 @@ func init() { rootCmd.AddCommand(addPeerCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(signalCmd) - rootCmd.AddCommand(mgmtCmd) rootCmd.AddCommand(serviceCmd) serviceCmd.AddCommand(runCmd, startCmd, stopCmd, restartCmd) // service control commands are subcommands of service serviceCmd.AddCommand(installCmd, uninstallCmd) // service installer commands are subcommands of service diff --git a/cmd/management.go b/management/cmd/management.go similarity index 95% rename from cmd/management.go rename to management/cmd/management.go index c4ee1490a..d317c54b1 100644 --- a/cmd/management.go +++ b/management/cmd/management.go @@ -3,17 +3,19 @@ package cmd import ( "flag" "fmt" + server2 "github.com/wiretrustee/wiretrustee/management/server" + + "net" + "os" + "time" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/wiretrustee/wiretrustee/encryption" - mgmt "github.com/wiretrustee/wiretrustee/management" mgmtProto "github.com/wiretrustee/wiretrustee/management/proto" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" - "net" - "os" - "time" ) var ( @@ -56,7 +58,7 @@ var ( opts = append(opts, grpc.KeepaliveEnforcementPolicy(kaep), grpc.KeepaliveParams(kasp)) grpcServer := grpc.NewServer(opts...) - server, err := mgmt.NewServer(mgmtDataDir) + server, err := server2.NewServer(mgmtDataDir) if err != nil { log.Fatalf("failed creating new server: %v", err) } diff --git a/management/cmd/root.go b/management/cmd/root.go new file mode 100644 index 000000000..ef7a64c2d --- /dev/null +++ b/management/cmd/root.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "fmt" + "os" + "os/signal" + "runtime" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +const ( + // ExitSetupFailed defines exit code + ExitSetupFailed = 1 +) + +var ( + configPath string + defaultConfigPath string + logLevel string + + rootCmd = &cobra.Command{ + Use: "wiretrustee", + Short: "", + Long: "", + } + + // Execution control channel for stopCh signal + stopCh chan int +) + +// Execute executes the root command. +func Execute() error { + return rootCmd.Execute() +} +func init() { + + stopCh = make(chan int) + + defaultConfigPath = "/etc/wiretrustee/config.json" + if runtime.GOOS == "windows" { + defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Wiretrustee\\" + "config.json" + } + rootCmd.PersistentFlags().StringVar(&configPath, "config", defaultConfigPath, "Wiretrustee config file location to write new config to") + rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "") + rootCmd.AddCommand(mgmtCmd) +} + +// SetupCloseHandler handles SIGTERM signal and exits with success +func SetupCloseHandler() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for range c { + fmt.Println("\r- Ctrl+C pressed in Terminal") + stopCh <- 0 + } + }() +} + +// InitLog parses and sets log-level input +func InitLog(logLevel string) { + level, err := log.ParseLevel(logLevel) + if err != nil { + log.Errorf("Failed parsing log-level %s: %s", logLevel, err) + os.Exit(ExitSetupFailed) + } + log.SetLevel(level) +} diff --git a/management/main.go b/management/main.go new file mode 100644 index 000000000..64ff15ccd --- /dev/null +++ b/management/main.go @@ -0,0 +1,12 @@ +package main + +import ( + cmd2 "github.com/wiretrustee/wiretrustee/management/cmd" + "os" +) + +func main() { + if err := cmd2.Execute(); err != nil { + os.Exit(1) + } +} diff --git a/management/management_suite_test.go b/management/server/management_suite_test.go similarity index 88% rename from management/management_suite_test.go rename to management/server/management_suite_test.go index 70aab51f4..cc99624a0 100644 --- a/management/management_suite_test.go +++ b/management/server/management_suite_test.go @@ -1,4 +1,4 @@ -package management_test +package server_test import ( . "github.com/onsi/ginkgo" diff --git a/management/management_test.go b/management/server/management_test.go similarity index 98% rename from management/management_test.go rename to management/server/management_test.go index 499e1b528..dd8fb9a8c 100644 --- a/management/management_test.go +++ b/management/server/management_test.go @@ -1,10 +1,8 @@ -package management_test +package server_test import ( "context" - pb "github.com/golang/protobuf/proto" //nolint - log "github.com/sirupsen/logrus" - "github.com/wiretrustee/wiretrustee/encryption" + server2 "github.com/wiretrustee/wiretrustee/management/server" "io/ioutil" "math/rand" "net" @@ -13,9 +11,12 @@ import ( sync2 "sync" "time" + pb "github.com/golang/protobuf/proto" //nolint + log "github.com/sirupsen/logrus" + "github.com/wiretrustee/wiretrustee/encryption" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - mgmt "github.com/wiretrustee/wiretrustee/management" mgmtProto "github.com/wiretrustee/wiretrustee/management/proto" "github.com/wiretrustee/wiretrustee/util" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -323,7 +324,7 @@ func startServer(dataDir string) (*grpc.Server, net.Listener) { lis, err := net.Listen("tcp", ":0") Expect(err).NotTo(HaveOccurred()) s := grpc.NewServer() - server, err := mgmt.NewServer(dataDir) + server, err := server2.NewServer(dataDir) Expect(err).NotTo(HaveOccurred()) mgmtProto.RegisterManagementServiceServer(s, server) go func() { diff --git a/management/server.go b/management/server/server.go similarity index 98% rename from management/server.go rename to management/server/server.go index 0a4dca9e9..f9d9ac760 100644 --- a/management/server.go +++ b/management/server/server.go @@ -1,7 +1,11 @@ -package management +package server import ( "context" + "github.com/wiretrustee/wiretrustee/management/store" + "sync" + "time" + "github.com/golang/protobuf/ptypes/timestamp" log "github.com/sirupsen/logrus" "github.com/wiretrustee/wiretrustee/encryption" @@ -9,13 +13,11 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "sync" - "time" ) // Server an instance of a Management server type Server struct { - Store *FileStore + Store *store.FileStore wgKey wgtypes.Key proto.UnimplementedManagementServiceServer peerChannels map[string]chan *UpdateChannelMessage @@ -32,7 +34,7 @@ func NewServer(dataDir string) (*Server, error) { if err != nil { return nil, err } - store, err := NewStore(dataDir) + store, err := store.NewStore(dataDir) if err != nil { return nil, err } diff --git a/management/testdata/store.json b/management/server/testdata/store.json similarity index 100% rename from management/testdata/store.json rename to management/server/testdata/store.json diff --git a/management/file_store.go b/management/store/file_store.go similarity index 99% rename from management/file_store.go rename to management/store/file_store.go index ea18ad579..f13e3b473 100644 --- a/management/file_store.go +++ b/management/store/file_store.go @@ -1,4 +1,4 @@ -package management +package store import ( "os" diff --git a/management/store.go b/management/store/store.go similarity index 97% rename from management/store.go rename to management/store/store.go index 2899cc847..c19ee84e7 100644 --- a/management/store.go +++ b/management/store/store.go @@ -1,4 +1,4 @@ -package management +package store // Account represents a unique account of the system type Account struct {