Skip to content

Commit

Permalink
refactoring migration function to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Jan 25, 2024
1 parent dddf842 commit 6b6d3dd
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 172 deletions.
2 changes: 1 addition & 1 deletion mgradm/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewUyuniadmCommand() *cobra.Command {
rootCmd.AddCommand(uninstall.NewCommand(globalFlags))
rootCmd.AddCommand(distro.NewCommand(globalFlags))
rootCmd.AddCommand(completion.NewCommand(globalFlags))
rootCmd.AddCommand(support.NewCommand(globalFlags))
rootCmd.AddCommand(support.NewCommand(globalFlags))
rootCmd.AddCommand(upgrade.NewCommand(globalFlags))

return rootCmd
Expand Down
62 changes: 8 additions & 54 deletions mgradm/cmd/migrate/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
package kubernetes

import (
"encoding/base64"
"fmt"
"os"
"os/exec"
"path"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
migration_shared "github.com/uyuni-project/uyuni-tools/mgradm/cmd/migrate/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/ssl"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
shared_kubernetes "github.com/uyuni-project/uyuni-tools/shared/kubernetes"
Expand All @@ -45,7 +41,7 @@ func migrateToKubernetes(
sshConfigPath, sshKnownhostsPath := migration_shared.GetSshPaths()

// Prepare the migration script and folder
scriptDir := migration_shared.GenerateMigrationScript(fqdn, true)
scriptDir := adm_utils.GenerateMigrationScript(fqdn, true)
defer os.RemoveAll(scriptDir)

// Install Uyuni with generated CA cert: an empty struct means no 3rd party cert
Expand All @@ -64,9 +60,9 @@ func migrateToKubernetes(
"--set", "migration.dataPath="+scriptDir)

// Run the actual migration
runMigration(cnx, flags, scriptDir)
adm_utils.RunMigration(cnx, scriptDir)

tz, oldPgVersion, newPgVersion := migration_shared.ReadContainerData(scriptDir)
tz, oldPgVersion, newPgVersion := adm_utils.ReadContainerData(scriptDir)

helmArgs := []string{
"--reset-values",
Expand All @@ -85,61 +81,19 @@ func migrateToKubernetes(
log.Fatal().Err(err).Msg("Failed to compute image URL")
}
log.Info().Msgf("Using migration image %s", migrationImageUrl)
migration_shared.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)
adm_utils.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)

kubernetes.UyuniUpgrade(&migrationImage, &flags.Helm, kubeconfig, fqdn, clusterInfos.Ingress, helmArgs...)
runMigration(cnx, flags, scriptDir)
adm_utils.RunMigration(cnx, scriptDir)
}

migration_shared.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
adm_utils.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
kubernetes.UyuniUpgrade(&flags.Image, &flags.Helm, kubeconfig, fqdn, clusterInfos.Ingress, helmArgs...)
runMigration(cnx, flags, scriptDir)
adm_utils.RunMigration(cnx, scriptDir)

helmArgs = append(helmArgs, setupSsl(flags, kubeconfig, scriptDir)...)
helmArgs = append(helmArgs, kubernetes.SetupSsl(&flags.Helm, kubeconfig, scriptDir, flags.Ssl.Password, flags.Image.PullPolicy)...)

// As we upgrade the helm instance without the migration parameters the SSL certificate will be used
kubernetes.UyuniUpgrade(&flags.Image, &flags.Helm, kubeconfig, fqdn, clusterInfos.Ingress, helmArgs...)
return nil
}

func runMigration(cnx *shared.Connection, flags *kubernetesMigrateFlags, tmpPath string) {
log.Info().Msg("Migrating server")
err := adm_utils.ExecCommand(zerolog.InfoLevel, cnx, "/var/lib/uyuni-tools/migrate.sh")
if err != nil {
log.Fatal().Err(err).Msg("error running the migration script")
}
}

