Move management server to a separate directory (#67)

* Move management server to a separate directory
This commit is contained in:
andpar83 2021-07-24 07:14:29 -07:00 committed by GitHub
parent 83ac774264
commit 9e4aa4f1f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 109 additions and 22 deletions

View File

@ -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

View File

@ -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)
}

70
management/cmd/root.go Normal file
View File

@ -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)
}

12
management/main.go Normal file
View File

@ -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)
}
}

View File

@ -1,4 +1,4 @@
package management_test
package server_test
import (
. "github.com/onsi/ginkgo"

View File

@ -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() {

View File

@ -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
}

View File

@ -1,4 +1,4 @@
package management
package store
import (
"os"

View File

@ -1,4 +1,4 @@
package management
package store
// Account represents a unique account of the system
type Account struct {