mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-14 01:57:32 +08:00
Attach to the same terminal
This commit is contained in:
parent
7bbb284128
commit
02d50073c5
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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:"-"`
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user