8000 rkt/app: multiple bugfixes by lucab · Pull Request #3405 · rkt/rkt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 24, 2020. It is now read-only.

rkt/app: multiple bugfixes #3405

Merged
merged 5 commits into from
Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"github.com/appc/spec/schema/types"
)

const DefaultPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

var defaultEnv = map[string]string{
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PATH": DefaultPath,
"SHELL": "/bin/sh",
"USER": "root",
"LOGNAME": "root",
Expand Down
16 changes: 16 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,29 @@ func (l *Logger) Error(e error) {
l.Print(l.formatErr(e, ""))
}

// Errorf is a convenience function for formatting and printing errors.
func (l *Logger) Errorf(format string, a ...interface{}) {
l.Print(l.formatErr(fmt.Errorf(format, a), ""))
}

// FatalE prints a string and error then calls os.Exit(254).
func (l *Logger) FatalE(msg string, e error) {
l.Print(l.formatErr(e, msg))
os.Exit(254)
}

// Fatalf prints an error then calls os.Exit(254).
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.Print(l.formatErr(fmt.Errorf(format, a), ""))
os.Exit(254)
}

// PanicE prints a string and error then calls panic.
func (l *Logger) PanicE(msg string, e error) {
l.Panic(l.formatErr(e, msg))
}

// Panicf prints an error then calls panic.
func (l *Logger) Panicf(format string, a ...interface{}) {
l.Panic(l.formatErr(fmt.Errorf(format, a), ""))
}
63 changes: 48 additions & 15 deletions stage0/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,15 @@ func AddApp(cfg AddConfig) error {
return err
}

