diff --git a/bootcfg/rpc/grpc.go b/bootcfg/rpc/grpc.go new file mode 100644 index 00000000..cce91f5f --- /dev/null +++ b/bootcfg/rpc/grpc.go @@ -0,0 +1,16 @@ +package rpc + +import ( + "google.golang.org/grpc" + + "github.com/coreos/coreos-baremetal/bootcfg/server" + pb "github.com/coreos/coreos-baremetal/bootcfg/server/serverpb" +) + +// NewServer wraps the bootcfg Server to return a new gRPC Server. +func NewServer(s server.Server, opts ...grpc.ServerOption) (*grpc.Server, error) { + grpcServer := grpc.NewServer(opts...) + pb.RegisterGroupsServer(grpcServer, s) + pb.RegisterProfilesServer(grpcServer, s) + return grpcServer, nil +} diff --git a/build b/build index f600ea2c..6331bc64 100755 --- a/build +++ b/build @@ -2,6 +2,3 @@ LD_FLAGS="-w -X main.version=$(./git-version)" CGO_ENABLED=0 go build -o bin/bootcfg -ldflags "$LD_FLAGS" -a -tags netgo github.com/coreos/coreos-baremetal/cmd/bootcfg - -# gRPC server as a separate binary -CGO_ENABLED=0 go build -o bin/bootcfg-rpc -ldflags "$LD_FLAGS" -a -tags netgo github.com/coreos/coreos-baremetal/cmd/bootcfg-rpc diff --git a/cmd/bootcfg-rpc/main.go b/cmd/bootcfg-rpc/main.go deleted file mode 100644 index d2a6ba85..00000000 --- a/cmd/bootcfg-rpc/main.go +++ /dev/null @@ -1,93 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "net" - "net/url" - "os" - - "github.com/coreos/pkg/capnslog" - "github.com/coreos/pkg/flagutil" - "google.golang.org/grpc" - - "github.com/coreos/coreos-baremetal/bootcfg/config" - bootcfg "github.com/coreos/coreos-baremetal/bootcfg/server" - pb "github.com/coreos/coreos-baremetal/bootcfg/server/serverpb" - "github.com/coreos/coreos-baremetal/bootcfg/storage" -) - -var ( - // version provided by compile time flag: -ldflags "-X main.version $GIT_SHA" - version = "was not built properly" - log = capnslog.NewPackageLogger("github.com/coreos/coreos-baremetal/cmd/bootcfg-rpc", "main") -) - -func main() { - flags := struct { - address string - configPath string - dataPath string - version bool - help bool - }{} - flag.StringVar(&flags.address, "address", "127.0.0.1:8081", "gRPC listen address") - flag.StringVar(&flags.configPath, "config", "/etc/bootcfg.conf", "Path to config file") - flag.StringVar(&flags.dataPath, "data-path", "/etc/bootcfg", "Path to data directory") - // subcommands - flag.BoolVar(&flags.version, "version", false, "print version and exit") - flag.BoolVar(&flags.help, "help", false, "print usage and exit") - - // parse command-line and environment variable arguments - flag.Parse() - if err := flagutil.SetFlagsFromEnv(flag.CommandLine, "BOOTCFG"); err != nil { - log.Fatal(err.Error()) - } - - if flags.version { - fmt.Println(version) - return - } - - if flags.help { - flag.Usage() - return - } - - // validate arguments - if url, err := url.Parse(flags.address); err != nil || url.String() == "" { - log.Fatal("A valid HTTP listen address is required") - } - if finfo, err := os.Stat(flags.configPath); err != nil || finfo.IsDir() { - log.Fatal("A path to a config file is required") - } - if finfo, err := os.Stat(flags.dataPath); err != nil || !finfo.IsDir() { - log.Fatal("A path to a data directory is required") - } - - // load bootstrap config - cfg, err := config.LoadConfig(flags.configPath) - if err != nil { - log.Fatal(err) - } - - // storage - store := storage.NewFileStore(&storage.Config{ - Dir: flags.dataPath, - Groups: cfg.Groups, - }) - - // gRPC Server - log.Infof("starting bootcfg gRPC server on %s", flags.address) - lis, err := net.Listen("tcp", flags.address) - if err != nil { - log.Fatalf("failed to start listening: %v", err) - } - grpcServer := grpc.NewServer() - bootcfgServer := bootcfg.NewServer(&bootcfg.Config{ - Store: store, - }) - pb.RegisterGroupsServer(grpcServer, bootcfgServer) - pb.RegisterProfilesServer(grpcServer, bootcfgServer) - grpcServer.Serve(lis) -} diff --git a/cmd/bootcfg/main.go b/cmd/bootcfg/main.go index b1797d1f..89eb3a34 100644 --- a/cmd/bootcfg/main.go +++ b/cmd/bootcfg/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "net" "net/http" "net/url" "os" @@ -13,6 +14,8 @@ import ( "github.com/coreos/coreos-baremetal/bootcfg/api" "github.com/coreos/coreos-baremetal/bootcfg/config" + "github.com/coreos/coreos-baremetal/bootcfg/rpc" + "github.com/coreos/coreos-baremetal/bootcfg/server" "github.com/coreos/coreos-baremetal/bootcfg/sign" "github.com/coreos/coreos-baremetal/bootcfg/storage" ) @@ -26,6 +29,7 @@ var ( func main() { flags := struct { address string + rpcAddress string configPath string dataPath string assetsPath string @@ -35,6 +39,7 @@ func main() { help bool }{} flag.StringVar(&flags.address, "address", "127.0.0.1:8080", "HTTP listen address") + flag.StringVar(&flags.rpcAddress, "rpcAddress", "", "RPC listen address") flag.StringVar(&flags.configPath, "config", "/etc/bootcfg.conf", "Path to config file") flag.StringVar(&flags.dataPath, "data-path", "/etc/bootcfg", "Path to data directory") flag.StringVar(&flags.assetsPath, "assets-path", "/var/bootcfg", "Path to static assets") @@ -108,16 +113,35 @@ func main() { Groups: cfg.Groups, }) - // HTTP server + bootcfgServer := server.NewServer(&server.Config{ + Store: store, + }) + + // gRPC Server (feature hidden) + if flags.rpcAddress != "" { + grpcServer, err := rpc.NewServer(bootcfgServer) + if err != nil { + log.Fatal(err) + } + log.Infof("starting bootcfg gRPC server on %s", flags.rpcAddress) + lis, err := net.Listen("tcp", flags.rpcAddress) + if err != nil { + log.Fatalf("failed to start listening: %v", err) + } + go grpcServer.Serve(lis) + defer grpcServer.Stop() + } + + // HTTP Server config := &api.Config{ Store: store, AssetsPath: flags.assetsPath, Signer: signer, ArmoredSigner: armoredSigner, } - server := api.NewServer(config) + httpServer := api.NewServer(config) log.Infof("starting bootcfg HTTP server on %s", flags.address) - err = http.ListenAndServe(flags.address, server.HTTPHandler()) + err = http.ListenAndServe(flags.address, httpServer.HTTPHandler()) if err != nil { log.Fatalf("failed to start listening: %v", err) }