mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-18 21:31:39 +08:00
Make session replication
This commit is contained in:
parent
02d50073c5
commit
b3e7dcae3b
70
cookoo/multi.go
Normal file
70
cookoo/multi.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package cookoo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MultiWriter enables you to have a writer that passes on the writing to one
|
||||||
|
// of more Writers where the write is duplicated to each Writer. MultiWriter
|
||||||
|
// is similar to the multiWriter that is part of Go. The difference is
|
||||||
|
// this MultiWriter allows you to manager the Writers attached to it via CRUD
|
||||||
|
// operations. To do this you will need to mock the type. For example,
|
||||||
|
// mw := NewMultiWriter()
|
||||||
|
// mw.(*MultiWriter).AddWriter("foo", foo)
|
||||||
|
type MultiWriter struct {
|
||||||
|
writers map[string]io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write sends the bytes to each of the attached writers to be written.
|
||||||
|
func (t *MultiWriter) Write(p []byte) (n int, err error) {
|
||||||
|
for name, w := range t.writers {
|
||||||
|
n, err = w.Write(p)
|
||||||
|
if err != nil {
|
||||||
|
// One broken logger should not stop the others.
|
||||||
|
fmt.Fprintf(os.Stderr, "Error logging to '%s': %s", name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if n < len(p) {
|
||||||
|
// One broken logger should not stop the others.
|
||||||
|
err = io.ErrShortWrite
|
||||||
|
fmt.Fprintf(os.Stderr, "Short write logging to '%s': Expected to write %d (%V), wrote %d", name, len(p), w, n)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(p), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the MultiWriter.
|
||||||
|
func (t *MultiWriter) Init() *MultiWriter {
|
||||||
|
t.writers = make(map[string]io.Writer)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writer retrieves a given io.Writer given its name.
|
||||||
|
func (t *MultiWriter) Writer(name string) (io.Writer, bool) {
|
||||||
|
value, found := t.writers[name]
|
||||||
|
return value, found
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writers retrieves a map of all io.Writers keyed by name.
|
||||||
|
func (t *MultiWriter) Writers() map[string]io.Writer {
|
||||||
|
return t.writers
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddWriter adds an io.Writer with an associated name.
|
||||||
|
func (t *MultiWriter) AddWriter(name string, writer io.Writer) {
|
||||||
|
t.writers[name] = writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveWriter removes an io.Writer given a name.
|
||||||
|
func (t *MultiWriter) RemoveWriter(name string) {
|
||||||
|
delete(t.writers, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMultiWriter returns an initialized MultiWriter.
|
||||||
|
func NewMultiWriter() io.Writer {
|
||||||
|
w := new(MultiWriter).Init()
|
||||||
|
return w
|
||||||
|
}
|
@ -6,8 +6,10 @@ import (
|
|||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/cookoo"
|
||||||
"github.com/franela/play-with-docker/services"
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
|
"github.com/twinj/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Echo the data received on the WebSocket.
|
// Echo the data received on the WebSocket.
|
||||||
@ -20,26 +22,41 @@ func Exec(ws *websocket.Conn) {
|
|||||||
session := services.GetSession(sessionId)
|
session := services.GetSession(sessionId)
|
||||||
instance := services.GetInstance(session, instanceId)
|
instance := services.GetInstance(session, instanceId)
|
||||||
|
|
||||||
if instance.ExecId == "" {
|
if instance.Stdout == nil {
|
||||||
execId, err := services.CreateExecConnection(instance.Name, ctx)
|
id, err := services.CreateExecConnection(instance.Name, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
instance.ExecId = execId
|
conn, err := services.AttachExecConnection(id, ctx)
|
||||||
}
|
|
||||||
conn, err := services.AttachExecConnection(instance.ExecId, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instance.Conn = conn
|
||||||
|
instance.Stdout = &cookoo.MultiWriter{}
|
||||||
|
instance.Stdout.Init()
|
||||||
|
u1 := uuid.NewV4()
|
||||||
|
instance.Stdout.AddWriter(u1.String(), ws)
|
||||||
|
go func() {
|
||||||
|
io.Copy(instance.Stdout, instance.Conn.Reader)
|
||||||
|
}()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
go func() {
|
go func() {
|
||||||
io.Copy(ws, conn.Reader)
|
io.Copy(instance.Conn.Conn, ws)
|
||||||
}()
|
}()
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
u1 := uuid.NewV4()
|
||||||
|
instance.Stdout.AddWriter(u1.String(), ws)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
io.Copy(conn.Conn, ws)
|
io.Copy(instance.Conn.Conn, ws)
|
||||||
}()
|
}()
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -60,7 +60,7 @@ func CreateExecConnection(id string, ctx context.Context) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AttachExecConnection(execId string, ctx context.Context) (*types.HijackedResponse, error) {
|
func AttachExecConnection(execId string, ctx context.Context) (*types.HijackedResponse, 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}
|
||||||
conn, err := c.ContainerExecAttach(ctx, execId, conf)
|
conn, err := c.ContainerExecAttach(ctx, execId, conf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/franela/play-with-docker/cookoo"
|
||||||
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Instances map[string]*Instance `json:"instances"`
|
Instances map[string]*Instance `json:"instances"`
|
||||||
@ -8,5 +13,6 @@ 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:"-"`
|
Stdout *cookoo.MultiWriter `json:"-"`
|
||||||
|
Conn *types.HijackedResponse `json:"-"`
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user