Exec using API

This commit is contained in:
Stanislav Pavlovichev 2018-10-13 08:33:53 +03:00
parent e68f7ba96a
commit 967a87a65f
7 changed files with 105 additions and 17 deletions

76
Gopkg.lock generated
View File

@ -3,7 +3,10 @@
[[projects]] [[projects]]
name = "github.com/Azure/go-ansiterm" name = "github.com/Azure/go-ansiterm"
packages = [".","winterm"] packages = [
".",
"winterm"
]
revision = "fa152c58bc15761d0200cb75fe958b89a9d4888e" revision = "fa152c58bc15761d0200cb75fe958b89a9d4888e"
[[projects]] [[projects]]
@ -36,13 +39,44 @@
[[projects]] [[projects]]
name = "github.com/coreos/go-systemd" name = "github.com/coreos/go-systemd"
packages = ["dbus","util"] packages = [
"dbus",
"util"
]
revision = "b4a58d95188dd092ae20072bac14cece0e67c388" revision = "b4a58d95188dd092ae20072bac14cece0e67c388"
version = "v4" version = "v4"
[[projects]] [[projects]]
name = "github.com/docker/docker" 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" revision = "90d35abf7b3535c1c319c872900fbd76374e521c"
version = "v17.05.0-ce-rc3" version = "v17.05.0-ce-rc3"
@ -128,7 +162,24 @@
[[projects]] [[projects]]
name = "github.com/opencontainers/runc" 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" revision = "baf6536d6259209c3edfa2b22237af82942d3dfa"
version = "v0.1.1" version = "v0.1.1"
@ -144,22 +195,31 @@
[[projects]] [[projects]]
name = "github.com/vishvananda/netlink" name = "github.com/vishvananda/netlink"
packages = [".","nl"] packages = [
".",
"nl"
]
revision = "1e2e08e8a2dcdacaae3f14ac44c5cfa31361f270" revision = "1e2e08e8a2dcdacaae3f14ac44c5cfa31361f270"
[[projects]] [[projects]]
name = "golang.org/x/net" name = "golang.org/x/net"
packages = ["context","context/ctxhttp"] packages = [
"context",
"context/ctxhttp"
]
revision = "a6577fac2d73be281a500b310739095313165611" revision = "a6577fac2d73be281a500b310739095313165611"
[[projects]] [[projects]]
name = "golang.org/x/sys" name = "golang.org/x/sys"
packages = ["unix","windows"] packages = [
"unix",
"windows"
]
revision = "99f16d856c9836c42d24e7ab64ea72916925fa97" revision = "99f16d856c9836c42d24e7ab64ea72916925fa97"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "a21d4c707f08f26de894adbdd00d5d6e82a54f5e0f52566229dd2da9b94e26ec" inputs-digest = "f46f5c696ecb0b0c42a38dac512df21fc1f5fb2bfda888434e005e69d1b6273b"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -3,6 +3,7 @@ package manager
import ( import (
"fmt" "fmt"
api "github.com/fsouza/go-dockerclient" api "github.com/fsouza/go-dockerclient"
"os"
) )
type Docker struct { 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 { func (dc *Docker) Start() error {
c, err := dc.client.InspectContainer(dc.id) c, err := dc.client.InspectContainer(dc.id)
if err != nil { if err != nil {

View File

@ -7,4 +7,5 @@ type Manager interface {
Pause() error Pause() error
Unpause() error Unpause() error
Restart() error Restart() error
Exec(cmd []string) error
} }

View File

@ -29,3 +29,7 @@ func (m *Mock) Unpause() error {
func (m *Mock) Restart() error { func (m *Mock) Restart() error {
return nil return nil
} }
func (m *Mock) Exec(cmd []string) error {
return nil
}

View File

@ -29,3 +29,7 @@ func (rc *Runc) Unpause() error {
func (rc *Runc) Restart() error { func (rc *Runc) Restart() error {
return nil return nil
} }
func (rc *Runc) Exec(cmd []string) error {
return nil
}

View File

@ -149,3 +149,7 @@ func (c *Container) Restart() {
} }
} }
} }
func (c *Container) Exec(cmd []string) error {
return c.manager.Exec(cmd)
}

View File

@ -2,8 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"os"
"os/exec"
"time" "time"
"github.com/bcicen/ctop/config" "github.com/bcicen/ctop/config"
@ -225,13 +223,7 @@ func ExecSh() MenuFn {
ui.StopLoop() ui.StopLoop()
defer ui.Loop() defer ui.Loop()
// Reset colors && clear screen && run sh c.Exec([]string{"sh", "-c", "echo '\033[0m' && clear && 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()
return nil return nil
} }