Skip to content

Commit

Permalink
CoreOS/SystemD Service Start Fix
Browse files Browse the repository at this point in the history
This patch is a fix for issue #326, where CoreOS is placing SSH sessions
into a CGroup, thus killing any REX-Ray daemons not specifically
launched via SystemD.

This path actually adjusts REX-Ray's behavior for not just CoreOS
systems, but any Linux using SystemD. All SystemD-based systems now
prefer SystemD for service-control-management (SCM) commands.
  • Loading branch information
akutz committed Mar 2, 2016
1 parent 47fb527 commit 47f2500
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions rexray/cli/service.go
Expand Up @@ -17,7 +17,17 @@ import (
"github.com/emccode/rexray/util"
)

var (
useSystemDForSCMCmds = gotil.FileExists(util.UnitFilePath) &&
getInitSystemType() == SystemD
)

func (c *CLI) start() {
if !c.fg && useSystemDForSCMCmds {
startViaSystemD()
return
}

checkOpPerms("started")

log.WithField("os.Args", os.Args).Debug("invoking service start")
Expand Down Expand Up @@ -62,6 +72,33 @@ func failOnError(err error) {
}
}

func startViaSystemD() {
execSystemDCmd("start")
statusViaSystemD()
}

func stopViaSystemD() {
execSystemDCmd("stop")
statusViaSystemD()
}

func statusViaSystemD() {
execSystemDCmd("status")
}

func execSystemDCmd(cmdType string) {
cmd := exec.Command("systemctl", cmdType, "-l", "rexray")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
panic(status.ExitStatus())
}
}
}
}

func (c *CLI) startDaemon() {

var out io.Writer = os.Stdout
Expand Down Expand Up @@ -213,6 +250,11 @@ func (c *CLI) tryToStartDaemon() {
}

func stop() {
if useSystemDForSCMCmds {
stopViaSystemD()
return
}

checkOpPerms("stopped")

if !gotil.FileExists(util.PidFilePath()) {
Expand All @@ -235,6 +277,10 @@ func stop() {
}

func (c *CLI) status() {
if useSystemDForSCMCmds {
statusViaSystemD()
return
}

pidFile := util.PidFilePath()

Expand Down

0 comments on commit 47f2500

Please sign in to comment.