Refactor large funcs
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
b48bc85967
commit
e3d5adc9e2
5 changed files with 107 additions and 96 deletions
|
@ -1,14 +1,13 @@
|
||||||
package libcontainer
|
package libcontainer
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
ID string `json:"id,omitempty"`
|
Hostname string `json:"hostname,omitempty"`
|
||||||
Command *Command `json:"command,omitempty"`
|
|
||||||
ReadonlyFs bool `json:"readonly_fs,omitempty"`
|
ReadonlyFs bool `json:"readonly_fs,omitempty"`
|
||||||
User string `json:"user,omitempty"`
|
User string `json:"user,omitempty"`
|
||||||
WorkingDir string `json:"working_dir,omitempty"`
|
WorkingDir string `json:"working_dir,omitempty"`
|
||||||
|
Command *Command `json:"command,omitempty"`
|
||||||
Namespaces Namespaces `json:"namespaces,omitempty"`
|
Namespaces Namespaces `json:"namespaces,omitempty"`
|
||||||
Capabilities Capabilities `json:"capabilities,omitempty"`
|
Capabilities Capabilities `json:"capabilities,omitempty"`
|
||||||
LogFile string `json:"log_file,omitempty"`
|
|
||||||
Network *Network `json:"network,omitempty"`
|
Network *Network `json:"network,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
{
|
{
|
||||||
"id": "koye",
|
"id": "koye",
|
||||||
"log_file": "/root/logs",
|
|
||||||
"command": {
|
"command": {
|
||||||
"args": [
|
"args": [
|
||||||
"/bin/bash"
|
"/bin/bash"
|
||||||
|
@ -9,7 +8,7 @@
|
||||||
"HOME=/",
|
"HOME=/",
|
||||||
"PATH=PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin",
|
"PATH=PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin",
|
||||||
"container=docker",
|
"container=docker",
|
||||||
"TERM=xterm"
|
"TERM=xterm-256color"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"namespaces": [
|
"namespaces": [
|
||||||
|
|
|
@ -27,6 +27,8 @@ func execCommand(container *libcontainer.Container) (int, error) {
|
||||||
Cloneflags: uintptr(getNamespaceFlags(container.Namespaces) | syscall.CLONE_VFORK), // we need CLONE_VFORK so we can wait on the child
|
Cloneflags: uintptr(getNamespaceFlags(container.Namespaces) | syscall.CLONE_VFORK), // we need CLONE_VFORK so we can wait on the child
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create a pipe so that we can syncronize with the namespaced process and
|
||||||
|
// pass the veth name to the child
|
||||||
inPipe, err := command.StdinPipe()
|
inPipe, err := command.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
|
@ -39,34 +41,17 @@ func execCommand(container *libcontainer.Container) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if container.Network != nil {
|
if container.Network != nil {
|
||||||
name1, name2, err := createVethPair()
|
vethPair, err := setupVeth(container.Network.Bridge, command.Process.Pid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
if err := network.SetInterfaceMaster(name1, container.Network.Bridge); err != nil {
|
sendVethName(vethPair, inPipe)
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
if err := network.InterfaceUp(name1); err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
if err := network.SetInterfaceInNamespacePid(name2, command.Process.Pid); err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
fmt.Fprint(inPipe, name2)
|
|
||||||
inPipe.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go io.Copy(os.Stdout, master)
|
go io.Copy(os.Stdout, master)
|
||||||
go io.Copy(master, os.Stdin)
|
go io.Copy(master, os.Stdin)
|
||||||
|
|
||||||
ws, err := term.GetWinsize(os.Stdin.Fd())
|
state, err := setupWindow(master)
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
if err := term.SetWinsize(master.Fd(), ws); err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
command.Process.Kill()
|
command.Process.Kill()
|
||||||
return -1, err
|
return -1, err
|
||||||
|
@ -81,6 +66,41 @@ func execCommand(container *libcontainer.Container) (int, error) {
|
||||||
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendVethName(name string, pipe io.WriteCloser) {
|
||||||
|
// write the veth pair name to the child's stdin then close the
|
||||||
|
// pipe so that the child stops waiting
|
||||||
|
fmt.Fprint(pipe, name)
|
||||||
|
pipe.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupVeth(bridge string, nspid int) (string, error) {
|
||||||
|
name1, name2, err := createVethPair()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := network.SetInterfaceMaster(name1, bridge); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := network.InterfaceUp(name1); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := network.SetInterfaceInNamespacePid(name2, nspid); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return name2, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupWindow(master *os.File) (*term.State, error) {
|
||||||
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := term.SetWinsize(master.Fd(), ws); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return term.SetRawTerminal(os.Stdin.Fd())
|
||||||
|
}
|
||||||
|
|
||||||
func createMasterAndConsole() (*os.File, string, error) {
|
func createMasterAndConsole() (*os.File, string, error) {
|
||||||
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -9,17 +9,12 @@ import (
|
||||||
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
||||||
"github.com/dotcloud/docker/pkg/system"
|
"github.com/dotcloud/docker/pkg/system"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCommand(container *libcontainer.Container, console string) error {
|
func initCommand(container *libcontainer.Container, console string) error {
|
||||||
if err := setLogFile(container); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rootfs, err := resolveRootfs()
|
rootfs, err := resolveRootfs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -27,11 +22,10 @@ func initCommand(container *libcontainer.Container, console string) error {
|
||||||
|
|
||||||
var tempVethName string
|
var tempVethName string
|
||||||
if container.Network != nil {
|
if container.Network != nil {
|
||||||
data, err := ioutil.ReadAll(os.Stdin)
|
tempVethName, err = getVethName()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading from stdin %s", err)
|
return err
|
||||||
}
|
}
|
||||||
tempVethName = string(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// close pipes so that we can replace it with the pty
|
// close pipes so that we can replace it with the pty
|
||||||
|
@ -61,13 +55,10 @@ func initCommand(container *libcontainer.Container, console string) error {
|
||||||
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
|
||||||
return fmt.Errorf("setup mount namespace %s", err)
|
return fmt.Errorf("setup mount namespace %s", err)
|
||||||
}
|
}
|
||||||
if container.Network != nil {
|
|
||||||
if err := setupNetworking(container.Network, tempVethName); err != nil {
|
if err := setupNetworking(container.Network, tempVethName); err != nil {
|
||||||
return fmt.Errorf("setup networking %s", err)
|
return fmt.Errorf("setup networking %s", err)
|
||||||
}
|
}
|
||||||
}
|
if err := system.Sethostname(container.Hostname); err != nil {
|
||||||
|
|
||||||
if err := system.Sethostname(container.ID); err != nil {
|
|
||||||
return fmt.Errorf("sethostname %s", err)
|
return fmt.Errorf("sethostname %s", err)
|
||||||
}
|
}
|
||||||
if err := capabilities.DropCapabilities(container); err != nil {
|
if err := capabilities.DropCapabilities(container); err != nil {
|
||||||
|
@ -136,18 +127,8 @@ func openTerminal(name string, flag int) (*os.File, error) {
|
||||||
return os.NewFile(uintptr(r), name), nil
|
return os.NewFile(uintptr(r), name), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setLogFile(container *libcontainer.Container) error {
|
|
||||||
if container.LogFile != "" {
|
|
||||||
f, err := os.OpenFile(container.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0655)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.SetOutput(f)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupNetworking(config *libcontainer.Network, tempVethName string) error {
|
func setupNetworking(config *libcontainer.Network, tempVethName string) error {
|
||||||
|
if config != nil {
|
||||||
if err := network.InterfaceDown(tempVethName); err != nil {
|
if err := network.InterfaceDown(tempVethName); err != nil {
|
||||||
return fmt.Errorf("interface down %s %s", tempVethName, err)
|
return fmt.Errorf("interface down %s %s", tempVethName, err)
|
||||||
}
|
}
|
||||||
|
@ -174,5 +155,17 @@ func setupNetworking(config *libcontainer.Network, tempVethName string) error {
|
||||||
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
|
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getVethName reads from Stdin the temp veth name
|
||||||
|
// sent by the parent processes after the veth pair
|
||||||
|
// has been created and setup
|
||||||
|
func getVethName() (string, error) {
|
||||||
|
data, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error reading from stdin %s", err)
|
||||||
|
}
|
||||||
|
return string(data), nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,29 +1,5 @@
|
||||||
package libcontainer
|
package libcontainer
|
||||||
|
|
||||||
type Namespace string
|
|
||||||
type Namespaces []Namespace
|
|
||||||
|
|
||||||
func (n Namespaces) Contains(ns Namespace) bool {
|
|
||||||
for _, nns := range n {
|
|
||||||
if nns == ns {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
type Capability string
|
|
||||||
type Capabilities []Capability
|
|
||||||
|
|
||||||
func (c Capabilities) Contains(capp Capability) bool {
|
|
||||||
for _, cc := range c {
|
|
||||||
if cc == capp {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CAP_SETPCAP Capability = "SETPCAP"
|
CAP_SETPCAP Capability = "SETPCAP"
|
||||||
CAP_SYS_MODULE Capability = "SYS_MODULE"
|
CAP_SYS_MODULE Capability = "SYS_MODULE"
|
||||||
|
@ -47,3 +23,27 @@ const (
|
||||||
CLONE_NEWPID Namespace = "NEWPID" // pid
|
CLONE_NEWPID Namespace = "NEWPID" // pid
|
||||||
CLONE_NEWNET Namespace = "NEWNET" // network
|
CLONE_NEWNET Namespace = "NEWNET" // network
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Namespace string
|
||||||
|
type Namespaces []Namespace
|
||||||
|
|
||||||
|
func (n Namespaces) Contains(ns Namespace) bool {
|
||||||
|
for _, nns := range n {
|
||||||
|
if nns == ns {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type Capability string
|
||||||
|
type Capabilities []Capability
|
||||||
|
|
||||||
|
func (c Capabilities) Contains(capp Capability) bool {
|
||||||
|
for _, cc := range c {
|
||||||
|
if cc == capp {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue