diff --git a/handlers/exec.go b/handlers/exec.go index 005aca1..5518e3a 100644 --- a/handlers/exec.go +++ b/handlers/exec.go @@ -12,9 +12,22 @@ import ( // Echo the data received on the WebSocket. func Exec(ws *websocket.Conn) { - id := bone.GetValue(ws.Request(), "instanceId") + sessionId := bone.GetValue(ws.Request(), "sessionId") + instanceId := bone.GetValue(ws.Request(), "instanceId") + ctx := context.Background() - conn, err := services.GetExecConnection(id, ctx) + + session := services.GetSession(sessionId) + instance := services.GetInstance(session, instanceId) + + if instance.ExecId == "" { + execId, err := services.CreateExecConnection(instance.Name, ctx) + if err != nil { + return + } + instance.ExecId = execId + } + conn, err := services.AttachExecConnection(instance.ExecId, ctx) if err != nil { return } diff --git a/services/docker.go b/services/docker.go index 3bc1ca9..7f232d6 100644 --- a/services/docker.go +++ b/services/docker.go @@ -49,26 +49,25 @@ func DeleteNetwork(id string) error { return nil } -func GetExecConnection(id string, ctx context.Context) (*types.HijackedResponse, error) { +func CreateExecConnection(id string, ctx context.Context) (string, error) { conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}} resp, err := c.ContainerExecCreate(ctx, id, conf) if err != nil { - return nil, err + return "", err } - //err = c.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{Tty: true}) - //if err != nil { - //return nil, err - //} + return resp.ID, nil +} - conn, err := c.ContainerExecAttach(ctx, resp.ID, conf) +func AttachExecConnection(execId string, ctx context.Context) (*types.HijackedResponse, error) { + conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}} + conn, err := c.ContainerExecAttach(ctx, execId, conf) if err != nil { return nil, err } return &conn, nil - } func CreateInstance(net string) (*ptypes.Instance, error) { diff --git a/types/session.go b/types/session.go index e0c3b60..ad49c22 100644 --- a/types/session.go +++ b/types/session.go @@ -6,6 +6,7 @@ type Session struct { } type Instance struct { - Name string `json:"name"` - IP string `json:"ip"` + Name string `json:"name"` + IP string `json:"ip"` + ExecId string `json:"-"` }