// updateIssuer replaces the temporary SSL certificate issuer with the source server CA.
// Return additional helm args to use the SSL certificates
func setupSsl(flags *kubernetesMigrateFlags, kubeconfig string, scriptDir string) []string {
caCert := path.Join(scriptDir, "RHN-ORG-TRUSTED-SSL-CERT")
caKey := path.Join(scriptDir, "RHN-ORG-PRIVATE-SSL-KEY")

if utils.FileExists(caCert) && utils.FileExists(caKey) {
key := base64.StdEncoding.EncodeToString(ssl.GetRsaKey(caKey, flags.Ssl.Password))

// Strip down the certificate text part
out, err := utils.RunCmdOutput(zerolog.DebugLevel, "openssl", "x509", "-in", caCert)
if err != nil {
log.Fatal().Err(err).Msg("Failed to strip text part of CA certificate")
}
cert := base64.StdEncoding.EncodeToString(out)
ca := ssl.SslPair{Cert: cert, Key: key}

// An empty struct means no third party certificate
sslFlags := adm_utils.SslCertFlags{}
return kubernetes.DeployCertificate(&flags.Helm, &sslFlags, cert, &ca, kubeconfig, "", flags.Image.PullPolicy)
} else {
// Handle third party certificates and CA
sslFlags := adm_utils.SslCertFlags{
Ca: ssl.CaChain{Root: caCert},
Server: ssl.SslPair{
Key: path.Join(scriptDir, "spacewalk.key"),
Cert: path.Join(scriptDir, "spacewalk.crt"),
},
}
kubernetes.DeployExistingCertificate(&flags.Helm, &sslFlags, kubeconfig)
}
return []string{}
}
17 changes: 8 additions & 9 deletions mgradm/cmd/migrate/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/viper"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/migrate/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/podman"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
podman_utils "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,7 +30,7 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
sshAuthSocket := shared.GetSshAuthSocket()
sshConfigPath, sshKnownhostsPath := shared.GetSshPaths()

scriptDir := shared.GenerateMigrationScript(args[0], false)
scriptDir := adm_utils.GenerateMigrationScript(args[0], false)
defer os.RemoveAll(scriptDir)

extraArgs := []string{
Expand Down Expand Up @@ -58,7 +59,7 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
[]string{"/var/lib/uyuni-tools/migrate.sh"})

// Read the extracted data
tz, oldPgVersion, newPgVersion := shared.ReadContainerData(scriptDir)
tz, oldPgVersion, newPgVersion := adm_utils.ReadContainerData(scriptDir)

if oldPgVersion != newPgVersion {
var migrationImage types.ImageFlags
Expand All @@ -74,18 +75,16 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
log.Fatal().Err(err).Msg("Failed to compute image URL")
}
podman_utils.PrepareImage(image, flags.Image.PullPolicy)
shared.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)
podman.RunContainer("uyuni-pg-migration", migrationImage.Name, migrationImage.Tag, extraArgs,
adm_utils.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)
podman.RunContainer("uyuni-pg-migration", image, extraArgs,
[]string{"/var/lib/uyuni-tools/migrate.sh"})
}

