Skip to content

Commit

Permalink
Add start and stop functions for systemd
Browse files Browse the repository at this point in the history
Those functions will be needed in several places like update or mgradm
commands. Also add error handling to the existing restart.
  • Loading branch information
cbosdo committed Feb 7, 2024
1 parent 44e71f2 commit 5a17c08
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion mgradm/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func waitForSystemStart(cnx *shared.Connection, flags *podmanInstallFlags) {
podman.GenerateSystemdService(flags.TZ, image, flags.Debug.Java, podmanArgs)

log.Info().Msg("Waiting for the server to start...")
shared_podman.EnableService("uyuni-server")
shared_podman.EnableService(shared_podman.ServerService)

cnx.WaitForServer()
}
Expand Down
6 changes: 4 additions & 2 deletions mgradm/cmd/migrate/podman/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 SUSE LLC
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -86,7 +86,9 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
podman.GenerateSystemdService(tz, serverImage, false, viper.GetStringSlice("podman.arg"))

// Start the service
podman_utils.EnableService("uyuni-server")
if err := podman_utils.EnableService(podman_utils.ServerService); err != nil {
return err
}

log.Info().Msg("Server migrated")

Expand Down
19 changes: 9 additions & 10 deletions mgrpxy/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2023 SUSE LLC
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package podman

import (
"fmt"
"os"
"os/exec"

Expand All @@ -18,23 +19,22 @@ import (
)

// Start the proxy services.
func startPod() {
const servicePod = "uyuni-proxy-pod"
if shared_podman.IsServiceRunning(servicePod) {
shared_podman.RestartService(servicePod)
func startPod() error {
if shared_podman.IsServiceRunning(shared_podman.ProxyService) {
return shared_podman.RestartService(shared_podman.ProxyService)
} else {
shared_podman.EnableService(servicePod)
return shared_podman.EnableService(shared_podman.ProxyService)
}
}

func installForPodman(globalFlags *types.GlobalFlags, flags *podmanProxyInstallFlags, cmd *cobra.Command, args []string) error {
if _, err := exec.LookPath("podman"); err != nil {
log.Fatal().Err(err).Msgf("install podman before running this command")
return fmt.Errorf("install podman before running this command")
}

configPath := utils.GetConfigPath(args)
if err := unpackConfig(configPath); err != nil {
log.Fatal().Err(err).Msgf("Failed to extract proxy config from %s file", configPath)
return fmt.Errorf("failed to extract proxy config from %s file: %s", configPath, err)
}

httpdImage := getContainerImage(flags, "httpd")
Expand All @@ -46,8 +46,7 @@ func installForPodman(globalFlags *types.GlobalFlags, flags *podmanProxyInstallF
// Setup the systemd service configuration options
podman.GenerateSystemdService(httpdImage, saltBrokerImage, squidImage, sshImage, tftpdImage, flags.Podman.Args)

startPod()
return nil
return startPod()
}

func getContainerImage(flags *podmanProxyInstallFlags, name string) string {
Expand Down
35 changes: 30 additions & 5 deletions shared/podman/systemd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2023 SUSE LLC
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package podman

import (
"fmt"
"os"
"os/exec"
"path"
Expand All @@ -15,6 +16,12 @@ import (

const servicesPath = "/etc/systemd/system/"

// Name of the systemd service for the server.
const ServerService = "uyuni-server"

// Name of the systemd service for the proxy.
const ProxyService = "uyuni-proxy-pod"

// HasService returns if a systemd service is installed.
// name is the name of the service without the '.service' part.
func HasService(name string) bool {
Expand Down Expand Up @@ -79,15 +86,33 @@ func IsServiceRunning(service string) bool {
}

// RestartService restarts the systemd service.
func RestartService(service string) {
func RestartService(service string) error {
if err := utils.RunCmd("systemctl", "restart", service); err != nil {
log.Fatal().Err(err).Msgf("Failed to restart systemd %s.service", service)
return fmt.Errorf("failed to restart systemd %s.service: %s", service, err)
}
return nil
}

// StartService starts the systemd service.
func StartService(service string) error {
if err := utils.RunCmd("systemctl", "start", service); err != nil {
return fmt.Errorf("failed to start systemd %s.service: %s", service, err)
}
return nil
}

// StopService starts the systemd service.
func StopService(service string) error {
if err := utils.RunCmd("systemctl", "stop", service); err != nil {
return fmt.Errorf("failed to stop systemd %s.service: %s", service, err)
}
return nil
}

// EnableService enables and starts a systemd service.
func EnableService(service string) {
func EnableService(service string) error {
if err := utils.RunCmd("systemctl", "enable", "--now", service); err != nil {
log.Fatal().Err(err).Msgf("Failed to enable %s systemd service", service)
return fmt.Errorf("failed to enable %s systemd service: %s", service, err)
}
return nil
}

0 comments on commit 5a17c08

Please sign in to comment.