if err := RunCrossingEntrypoint(cfg.PodPath, cfg.PodPID, appName.String(), appAddEntrypoint, args); err != nil {
ce := CrossingEntrypoint{
PodPath: cfg.PodPath,
PodPID: cfg.PodPID,
AppName: appName.String(),
EntrypointName: appAddEntrypoint,
EntrypointArgs: args,
Interactive: false,
}
if err := ce.Run(); err != nil {
return err
}

Expand Down Expand Up @@ -364,11 +372,6 @@ func RmApp(cfg RmConfig) error {
return fmt.Errorf("error: nonexistent app %q", *cfg.AppName)
}

treeStoreID, err := ioutil.ReadFile(common.AppTreeStoreIDPath(cfg.PodPath, *cfg.AppName))
if err != nil {
return err
}

if cfg.PodPID > 0 {
// Call app-stop and app-rm entrypoint only if the pod is still running.
// Otherwise, there's not much we can do about it except unmounting/removing
Expand All @@ -377,7 +380,15 @@ func RmApp(cfg RmConfig) error {
fmt.Sprintf("--app=%s", cfg.AppName),
}

if err := RunCrossingEntrypoint(cfg.PodPath, cfg.PodPID, cfg.AppName.String(), appStopEntrypoint, args); err != nil {
ce := CrossingEntrypoint{
PodPath: cfg.PodPath,
PodPID: cfg.PodPID,
AppName: cfg.AppName.String(),
EntrypointName: appStopEntrypoint,
EntrypointArgs: args,
Interactive: false,
}
if err := ce.Run(); err != nil {
status, err := common.GetExitStatus(err)
// ignore nonexistent units failing to stop. Exit status 5
// comes from systemctl and means the unit doesn't exist
Expand All @@ -388,17 +399,18 @@ func RmApp(cfg RmConfig) error {
}
}

if err := RunCrossingEntrypoint(cfg.PodPath, cfg.PodPID, cfg.AppName.String(), appRmEntrypoint, args); err != nil {
ce.EntrypointName = appRmEntrypoint
if err := ce.Run(); err != nil {
return err
}
}

appInfoDir := common.AppInfoPath(cfg.PodPath, *cfg.AppName)
if err := os.RemoveAll(appInfoDir); err != nil {
return errwrap.Wrap(errors.New("error removing app info directory"), err)
}

if cfg.UsesOverlay {
treeStoreID, err := ioutil.ReadFile(common.AppTreeStoreIDPath(cfg.PodPath, *cfg.AppName))
if err != nil {
return err
}

appRootfs := common.AppRootfsPath(cfg.PodPath, *cfg.AppName)
if err := syscall.Unmount(appRootfs, 0); err != nil {
return err
Expand All @@ -410,6 +422,11 @@ func RmApp(cfg RmConfig) error {
}
}

appInfoDir := common.AppInfoPath(cfg.PodPath, *cfg.AppName)
if err := os.RemoveAll(appInfoDir); err != nil {
return errwrap.Wrap(errors.New("error removing app info directory"), err)
}

if err := os.RemoveAll(common.AppPath(cfg.PodPath, *cfg.AppName)); err != nil {
return err
}
Expand Down Expand Up @@ -475,7 +492,15 @@ func StartApp(cfg StartConfig) error {
log.FatalE(fmt.Sprintf("error creating %s-started file", cfg.AppName.String()), err)
}

if err := RunCrossingEntrypoint(cfg.Dir, cfg.PodPID, cfg.AppName.String(), appStartEntrypoint, args); err != nil {
ce := CrossingEntrypoint{
PodPath: cfg.Dir,
PodPID: cfg.PodPID,
AppName: cfg.AppName.String(),
EntrypointName: appStartEntrypoint,
EntrypointArgs: args,
Interactive: false,
}
if err := ce.Run(); err != nil {
return err
}

Expand Down Expand Up @@ -512,7 +537,15 @@ func StopApp(cfg StopConfig) error {
fmt.Sprintf("--app=%s", cfg.AppName),
}

if err := RunCrossingEntrypoint(cfg.Dir, cfg.PodPID, cfg.AppName.String(), appStopEntrypoint, args); err != nil {
ce := CrossingEntrypoint{
PodPath: cfg.Dir,
PodPID: cfg.PodPID,
AppName: cfg.AppName.String(),
EntrypointName: appStopEntrypoint,
8000 EntrypointArgs: args,
Interactive: false,
}
if err := ce.Run(); err != nil {
status, err := common.GetExitStatus(err)
// exit status 5 comes from systemctl and means the unit doesn't exist
if status == 5 {
Expand Down
63 changes: 44 additions & 19 deletions stage0/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ import (
"github.com/hashicorp/errwrap"
)

// RunCrossingEntrypoint wraps the execution of a stage1 entrypoint which
// CrossingEntrypoint represents a stage1 entrypoint whose execution
// needs to cross the stage0/stage1/stage2 boundary.
type CrossingEntrypoint struct {
PodPath string
PodPID int
AppName string
EntrypointName string
EntrypointArgs []string
Interactive bool
}

// Run wraps the execution of a stage1 entrypoint which
// requires crossing the stage0/stage1/stage2 boundary during its execution,
// by setting up proper environment variables for enter.
func RunCrossingEntrypoint(dir string, podPID int, appName string, entrypoint string, entrypointArgs []string) error {
enterCmd, err := getStage1Entrypoint(dir, enterEntrypoint)
func (ce CrossingEntrypoint) Run() error {
enterCmd, err := getStage1Entrypoint(ce.PodPath, enterEntrypoint)
if err != nil {
return errwrap.Wrap(errors.New("error determining 'enter' entrypoint"), err)
}
Expand All @@ -39,31 +50,45 @@ func RunCrossingEntrypoint(dir string, podPID int, appName string, entrypoint st
return err
}

if err := os.Chdir(dir); err != nil {
if err := os.Chdir(ce.PodPath); err != nil {
return errwrap.Wrap(errors.New("failed changing to dir"), err)
}

ep, err := getStage1Entrypoint(dir, entrypoint)
ep, err := getStage1Entrypoint(ce.PodPath, ce.EntrypointName)
if err != nil {
return fmt.Errorf("%q not implemented for pod's stage1: %v", entrypoint, err)
return fmt.Errorf("%q not implemented for pod's stage1: %v", ce.EntrypointName, err)
}
execArgs := []string{filepath.Join(common.Stage1RootfsPath(ce.PodPath), ep)}
execArgs = append(execArgs, ce.EntrypointArgs...)

pathEnv := os.Getenv("PATH")
if pathEnv == "" {
pathEnv = common.DefaultPath
}
execEnv := []string{
fmt.Sprintf("%s=%s", common.CrossingEnterCmd, filepath.Join(common.Stage1RootfsPath(ce.PodPath), enterCmd)),
fmt.Sprintf("%s=%d", common.CrossingEnterPID, ce.PodPID),
fmt.Sprintf("PATH=%s", pathEnv),
}
execArgs := []string{filepath.Join(common.Stage1RootfsPath(dir), ep)}
execArgs = append(execArgs, entrypointArgs...)

c := exec.Cmd{
Path: execArgs[0],
Args: execArgs,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: []string{
fmt.Sprintf("%s=%s", common.CrossingEnterCmd, filepath.Join(common.Stage1RootfsPath(dir), enterCmd)),
fmt.Sprintf("%s=%d", common.CrossingEnterPID, podPID),
},
Path: execArgs[0],
Args: execArgs,
Env: execEnv,
}

if err := c.Run(); err != nil {
return fmt.Errorf("error executing stage1 entrypoint: %v", err)
if ce.Interactive {
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
return fmt.Errorf("error executing stage1 entrypoint: %v", err)
}
} else {
out, err := c.CombinedOutput()
if err != nil {
return fmt.Errorf("error executing stage1 entrypoint: %s", string(out))
}
}

if err := os.Chdir(previousDir); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions stage1/aci/aci-install.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ AMI_GEN_MANIFEST := $(AMI_TMPDIR)/aci-manifest-$(AMI_FLAVOR)
AMI_SRC_MANIFEST := $(MK_SRCDIR)/aci-manifest.in
# a name for this flavor
AMI_NAME := coreos.com/rkt/stage1-$(AMI_FLAVOR)
# list of installed files
# list of installed files and symlinks
AMI_INSTALLED_FILES := \
$(AMI_ACI_OS_RELEASE) \
$(AMI_ACI_MANIFEST)
$(AMI_ACI_MANIFEST) \
$(AMI_ACI_DIRS_BASE)/etc/mtab

ifeq ($(AMI_FLAVOR),src)

Expand Down Expand Up @@ -115,6 +116,8 @@ STAGE1_INSTALL_DIRS_$(AMI_FLAVOR) += $(addsuffix :0755,$(AMI_ACI_DIR_CHAINS))
STAGE1_INSTALL_FILES_$(AMI_FLAVOR) += \
$(AMI_GEN_MANIFEST):$(AMI_ACI_MANIFEST):0644 \
$(MK_SRCDIR)/os-release:$(AMI_ACI_OS_RELEASE):0644
STAGE1_INSTALL_SYMLINKS_$(AMI_FLAVOR) += \
../proc/self/mounts:$(AMI_ACI_DIRS_BASE)/etc/mtab
STAGE1_SECONDARY_STAMPS_$(AMI_FLAVOR) += $(AMI_STAMP)
CLEAN_FILES += $(AMI_GEN_MANIFEST)

Expand Down
Loading
0