Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore: restart services with restart command
Browse files Browse the repository at this point in the history
Instead of calling stop & start right after it, we are leveraging services
ability to be restarted. For linux, systemctl will use "restart", for MacOS
it will use "stop and start", for Windows, it's not supported yet
  • Loading branch information
mdelapenya committed Feb 4, 2022
1 parent be2e305 commit 34eedbe
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 7 deletions.
6 changes: 1 addition & 5 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,11 @@ func (fts *FleetTestSuite) processStateChangedOnTheHost(process string, state st
err := agentInstaller.Start(fts.currentContext)
return err
} else if state == "restarted" {
err := agentInstaller.Stop(fts.currentContext)
err := agentInstaller.Restart(fts.currentContext)
if err != nil {
return err
}

err = agentInstaller.Start(fts.currentContext)
if err != nil {
return err
}
return nil
} else if state == "uninstalled" {
err := agentInstaller.Uninstall(fts.currentContext)
Expand Down
5 changes: 3 additions & 2 deletions internal/deploy/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type ServiceOperator interface {
Logs(ctx context.Context) error
Postinstall(ctx context.Context) error
Preinstall(ctx context.Context) error
Start(ctx context.Context) error // will start a service
Stop(ctx context.Context) error // will stop a service
Restart(ctx context.Context) error // will restart a service
Start(ctx context.Context) error // will start a service
Stop(ctx context.Context) error // will stop a service
Uninstall(ctx context.Context) error
}

Expand Down
17 changes: 17 additions & 0 deletions internal/installer/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ func systemCtlPostInstall(ctx context.Context, linux string, artifact string, ex
return nil
}

func systemCtlRestart(ctx context.Context, linux string, artifact string, execFn func(ctx context.Context, args []string) (string, error)) error {
cmds := systemd.RestartCmds(artifact)
span, _ := apm.StartSpanOptions(ctx, "Restarting "+artifact+" service", artifact+"."+linux+".restart", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
span.Context.SetLabel("arguments", cmds)
span.Context.SetLabel("artifact", artifact)
span.Context.SetLabel("linux", linux)
defer span.End()

_, err := execFn(ctx, cmds)
if err != nil {
return err
}
return nil
}

func systemCtlStart(ctx context.Context, linux string, artifact string, execFn func(ctx context.Context, args []string) (string, error)) error {
cmds := systemd.StartCmds(artifact)
span, _ := apm.StartSpanOptions(ctx, "Starting "+artifact+" service", artifact+"."+linux+".start", apm.SpanOptions{
Expand Down
15 changes: 15 additions & 0 deletions internal/installer/elasticagent_deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ func (i *elasticAgentDEBPackage) Preinstall(ctx context.Context) error {
return installArtifactFn(ctx, "elastic-agent")
}

// Restart will restart a service
func (i *elasticAgentDEBPackage) Restart(ctx context.Context) error {
for _, bp := range i.service.BackgroundProcesses {
if strings.EqualFold(bp, "filebeat") || strings.EqualFold(bp, "metricbeat") {
// restart the dependant binary first
err := systemCtlRestart(ctx, "debian", bp, i.Exec)
if err != nil {
return err
}
}
}

return systemCtlRestart(ctx, "debian", "elastic-agent", i.Exec)
}

// Start will start a service
func (i *elasticAgentDEBPackage) Start(ctx context.Context) error {
for _, bp := range i.service.BackgroundProcesses {
Expand Down
16 changes: 16 additions & 0 deletions internal/installer/elasticagent_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ func (i *elasticAgentDockerPackage) Preinstall(ctx context.Context) error {
)
}

// Restart will restart a service
func (i *elasticAgentDockerPackage) Restart(ctx context.Context) error {
cmds := []string{"systemctl", "restart", "elastic-agent"}
span, _ := apm.StartSpanOptions(ctx, "Restarting Elastic Agent service", "elastic-agent.docker.restart", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
span.Context.SetLabel("arguments", cmds)
defer span.End()

_, err := i.Exec(ctx, cmds)
if err != nil {
return err
}
return nil
}

// Start will start a service
func (i *elasticAgentDockerPackage) Start(ctx context.Context) error {
cmds := []string{"systemctl", "start", "elastic-agent"}
Expand Down
15 changes: 15 additions & 0 deletions internal/installer/elasticagent_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ func (i *elasticAgentRPMPackage) Preinstall(ctx context.Context) error {
return installArtifactFn(ctx, "elastic-agent")
}

// Restart will restart a service
func (i *elasticAgentRPMPackage) Restart(ctx context.Context) error {
for _, bp := range i.service.BackgroundProcesses {
if strings.EqualFold(bp, "filebeat") || strings.EqualFold(bp, "metricbeat") {
// start the dependant binary first
err := systemCtlRestart(ctx, "centos", bp, i.Exec)
if err != nil {
return err
}
}
}

return systemCtlRestart(ctx, "centos", "elastic-agent", i.Exec)
}

// Start will start a service
func (i *elasticAgentRPMPackage) Start(ctx context.Context) error {
for _, bp := range i.service.BackgroundProcesses {
Expand Down
16 changes: 16 additions & 0 deletions internal/installer/elasticagent_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ func (i *elasticAgentTARPackage) Preinstall(ctx context.Context) error {

}

// Restart will restart a service
func (i *elasticAgentTARPackage) Restart(ctx context.Context) error {
cmds := []string{"systemctl", "restart", "elastic-agent"}
span, _ := apm.StartSpanOptions(ctx, "Restarting Elastic Agent service", "elastic-agent.tar.restart", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
span.Context.SetLabel("arguments", cmds)
defer span.End()

_, err := i.Exec(ctx, cmds)
if err != nil {
return err
}
return nil
}

// Start will start a service
func (i *elasticAgentTARPackage) Start(ctx context.Context) error {
cmds := []string{"systemctl", "start", "elastic-agent"}
Expand Down
9 changes: 9 additions & 0 deletions internal/installer/elasticagent_tar_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ func (i *elasticAgentTARDarwinPackage) Preinstall(ctx context.Context) error {
return nil
}

// Restart will restart a service
func (i *elasticAgentTARDarwinPackage) Restart(ctx context.Context) error {
err := i.Stop(ctx)
if err != nil {
return err
}
return i.Start(ctx)
}

// Start will start a service
func (i *elasticAgentTARDarwinPackage) Start(ctx context.Context) error {
cmds := []string{"launchctl", "start", "elastic-agent"}
Expand Down
5 changes: 5 additions & 0 deletions internal/installer/elasticagent_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func (i *elasticAgentZIPPackage) Preinstall(ctx context.Context) error {
return nil
}

// Restart will restart a service
func (i *elasticAgentZIPPackage) Restart(ctx context.Context) error {
return nil
}

// Start will start a service
func (i *elasticAgentZIPPackage) Start(ctx context.Context) error {
return nil
Expand Down

0 comments on commit 34eedbe

Please sign in to comment.