Skip to content

Commit

Permalink
MID-8842 more implementation added to different upgrade steps, still …
Browse files Browse the repository at this point in the history
…a long way to go
  • Loading branch information
1azyman committed May 29, 2023
1 parent 1e456c1 commit fce5e29
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ConsoleProgressListener implements ProgressListener {
@Override
public void update(long bytesRead, long contentLength, boolean done) {
if (done) {
System.out.println(Ansi.ansi().eraseLine().fgGreen().a("Complete").reset());
System.out.println(Ansi.ansi().eraseLine().fgGreen().a("Download complete").reset());
return;
}

Expand All @@ -37,7 +37,7 @@ public void update(long bytesRead, long contentLength, boolean done) {

double newProgress = (double) (100 * bytesRead) / contentLength;
if (newProgress - progress > 1) {
System.out.print(Ansi.ansi().eraseLine(Ansi.Erase.ALL).fgBlue().a("Progress: " + FORMAT.format(newProgress) + "%\n").reset());
System.out.print(Ansi.ansi().eraseLine(Ansi.Erase.ALL).a("Progress: ").fgBlue().a(FORMAT.format(newProgress) + "%\n").reset());

progress = newProgress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public DistributionManager(@NotNull File tempDirectory) {
}

public File downloadDistribution(@NotNull String version, ProgressListener listener) throws IOException {
String versionNumber = LATEST_VERSION.equals(version) ? LATEST_VERSION_NUMBER : version;

String distributionFile = createFileName(versionNumber);
String distributionFile = createFileName(version);

File file = new File(tempDirectory, System.currentTimeMillis() + "-" + distributionFile);
FileUtils.forceMkdirParent(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
public class UpgradeAction extends Action<UpgradeOptions> {

private static final Class<? extends UpgradeStep>[] STEPS = new Class[] {
// todo upgrade initial objects, also all other objects that can be upgraded before midpoint version/DB/midpoint home was upgraded
UpgradePrecheckStep.class,
// UpgradePrecheckStep.class,
// VerifyStep.class,
// UpgradeObjectsBeforeShutdownStep.class,
// DownloadDistributionStep.class,
// UpgradeObjectsBeforeShutdownStep.class, // todo upgrade initial objects, also all other objects that can be upgraded before midpoint version/DB/midpoint home was upgraded
DownloadDistributionStep.class,
// DatabaseSchemaStep.class,
// UpgradeMidpointHomeStep.class,
UpgradeMidpointInstallationStep.class,
// UpgradeObjectsAfterShutdownStep.class, // todo upgrade initial objects, also all other objects (changes that had to be done after DB upgrade)
// todo what if recomputation/reconciliation/whatever task is needed?
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.evolveum.midpoint.ninja.action.upgrade;

public class UpgradeConstants {

public static final String SUPPORTED_VERSION_LTS = "4.4.4";

public static final String SUPPORTED_VERSION_FEATURE = "4.7.1";

public static final String SUPPORTED_VERSION_TARGET = "latest"; // todo change to 4.8

public static final String[] SUPPORTED_VERSIONS = { SUPPORTED_VERSION_LTS, SUPPORTED_VERSION_FEATURE, "4.8-SNAPSHOT" }; // todo remove viliam
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class UpgradeOptions {

public static final String P_BACKUP_MIDPOINT_DIRECTORY = "--backup-midpoint-directory";

public static final String P_INSTALLATION_DIRECTORY = "--installation-directory";

@Parameter(names = { P_ABORT_LONG }, descriptionKey = "upgrade.abort")
private Boolean abort;

Expand All @@ -40,6 +42,9 @@ public class UpgradeOptions {
@Parameter(names = { P_BACKUP_MIDPOINT_DIRECTORY }, descriptionKey = "upgrade.backupMidpointDirectory")
private Boolean backupMidpointDirectory;

@Parameter(names = { P_INSTALLATION_DIRECTORY }, descriptionKey = "upgrade.installationDirectory")
private File installationDirectory;

public Boolean isAbort() {
return abort;
}
Expand All @@ -59,4 +64,8 @@ public File getDistributionArchive() {
public Boolean isBackupMidpointDirectory() {
return backupMidpointDirectory;
}

public File getInstallationDirectory() {
return installationDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,22 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.evolveum.midpoint.ninja.util.Log;

import org.apache.commons.lang3.StringUtils;

import com.evolveum.midpoint.ninja.action.upgrade.*;

import org.fusesource.jansi.Ansi;

public class DownloadDistributionStep implements UpgradeStep<DownloadDistributionResult> {

private UpgradeStepsContext context;

private String version;

public DownloadDistributionStep(UpgradeStepsContext context) {
this.context = context;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public String getIdentifier() {
return "downloadDistribution";
Expand All @@ -48,15 +42,19 @@ public String getPresentableName() {

@Override
public DownloadDistributionResult execute() throws IOException {
final Log log = context.getContext().getLog();

final File tempDirectory = context.getTempDirectory();
final UpgradeOptions options = context.getOptions();
final File tempDirectory = options.getTempDirectory();

File distributionZipFile = options.getDistributionArchive();
if (distributionZipFile == null || !distributionZipFile.exists()) {
DistributionManager manager = new DistributionManager(tempDirectory);
ProgressListener listener = new ConsoleProgressListener();

distributionZipFile = manager.downloadDistribution(version, listener);
distributionZipFile = manager.downloadDistribution(UpgradeConstants.SUPPORTED_VERSION_TARGET, listener);
} else {
log.info(Ansi.ansi().fgGreen().a("Distribution zip already downloaded here: " + distributionZipFile.getAbsolutePath()).reset().toString());
}

File distributionDirectory = unzipDistribution(distributionZipFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,50 @@
import org.apache.commons.lang3.BooleanUtils;
import org.jetbrains.annotations.NotNull;

public class UpgradeMidpointHomeStep implements UpgradeStep<StepResult> {
public class UpgradeMidpointInstallationStep implements UpgradeStep<StepResult> {

private static final String VAR_DIRECTORY = "var";

private final UpgradeStepsContext context;

public UpgradeMidpointHomeStep(@NotNull UpgradeStepsContext context) {
public UpgradeMidpointInstallationStep(@NotNull UpgradeStepsContext context) {
this.context = context;
}

@Override
public String getIdentifier() {
return "upgradeMidpointHome";
return "upgradeMidpointInstallation";
}

public StepResult execute() throws Exception {
final ConnectionOptions connectionOptions = context.getContext().getOptions(ConnectionOptions.class);
final File midpointHomeDirectory = new File(connectionOptions.getMidpointHome());
@Override
public String getPresentableName() {
return "upgrade midpoint installation";
}

public StepResult execute() throws Exception {
final DownloadDistributionResult distributionResult = context.getResult(DownloadDistributionResult.class);
final File distributionDirectory = distributionResult.getDistributionDirectory();

final UpgradeOptions upgradeOptions = context.getOptions();
final boolean backupFiles = BooleanUtils.isTrue(upgradeOptions.isBackupMidpointDirectory());

File midpointInstallation = upgradeOptions.getInstallationDirectory();
if (midpointInstallation == null) {
final ConnectionOptions connectionOptions = context.getContext().getOptions(ConnectionOptions.class);
midpointInstallation = new File(connectionOptions.getMidpointHome()).getParentFile();
}

File backupDirectory = null;
if (backupFiles) {
backupDirectory = new File(midpointHomeDirectory, ".backup-" + System.currentTimeMillis());
backupDirectory = new File(midpointInstallation, ".backup-" + System.currentTimeMillis());
backupDirectory.mkdir();
}

for (File file : distributionDirectory.listFiles()) {
String fileName = file.getName();

if (backupFiles) {
File newFile = new File(midpointHomeDirectory, fileName);
File newFile = new File(midpointInstallation, fileName);

if (!VAR_DIRECTORY.equals(fileName)) {
if (newFile.exists()) {
Expand All @@ -71,9 +79,9 @@ public StepResult execute() throws Exception {
}

if (VAR_DIRECTORY.equals(fileName)) {
copyFiles(file, new File(midpointHomeDirectory, fileName), new File(backupDirectory, fileName), backupFiles);
copyFiles(file, new File(midpointInstallation, fileName), new File(backupDirectory, fileName), backupFiles);
} else {
FileUtils.moveToDirectory(file, midpointHomeDirectory, false);
FileUtils.moveToDirectory(file, midpointInstallation, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.HashSet;
import java.util.Set;

import com.evolveum.midpoint.ninja.action.upgrade.UpgradeConstants;

import org.fusesource.jansi.Ansi;
import org.jetbrains.annotations.NotNull;

Expand All @@ -25,14 +27,6 @@

public class UpgradePrecheckStep implements UpgradeStep<UpgradePreCheckResult> {

public static final String SUPPORTED_VERSION_LTS = "4.4.4";

public static final String SUPPORTED_VERSION_FEATURE = "4.7.1";

public static final String SUPPORTED_VERSION_TARGET = "4.8";

public static final String[] SUPPORTED_VERSIONS = { SUPPORTED_VERSION_LTS, SUPPORTED_VERSION_FEATURE, "4.8-SNAPSHOT" }; // todo remove viliam

private UpgradeStepsContext context;

public UpgradePrecheckStep(@NotNull UpgradeStepsContext context) {
Expand Down Expand Up @@ -84,7 +78,7 @@ public UpgradePreCheckResult execute() throws Exception {
}

String version = versions.iterator().next();
boolean match = Arrays.stream(SUPPORTED_VERSIONS).anyMatch(v -> v.equals(version));
boolean match = Arrays.stream(UpgradeConstants.SUPPORTED_VERSIONS).anyMatch(v -> v.equals(version));
if (!match) {
// todo error, version didn't match. Ask whether to continue?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ public String getIdentifier() {
public StepResult execute() throws Exception {
// todo implement


VerifyOptions options = new VerifyOptions();
// options.isCreateReport();
// options.getMultiThread();
// options.getOutput();
// options.isOverwrite();

VerifyRepositoryAction action = new VerifyRepositoryAction();
action.init(context.getContext(), options);
action.execute();

return new StepResult() {};
return new StepResult() {
};
}
}
1 change: 1 addition & 0 deletions tools/ninja/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ upgrade.tempDir=Temp directory. By default, JVM temp directory is used.
upgrade.confirmSteps=Confirm execution after each step.
upgrade.distributionArchive=Distribution archive file. This can be used to skip downloading archive.
upgrade.backupMidpointDirectory=Backup midPoint home directory
upgrade.installationDirectory=MidPoint installation directory
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public static void main(String[] args) {

// input = "-m /Users/lazyman/Work/monoted/git/evolveum/midpoint/_mess/midpoint-home verify --create-report".split(" ");
// input = "-h upgrade".split(" ");
input = "-m /Users/lazyman/Work/monoted/git/evolveum/midpoint/_mess/midpoint-home upgrade".split(" ");
input = (""
+ "-m /Users/lazyman/Work/monoted/git/evolveum/midpoint-support/_mess/mid8842/var "
+ "upgrade "
+ "--distribution-archive /Users/lazyman/Downloads/midpoint-latest-dist.zip "
+ "--installation-directory /Users/lazyman/Work/monoted/git/evolveum/midpoint-support/_mess/mid8842 "
+ "--backup-midpoint-directory "
+ "").split(" ");

Main.main(input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testJANSI() {

System.out.println(Ansi.ansi().fgBlue().a("Start").reset());
for (int i = 0; i < 10; i++) {
System.out.println(Ansi.ansi().eraseLine().fgGreen().a(i).reset());
System.out.println(Ansi.ansi().eraseLine(Ansi.Erase.ALL).fgGreen().a(i).reset());
}
System.out.println(Ansi.ansi().fgRed().a("Complete").reset());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.apache.commons.io.FileUtils;
import org.testng.annotations.Test;

import com.evolveum.midpoint.ninja.action.upgrade.step.UpgradeMidpointHomeStep;
import com.evolveum.midpoint.ninja.action.upgrade.step.UpgradeMidpointInstallationStep;

public class UpgradeMidpointHomeStepTest {

Expand All @@ -40,7 +40,7 @@ private void testUpgrade(boolean withBackup) throws Exception {
// opts.setMidpointHomeDirectory(midpointHome);
// opts.setBackupFiles(withBackup);

UpgradeMidpointHomeStep step = new UpgradeMidpointHomeStep(null);
UpgradeMidpointInstallationStep step = new UpgradeMidpointInstallationStep(null);
step.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class UpgradeStepsTest {

@Test(enabled = false)
public void test100DownloadDistribution() throws Exception {
DownloadDistributionStep step = new DownloadDistributionStep();
step.setVersion("4.4.4");
DownloadDistributionStep step = new DownloadDistributionStep(null);
// step.setVersion("4.4.4");
// step.execute();
}

Expand All @@ -48,8 +48,8 @@ public void test200UpgradeDatabaseSchema() throws Exception {

UpgradeStepsContext ctx = new UpgradeStepsContext(ninjaContext, upgradeOptions);

DownloadDistributionStep distributionStep = new DownloadDistributionStep();
distributionStep.setVersion("4.4.4");
DownloadDistributionStep distributionStep = new DownloadDistributionStep(ctx);
// distributionStep.setVersion("4.4.4");
DownloadDistributionResult result = distributionStep.execute();

ctx.addResult(DownloadDistributionStep.class, result);
Expand Down

0 comments on commit fce5e29

Please sign in to comment.