added server; initial cluster state
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
6fed0fa858
commit
562f1caa54
704 changed files with 361956 additions and 534 deletions
104
cmd/hctl/main.go
Normal file
104
cmd/hctl/main.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
Copyright 2019 Stellar Project
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without limitation the rights to use, copy,
|
||||
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies
|
||||
or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stellarproject/heimdall"
|
||||
"github.com/stellarproject/heimdall/client"
|
||||
"github.com/stellarproject/heimdall/version"
|
||||
"github.com/urfave/cli"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "actl"
|
||||
app.Version = version.BuildVersion()
|
||||
app.Author = "@stellarproject"
|
||||
app.Email = ""
|
||||
app.Usage = version.Description
|
||||
app.Flags = []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "debug, D",
|
||||
Usage: "Enable debug logging",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "addr, a",
|
||||
Usage: "heimdall grpc address",
|
||||
Value: "tcp://127.0.0.1:9000",
|
||||
EnvVar: "ATLAS_ADDR",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "cert, c",
|
||||
Usage: "heimdall client certificate",
|
||||
Value: "",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "key, k",
|
||||
Usage: "heimdall client key",
|
||||
Value: "",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "skip-verify",
|
||||
Usage: "skip TLS verification",
|
||||
},
|
||||
}
|
||||
app.Before = func(c *cli.Context) error {
|
||||
if c.Bool("debug") {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
app.Commands = []cli.Command{}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getClient(c *cli.Context) (*client.Client, error) {
|
||||
cert := c.GlobalString("cert")
|
||||
key := c.GlobalString("key")
|
||||
skipVerification := c.GlobalBool("skip-verify")
|
||||
|
||||
cfg := &heimdall.Config{
|
||||
TLSClientCertificate: cert,
|
||||
TLSClientKey: key,
|
||||
TLSInsecureSkipVerify: skipVerification,
|
||||
}
|
||||
|
||||
opts, err := client.DialOptionsFromConfig(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts = append(opts,
|
||||
grpc.WithBlock(),
|
||||
grpc.WithUserAgent(fmt.Sprintf("%s/%s", version.Name, version.Version)),
|
||||
)
|
||||
|
||||
addr := c.GlobalString("addr")
|
||||
return client.NewClient(heimdall.NodeID(), addr, opts...)
|
||||
}
|
92
cmd/heimdall/main.go
Normal file
92
cmd/heimdall/main.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
Copyright 2019 Stellar Project
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without limitation the rights to use, copy,
|
||||
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies
|
||||
or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stellarproject/heimdall"
|
||||
"github.com/stellarproject/heimdall/version"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultGRPCPort = 9000
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = version.Name
|
||||
app.Version = version.BuildVersion()
|
||||
app.Author = "@stellarproject"
|
||||
app.Email = ""
|
||||
app.Usage = version.Description
|
||||
app.Flags = []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "debug, D",
|
||||
Usage: "enable debug logging",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "id",
|
||||
Usage: "node id",
|
||||
Value: heimdall.NodeID(),
|
||||
EnvVar: "HEIMDALL_NODE_ID",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "addr, a",
|
||||
Usage: "grpc address",
|
||||
Value: fmt.Sprintf("tcp://%s:%d", heimdall.GetIP(), defaultGRPCPort),
|
||||
EnvVar: "HEIMDALL_GRPC_ADDR",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "redis-url, r",
|
||||
Usage: "uri for datastore backend",
|
||||
Value: "redis://127.0.0.1:6379/0",
|
||||
EnvVar: "HEIMDALL_REDIS_URL",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "advertise-redis-url, p",
|
||||
Usage: "advertise uri for peers",
|
||||
Value: fmt.Sprintf("redis://%s:6379/0", heimdall.GetIP()),
|
||||
EnvVar: "HEIMDALL_ADVERTISE_REDIS_URL",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "peer",
|
||||
Usage: "grpc address to join a peer",
|
||||
EnvVar: "HEIMDALL_PEER",
|
||||
},
|
||||
}
|
||||
app.Before = func(c *cli.Context) error {
|
||||
if c.Bool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
app.Action = runServer
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
171
cmd/heimdall/run.go
Normal file
171
cmd/heimdall/run.go
Normal file
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
Copyright 2019 Stellar Project
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without limitation the rights to use, copy,
|
||||
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies
|
||||
or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stellarproject/heimdall"
|
||||
"github.com/stellarproject/heimdall/server"
|
||||
"github.com/stellarproject/heimdall/version"
|
||||
"github.com/urfave/cli"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
func runServer(cx *cli.Context) error {
|
||||
cfg := &heimdall.Config{
|
||||
ID: cx.String("id"),
|
||||
GRPCAddress: cx.String("addr"),
|
||||
GRPCPeerAddress: cx.String("peer"),
|
||||
RedisURL: cx.String("redis-url"),
|
||||
AdvertiseRedisURL: cx.String("advertise-redis-url"),
|
||||
}
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
|
||||
srv, err := server.NewServer(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// create grpc server
|
||||
grpcOpts, err := getGRPCOptions(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
grpcServer := grpc.NewServer(grpcOpts...)
|
||||
|
||||
// register heimdall server
|
||||
if err := srv.Register(grpcServer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
proto, ep, err := getGRPCEndpoint(cfg.GRPCAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l, err := net.Listen(proto, ep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
logrus.WithField("addr", cfg.GRPCAddress).Debug("starting grpc server")
|
||||
go grpcServer.Serve(l)
|
||||
|
||||
signals := make(chan os.Signal)
|
||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
|
||||
doneCh := make(chan bool, 1)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case sig := <-signals:
|
||||
switch sig {
|
||||
case syscall.SIGUSR1:
|
||||
logrus.Debug("generating debug profile")
|
||||
profilePath, err := srv.GenerateProfile()
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
continue
|
||||
}
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"profile": profilePath,
|
||||
}).Info("generated memory profile")
|
||||
case syscall.SIGTERM, syscall.SIGINT:
|
||||
logrus.Info("shutting down")
|
||||
if err := srv.Stop(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
doneCh <- true
|
||||
default:
|
||||
logrus.Warnf("unhandled signal %s", sig)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"version": version.Version,
|
||||
"commit": version.GitCommit,
|
||||
}).Infof("starting %s", version.Name)
|
||||
go func() {
|
||||
if err := srv.Run(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-doneCh:
|
||||
return nil
|
||||
case err := <-errCh:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func getGRPCEndpoint(addr string) (string, string, error) {
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
// only tcp/unix are allowed
|
||||
var ep string
|
||||
switch u.Scheme {
|
||||
case "tcp":
|
||||
ep = u.Host
|
||||
case "unix":
|
||||
ep = u.Path
|
||||
default:
|
||||
return "", "", fmt.Errorf("unsupported grpc listener protocol: %s", u.Scheme)
|
||||
}
|
||||
|
||||
return u.Scheme, ep, nil
|
||||
}
|
||||
|
||||
func getGRPCOptions(cfg *heimdall.Config) ([]grpc.ServerOption, error) {
|
||||
grpcOpts := []grpc.ServerOption{}
|
||||
if cfg.TLSServerCertificate != "" && cfg.TLSServerKey != "" {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"cert": cfg.TLSServerCertificate,
|
||||
"key": cfg.TLSServerKey,
|
||||
}).Debug("configuring TLS for GRPC")
|
||||
cert, err := tls.LoadX509KeyPair(cfg.TLSServerCertificate, cfg.TLSServerKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
creds := credentials.NewTLS(&tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
ClientAuth: tls.RequestClientCert,
|
||||
InsecureSkipVerify: cfg.TLSInsecureSkipVerify,
|
||||
})
|
||||
grpcOpts = append(grpcOpts, grpc.Creds(creds))
|
||||
}
|
||||
return grpcOpts, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue