Add console pkg and update go-runc

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-01-26 10:33:33 -08:00
parent 07c81ccac4
commit 8e5e9ae70e
7 changed files with 279 additions and 47 deletions

View file

@ -3,22 +3,26 @@ package runc
import (
"fmt"
"net"
"os"
"path/filepath"
"github.com/docker/docker/pkg/term"
"github.com/crosbymichael/console"
"github.com/opencontainers/runc/libcontainer/utils"
)
// NewConsoleSocket creates a new unix socket at the provided path to accept a
// pty master created by runc for use by the container
func NewConsoleSocket(path string) (*ConsoleSocket, error) {
l, err := net.Listen("unix", path)
abs, err := filepath.Abs(path)
if err != nil {
return nil, err
}
l, err := net.Listen("unix", abs)
if err != nil {
return nil, err
}
return &ConsoleSocket{
l: l,
path: path,
path: abs,
}, nil
}
@ -34,7 +38,7 @@ func (c *ConsoleSocket) Path() string {
}
// ReceiveMaster blocks until the socket receives the pty master
func (c *ConsoleSocket) ReceiveMaster() (*Console, error) {
func (c *ConsoleSocket) ReceiveMaster() (console.Console, error) {
conn, err := c.l.Accept()
if err != nil {
return nil, err
@ -52,9 +56,7 @@ func (c *ConsoleSocket) ReceiveMaster() (*Console, error) {
if err != nil {
return nil, err
}
return &Console{
master: f,
}, nil
return console.ConsoleFromFile(f)
}
// Close closes the unix socket
@ -69,31 +71,3 @@ type WinSize struct {
// Height of the console
Height uint16
}
// Console is a pty master
type Console struct {
master *os.File
}
// Read from the console
func (c *Console) Read(b []byte) (int, error) {
return c.master.Read(b)
}
// Write writes to the console
func (c *Console) Write(b []byte) (int, error) {
return c.master.Write(b)
}
// Resize the console
func (c *Console) Resize(ws WinSize) error {
return term.SetWinsize(c.master.Fd(), &term.Winsize{
Width: ws.Width,
Height: ws.Height,
})
}
// Close the console
func (c *Console) Close() error {
return c.master.Close()
}

View file

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"time"
@ -73,9 +74,13 @@ type CreateOpts struct {
NoNewKeyring bool
}
func (o *CreateOpts) args() (out []string) {
func (o *CreateOpts) args() (out []string, err error) {
if o.PidFile != "" {
out = append(out, "--pid-file", o.PidFile)
abs, err := filepath.Abs(o.PidFile)
if err != nil {
return nil, err
}
out = append(out, "--pid-file", abs)
}
if o.ConsoleSocket != nil {
out = append(out, "--console-socket", o.ConsoleSocket.Path())
@ -89,17 +94,21 @@ func (o *CreateOpts) args() (out []string) {
if o.Detach {
out = append(out, "--detach")
}
return out
return out, nil
}
// Create creates a new container and returns its pid if it was created successfully
func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOpts) error {
args := []string{"create", "--bundle", bundle}
if opts != nil {
args = append(args, opts.args()...)
oargs, err := opts.args()
if err != nil {
return err
}
args = append(args, oargs...)
}
cmd := r.command(context, append(args, id)...)
if opts != nil {
if opts != nil && opts.IO != nil {
opts.Set(cmd)
}
if cmd.Stdout == nil && cmd.Stderr == nil {
@ -138,7 +147,7 @@ type ExecOpts struct {
Detach bool
}
func (o *ExecOpts) args() (out []string) {
func (o *ExecOpts) args() (out []string, err error) {
out = append(out, "--user", fmt.Sprintf("%d:%d", o.Uid, o.Gid))
if o.Tty {
out = append(out, "--tty")
@ -153,9 +162,13 @@ func (o *ExecOpts) args() (out []string) {
out = append(out, "--detach")
}
if o.PidFile != "" {
out = append(out, "--pid-file", o.PidFile)
abs, err := filepath.Abs(o.PidFile)
if err != nil {
return nil, err
}
out = append(out, "--pid-file", abs)
}
return out
return out, nil
}
// Exec executres and additional process inside the container based on a full
@ -173,7 +186,11 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
}
args := []string{"exec", "--process", f.Name()}
if opts != nil {
args = append(args, opts.args()...)
oargs, err := opts.args()
if err != nil {
return err
}
args = append(args, oargs...)
}
cmd := r.command(context, append(args, id)...)
if opts != nil {
@ -187,7 +204,11 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts) (int, error) {
args := []string{"run", "--bundle", bundle}
if opts != nil {
args = append(args, opts.args()...)
oargs, err := opts.args()
if err != nil {
return -1, err
}
args = append(args, oargs...)
}
cmd := r.command(context, append(args, id)...)
if opts != nil {