From 967a87a65f16c08994cc10944cc0d87e425df921 Mon Sep 17 00:00:00 2001 From: Stanislav Pavlovichev Date: Sat, 13 Oct 2018 08:33:53 +0300 Subject: [PATCH] Exec using API --- Gopkg.lock | 76 +++++++++++++++++++++++++++++++++---- connector/manager/docker.go | 23 +++++++++++ connector/manager/main.go | 1 + connector/manager/mock.go | 4 ++ connector/manager/runc.go | 4 ++ container/main.go | 4 ++ menus.go | 10 +---- 7 files changed, 105 insertions(+), 17 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 8d7a4ce..515b296 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -3,7 +3,10 @@ [[projects]] name = "github.com/Azure/go-ansiterm" - packages = [".","winterm"] + packages = [ + ".", + "winterm" + ] revision = "fa152c58bc15761d0200cb75fe958b89a9d4888e" [[projects]] @@ -36,13 +39,44 @@ [[projects]] name = "github.com/coreos/go-systemd" - packages = ["dbus","util"] + packages = [ + "dbus", + "util" + ] revision = "b4a58d95188dd092ae20072bac14cece0e67c388" version = "v4" [[projects]] name = "github.com/docker/docker" - packages = ["api/types","api/types/blkiodev","api/types/container","api/types/filters","api/types/mount","api/types/network","api/types/registry","api/types/strslice","api/types/swarm","api/types/versions","opts","pkg/archive","pkg/fileutils","pkg/homedir","pkg/idtools","pkg/ioutils","pkg/jsonlog","pkg/jsonmessage","pkg/longpath","pkg/mount","pkg/pools","pkg/promise","pkg/stdcopy","pkg/symlink","pkg/system","pkg/term","pkg/term/windows"] + packages = [ + "api/types", + "api/types/blkiodev", + "api/types/container", + "api/types/filters", + "api/types/mount", + "api/types/network", + "api/types/registry", + "api/types/strslice", + "api/types/swarm", + "api/types/versions", + "opts", + "pkg/archive", + "pkg/fileutils", + "pkg/homedir", + "pkg/idtools", + "pkg/ioutils", + "pkg/jsonlog", + "pkg/jsonmessage", + "pkg/longpath", + "pkg/mount", + "pkg/pools", + "pkg/promise", + "pkg/stdcopy", + "pkg/symlink", + "pkg/system", + "pkg/term", + "pkg/term/windows" + ] revision = "90d35abf7b3535c1c319c872900fbd76374e521c" version = "v17.05.0-ce-rc3" @@ -128,7 +162,24 @@ [[projects]] name = "github.com/opencontainers/runc" - packages = ["libcontainer","libcontainer/apparmor","libcontainer/cgroups","libcontainer/cgroups/fs","libcontainer/cgroups/systemd","libcontainer/configs","libcontainer/configs/validate","libcontainer/criurpc","libcontainer/keys","libcontainer/label","libcontainer/seccomp","libcontainer/selinux","libcontainer/stacktrace","libcontainer/system","libcontainer/user","libcontainer/utils"] + packages = [ + "libcontainer", + "libcontainer/apparmor", + "libcontainer/cgroups", + "libcontainer/cgroups/fs", + "libcontainer/cgroups/systemd", + "libcontainer/configs", + "libcontainer/configs/validate", + "libcontainer/criurpc", + "libcontainer/keys", + "libcontainer/label", + "libcontainer/seccomp", + "libcontainer/selinux", + "libcontainer/stacktrace", + "libcontainer/system", + "libcontainer/user", + "libcontainer/utils" + ] revision = "baf6536d6259209c3edfa2b22237af82942d3dfa" version = "v0.1.1" @@ -144,22 +195,31 @@ [[projects]] name = "github.com/vishvananda/netlink" - packages = [".","nl"] + packages = [ + ".", + "nl" + ] revision = "1e2e08e8a2dcdacaae3f14ac44c5cfa31361f270" [[projects]] name = "golang.org/x/net" - packages = ["context","context/ctxhttp"] + packages = [ + "context", + "context/ctxhttp" + ] revision = "a6577fac2d73be281a500b310739095313165611" [[projects]] name = "golang.org/x/sys" - packages = ["unix","windows"] + packages = [ + "unix", + "windows" + ] revision = "99f16d856c9836c42d24e7ab64ea72916925fa97" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "a21d4c707f08f26de894adbdd00d5d6e82a54f5e0f52566229dd2da9b94e26ec" + inputs-digest = "f46f5c696ecb0b0c42a38dac512df21fc1f5fb2bfda888434e005e69d1b6273b" solver-name = "gps-cdcl" solver-version = 1 diff --git a/connector/manager/docker.go b/connector/manager/docker.go index 77dc987..0f66298 100644 --- a/connector/manager/docker.go +++ b/connector/manager/docker.go @@ -3,6 +3,7 @@ package manager import ( "fmt" api "github.com/fsouza/go-dockerclient" + "os" ) type Docker struct { @@ -17,6 +18,28 @@ func NewDocker(client *api.Client, id string) *Docker { } } +func (dc *Docker) Exec(cmd []string) error { + execCmd, err := dc.client.CreateExec(api.CreateExecOptions{ + AttachStdin: true, + AttachStdout: true, + AttachStderr: true, + Cmd: cmd, + Container: dc.id, + Tty: true, + }) + + if err != nil { + return err + } + + return dc.client.StartExec(execCmd.ID, api.StartExecOptions{ + InputStream: os.Stdin, + OutputStream: os.Stdout, + ErrorStream: os.Stderr, + RawTerminal: true, + }) +} + func (dc *Docker) Start() error { c, err := dc.client.InspectContainer(dc.id) if err != nil { diff --git a/connector/manager/main.go b/connector/manager/main.go index b6debaa..f65aad3 100644 --- a/connector/manager/main.go +++ b/connector/manager/main.go @@ -7,4 +7,5 @@ type Manager interface { Pause() error Unpause() error Restart() error + Exec(cmd []string) error } diff --git a/connector/manager/mock.go b/connector/manager/mock.go index f33fd77..f6fd62f 100644 --- a/connector/manager/mock.go +++ b/connector/manager/mock.go @@ -29,3 +29,7 @@ func (m *Mock) Unpause() error { func (m *Mock) Restart() error { return nil } + +func (m *Mock) Exec(cmd []string) error { + return nil +} diff --git a/connector/manager/runc.go b/connector/manager/runc.go index cf61f14..07a4b58 100644 --- a/connector/manager/runc.go +++ b/connector/manager/runc.go @@ -29,3 +29,7 @@ func (rc *Runc) Unpause() error { func (rc *Runc) Restart() error { return nil } + +func (rc *Runc) Exec(cmd []string) error { + return nil +} diff --git a/container/main.go b/container/main.go index 586dd94..735fff1 100644 --- a/container/main.go +++ b/container/main.go @@ -149,3 +149,7 @@ func (c *Container) Restart() { } } } + +func (c *Container) Exec(cmd []string) error { + return c.manager.Exec(cmd) +} diff --git a/menus.go b/menus.go index aa445c3..9ed7542 100644 --- a/menus.go +++ b/menus.go @@ -2,8 +2,6 @@ package main import ( "fmt" - "os" - "os/exec" "time" "github.com/bcicen/ctop/config" @@ -225,13 +223,7 @@ func ExecSh() MenuFn { ui.StopLoop() defer ui.Loop() - // Reset colors && clear screen && run sh - cmdName := fmt.Sprintf("echo '\033[0m' && clear && docker exec -it %s sh", c.GetMeta("name")) - cmd := exec.Command("bash", "-c", cmdName) - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - cmd.Run() + c.Exec([]string{"sh", "-c", "echo '\033[0m' && clear && sh"}) return nil }