/
node-management.go
64 lines (54 loc) · 1.55 KB
/
node-management.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
package coordination
import (
"context"
"database/sql"
"fmt"
pb "github.com/charles-d-burton/tailsys/commands"
"github.com/charles-d-burton/tailsys/data/queries"
"google.golang.org/protobuf/proto"
)
// RegistrationServer struct to contain proto for gRPC
type RegistrationServer struct {
pb.UnimplementedRegistrationServer
DevMode bool
ID string
Hostname string
DB *sql.DB
}
func (r *RegistrationServer) createRegistration(nrr *pb.NodeRegistrationRequest) error {
data, err := proto.Marshal(nrr)
if err != nil {
return err
}
clientName := nrr.GetInfo().Hostname
nhr := &queries.RegisteredHostsData{
Hostname: nrr.GetInfo().Hostname,
Key: nrr.GetKey().Key,
Data: data,
}
// clientKey := nrr.Key.GetKey()
fmt.Printf("registering %s\n", clientName)
err = queries.InsertHostRegistration(r.DB, nhr)
if err != nil {
fmt.Println("could not create bucket")
return err
}
return nil
}
// Register registers a node with the database when a node sends a request. Returns the server id so the node can verify further requests
func (r *RegistrationServer) Register(ctx context.Context, in *pb.NodeRegistrationRequest) (*pb.NodeRegistrationResponse, error) {
fmt.Println("received coordination request")
fmt.Println(in)
in.Accepted = r.DevMode
if r.DevMode {
fmt.Println("running in dev mode, accepting all incoming connections")
}
if err := r.createRegistration(in); err != nil {
return nil, err
}
return &pb.NodeRegistrationResponse{
Accepted: r.DevMode,
Key: &pb.Key{Key: r.ID},
Hostname: r.Hostname,
}, nil
}