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

Add --prepare option to migrate command #377

Closed
wants to merge 2 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
2 changes: 1 addition & 1 deletion mgradm/cmd/install/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func installForKubernetes(globalFlags *types.GlobalFlags,
helmArgs = append(helmArgs, sslArgs...)

// Deploy Uyuni and wait for it to be up
if err := kubernetes.Deploy(cnx, &flags.Image, &flags.Helm, &flags.Ssl, clusterInfos, fqdn, flags.Debug.Java, helmArgs...); err != nil {
if err := kubernetes.Deploy(cnx, &flags.Image, &flags.Helm, &flags.Ssl, clusterInfos, fqdn, flags.Debug.Java, false, helmArgs...); err != nil {
return shared_utils.Errorf(err, L("cannot deploy uyuni"))
}

Expand Down
9 changes: 7 additions & 2 deletions mgradm/cmd/migrate/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func migrateToKubernetes(
sshConfigPath, sshKnownhostsPath := migration_shared.GetSshPaths()

// Prepare the migration script and folder
scriptDir, err := adm_utils.GenerateMigrationScript(fqdn, flags.User, true)
scriptDir, err := adm_utils.GenerateMigrationScript(fqdn, flags.User, true, flags.Prepare)
if err != nil {
return utils.Errorf(err, L("failed to generate migration script"))
}
Expand All @@ -71,7 +71,7 @@ func migrateToKubernetes(
var sslFlags adm_utils.SslCertFlags

// Deploy for running migration command
if err := kubernetes.Deploy(cnx, &flags.Image, &flags.Helm, &sslFlags, clusterInfos, fqdn, false,
if err := kubernetes.Deploy(cnx, &flags.Image, &flags.Helm, &sslFlags, clusterInfos, fqdn, false, flags.Prepare,
"--set", "migration.ssh.agentSocket="+sshAuthSocket,
"--set", "migration.ssh.configPath="+sshConfigPath,
"--set", "migration.ssh.knownHostsPath="+sshKnownhostsPath,
Expand Down Expand Up @@ -101,6 +101,11 @@ func migrateToKubernetes(
return utils.Errorf(err, L("cannot set replicas to 0"))
}

if flags.Prepare {
log.Info().Msg(L("Migration prepared. Run the 'migrate' command without '--prepare' to finish the migration."))
return nil
}

defer func() {
// if something is running, we don't need to set replicas to 1
if _, err = shared_kubernetes.GetNode("uyuni"); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion mgradm/cmd/migrate/podman/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ func migrateToPodman(globalFlags *types.GlobalFlags, flags *podmanMigrateFlags,
sshAuthSocket := migration_shared.GetSshAuthSocket()
sshConfigPath, sshKnownhostsPath := migration_shared.GetSshPaths()

tz, oldPgVersion, newPgVersion, err := podman.RunMigration(serverImage, flags.Image.PullPolicy, sshAuthSocket, sshConfigPath, sshKnownhostsPath, sourceFqdn, flags.User)
tz, oldPgVersion, newPgVersion, err := podman.RunMigration(serverImage, flags.Image.PullPolicy, sshAuthSocket, sshConfigPath, sshKnownhostsPath, sourceFqdn, flags.User, flags.Prepare)
if err != nil {
return utils.Errorf(err, L("cannot run migration script"))
}
if flags.Prepare {
log.Info().Msg(L("Migration prepared. Run the 'migrate' command without '--prepare' to finish the migration."))
cbosdo marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

if oldPgVersion != newPgVersion {
if err := podman.RunPgsqlVersionUpgrade(flags.Image, flags.DbUpgradeImage, oldPgVersion, newPgVersion); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions mgradm/cmd/migrate/shared/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

// MigrateFlags represents flag required by migration command.
type MigrateFlags struct {
Prepare bool
Image types.ImageFlags `mapstructure:",squash"`
DbUpgradeImage types.ImageFlags `mapstructure:"dbupgrade"`
User string
Expand All @@ -21,6 +22,7 @@ type MigrateFlags struct {

// AddMigrateFlags add migration flags to a command.
func AddMigrateFlags(cmd *cobra.Command) {
cmd.Flags().Bool("prepare", false, L("Prepare the mgration - copy the data without stopping the source server."))
utils.AddMirrorFlag(cmd)
utils.AddImageFlag(cmd)
utils.AddDbUpgradeImageFlag(cmd)
Expand Down
12 changes: 7 additions & 5 deletions mgradm/shared/kubernetes/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ const HELM_APP_NAME = "uyuni"
// Deploy execute a deploy of a given image and helm to a cluster.
func Deploy(cnx *shared.Connection, imageFlags *types.ImageFlags,
helmFlags *cmd_utils.HelmFlags, sslFlags *cmd_utils.SslCertFlags, clusterInfos *kubernetes.ClusterInfos,
fqdn string, debug bool, helmArgs ...string) error {
fqdn string, debug bool, prepare bool, helmArgs ...string) error {
// If installing on k3s, install the traefik helm config in manifests
isK3s := clusterInfos.IsK3s()
IsRke2 := clusterInfos.IsRke2()
if isK3s {
InstallK3sTraefikConfig(debug)
} else if IsRke2 {
kubernetes.InstallRke2NginxConfig(utils.TCP_PORTS, utils.UDP_PORTS, helmFlags.Uyuni.Namespace)
if !prepare {
if isK3s {
InstallK3sTraefikConfig(debug)
} else if IsRke2 {
kubernetes.InstallRke2NginxConfig(utils.TCP_PORTS, utils.UDP_PORTS, helmFlags.Uyuni.Namespace)
}
}

serverImage, err := utils.ComputeImage(*imageFlags)
Expand Down
4 changes: 2 additions & 2 deletions mgradm/shared/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ func UpdateSslCertificate(cnx *shared.Connection, chain *ssl.CaChain, serverPair
}

// RunMigration migrate an existing remote server to a container.
func RunMigration(serverImage string, pullPolicy string, sshAuthSocket string, sshConfigPath string, sshKnownhostsPath string, sourceFqdn string, user string) (string, string, string, error) {
scriptDir, err := adm_utils.GenerateMigrationScript(sourceFqdn, user, false)
func RunMigration(serverImage string, pullPolicy string, sshAuthSocket string, sshConfigPath string, sshKnownhostsPath string, sourceFqdn string, user string, prepare bool) (string, string, string, error) {
scriptDir, err := adm_utils.GenerateMigrationScript(sourceFqdn, user, false, prepare)
if err != nil {
return "", "", "", utils.Errorf(err, L("cannot generate migration script"))
}
Expand Down
8 changes: 8 additions & 0 deletions mgradm/shared/templates/migrateScriptTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ if test -e /tmp/ssh_config; then
fi
SSH="ssh -o User={{ .User }} -A $SSH_CONFIG "

{{ if .Prepare }}
echo "Preparing migration..."
$SSH {{ .SourceFqdn }} "sudo systemctl start postgresql.service"
{{ else }}
echo "Stopping spacewalk service..."
$SSH {{ .SourceFqdn }} "sudo spacewalk-service stop ; sudo systemctl start postgresql.service"
{{ end }}

$SSH {{ .SourceFqdn }} \
"echo \"COPY (SELECT MIN(CONCAT(org_id, '-', label)) AS target, base_path FROM rhnKickstartableTree GROUP BY base_path) TO STDOUT WITH CSV;\" \
|sudo spacewalk-sql --select-mode - " > distros

{{ if not .Prepare }}
echo "Stopping posgresql service..."
$SSH {{ .SourceFqdn }} "sudo systemctl stop postgresql.service"
{{ end }}

while IFS="," read -r target path ; do
echo "-/ $path"
Expand Down Expand Up @@ -144,6 +151,7 @@ type MigrateScriptTemplateData struct {
SourceFqdn string
User string
Kubernetes bool
Prepare bool
}

// Render will create migration script.
Expand Down
3 changes: 2 additions & 1 deletion mgradm/shared/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func RunMigration(cnx *shared.Connection, tmpPath string, scriptName string) err
}

// GenerateMigrationScript generates the script that perform migration.
func GenerateMigrationScript(sourceFqdn string, user string, kubernetes bool) (string, error) {
func GenerateMigrationScript(sourceFqdn string, user string, kubernetes bool, prepare bool) (string, error) {
scriptDir, err := os.MkdirTemp("", "mgradm-*")
if err != nil {
return "", utils.Errorf(err, L("failed to create temporary directory"))
Expand All @@ -142,6 +142,7 @@ func GenerateMigrationScript(sourceFqdn string, user string, kubernetes bool) (s
SourceFqdn: sourceFqdn,
User: user,
Kubernetes: kubernetes,
Prepare: prepare,
}

scriptPath := filepath.Join(scriptDir, "migrate.sh")
Expand Down
1 change: 1 addition & 0 deletions uyuni-tools.changes.nadvornik.prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add --prepare option to migrate command