Create some util funcs that are common between start and exec

and it'll really help my start/create PR when I need to rebase  :-)

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2016-02-04 17:58:46 -08:00
parent bfd3345be9
commit ad26ef1afc
3 changed files with 73 additions and 73 deletions

31
exec.go
View File

@ -9,7 +9,6 @@ import (
"path" "path"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
@ -71,39 +70,19 @@ func execProcess(context *cli.Context) (int, error) {
if err != nil { if err != nil {
return -1, err return -1, err
} }
var ( var (
detach = context.Bool("detach") detach = context.Bool("detach")
rootfs = container.Config().Rootfs rootfs = container.Config().Rootfs
) )
rootuid, err := container.Config().HostUID()
if err != nil {
return -1, err
}
p, err := getProcess(context, path.Dir(rootfs)) p, err := getProcess(context, path.Dir(rootfs))
if err != nil { if err != nil {
return -1, err return -1, err
} }
process := newProcess(*p)
tty, err := setupIO(process, rootuid, context.String("console"), p.Terminal, detach) return runProcess(container, p, nil, context.String("console"), context.String("pid-file"), detach)
if err != nil {
return -1, err
}
if err := container.Start(process); err != nil {
return -1, err
}
if pidFile := context.String("pid-file"); pidFile != "" {
if err := createPidFile(pidFile, process); err != nil {
process.Signal(syscall.SIGKILL)
process.Wait()
return -1, err
}
}
if detach {
return 0, nil
}
handler := newSignalHandler(tty)
defer handler.Close()
return handler.forward(process)
} }
func getProcess(context *cli.Context, bundle string) (*specs.Process, error) { func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {

View File

@ -3,10 +3,8 @@
package main package main
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"syscall"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
@ -88,61 +86,23 @@ func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
if id == "" { if id == "" {
return -1, errEmptyID return -1, errEmptyID
} }
config, err := createLibcontainerConfig(id, spec) container, err := createContainer(context, id, spec)
if err != nil {
return -1, err
}
if _, err := os.Stat(config.Rootfs); err != nil {
if os.IsNotExist(err) {
return -1, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs)
}
return -1, err
}
rootuid, err := config.HostUID()
if err != nil {
return -1, err
}
factory, err := loadFactory(context)
if err != nil {
return -1, err
}
container, err := factory.Create(id, config)
if err != nil { if err != nil {
return -1, err return -1, err
} }
// ensure that the container is always removed if we were the process // ensure that the container is always removed if we were the process
// that created it. // that created it.
detach := context.Bool("detach") detach := context.Bool("detach")
if !detach { if !detach {
defer destroy(container) defer destroy(container)
} }
process := newProcess(spec.Process)
// Support on-demand socket activation by passing file descriptors into the container init process. // Support on-demand socket activation by passing file descriptors into the container init process.
listenFDs := []*os.File{}
if os.Getenv("LISTEN_FDS") != "" { if os.Getenv("LISTEN_FDS") != "" {
listenFds := activation.Files(false) listenFDs = activation.Files(false)
if len(listenFds) > 0 {
process.Env = append(process.Env, fmt.Sprintf("LISTEN_FDS=%d", len(listenFds)), "LISTEN_PID=1")
process.ExtraFiles = append(process.ExtraFiles, listenFds...)
}
} }
tty, err := setupIO(process, rootuid, context.String("console"), spec.Process.Terminal, detach)
if err != nil { return runProcess(container, &spec.Process, listenFDs, context.String("console"), context.String("pid-file"), detach)
return -1, err
}
if err := container.Start(process); err != nil {
return -1, err
}
if pidFile := context.String("pid-file"); pidFile != "" {
if err := createPidFile(pidFile, process); err != nil {
process.Signal(syscall.SIGKILL)
process.Wait()
return -1, err
}
}
if detach {
return 0, nil
}
handler := newSignalHandler(tty)
defer handler.Close()
return handler.forward(process)
} }

View File

@ -265,3 +265,64 @@ func createPidFile(path string, process *libcontainer.Process) error {
_, err = fmt.Fprintf(f, "%d", pid) _, err = fmt.Fprintf(f, "%d", pid)
return err return err
} }
func createContainer(context *cli.Context, id string, spec *specs.LinuxSpec) (libcontainer.Container, error) {
config, err := createLibcontainerConfig(id, spec)
if err != nil {
return nil, err
}
if _, err := os.Stat(config.Rootfs); err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs)
}
return nil, err
}
factory, err := loadFactory(context)
if err != nil {
return nil, err
}
return factory.Create(id, config)
}
// runProcess will create a new process in the specified container
// by executing the process specified in the 'config'.
func runProcess(container libcontainer.Container, config *specs.Process, listenFDs []*os.File, console string, pidFile string, detach bool) (int, error) {
process := newProcess(*config)
// Add extra file descriptors if needed
if len(listenFDs) > 0 {
process.Env = append(process.Env, fmt.Sprintf("LISTEN_FDS=%d", len(listenFDs)), "LISTEN_PID=1")
process.ExtraFiles = append(process.ExtraFiles, listenFDs...)
}
rootuid, err := container.Config().HostUID()
if err != nil {
return -1, err
}
tty, err := setupIO(process, rootuid, console, config.Terminal, detach)
if err != nil {
return -1, err
}
if err := container.Start(process); err != nil {
return -1, err
}
if pidFile != "" {
if err := createPidFile(pidFile, process); err != nil {
process.Signal(syscall.SIGKILL)
process.Wait()
return -1, err
}
}
if detach {
return 0, nil
}
handler := newSignalHandler(tty)
defer handler.Close()
return handler.forward(process)
}