feat: implement ssh forwarding

This commit is contained in:
2024-11-17 18:10:35 +00:00
parent a7933f8b06
commit 45bfbe093a
21 changed files with 1175 additions and 296 deletions

View File

@@ -0,0 +1,55 @@
package workspace
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"io"
)
type spawnedShell struct {
io.Reader
io.Writer
execID string
}
func stopContainer(ctx context.Context, docker *client.Client, containerID string) error {
return docker.ContainerStop(ctx, containerID, container.StopOptions{})
}
func startContainer(ctx context.Context, docker *client.Client, containerID string) error {
return docker.ContainerStart(ctx, containerID, container.StartOptions{})
}
func deleteContainer(ctx context.Context, docker *client.Client, containerID string) error {
return docker.ContainerRemove(ctx, containerID, container.RemoveOptions{
RemoveVolumes: true,
})
}
func inspectContainer(ctx context.Context, docker *client.Client, containerID string) (types.ContainerJSON, error) {
return docker.ContainerInspect(ctx, containerID)
}
func spawnNewShell(ctx context.Context, docker *client.Client, containerID string) (*spawnedShell, error) {
res, err := docker.ContainerExecCreate(ctx, containerID, container.ExecOptions{
Tty: true,
Detach: true,
})
if err != nil {
return nil, err
}
attached, err := docker.ContainerExecAttach(ctx, res.ID, container.ExecAttachOptions{})
if err != nil {
return nil, err
}
return &spawnedShell{
Reader: attached.Reader,
Writer: attached.Conn,
execID: res.ID,
}, nil
}