/
spicedb.go
204 lines (183 loc) · 5.25 KB
/
spicedb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package spice
import (
"context"
"fmt"
"io"
"net"
"os"
"strconv"
"time"
"github.com/authzed/grpcutil"
"github.com/authzed/spicedb/e2e"
"google.golang.org/grpc"
"github.com/authzed/spicedb/e2e/cockroach"
)
//go:generate go run github.com/ecordell/optgen -output spicedb_options.go . SpiceDb
// Node represents a single instance of spicedb started via exec
type Node struct {
ID string
PresharedKey string
Datastore string
DbName string
URI string
GrpcPort int
HttpPort int
DispatchPort int
MetricsPort int
DashboardPort int
Pid int
Cancel context.CancelFunc
client e2e.Client
}
// WithTestDefaults sets the default values for Node
func WithTestDefaults(opts ...SpiceDbOption) SpiceDbOption {
return func(s *Node) {
for _, o := range opts {
o(s)
}
if s.GrpcPort == 0 {
s.GrpcPort = 50051
}
if s.DispatchPort == 0 {
s.DispatchPort = 50052
}
if s.HttpPort == 0 {
s.HttpPort = 8443
}
if s.MetricsPort == 0 {
s.MetricsPort = 9090
}
if s.DashboardPort == 0 {
// this would typically be 8080, but conflicts with Node's dash
s.DashboardPort = 8090
}
if len(s.DbName) == 0 {
s.DbName = "spicedb"
}
if len(s.PresharedKey) == 0 {
s.PresharedKey = "testtesttesttest"
}
}
}
// Start starts an instance of spicedb using the configuration
func (s *Node) Start(ctx context.Context, logprefix string, args ...string) error {
logfile, err := e2e.File(ctx, fmt.Sprintf("spicedb-%s-%s.log", logprefix, s.ID))
if err != nil {
return err
}
cmd := []string{
"./spicedb",
"serve",
"--log-level=debug",
"--datastore-request-hedging=false",
"--grpc-preshared-key=" + s.PresharedKey,
"--datastore-engine=" + s.Datastore,
"--datastore-conn-uri=" + s.URI,
fmt.Sprintf("--grpc-addr=:%d", s.GrpcPort),
fmt.Sprintf("--http-addr=:%d", s.HttpPort),
fmt.Sprintf("--dispatch-cluster-addr=:%d", s.DispatchPort),
fmt.Sprintf("--metrics-addr=:%d", s.MetricsPort),
fmt.Sprintf("--dashboard-addr=:%d", s.DashboardPort),
}
ctx, cancel := context.WithCancel(ctx)
s.Cancel = cancel
s.Pid, err = e2e.GoRun(ctx, logfile, logfile, append(cmd, args...)...)
return err
}
// Stop will Cancel a running spicedb process
func (s *Node) Stop() error {
if s.Pid < 1 {
return fmt.Errorf("can't stop an unstarted spicedb")
}
s.Cancel()
return nil
}
// Connect blocks until a connection to the spicedb instance can be established.
// Once connected, the client is avaialable via Client()
func (s *Node) Connect(ctx context.Context, out io.Writer) error {
if s.Pid < 1 {
return fmt.Errorf("can't create client for unstarted spicedb")
}
addr := net.JoinHostPort("localhost", strconv.Itoa(s.GrpcPort))
e2e.WaitForServerReady(addr, out)
conn, err := grpc.DialContext(ctx, addr,
grpc.WithBlock(), grpc.WithInsecure(),
grpcutil.WithInsecureBearerToken(s.PresharedKey))
if err != nil {
return err
}
s.client = e2e.NewClient(conn)
return nil
}
// Client returns a client that can talk to a started spicedb instance
func (s *Node) Client() e2e.Client {
return s.client
}
// Cluster is a set of spicedb nodes
type Cluster []*Node
// NewClusterFromCockroachCluster creates a spicedb instance for every
// cockroach instance, with each spicedb configured to talk to the corresponding
// cockraoch node.
func NewClusterFromCockroachCluster(c cockroach.Cluster, opts ...SpiceDbOption) Cluster {
ss := make([]*Node, 0, len(c))
// the prototypical node will be used to generate a set of nodes
proto := NewSpiceDbWithOptions(WithTestDefaults(opts...))
for i := 0; i < len(c); i++ {
ss = append(ss, &Node{
ID: strconv.Itoa(i + 1),
PresharedKey: proto.PresharedKey,
Datastore: "cockroachdb",
URI: c[i].ConnectionString(proto.DbName),
GrpcPort: proto.GrpcPort + 2*i,
DispatchPort: proto.DispatchPort + 2*i,
HttpPort: proto.HttpPort + 2*i,
MetricsPort: proto.MetricsPort + i,
DashboardPort: proto.DashboardPort + i,
})
}
return ss
}
// Start starts the entire cluster of spicedb instances
func (c *Cluster) Start(ctx context.Context, out io.Writer, prefix string, args ...string) error {
for _, s := range *c {
fmt.Fprintln(out, "starting spice node", s.ID)
if err := s.Start(ctx, prefix, args...); err != nil {
return err
}
}
return nil
}
// Stop stops the entire cluster of spicedb instances
func (c *Cluster) Stop(out io.Writer) error {
for _, s := range *c {
fmt.Fprintln(out, "stopping spice node", s.ID)
if err := s.Stop(); err != nil {
return err
}
}
return nil
}
// Connect blocks until a connection can be made to each instance in the cluster
func (c *Cluster) Connect(ctx context.Context, out io.Writer) error {
for _, s := range *c {
fmt.Fprintln(out, "connecting to", s.GrpcPort)
if err := s.Connect(ctx, out); err != nil {
return err
}
}
return nil
}
// MigrateHead migrates a Datastore to the latest revision defined in spicedb
func MigrateHead(ctx context.Context, datastore, uri string) error {
for i := 0; i < 5; i++ {
if err := e2e.Run(ctx, os.Stdout, os.Stderr,
"./spicedb",
"migrate", "head", "--datastore-engine="+datastore,
"--datastore-conn-uri="+uri,
); err == nil {
return nil
}
time.Sleep(1 * time.Second)
}
return fmt.Errorf("failed to migrate spicedb")
}