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

Inspection refactoring and cleanup #400

Closed
wants to merge 6 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
13 changes: 4 additions & 9 deletions mgradm/cmd/install/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ func installForPodman(
return errors.New(L("install podman before running this command"))
}

inspectedHostValues, err := utils.InspectHost(false)
authFile, cleaner, err := shared_podman.PodmanLogin()
if err != nil {
return utils.Errorf(err, L("cannot inspect host values"))
return utils.Errorf(err, L("failed to login to registry.suse.com"))
}
defer cleaner()

fqdn, err := getFqdn(args)
if err != nil {
Expand All @@ -87,14 +88,8 @@ func installForPodman(
if err != nil {
return utils.Errorf(err, L("failed to compute image URL"))
}
pullArgs := []string{}
_, scc_user_exist := inspectedHostValues["host_scc_username"]
_, scc_user_password := inspectedHostValues["host_scc_password"]
if scc_user_exist && scc_user_password {
pullArgs = append(pullArgs, "--creds", inspectedHostValues["host_scc_username"]+":"+inspectedHostValues["host_scc_password"])
}

preparedImage, err := shared_podman.PrepareImage(image, flags.Image.PullPolicy, pullArgs...)
preparedImage, err := shared_podman.PrepareImage(authFile, image, flags.Image.PullPolicy)
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions mgradm/cmd/install/shared/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ func AddInstallFlags(cmd *cobra.Command) {
cmd.Flags().Bool("debug-java", false, L("Enable tomcat and taskomatic remote debugging"))
cmd_utils.AddImageFlag(cmd)

cmd_utils.AddContainerImageFlags(cmd, "coco", L("confidential computing attestation"))
cmd.Flags().Int("coco-replicas", 0, L("How many replicas of the confidential computing container should be started. (only 0 or 1 supported for now)"))

_ = utils.AddFlagHelpGroup(cmd, &utils.Group{ID: "coco-container", Title: L("Confidential Computing Flags")})
_ = utils.AddFlagToHelpGroupID(cmd, "coco-replicas", "coco-container")
_ = utils.AddFlagToHelpGroupID(cmd, "coco-image", "coco-container")
_ = utils.AddFlagToHelpGroupID(cmd, "coco-tag", "coco-container")
cmd_utils.AddCocoFlag(cmd)

cmd.Flags().Int("hubxmlrpc-replicas", 0, L("How many replicas of the Hub XML-RPC API service container should be started. (only 0 or 1 supported for now)"))
hubXmlrpcImage := path.Join(utils.DefaultNamespace, "server-hub-xmlrpc-api")
Expand Down
11 changes: 7 additions & 4 deletions mgradm/cmd/migrate/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func migrateToKubernetes(
return utils.Errorf(err, L("cannot run migration"))
}

tz, oldPgVersion, newPgVersion, err := adm_utils.ReadContainerData(scriptDir)
extractedData, err := utils.ReadInspectData[utils.InspectResult](path.Join(scriptDir, "data"))
if err != nil {
return utils.Errorf(err, L("cannot read data from container"))
}
Expand All @@ -115,7 +115,7 @@ func migrateToKubernetes(

helmArgs := []string{
"--reset-values",
"--set", "timezone=" + tz,
"--set", "timezone=" + extractedData.Timezone,
}
if flags.Mirror != "" {
log.Warn().Msgf(L("The mirror data will not be migrated, ensure it is available at %s"), flags.Mirror)
Expand All @@ -139,15 +139,18 @@ func migrateToKubernetes(
return utils.Errorf(err, L("cannot set replicas to 0"))
}

oldPgVersion := extractedData.CurrentPgVersion
newPgVersion := extractedData.ImagePgVersion

if oldPgVersion != newPgVersion {
if err := kubernetes.RunPgsqlVersionUpgrade(flags.Image, flags.DbUpgradeImage, nodeName, oldPgVersion, newPgVersion); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL version upgrade script"))
}
}

schemaUpdateRequired := oldPgVersion != newPgVersion
if err := kubernetes.RunPgsqlFinalizeScript(serverImage, flags.Image.PullPolicy, nodeName, schemaUpdateRequired); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL version upgrade script"))
if err := kubernetes.RunPgsqlFinalizeScript(serverImage, flags.Image.PullPolicy, nodeName, schemaUpdateRequired, true); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL finalisation script"))
}

if err := kubernetes.RunPostUpgradeScript(serverImage, flags.Image.PullPolicy, nodeName); err != nil {
Expand Down
46 changes: 31 additions & 15 deletions mgradm/cmd/migrate/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
migration_shared "github.com/uyuni-project/uyuni-tools/mgradm/cmd/migrate/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/coco"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared"
podman_utils "github.com/uyuni-project/uyuni-tools/shared/podman"
Expand All @@ -31,20 +32,13 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
return utils.Errorf(err, L("cannot compute image"))
}

// FIXME all this code should be centralized. Now it being called in several different places.
inspectedHostValues, err := utils.InspectHost(false)
authFile, cleaner, err := podman_utils.PodmanLogin()
if err != nil {
return utils.Errorf(err, L("cannot inspect host values"))
return utils.Errorf(err, L("failed to login to registry.suse.com"))
}
defer cleaner()

pullArgs := []string{}
_, scc_user_exist := inspectedHostValues["host_scc_username"]
_, scc_user_password := inspectedHostValues["host_scc_password"]
if scc_user_exist && scc_user_password {
pullArgs = append(pullArgs, "--creds", inspectedHostValues["host_scc_username"]+":"+inspectedHostValues["host_scc_password"])
}

preparedImage, err := podman_utils.PrepareImage(serverImage, flags.Image.PullPolicy, pullArgs...)
preparedImage, err := podman_utils.PrepareImage(authFile, serverImage, flags.Image.PullPolicy)
if err != nil {
return err
}
Expand All @@ -53,27 +47,34 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
sshAuthSocket := migration_shared.GetSshAuthSocket()
sshConfigPath, sshKnownhostsPath := migration_shared.GetSshPaths()

tz, oldPgVersion, newPgVersion, err := podman.RunMigration(preparedImage, sshAuthSocket, sshConfigPath, sshKnownhostsPath, sourceFqdn, flags.User)
extractedData, err := podman.RunMigration(preparedImage, sshAuthSocket, sshConfigPath, sshKnownhostsPath, sourceFqdn, flags.User)
if err != nil {
return utils.Errorf(err, L("cannot run migration script"))
}

oldPgVersion := extractedData.CurrentPgVersion
newPgVersion := extractedData.ImagePgVersion

if oldPgVersion != newPgVersion {
if err := podman.RunPgsqlVersionUpgrade(flags.Image, flags.DbUpgradeImage, oldPgVersion, newPgVersion); err != nil {
if err := podman.RunPgsqlVersionUpgrade(
authFile, flags.Image, flags.DbUpgradeImage, oldPgVersion, newPgVersion,
); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL version upgrade script"))
}
}

schemaUpdateRequired := oldPgVersion != newPgVersion
if err := podman.RunPgsqlFinalizeScript(preparedImage, schemaUpdateRequired); err != nil {
if err := podman.RunPgsqlFinalizeScript(preparedImage, schemaUpdateRequired, true); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL finalize script"))
}

if err := podman.RunPostUpgradeScript(preparedImage); err != nil {
return utils.Errorf(err, L("cannot run post upgrade script"))
}

if err := podman.GenerateSystemdService(tz, preparedImage, false, flags.Mirror, viper.GetStringSlice("podman.arg")); err != nil {
if err := podman.GenerateSystemdService(
extractedData.Timezone, preparedImage, false, flags.Mirror, viper.GetStringSlice("podman.arg"),
); err != nil {
return utils.Errorf(err, L("cannot generate systemd service file"))
}

Expand All @@ -82,6 +83,21 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
return err
}

// Prepare confidential computing containers
cbosdo marked this conversation as resolved.
Show resolved Hide resolved
if err = coco.Upgrade(
flags.Coco.Image, flags.Image, extractedData.DbPort, extractedData.DbName,
extractedData.DbUser, extractedData.DbPassword,
); err != nil {
return utils.Errorf(err, L("cannot setup confidential computing attestation service"))
}

if flags.Coco.Replicas > 0 {
err := podman_utils.ScaleService(flags.Coco.Replicas, podman_utils.ServerAttestationService)
if err != nil {
return err
}
}

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

if err := podman_utils.EnablePodmanSocket(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions mgradm/cmd/migrate/shared/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package shared

import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
Expand All @@ -15,6 +16,7 @@ import (
type MigrateFlags struct {
Image types.ImageFlags `mapstructure:",squash"`
DbUpgradeImage types.ImageFlags `mapstructure:"dbupgrade"`
Coco shared.CocoFlags
User string
Mirror string
}
Expand All @@ -24,5 +26,6 @@ func AddMigrateFlags(cmd *cobra.Command) {
utils.AddMirrorFlag(cmd)
utils.AddImageFlag(cmd)
utils.AddDbUpgradeImageFlag(cmd)
utils.AddCocoFlag(cmd)
cmd.Flags().String("user", "root", L("User on the source server. Non-root user must have passwordless sudo privileges (NOPASSWD tag in /etc/sudoers)."))
}
5 changes: 1 addition & 4 deletions mgradm/cmd/status/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/spf13/cobra"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
Expand All @@ -29,9 +28,7 @@ func podmanStatus(
} else {
// Run spacewalk-service status in the container
cnx := shared.NewConnection("podman", podman.ServerContainerName, "")
if err := adm_utils.ExecCommand(zerolog.InfoLevel, cnx, "spacewalk-service", "status"); err != nil {
return utils.Errorf(err, L("failed to run spacewalk-service status"))
}
_ = adm_utils.ExecCommand(zerolog.InfoLevel, cnx, "spacewalk-service", "status")
}

for i := 0; i < podman.CurrentReplicaCount(podman.ServerAttestationService); i++ {
Expand Down
9 changes: 8 additions & 1 deletion mgradm/cmd/support/ptf/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func ptfForPodman(
if err := flags.checkParameters(); err != nil {
return err
}
return podman.Upgrade(flags.Image, dummyMigration, dummyCoco, args)

authFile, cleaner, err := podman_shared.PodmanLogin()
if err != nil {
return utils.Errorf(err, L("failed to login to registry.suse.com"))
}
defer cleaner()

return podman.Upgrade(authFile, flags.Image, dummyMigration, dummyCoco, args)
}

func (flags *podmanPTFFlags) checkParameters() error {
Expand Down
11 changes: 10 additions & 1 deletion mgradm/cmd/upgrade/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ package podman
import (
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
shared_podman "github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

func upgradePodman(globalFlags *types.GlobalFlags, flags *podmanUpgradeFlags, cmd *cobra.Command, args []string) error {
return podman.Upgrade(flags.Image, flags.DbUpgradeImage, flags.Coco.Image, args)
authFile, cleaner, err := shared_podman.PodmanLogin()
if err != nil {
return utils.Errorf(err, L("failed to login to registry.suse.com"))
}
defer cleaner()

return podman.Upgrade(authFile, flags.Image, flags.DbUpgradeImage, flags.Coco.Image, args)
}
22 changes: 11 additions & 11 deletions mgradm/shared/kubernetes/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func Upgrade(
return err
}

fqdn, exist := inspectedValues["fqdn"]
if !exist {
fqdn := inspectedValues.Fqdn
if fqdn == "" {
return fmt.Errorf(L("inspect function did non return fqdn value"))
}

Expand Down Expand Up @@ -182,25 +182,25 @@ func Upgrade(
err = kubernetes.ReplicasTo(kubernetes.ServerApp, 1)
}
}()
if inspectedValues["image_pg_version"] > inspectedValues["current_pg_version"] {
if inspectedValues.ImagePgVersion > inspectedValues.CurrentPgVersion {
log.Info().Msgf(L("Previous PostgreSQL is %[1]s, new one is %[2]s. Performing a DB version upgrade…"),
inspectedValues["current_pg_version"], inspectedValues["image_pg_version"])
inspectedValues.CurrentPgVersion, inspectedValues.ImagePgVersion)

if err := RunPgsqlVersionUpgrade(*image, *upgradeImage, nodeName,
inspectedValues["current_pg_version"], inspectedValues["image_pg_version"],
inspectedValues.CurrentPgVersion, inspectedValues.ImagePgVersion,
); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL version upgrade script"))
}
} else if inspectedValues["image_pg_version"] == inspectedValues["current_pg_version"] {
log.Info().Msgf(L("Upgrading to %s without changing PostgreSQL version"), inspectedValues["uyuni_release"])
} else if inspectedValues.ImagePgVersion == inspectedValues.CurrentPgVersion {
log.Info().Msgf(L("Upgrading to %s without changing PostgreSQL version"), inspectedValues.UyuniRelease)
} else {
return fmt.Errorf(L("trying to downgrade PostgreSQL from %[1]s to %[2]s"),
inspectedValues["current_pg_version"], inspectedValues["image_pg_version"])
inspectedValues.CurrentPgVersion, inspectedValues.ImagePgVersion)
}

schemaUpdateRequired := inspectedValues["current_pg_version"] != inspectedValues["image_pg_version"]
if err := RunPgsqlFinalizeScript(serverImage, image.PullPolicy, nodeName, schemaUpdateRequired); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL version upgrade script"))
schemaUpdateRequired := inspectedValues.CurrentPgVersion != inspectedValues.ImagePgVersion
if err := RunPgsqlFinalizeScript(serverImage, image.PullPolicy, nodeName, schemaUpdateRequired, false); err != nil {
return utils.Errorf(err, L("cannot run PostgreSQL finalize script"))
}

if err := RunPostUpgradeScript(serverImage, image.PullPolicy, nodeName); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions mgradm/shared/kubernetes/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ func RunPgsqlVersionUpgrade(image types.ImageFlags, upgradeImage types.ImageFlag
}

// RunPgsqlFinalizeScript run the script with all the action required to a db after upgrade.
func RunPgsqlFinalizeScript(serverImage string, pullPolicy string, nodeName string, schemaUpdateRequired bool) error {
func RunPgsqlFinalizeScript(
serverImage string, pullPolicy string, nodeName string, schemaUpdateRequired bool, migration bool,
) error {
scriptDir, err := os.MkdirTemp("", "mgradm-*")
defer os.RemoveAll(scriptDir)
if err != nil {
return fmt.Errorf(L("failed to create temporary directory: %s"))
}
pgsqlFinalizeContainer := "uyuni-finalize-pgsql"
pgsqlFinalizeScriptName, err := adm_utils.GenerateFinalizePostgresScript(scriptDir, true, schemaUpdateRequired, true, true, true)
pgsqlFinalizeScriptName, err := adm_utils.GenerateFinalizePostgresScript(scriptDir, true, schemaUpdateRequired, true, migration, true)
if err != nil {
return utils.Errorf(err, L("cannot generate PostgreSQL finalization script"))
}
Expand Down
Loading