Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-systemd #401

Closed
wants to merge 3 commits into from
Closed
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
64 changes: 41 additions & 23 deletions core/commands/daemon_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/opensvc/om3/daemon/daemoncli"
"github.com/opensvc/om3/util/command"
"github.com/opensvc/om3/util/systemd"
)

type (
Expand All @@ -18,35 +19,52 @@ type (
)

func (t *CmdDaemonRestart) Run() error {
cli, err := newClient(t.Server)
if err != nil {
return err
}
if t.Foreground {
cli, err := newClient(t.Server)
if err != nil {
return err
}
if err := daemoncli.New(cli).ReStart(); err != nil {
return err
}
} else if os.Getenv("INVOCATION_ID") != "" {
// called from systemd
t.nativeRestart()
} else if systemd.HasSystemd() {
// systemd is running, delegate to systemd
return command.New(
command.WithName("systemctl"),
command.WithVarArgs("restart", "opensvc-agent"),
).Run()
} else {
args := []string{"daemon", "restart"}
if t.Log != "" {
args = append(args, "--log", t.Log)
}
if t.Server != "" {
args = append(args, "--server", t.Server)
t.nativeRestart()
}
return nil
}

func (t *CmdDaemonRestart) nativeRestart() {
args := []string{"daemon", "restart"}
if t.Log != "" {
args = append(args, "--log", t.Log)
}
if t.Server != "" {
args = append(args, "--server", t.Server)
}
args = append(args, "--foreground")
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs(args),
)
checker := func() error {
cli, err := newClient(t.Server)
if err != nil {
return err
}
args = append(args, "--foreground")
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs(args),
)
checker := func() error {
time.Sleep(60 * time.Millisecond)
if err := daemoncli.New(cli).WaitRunning(); err != nil {
return fmt.Errorf("daemon not running")
}
return nil
time.Sleep(60 * time.Millisecond)
if err := daemoncli.New(cli).WaitRunning(); err != nil {
return fmt.Errorf("daemon not running")
}
daemoncli.LockCmdExit(cmd, checker, "daemon restart")
return nil
}
return nil
daemoncli.LockCmdExit(cmd, checker, "daemon restart")
}
96 changes: 66 additions & 30 deletions core/commands/daemon_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"runtime/pprof"
"time"

"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/daemon/daemoncli"
"github.com/opensvc/om3/util/command"
"github.com/opensvc/om3/util/systemd"
)

type (
Expand All @@ -25,40 +27,74 @@ func (t *CmdDaemonStart) Run() error {
return err
}
if t.Foreground {
if t.CpuProfile != "" {
f, err := os.Create(t.CpuProfile)
if err != nil {
return fmt.Errorf("create CPU profile: %w", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("start CPU profile: %w", err)
}
defer pprof.StopCPUProfile()
}
if err := daemoncli.New(cli).Start(); err != nil {
return fmt.Errorf("start daemon cli: %w", err)
}
return t.runForeground(cli)
} else if os.Getenv("INVOCATION_ID") != "" {
// called from systemd
cmd := t.startNativeCmd()
return t.runBackground(cli, cmd)
} else {
args := []string{"daemon", "start", "--foreground"}
if t.Log != "" {
args = append(args, "--log", t.Log)
// not called from systemd
var cmd *command.T
if systemd.HasSystemd() {
// systemd is running, delegate to systemd
cmd = t.startUnitCmd()
return cmd.Run()
} else {
// systemd is not running, start background
cmd = t.startNativeCmd()
return t.runBackground(cli, cmd)
}
if t.Server != "" {
args = append(args, "--server", t.Server)
}
}

func (t *CmdDaemonStart) runBackground(cli *client.T, cmd *command.T) error {
checker := func() error {
time.Sleep(60 * time.Millisecond)
if err := daemoncli.New(cli).WaitRunning(); err != nil {
return fmt.Errorf("daemon not running: %w", err)
}
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs(args),
)
checker := func() error {
time.Sleep(60 * time.Millisecond)
if err := daemoncli.New(cli).WaitRunning(); err != nil {
return fmt.Errorf("daemon not running: %w", err)
}
return nil
return nil
}
daemoncli.LockCmdExit(cmd, checker, "daemon start")
return nil
}

func (t *CmdDaemonStart) runForeground(cli *client.T) error {
if t.CpuProfile != "" {
f, err := os.Create(t.CpuProfile)
if err != nil {
return fmt.Errorf("create CPU profile: %w", err)
}
daemoncli.LockCmdExit(cmd, checker, "daemon start")
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("start CPU profile: %w", err)
}
defer pprof.StopCPUProfile()
}
if err := daemoncli.New(cli).Start(); err != nil {
return fmt.Errorf("start daemon cli: %w", err)
}
return nil
}

func (t *CmdDaemonStart) startUnitCmd() *command.T {
return command.New(
command.WithName("systemctl"),
command.WithVarArgs("start", "opensvc-agent"),
)
}

func (t *CmdDaemonStart) startNativeCmd() *command.T {
args := []string{"daemon", "start", "--foreground"}
if t.Log != "" {
args = append(args, "--log", t.Log)
}
if t.Server != "" {
args = append(args, "--server", t.Server)
}
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs(args),
)
return cmd
}
20 changes: 19 additions & 1 deletion core/commands/daemon_stop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package commands

import "github.com/opensvc/om3/daemon/daemoncli"
import (
"os"

"github.com/opensvc/om3/daemon/daemoncli"
"github.com/opensvc/om3/util/command"
"github.com/opensvc/om3/util/systemd"
)

type (
CmdDaemonStop struct {
Expand All @@ -9,6 +15,18 @@ type (
)

func (t *CmdDaemonStop) Run() error {
if os.Getenv("INVOCATION_ID") != "" || !systemd.HasSystemd() {
// called from systemd, or systemd is not running
return t.nativeStop()
}
// systemd is running, ask systemd to stop opensvc-agent unit
return command.New(
command.WithName("systemctl"),
command.WithVarArgs("stop", "opensvc-agent"),
).Run()
}

func (t *CmdDaemonStop) nativeStop() error {
cli, err := newClient(t.Server)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemoncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (t *T) stop() error {
// one more delay before return listener not anymore responding
time.Sleep(WaitStoppedDelay)
default:
return fmt.Errorf("Unexpected status code: %s", resp.Status)
return fmt.Errorf("unexpected status code: %s", resp.Status)
}
return nil
}
Expand Down
14 changes: 4 additions & 10 deletions util/systemd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

package systemd

import "os"

var (
procOneComm = "/proc/1/comm"
import (
"os"
)

// HasSystemd return true if systemd is detected on current os
func HasSystemd() bool {
var (
b []byte
err error
)
if b, err = os.ReadFile(procOneComm); err != nil {
if _, err := os.Stat("/run/systemd/system"); err != nil {
return false
}
return string(b) == "systemd\n"
return true
}