mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(agent): wire up agentssh server to allow exec into container (#16638)
Builds on top of https://github.com/coder/coder/pull/16623/ and wires up the ReconnectingPTY server. This does nothing to wire up the web terminal yet but the added test demonstrates the functionality working. Other changes: * Refactors and moves the `SystemEnvInfo` interface to the `agent/usershell` package to address follow-up from https://github.com/coder/coder/pull/16623#discussion_r1967580249 * Marks `usershellinfo.Get` as deprecated. Consumers should use the `EnvInfoer` interface instead. --------- Co-authored-by: Mathias Fredriksson <mafredri@gmail.com> Co-authored-by: Danny Kopping <danny@coder.com>
This commit is contained in:
co-authored by
Mathias Fredriksson
Danny Kopping
parent
a3223397cb
commit
172e52317c
@@ -93,6 +93,24 @@ type AgentReconnectingPTYInit struct {
|
||||
Height uint16
|
||||
Width uint16
|
||||
Command string
|
||||
// Container, if set, will attempt to exec into a running container visible to the agent.
|
||||
// This should be a unique container ID (implementation-dependent).
|
||||
Container string
|
||||
// ContainerUser, if set, will set the target user when execing into a container.
|
||||
// This can be a username or UID, depending on the underlying implementation.
|
||||
// This is ignored if Container is not set.
|
||||
ContainerUser string
|
||||
}
|
||||
|
||||
// AgentReconnectingPTYInitOption is a functional option for AgentReconnectingPTYInit.
|
||||
type AgentReconnectingPTYInitOption func(*AgentReconnectingPTYInit)
|
||||
|
||||
// AgentReconnectingPTYInitWithContainer sets the container and container user for the reconnecting PTY session.
|
||||
func AgentReconnectingPTYInitWithContainer(container, containerUser string) AgentReconnectingPTYInitOption {
|
||||
return func(init *AgentReconnectingPTYInit) {
|
||||
init.Container = container
|
||||
init.ContainerUser = containerUser
|
||||
}
|
||||
}
|
||||
|
||||
// ReconnectingPTYRequest is sent from the client to the server
|
||||
@@ -107,7 +125,7 @@ type ReconnectingPTYRequest struct {
|
||||
// ReconnectingPTY spawns a new reconnecting terminal session.
|
||||
// `ReconnectingPTYRequest` should be JSON marshaled and written to the returned net.Conn.
|
||||
// Raw terminal output will be read from the returned net.Conn.
|
||||
func (c *AgentConn) ReconnectingPTY(ctx context.Context, id uuid.UUID, height, width uint16, command string) (net.Conn, error) {
|
||||
func (c *AgentConn) ReconnectingPTY(ctx context.Context, id uuid.UUID, height, width uint16, command string, initOpts ...AgentReconnectingPTYInitOption) (net.Conn, error) {
|
||||
ctx, span := tracing.StartSpan(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -119,12 +137,16 @@ func (c *AgentConn) ReconnectingPTY(ctx context.Context, id uuid.UUID, height, w
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(AgentReconnectingPTYInit{
|
||||
rptyInit := AgentReconnectingPTYInit{
|
||||
ID: id,
|
||||
Height: height,
|
||||
Width: width,
|
||||
Command: command,
|
||||
})
|
||||
}
|
||||
for _, o := range initOpts {
|
||||
o(&rptyInit)
|
||||
}
|
||||
data, err := json.Marshal(rptyInit)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user