shared.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
podman.RunContainer("uyuni-migration", flags.Image.Name, flags.Image.Tag, extraArgs,
adm_utils.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
podman.RunContainer("uyuni-migration", serverImage, extraArgs,
[]string{"/var/lib/uyuni-tools/migrate.sh"})

fullImage := fmt.Sprintf("%s:%s", flags.Image.Name, flags.Image.Tag)

podman.GenerateSystemdService(tz, fullImage, false, viper.GetStringSlice("podman.arg"))
podman.GenerateSystemdService(tz, serverImage, false, viper.GetStringSlice("podman.arg"))

// Start the service
podman_utils.EnableService("uyuni-server")
Expand Down
61 changes: 0 additions & 61 deletions mgradm/cmd/migrate/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
package shared

import (
"bytes"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/uyuni-project/uyuni-tools/shared/utils"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/templates"
)

func GetSshAuthSocket() string {
Expand Down Expand Up @@ -43,61 +40,3 @@ func GetSshPaths() (string, string) {

return sshConfigPath, sshKnownhostsPath
}

func GenerateMigrationScript(sourceFqdn string, kubernetes bool) string {
scriptDir, err := os.MkdirTemp("", "mgradm-*")
if err != nil {
log.Fatal().Err(err).Msgf("Failed to create temporary directory")
}

data := templates.MigrateScriptTemplateData{
Volumes: utils.VOLUMES,
SourceFqdn: sourceFqdn,
Kubernetes: kubernetes,
}

scriptPath := filepath.Join(scriptDir, "migrate.sh")
if err = utils.WriteTemplateToFile(data, scriptPath, 0555, true); err != nil {
log.Fatal().Err(err).Msgf("Failed to generate migration script")
}

return scriptDir
}

func GeneratePgMigrationScript(scriptDir string, oldPgVersion string, newPgVersion string, kubernetes bool) {
data := templates.MigratePostgresVersionTemplateData {
OldVersion: oldPgVersion,
NewVersion: newPgVersion,
Kubernetes: kubernetes,
}

scriptPath := filepath.Join(scriptDir, "migrate.sh")
if err := utils.WriteTemplateToFile(data, scriptPath, 0555, true); err != nil {
log.Fatal().Err(err).Msgf("Failed to generate migration script")
}
}

func GenerateFinalizePostgresMigrationScript(scriptDir string, RunAutotune bool, RunReindex bool, RunSchemaUpdate bool, RunDistroMigration bool, kubernetes bool) {
data := templates.FinalizePostgresTemplateData {
RunAutotune: RunAutotune,
RunReindex: RunReindex,
RunSchemaUpdate: RunSchemaUpdate,
RunDistroMigration: RunDistroMigration,
Kubernetes: kubernetes,
}

scriptPath := filepath.Join(scriptDir, "migrate.sh")
if err := utils.WriteTemplateToFile(data, scriptPath, 0555, true); err != nil {
log.Fatal().Err(err).Msgf("Failed to generate migration script")
}
}

func ReadContainerData(scriptDir string) (string, string, string) {
data, err := os.ReadFile(filepath.Join(scriptDir, "data"))
if err != nil {
log.Fatal().Msgf("Failed to read data extracted from source host")
}
viper.SetConfigType("env")
viper.ReadConfig(bytes.NewBuffer(data))
return viper.GetString("Timezone"), viper.GetString("old_pg_version"), viper.GetString("new_pg_version")
}
4 changes: 2 additions & 2 deletions mgradm/cmd/upgrade/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TODO: looong description
`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
viper := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)
viper, _ := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)
var flags kubernetesUpgradeFlags
if err := viper.Unmarshal(&flags); err != nil {
log.Fatal().Err(err).Msg("Failed to Unmarshal configuration")
Expand All @@ -44,7 +44,7 @@ TODO: looong description
`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
viper := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)
viper, _ := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)
var flags kubernetesUpgradeFlags
if err := viper.Unmarshal(&flags); err != nil {
log.Fatal().Err(err).Msg("Failed to unmarshall configuration")
Expand Down
18 changes: 9 additions & 9 deletions mgradm/cmd/upgrade/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/migrate/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/kubernetes"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
shared_kubernetes "github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

func upgradeKubernetes(globalFlags *types.GlobalFlags, flags *kubernetesUpgradeFlags, cmd *cobra.Command, args []string) {
cnx := utils.NewConnection("kubectl", "", shared_kubernetes.ServerFilter)
cnx := shared.NewConnection("kubectl", "", shared_kubernetes.ServerFilter)
fqdn := args[0]

// Prepare the migration script and folder
scriptDir := shared.GenerateMigrationScript(fqdn, true)
scriptDir := adm_utils.GenerateMigrationScript(fqdn, true)
defer os.RemoveAll(scriptDir)

// We don't need the SSL certs at this point of the migration
Expand All @@ -29,7 +29,7 @@ func upgradeKubernetes(globalFlags *types.GlobalFlags, flags *kubernetesUpgradeF
kubeconfig := clusterInfos.GetKubeconfig()
//TODO: check if we need to handle SELinux policies, as we do in podman

tz, oldPgVersion, newPgVersion := shared.ReadContainerData(scriptDir)
tz, oldPgVersion, newPgVersion := adm_utils.ReadContainerData(scriptDir)

helmArgs := []string{
"--reset-values",
Expand All @@ -47,15 +47,15 @@ func upgradeKubernetes(globalFlags *types.GlobalFlags, flags *kubernetesUpgradeF
if migrationImage.Name == "" {
migrationImage.Name = fmt.Sprintf("%s-migration-%s-%s", flags.Image.Name, oldPgVersion, newPgVersion)
}
shared.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)
adm_utils.GeneratePgMigrationScript(scriptDir, oldPgVersion, newPgVersion, false)

kubernetes.UyuniUpgrade(&migrationImage, &flags.Helm, kubeconfig, fqdn, clusterInfos.Ingress, helmArgs...)
kubernetes.RunMigration(cnx, scriptDir)
adm_utils.RunMigration(cnx, scriptDir)
}

shared.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
adm_utils.GenerateFinalizePostgresMigrationScript(scriptDir, true, oldPgVersion != newPgVersion, true, true, false)
kubernetes.UyuniUpgrade(&flags.Image, &flags.Helm, kubeconfig, fqdn, clusterInfos.Ingress, helmArgs...)
kubernetes.RunMigration(cnx, scriptDir)
adm_utils.RunMigration(cnx, scriptDir)

helmArgs = append(helmArgs, kubernetes.SetupSsl(&flags.Helm, kubeconfig, scriptDir, flags.Ssl.Password, flags.Image.PullPolicy)...)

Expand Down
7 changes: 5 additions & 2 deletions mgradm/cmd/upgrade/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ TODO: looong description
`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
viper := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)
viper, _ := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)

var flags podmanUpgradeFlags
if err := viper.Unmarshal(&flags); err != nil {
log.Fatal().Err(err).Msg("Failed to Unmarshal configuration")
Expand All @@ -43,7 +44,9 @@ TODO: looong description
`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
viper := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)

viper, _ := utils.ReadConfig(globalFlags.ConfigPath, "admconfig", cmd)

var flags podmanUpgradeFlags
if err := viper.Unmarshal(&flags); err != nil {
log.Fatal().Err(err).Msg("Failed to unmarshall configuration")
Expand Down
Loading

0 comments on commit 6b6d3dd

Please sign in to comment.