Skip to content

Commit

Permalink
MID-8842 upgrade action main implementation, skeleton, work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed May 26, 2023
1 parent 27c82c6 commit 38b4e20
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;

import com.evolveum.midpoint.ninja.action.Action;
import com.evolveum.midpoint.ninja.action.upgrade.step.*;
import com.evolveum.midpoint.ninja.action.upgrade.step.UpgradeObjectsAfterShutdownStep;
import com.evolveum.midpoint.ninja.action.upgrade.step.UpgradeObjectsBeforeShutdownStep;
import com.evolveum.midpoint.ninja.action.upgrade.step.VerifyStep;
import com.evolveum.midpoint.ninja.action.upgrade.step.VersionCheckStep;
import com.evolveum.midpoint.ninja.util.Log;

public class UpgradeAction extends Action<UpgradeOptions> {
Expand All @@ -24,34 +29,45 @@ public class UpgradeAction extends Action<UpgradeOptions> {
VersionCheckStep.class,
VerifyStep.class,
UpgradeObjectsBeforeShutdownStep.class,
DownloadDistributionStep.class,
DatabaseSchemaStep.class,
UpgradeMidpointHomeStep.class,
// DownloadDistributionStep.class,
// DatabaseSchemaStep.class,
// UpgradeMidpointHomeStep.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?
};

@Override
public void execute() throws Exception {
final Log log = context.getLog();

UpgradeStepsContext ctx = new UpgradeStepsContext(context, options);
File tempDirectory = ctx.getTempDirectory();

final Log log = context.getLog();
log.info("Using '" + tempDirectory.getAbsolutePath() + "' as temp directory");

FileUtils.forceMkdir(tempDirectory);

for (Class<? extends UpgradeStep> stepType : STEPS) {
long startTime = System.currentTimeMillis();

UpgradeStep step = createStepInstance(stepType, ctx);
try {
log.info("Starting step: " + step.getPresentableName());
log.info("Starting " + step.getPresentableName());

StepResult result = step.execute();
ctx.addResult(step.getClass(), result);

String identifier = step.getIdentifier();

File tmpFolder = null; // todo fix
File file = new File(tmpFolder, "step"); // todo check for existence
File file = new File(tempDirectory, "step");
if (!file.exists()) {
file.createNewFile();
}
FileUtils.write(file, identifier, StandardCharsets.UTF_8, false);

log.info(step.getPresentableName() + " finished");
long duration = System.currentTimeMillis() - startTime;
log.info(StringUtils.capitalize(step.getPresentableName()) + " finished in " +
DurationFormatUtils.formatDurationWords(duration, true, true));

if (!result.shouldContinue()) {
// todo print warning that processing was interrupted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

package com.evolveum.midpoint.ninja.action.upgrade;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.ninja.impl.NinjaContext;
Expand All @@ -20,11 +22,16 @@ public class UpgradeStepsContext {

private final UpgradeOptions options;

private final File tempDirectory;

private final Map<Class<?>, Object> result = new HashMap<>();

public UpgradeStepsContext(NinjaContext context, UpgradeOptions options) {
this.context = context;
this.options = options;

this.tempDirectory = options.getTempDirectory() != null ?
options.getTempDirectory() : new File(FileUtils.getTempDirectory(), "midpoint-upgrade");
}

public void addResult(@NotNull Class<?> step, Object result) {
Expand All @@ -49,4 +56,8 @@ public <T> T getResult(@NotNull Class<T> type) {
public NinjaContext getContext() {
return context;
}

public File getTempDirectory() {
return tempDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public String getIdentifier() {
return "downloadDistribution";
}

@Override
public String getPresentableName() {
return "download distribution";
}

@Override
public DownloadDistributionResult execute() throws IOException {
DistributionManager manager = new DistributionManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public String getIdentifier() {
return "upgradeObjectsAfterShutdown";
}

@Override
public String getPresentableName() {
return "upgrade objects after shutdown";
}

@Override
public StepResult execute() throws Exception {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public String getIdentifier() {
return "upgradeObjectsBeforeShutdown";
}

@Override
public String getPresentableName() {
return "upgrade objects before shutdown";
}

@Override
public StepResult execute() throws Exception {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja.action.upgrade;
package com.evolveum.midpoint.ninja.action.upgrade.step;

import com.evolveum.midpoint.ninja.action.upgrade.StepResult;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeStep;

public class VerifyStep implements UpgradeStep<StepResult> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public String getIdentifier() {
return "versionCheck";
}

@Override
public String getPresentableName() {
return "version check";
}

@Override
public VersionCheckResult execute() throws Exception {
String version = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public static void main(String[] args) {
"-t", "roles"};
// "-f", "<inOid xmlns=\"http://prism.evolveum.com/xml/ns/public/query-3\"><value>00000000-0000-0000-0000-000000000002</value></inOid>");

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 verify --create-report".split(" ");
// input = "-h upgrade".split(" ");
input = "--offline -m /Users/lazyman/Work/monoted/git/evolveum/midpoint/_mess/midpoint-home upgrade".split(" ");

Main.main(input);
}
Expand Down

0 comments on commit 38b4e20

Please sign in to comment.