Skip to content

Commit 0308560

Browse files
committed
appease the linter
1 parent 42f3412 commit 0308560

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

agent/agentsocket/server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ import (
1919
"github.com/coder/coder/v2/codersdk/drpcsdk"
2020
)
2121

22+
// Client wraps a DRPC client with connection management.
23+
// This type is defined here so it's available on all platforms.
24+
type Client struct {
25+
proto.DRPCAgentSocketClient
26+
conn net.Conn
27+
session *yamux.Session
28+
}
29+
30+
// Close closes the client connection.
31+
func (c *Client) Close() error {
32+
if c.session != nil {
33+
_ = c.session.Close()
34+
}
35+
if c.conn != nil {
36+
return c.conn.Close()
37+
}
38+
return nil
39+
}
40+
2241
// Server provides access to the DRPCAgentSocketService via a Unix domain socket.
2342
// Do not invoke Server{} directly. Use NewServer() instead.
2443
type Server struct {

agent/agentsocket/service.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,23 @@ func (s *DRPCAgentSocketService) SyncStart(_ context.Context, req *proto.SyncSta
5959
}, nil
6060
}
6161
if !isReady {
62+
return nil, errors.New("Unit is not ready")
63+
}
64+
65+
// Check if unit is already started
66+
currentStatus, err := s.unitManager.GetStatus(req.Unit)
67+
if err != nil && !errors.Is(err, unit.ErrConsumerNotFound) {
6268
return &proto.SyncStartResponse{
6369
Success: false,
64-
Message: "Unit is not ready",
70+
Message: "Failed to get unit status: " + err.Error(),
6571
}, nil
6672
}
73+
if currentStatus == unit.StatusStarted {
74+
return nil, unit.ErrSameStatusAlreadySet
75+
}
6776

6877
if err := s.unitManager.UpdateStatus(req.Unit, unit.StatusStarted); err != nil {
69-
return &proto.SyncStartResponse{
70-
Success: false,
71-
Message: "Failed to update status: " + err.Error(),
72-
}, nil
78+
return nil, err
7379
}
7480

7581
return &proto.SyncStartResponse{

agent/agentsocket/socket_unix.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
package agentsocket
44

55
import (
6+
"context"
67
"fmt"
78
"net"
89
"os"
910
"path/filepath"
1011

12+
"github.com/hashicorp/yamux"
1113
"golang.org/x/xerrors"
14+
15+
"cdr.dev/slog"
16+
"github.com/coder/coder/v2/agent/agentsocket/proto"
17+
"github.com/coder/coder/v2/codersdk/drpcsdk"
1218
)
1319

1420
// createSocket creates a Unix domain socket listener
@@ -74,3 +80,25 @@ func isSocketAvailable(path string) bool {
7480
// Socket is in use
7581
return false
7682
}
83+
84+
// NewClient creates a DRPC client for the agent socket at the given path.
85+
func NewClient(path string, logger slog.Logger) (*Client, error) {
86+
conn, err := net.Dial("unix", path)
87+
if err != nil {
88+
return nil, xerrors.Errorf("dial unix socket: %w", err)
89+
}
90+
91+
config := yamux.DefaultConfig()
92+
config.LogOutput = nil
93+
config.Logger = slog.Stdlib(context.Background(), logger, slog.LevelInfo)
94+
session, err := yamux.Client(conn, config)
95+
if err != nil {
96+
_ = conn.Close()
97+
return nil, xerrors.Errorf("multiplex client: %w", err)
98+
}
99+
return &Client{
100+
DRPCAgentSocketClient: proto.NewDRPCAgentSocketClient(drpcsdk.MultiplexedConn(session)),
101+
conn: conn,
102+
session: session,
103+
}, nil
104+
}

agent/agentsocket/socket_windows.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
package agentsocket
44

55
import (
6+
"context"
67
"net"
78
"os"
89
"time"
910

11+
"cdr.dev/slog"
12+
"github.com/coder/coder/v2/agent/agentsocket/proto"
13+
"github.com/coder/coder/v2/codersdk/drpcsdk"
14+
"github.com/hashicorp/yamux"
1015
"golang.org/x/xerrors"
1116
)
1217

@@ -51,3 +56,25 @@ type SocketInfo struct {
5156
Owner string
5257
Group string
5358
}
59+
60+
// NewClient creates a DRPC client for the agent socket at the given path.
61+
func NewClient(path string, logger slog.Logger) (*Client, error) {
62+
conn, err := net.Dial("unix", path)
63+
if err != nil {
64+
return nil, xerrors.Errorf("dial unix socket: %w", err)
65+
}
66+
67+
config := yamux.DefaultConfig()
68+
config.LogOutput = nil
69+
config.Logger = slog.Stdlib(context.Background(), logger, slog.LevelInfo)
70+
session, err := yamux.Client(conn, config)
71+
if err != nil {
72+
_ = conn.Close()
73+
return nil, xerrors.Errorf("multiplex client: %w", err)
74+
}
75+
return &Client{
76+
DRPCAgentSocketClient: proto.NewDRPCAgentSocketClient(drpcsdk.MultiplexedConn(session)),
77+
conn: conn,
78+
session: session,
79+
}, nil
80+
}

cli/testdata/coder_agent_--help.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ OPTIONS:
6767
--script-data-dir string, $CODER_AGENT_SCRIPT_DATA_DIR (default: /tmp)
6868
Specify the location for storing script data.
6969

70+
--socket-path string, $CODER_AGENT_SOCKET_PATH
71+
Specify the path for the agent socket.
72+
7073
--ssh-max-timeout duration, $CODER_AGENT_SSH_MAX_TIMEOUT (default: 72h)
7174
Specify the max timeout for a SSH connection, it is advisable to set
7275
it to a minimum of 60s, but no more than 72h.

0 commit comments

Comments
 (0)