From 5a17c08b4fcde2cbe0633ef1fcbe786ee17c0691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Date: Fri, 2 Feb 2024 14:25:25 +0100 Subject: [PATCH] Add start and stop functions for systemd Those functions will be needed in several places like update or mgradm commands. Also add error handling to the existing restart. --- mgradm/cmd/install/podman/utils.go | 2 +- mgradm/cmd/migrate/podman/utils.go | 6 +++-- mgrpxy/cmd/install/podman/utils.go | 19 ++++++++-------- shared/podman/systemd.go | 35 +++++++++++++++++++++++++----- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/mgradm/cmd/install/podman/utils.go b/mgradm/cmd/install/podman/utils.go index b5d13675a..9d6f6af82 100644 --- a/mgradm/cmd/install/podman/utils.go +++ b/mgradm/cmd/install/podman/utils.go @@ -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() } diff --git a/mgradm/cmd/migrate/podman/utils.go b/mgradm/cmd/migrate/podman/utils.go index a45d9e880..5ff8b0242 100644 --- a/mgradm/cmd/migrate/podman/utils.go +++ b/mgradm/cmd/migrate/podman/utils.go @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 SUSE LLC +// SPDX-FileCopyrightText: 2024 SUSE LLC // // SPDX-License-Identifier: Apache-2.0 @@ -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") diff --git a/mgrpxy/cmd/install/podman/utils.go b/mgrpxy/cmd/install/podman/utils.go index 110750b78..70d91aa56 100644 --- a/mgrpxy/cmd/install/podman/utils.go +++ b/mgrpxy/cmd/install/podman/utils.go @@ -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" @@ -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") @@ -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 { diff --git a/shared/podman/systemd.go b/shared/podman/systemd.go index b036f3541..ff70788e3 100644 --- a/shared/podman/systemd.go +++ b/shared/podman/systemd.go @@ -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" @@ -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 { @@ -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 }