stage1: support interactive containers

By using systemd's Standard{Input,Output,Error} options we set
/dev/console in stage1 as the tty for the app (see systemd.exec(5)).
This makes interactive executables like bash work with rkt run (or
prepare+run-prepared).

This is only supported if the container has only one app.
This commit is contained in:
Iago López Galeiras
2015-03-12 20:22:01 +01:00
parent 9a8233e24a
commit 19b1d2414d
7 changed files with 79 additions and 14 deletions
+6 -1
View File
@@ -54,10 +54,15 @@ func ContainerManifestPath(root string) string {
return filepath.Join(root, "container")
}
// AppImagesPath returns the path where the app images live
func AppImagesPath(root string) string {
return filepath.Join(Stage1RootfsPath(root), stage2Dir)
}
// AppImagePath returns the path where an app image (i.e. unpacked ACI) is rooted (i.e.
// where its contents are extracted during stage0), based on the app image ID.
func AppImagePath(root string, imageID types.Hash) string {
return filepath.Join(Stage1RootfsPath(root), stage2Dir, types.ShortHash(imageID.String()))
return filepath.Join(AppImagesPath(root), types.ShortHash(imageID.String()))
}
// AppRootfsPath returns the path to an app's rootfs.
+34 -7
View File
@@ -27,6 +27,7 @@ import (
"github.com/coreos/rocket/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/schema/types"
"github.com/coreos/rocket/common"
"github.com/coreos/rocket/pkg/lock"
)
@@ -662,15 +663,41 @@ func (c *container) getPID() (int, error) {
return c.readIntFromFile("pid")
}
// getDirNames returns the list of names from a container's directory
func (c *container) getDirNames(path string) ([]string, error) {
dir, err := c.openFile(path, syscall.O_RDONLY|syscall.O_DIRECTORY)
if err != nil {
return nil, fmt.Errorf("unable to open directory: %v", err)
}
defer dir.Close()
ld, err := dir.Readdirnames(0)
if err != nil {
return nil, fmt.Errorf("unable to read directory: %v", err)
}
return ld, nil
}
// getAppCount returns the app count of a container. It can only be called on prepared containers.
func (c *container) getAppCount() (int, error) {
if !c.isPrepared {
return -1, fmt.Errorf("error: only prepared containers can get their app count")
}
appsPath := common.AppImagesPath(".")
lapps, err := c.getDirNames(appsPath)
if err != nil {
return -1, fmt.Errorf("error getting the list of names from %q: %v", appsPath, err)
}
return len(lapps), nil
}
// getExitStatuses returns a map of the statuses of the container.
func (c *container) getExitStatuses() (map[string]int, error) {
sdir, err := c.openFile(statusDir, syscall.O_RDONLY|syscall.O_DIRECTORY)
if err != nil {
return nil, fmt.Errorf("unable to open status directory: %v", err)
}
defer sdir.Close()
ls, err := sdir.Readdirnames(0)
ls, err := c.getDirNames(statusDir)
if err != nil {
return nil, fmt.Errorf("unable to read status directory: %v", err)
}
+7
View File
@@ -39,6 +39,7 @@ var (
flagSpawnMetadataService bool
flagInheritEnv bool
flagExplicitEnv envMap
flagInteractive bool
cmdRun = &Command{
Name: "run",
Summary: "Run image(s) in an application container in rocket",
@@ -70,6 +71,7 @@ func init() {
cmdRun.Flags.BoolVar(&flagSpawnMetadataService, "spawn-metadata-svc", false, "launch metadata svc if not running")
cmdRun.Flags.BoolVar(&flagInheritEnv, "inherit-env", false, "inherit all environment variables not set by apps")
cmdRun.Flags.Var(&flagExplicitEnv, "set-env", "an environment variable to set for apps in the form name=value")
cmdRun.Flags.BoolVar(&flagInteractive, "interactive", false, "the container is interactive")
flagVolumes = volumeList{}
}
@@ -174,6 +176,10 @@ func parseAppArgs(args []string) ([][]string, []string, error) {
}
func runRun(args []string) (exit int) {
if flagInteractive && len(args) > 1 {
stderr("run: interactive option only supports one image")
return 1
}
if globalFlags.Dir == "" {
log.Printf("dir unset - using temporary directory")
var err error
@@ -263,6 +269,7 @@ func runRun(args []string) (exit int) {
PrivateNet: flagPrivateNet,
SpawnMetadataService: flagSpawnMetadataService,
LockFd: lfd,
Interactive: flagInteractive,
}
stage0.Run(rcfg, c.path()) // execs, never returns
+14
View File
@@ -43,6 +43,7 @@ func init() {
commands = append(commands, cmdRunPrepared)
cmdRunPrepared.Flags.BoolVar(&flagPrivateNet, "private-net", false, "give container a private network")
cmdRunPrepared.Flags.BoolVar(&flagSpawnMetadataService, "spawn-metadata-svc", false, "launch metadata svc if not running")
cmdRunPrepared.Flags.BoolVar(&flagInteractive, "interactive", false, "the container is interactive")
}
func runRunPrepared(args []string) (exit int) {
@@ -84,6 +85,18 @@ func runRunPrepared(args []string) (exit int) {
return 1
}
if flagInteractive {
ac, err := c.getAppCount()
if err != nil {
stderr("prepared-run: cannot get container's app count: %v", err)
return 1
}
if ac > 1 {
stderr("prepared-run: interactive option only supports containers with one app")
return 1
}
}
if err := c.xToRun(); err != nil {
stderr("prepared-run: cannot transition to run: %v", err)
return 1
@@ -103,6 +116,7 @@ func runRunPrepared(args []string) (exit int) {
PrivateNet: flagPrivateNet,
SpawnMetadataService: flagSpawnMetadataService,
LockFd: lfd,
Interactive: flagInteractive,
}
stage0.Run(rcfg, c.path()) // execs, never returns
return 1
+4
View File
@@ -68,6 +68,7 @@ type RunConfig struct {
PrivateNet bool // container should have its own network stack
SpawnMetadataService bool // launch metadata service
LockFd int // lock file descriptor
Interactive bool // whether the container is interactive or not
}
// configuration shared by both Run and Prepare
@@ -207,6 +208,9 @@ func Run(cfg RunConfig, dir string) {
if cfg.PrivateNet {
args = append(args, "--private-net")
}
if cfg.Interactive {
args = append(args, "--interactive")
}
if err := syscall.Exec(args[0], args, os.Environ()); err != nil {
log.Fatalf("error execing init: %v", err)
}
+9 -3
View File
@@ -112,7 +112,7 @@ func newUnitOption(section, name, value string) *unit.UnitOption {
}
// appToSystemd transforms the provided RuntimeApp+ImageManifest into systemd units
func (c *Container) appToSystemd(ra *schema.RuntimeApp, am *schema.ImageManifest) error {
func (c *Container) appToSystemd(ra *schema.RuntimeApp, am *schema.ImageManifest, interactive bool) error {
name := ra.Name.String()
id := ra.Image.ID
app := am.App
@@ -147,6 +147,12 @@ func (c *Container) appToSystemd(ra *schema.RuntimeApp, am *schema.ImageManifest
newUnitOption("Service", "Group", app.Group),
}
if interactive {
opts = append(opts, newUnitOption("Service", "StandardInput", "tty"))
opts = append(opts, newUnitOption("Service", "StandardOutput", "tty"))
opts = append(opts, newUnitOption("Service", "StandardError", "tty"))
}
for _, eh := range app.EventHandlers {
var typ string
switch eh.Name {
@@ -237,14 +243,14 @@ func (c *Container) writeEnvFile(env types.Environment, id types.Hash) error {
// ContainerToSystemd creates the appropriate systemd service unit files for
// all the constituent apps of the Container
func (c *Container) ContainerToSystemd() error {
func (c *Container) ContainerToSystemd(interactive bool) error {
for _, am := range c.Apps {
ra := c.Manifest.Apps.Get(am.Name)
if ra == nil {
// should never happen
panic("app not found in container manifest")
}
if err := c.appToSystemd(ra, am); err != nil {
if err := c.appToSystemd(ra, am, interactive); err != nil {
return fmt.Errorf("failed to transform app %q into systemd service: %v", am.Name, err)
}
}
+5 -3
View File
@@ -76,13 +76,15 @@ func mirrorLocalZoneInfo(root string) {
}
var (
debug bool
privNet bool
debug bool
privNet bool
interactive bool
)
func init() {
flag.BoolVar(&debug, "debug", false, "Run in debug mode")
flag.BoolVar(&privNet, "private-net", false, "Setup private network (WIP!)")
flag.BoolVar(&interactive, "interactive", false, "The container is interactive")
// this ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
@@ -101,7 +103,7 @@ func stage1() int {
mirrorLocalZoneInfo(c.Root)
c.MetadataServiceURL = common.MetadataServicePublicURL()
if err = c.ContainerToSystemd(); err != nil {
if err = c.ContainerToSystemd(interactive); err != nil {
fmt.Fprintf(os.Stderr, "Failed to configure systemd: %v\n", err)
return 2
}