1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 18:27:25 +08:00

Attach to the same terminal

This commit is contained in:
Jonathan Leibiusky (@xetorthio) 2016-10-08 13:35:02 +02:00
parent 7bbb284128
commit 02d50073c5
3 changed files with 25 additions and 12 deletions

View File

@ -12,9 +12,22 @@ import (
// Echo the data received on the WebSocket. // Echo the data received on the WebSocket.
func Exec(ws *websocket.Conn) { 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() 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 { if err != nil {
return return
} }

View File

@ -49,26 +49,25 @@ func DeleteNetwork(id string) error {
return nil 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"}} conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}}
resp, err := c.ContainerExecCreate(ctx, id, conf) resp, err := c.ContainerExecCreate(ctx, id, conf)
if err != nil { if err != nil {
return nil, err return "", err
} }
//err = c.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{Tty: true}) return resp.ID, nil
//if err != nil { }
//return nil, err
//}
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 { if err != nil {
return nil, err return nil, err
} }
return &conn, nil return &conn, nil
} }
func CreateInstance(net string) (*ptypes.Instance, error) { func CreateInstance(net string) (*ptypes.Instance, error) {

View File

@ -6,6 +6,7 @@ type Session struct {
} }
type Instance struct { type Instance struct {
Name string `json:"name"` Name string `json:"name"`
IP string `json:"ip"` IP string `json:"ip"`
ExecId string `json:"-"`
} }