Skip to content

Commit 57b709c

Browse files
feat: common package [DTSS-595] (#115)
1 parent c4b0e2f commit 57b709c

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

cmd/localizer/expose.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21-
"os"
2221
"strings"
2322
"time"
2423

2524
apiv1 "github.com/getoutreach/localizer/api/v1"
26-
"github.com/getoutreach/localizer/internal/server"
25+
"github.com/getoutreach/localizer/pkg/localizer"
2726
"github.com/pkg/errors"
2827
"github.com/sirupsen/logrus"
2928
"github.com/urfave/cli/v2"
@@ -54,22 +53,20 @@ func NewExposeCommand(log logrus.FieldLogger) *cli.Command { //nolint:funlen
5453
serviceNamespace := split[0]
5554
serviceName := split[1]
5655

57-
if _, err := os.Stat(server.SocketPath); os.IsNotExist(err) {
56+
if !localizer.IsRunning() {
5857
return fmt.Errorf("localizer daemon not running (run localizer by itself?)")
5958
}
6059

6160
ctx, cancel := context.WithTimeout(c.Context, 30*time.Second)
6261
defer cancel()
6362

6463
log.Info("connecting to localizer daemon")
65-
conn, err := grpc.DialContext(ctx, "unix://"+server.SocketPath,
66-
grpc.WithBlock(), grpc.WithInsecure())
64+
65+
client, err := localizer.Connect(ctx, grpc.WithBlock(), grpc.WithInsecure())
6766
if err != nil {
68-
return errors.Wrap(err, "failed to talk to localizer daemon")
67+
return errors.Wrap(err, "failed to connect to localizer daemon")
6968
}
7069

71-
client := apiv1.NewLocalizerServiceClient(conn)
72-
7370
var stream apiv1.LocalizerService_ExposeServiceClient
7471
if c.Bool("stop") {
7572
log.Info("sending stop expose request to daemon")

cmd/localizer/list.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"time"
2525

2626
apiv1 "github.com/getoutreach/localizer/api/v1"
27-
"github.com/getoutreach/localizer/internal/server"
27+
"github.com/getoutreach/localizer/pkg/localizer"
2828
"github.com/pkg/errors"
2929
"github.com/sirupsen/logrus"
3030
"github.com/urfave/cli/v2"
@@ -37,20 +37,18 @@ func NewListCommand(_ logrus.FieldLogger) *cli.Command { //nolint:funlen
3737
Description: "list all port-forwarded services and their status(es)",
3838
Usage: "list",
3939
Action: func(c *cli.Context) error {
40-
if _, err := os.Stat(server.SocketPath); os.IsNotExist(err) {
40+
if !localizer.IsRunning() {
4141
return fmt.Errorf("localizer daemon not running (run localizer by itself?)")
4242
}
4343

4444
ctx, cancel := context.WithTimeout(c.Context, 30*time.Second)
4545
defer cancel()
4646

47-
conn, err := grpc.DialContext(ctx, "unix://"+server.SocketPath,
48-
grpc.WithBlock(), grpc.WithInsecure())
47+
client, err := localizer.Connect(ctx, grpc.WithBlock(), grpc.WithInsecure())
4948
if err != nil {
50-
return errors.Wrap(err, "failed to talk to localizer daemon")
49+
return errors.Wrap(err, "failed to connect to localizer daemon")
5150
}
5251

53-
client := apiv1.NewLocalizerServiceClient(conn)
5452
resp, err := client.List(ctx, &apiv1.ListRequest{})
5553
if err != nil {
5654
return err

internal/server/grpc.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import (
2727

2828
apiv1 "github.com/getoutreach/localizer/api/v1"
2929
"github.com/getoutreach/localizer/internal/kevents"
30+
"github.com/getoutreach/localizer/pkg/localizer"
3031
)
3132

32-
const SocketPath = "/var/run/localizer.sock"
33-
3433
type GRPCService struct {
3534
lis net.Listener
3635
srv *grpc.Server
@@ -53,45 +52,41 @@ func NewGRPCService(opts *RunOpts) *GRPCService {
5352
// CleanupPreviousInstance attempts to cleanup after a dead localizer instance
5453
// if a not dead one is found, an error is returned or if it fails to cleanup
5554
func (g *GRPCService) CleanupPreviousInstance(ctx context.Context, log logrus.FieldLogger) error {
56-
var cancel context.CancelFunc
57-
ctx, cancel = context.WithTimeout(ctx, time.Second*10)
55+
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
5856
defer cancel()
5957

6058
log.Info("checking if an instance of localizer is already running")
61-
conn, err := grpc.DialContext(ctx, "unix://"+SocketPath,
62-
grpc.WithBlock(), grpc.WithInsecure())
59+
client, err := localizer.Connect(ctx, grpc.WithBlock(), grpc.WithInsecure())
6360

6461
// if we made a connection, see if it's responding to pings
6562
// eventually we can expose useful information here?
6663
if err == nil {
67-
client := apiv1.NewLocalizerServiceClient(conn)
68-
_, err = client.Ping(ctx, &apiv1.PingRequest{})
69-
if err == nil {
64+
if _, err := client.Ping(ctx, &apiv1.PingRequest{}); err == nil {
7065
return fmt.Errorf("localizer instance is already running")
7166
}
7267
}
7368

7469
log.Warn("failed to contact existing instance, cleaning up socket")
75-
return errors.Wrap(os.Remove(SocketPath), "failed to cleanup socket from old localizer instance")
70+
71+
return errors.Wrap(os.Remove(localizer.Socket), "failed to cleanup socket from old localizer instance")
7672
}
7773

7874
// Run starts a grpc server with the internal server handler
7975
func (g *GRPCService) Run(ctx context.Context, log logrus.FieldLogger) error { //nolint:funlen
80-
if _, err := os.Stat(SocketPath); err == nil {
76+
if _, err := os.Stat(localizer.Socket); err == nil {
8177
// if we found an existing instance, attempt to cleanup after it
82-
err = g.CleanupPreviousInstance(ctx, log)
83-
if err != nil {
78+
if err := g.CleanupPreviousInstance(ctx, log); err != nil {
8479
return err
8580
}
8681
}
8782

88-
l, err := net.Listen("unix", SocketPath)
83+
l, err := net.Listen("unix", localizer.Socket)
8984
if err != nil {
9085
return errors.Wrap(err, "failed to listen on socket")
9186
}
92-
defer os.Remove(SocketPath)
87+
defer os.Remove(localizer.Socket)
9388

94-
err = os.Chmod(SocketPath, 0777)
89+
err = os.Chmod(localizer.Socket, 0777)
9590
if err != nil {
9691
return err
9792
}
@@ -122,7 +117,7 @@ func (g *GRPCService) Run(ctx context.Context, log logrus.FieldLogger) error { /
122117
}()
123118

124119
// One day Serve() will accept a context?
125-
log.Infof("starting GRPC server on '%s'", SocketPath)
120+
log.Infof("starting GRPC server on unix://'%s'", localizer.Socket)
126121
go func() {
127122
err := g.srv.Serve(g.lis)
128123
if err != nil {

pkg/localizer/localizer.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Package localizer is meant to contain useful helper functions, variables, and
2+
// constants in order to better programatically interact with localizer.
3+
package localizer
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
10+
apiv1 "github.com/getoutreach/localizer/api/v1"
11+
"github.com/pkg/errors"
12+
"google.golang.org/grpc"
13+
)
14+
15+
// Socket is the communication endpoint that the localizer server is listening
16+
// on.
17+
const Socket = "/var/run/localizer.sock"
18+
19+
// IsRunning checks to see if the localizer socket exists.
20+
func IsRunning() bool {
21+
if _, err := os.Stat(Socket); err != nil {
22+
return false
23+
}
24+
25+
return true
26+
}
27+
28+
// Connect returns a new instance of LocalizerServiceClient given a gRPC client
29+
// connection (returned from grpc.Dial*).
30+
func Connect(ctx context.Context, opts ...grpc.DialOption) (apiv1.LocalizerServiceClient, error) {
31+
clientConn, err := grpc.DialContext(ctx, fmt.Sprintf("unix://%s", Socket), opts...)
32+
if err != nil {
33+
return nil, errors.Wrap(err, "dial localizer")
34+
}
35+
36+
return apiv1.NewLocalizerServiceClient(clientConn), nil
37+
}

0 commit comments

Comments
 (0)