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 rexray#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 02481f0 commit 1fc3937
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions rexray/cli/service.go
Original file line number Diff line number Diff line change
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 Expand Up @@ -282,6 +328,33 @@ func checkOpPerms(op string) error {
return nil
}

const (
lsbReleasePath = "/etc/lsb-release"
)

/*func isCoreOS() bool {
if !gotil.FileExists(lsbReleasePath) {
return false
}
r, err := os.Open(lsbReleasePath)
if err != nil {
return false
}
defer r.Close()
scanner := bufio.NewScanner(r)
for {
if !scanner.Scan() {
break
}
if scanner.Text() == "DISTRIB_ID=CoreOS" {
return true
}
}
return false
}*/

const rexRayLogoASCII = `
⌐▄Q▓▄Ç▓▄,▄_
Σ▄▓▓▓▓▓▓▓▓▓▓▄π
Expand Down

0 comments on commit 1fc3937

Please sign in to comment.