Skip to content

Commit

Permalink
client: Add incus-user support
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Oct 7, 2023
1 parent e2a5052 commit 268946e
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions client/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,8 @@ func ConnectIncusUnixWithContext(ctx context.Context, path string, args *Connect

ctxConnected, ctxConnectedCancel := context.WithCancel(context.Background())

// Initialize the client struct
server := ProtocolIncus{
ctx: ctx,
httpBaseURL: *httpBaseURL,
httpUnixPath: path,
httpProtocol: "unix",
httpUserAgent: args.UserAgent,
ctxConnected: ctxConnected,
ctxConnectedCancel: ctxConnectedCancel,
eventConns: make(map[string]*websocket.Conn),
eventListeners: make(map[string][]*EventListener),
}

// Determine the socket path
var projectName string
if path == "" {
path = os.Getenv("INCUS_SOCKET")
if path == "" {
Expand All @@ -194,9 +182,32 @@ func ConnectIncusUnixWithContext(ctx context.Context, path string, args *Connect
}

path = filepath.Join(incusDir, "unix.socket")
userPath := filepath.Join(incusDir, "unix.socket.user")
if !util.PathIsWritable(path) && util.PathIsWritable(userPath) {
// Handle the use of incus-user.
path = userPath

// When using incus-user, the project list is typically restricted.
// So let's try to be smart about the project we're using.
projectName = fmt.Sprintf("user-%d", os.Geteuid())
}
}
}

// Initialize the client struct
server := ProtocolIncus{
ctx: ctx,
httpBaseURL: *httpBaseURL,
httpUnixPath: path,
httpProtocol: "unix",
httpUserAgent: args.UserAgent,
ctxConnected: ctxConnected,
ctxConnectedCancel: ctxConnectedCancel,
eventConns: make(map[string]*websocket.Conn),
eventListeners: make(map[string][]*EventListener),
project: projectName,
}

// Setup the HTTP client
httpClient, err := unixHTTPClient(args, path)
if err != nil {
Expand Down

0 comments on commit 268946e

Please sign in to comment.