Skip to content

Commit

Permalink
MID-8842 ninja - upgrade command, work started + some minor cleanup
Browse files Browse the repository at this point in the history
(cherry picked from commit bb8630c)
  • Loading branch information
1azyman committed Sep 4, 2023
1 parent e1e3d9d commit cecc18e
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static void main(String[] args) {

int exitCode = result.exitCode();
if (exitCode != 0) {
String exitMessage = result.exitMessage();
if (exitMessage != null) {
System.err.println(exitMessage);
}
System.exit(exitCode);
}
}
Expand Down Expand Up @@ -131,7 +135,7 @@ public void setErr(@NotNull PrintStream err) {
Object result = action.execute();
if (result instanceof ActionResult) {
ActionResult<?> actionResult = (ActionResult<?>) result;
return new MainResult<>(actionResult.result(), actionResult.exitCode());
return new MainResult<>(actionResult.result(), actionResult.exitCode(), actionResult.exitMessage());
}

return new MainResult<>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public final class MainResult<T> {

private final int exitCode;

private final String exitMessage;

public static final MainResult<?> EMPTY_ERROR = new MainResult<>(null, 1);

public static final MainResult<?> EMPTY_SUCCESS = new MainResult<>(null, 0);
Expand All @@ -27,8 +29,13 @@ public MainResult(T result) {
}

public MainResult(T result, int exitCode) {
this(result, exitCode, null);
}

public MainResult(T result, int exitCode, String exitMessage) {
this.result = result;
this.exitCode = exitCode;
this.exitMessage = exitMessage;
}

public T result() {
Expand All @@ -38,4 +45,8 @@ public T result() {
public int exitCode() {
return exitCode;
}

public String exitMessage() {
return exitMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ public final class ActionResult<T> {

private final int exitCode;

private final String exitMessage;

public ActionResult(T result) {
this(result, 0);
}

public ActionResult(T result, int exitCode) {
this(result, exitCode, null);
}

public ActionResult(T result, int exitCode, String exitMessage) {
this.result = result;
this.exitCode = exitCode;
this.exitMessage = exitMessage;
}

public T result() {
Expand All @@ -34,4 +41,8 @@ public T result() {
public int exitCode() {
return exitCode;
}

public String exitMessage() {
return exitMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja.action;

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

public abstract class ComplexAction<O, R> extends Action<O, R> {

protected <O, T> T executeAction(Action<O, T> action, O options) throws Exception {
action.init(context, options);

log.info("");
log.info(ConsoleFormat.formatActionStartMessage(action));
log.info("");

return action.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public ActionResult<Boolean> execute() throws Exception {
}

private boolean checkDatabaseSchemaVersion(RepositoryService repository) {
log.info("Checking database schema version");

RepositoryDiag repositoryInfo = repository.getRepositoryDiag();

boolean result = validateChangeNumber(
Expand Down Expand Up @@ -106,6 +108,8 @@ private String getValue(List<LabeledString> list, String name) {
}

private boolean checkNodesVersion(RepositoryService repository) throws SchemaException {
log.info("Checking node versions in midPoint cluster");

OperationResult result = new OperationResult("Search nodes");

SearchResultList<PrismObject<NodeType>> nodes = repository.searchObjects(NodeType.class, null, null, result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

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

import org.apache.commons.lang3.StringUtils;

import com.evolveum.midpoint.ninja.action.ActionResult;
import com.evolveum.midpoint.ninja.action.ComplexAction;
import com.evolveum.midpoint.ninja.action.VerifyAction;
import com.evolveum.midpoint.ninja.action.VerifyOptions;
import com.evolveum.midpoint.ninja.util.NinjaUtils;

public class UpgradeAction extends ComplexAction<UpgradeOptions, ActionResult<Void>> {

@Override
public String getOperationName() {
return "upgrade";
}

@Override
public ActionResult<Void> execute() throws Exception {
log.info("Do you want to run objects verification before upgrade? yes/skip/cancel (y/s/C) [y] ");
String response = NinjaUtils.readInput(log, input -> StringUtils.isEmpty(input) || input.matches("[ysC]"));

if (StringUtils.isEmpty(response) || "y".equalsIgnoreCase(response)) {
VerifyOptions verifyOptions = new VerifyOptions();
// todo options
executeAction(new VerifyAction(), verifyOptions);
}

if ("s".equalsIgnoreCase(response)) {
log.warn("Skipping objects verification");
}

if ("C".equals(response)) {
return new ActionResult<>(null, 0, "Upgrade cancelled by user");
}

// UpgradeObjectsOptions upgradeObjectsOptions = new UpgradeObjectsOptions();
// // todo options
// executeAction(new UpgradeObjectsAction(), upgradeObjectsOptions);
//
// PreUpgradeCheckOptions preUpgradeCheckOptions = new PreUpgradeCheckOptions();
// // todo options
// executeAction(new PreUpgradeCheckAction(), preUpgradeCheckOptions);
//
// DownloadDistributionOptions downloadDistributionOptions = new DownloadDistributionOptions();
// // todo options
// executeAction(new DownloadDistributionAction(), downloadDistributionOptions);
//
// // todo run sql 2x
//
// UpgradeInstallationOptions upgradeInstallationOptions = new UpgradeInstallationOptions();
// executeAction(new UpgradeInstallationAction(), upgradeInstallationOptions);

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.evolveum.midpoint.ninja.action.upgrade.action;

import com.evolveum.midpoint.ninja.action.*;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeConstants;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import java.io.File;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.fusesource.jansi.Ansi;

import java.io.File;
import java.util.stream.Collectors;
import com.evolveum.midpoint.ninja.action.*;
import com.evolveum.midpoint.ninja.action.upgrade.UpgradeConstants;

public class UpgradeDistributionAction extends Action<UpgradeDistributionOptions, ActionResult<Void>> {
public class UpgradeDistributionAction extends ComplexAction<UpgradeDistributionOptions, ActionResult<Void>> {

@Override
public String getOperationName() {
Expand All @@ -29,9 +28,7 @@ public ActionResult<Void> execute() throws Exception {
if (!options.isSkipPreCheck()) {
PreUpgradeCheckOptions preUpgradeCheckOptions = new PreUpgradeCheckOptions();

PreUpgradeCheckAction preUpgradeCheckAction = new PreUpgradeCheckAction();
preUpgradeCheckAction.init(context, preUpgradeCheckOptions);
ActionResult<Boolean> shouldContinue = executeAction(preUpgradeCheckAction);
ActionResult<Boolean> shouldContinue = executeAction(new PreUpgradeCheckAction(), preUpgradeCheckOptions);
if (shouldContinue.result()) {
log.info(Ansi.ansi().fgGreen().a("Pre-upgrade check succeeded.").reset().toString());
} else {
Expand All @@ -48,9 +45,7 @@ public ActionResult<Void> execute() throws Exception {
verifyOptions.setMultiThread(options.getVerificationThreads());
verifyOptions.setStopOnCriticalError(options.isStopOnCriticalError());

VerifyAction verifyAction = new VerifyAction();
verifyAction.init(context, verifyOptions);
VerifyResult verifyResult = executeAction(verifyAction);
VerifyResult verifyResult = executeAction(new VerifyAction(), verifyOptions);
if (!verifyResult.hasCriticalItems()) {
log.info(Ansi.ansi().fgGreen().a("Pre-upgrade verification succeeded.").reset().toString());
} else {
Expand All @@ -74,10 +69,7 @@ public ActionResult<Void> execute() throws Exception {
downloadOpts.setDistributionArchive(options.getDistributionArchive());
downloadOpts.setDistributionVersion(options.getDistributionVersion());

DownloadDistributionAction downloadAction = new DownloadDistributionAction();
downloadAction.init(context, downloadOpts);

DownloadDistributionResult downloadResult = executeAction(downloadAction);
DownloadDistributionResult downloadResult = executeAction(new DownloadDistributionAction(), downloadOpts);

File distributionDirectory = downloadResult.getDistributionDirectory();

Expand All @@ -95,21 +87,11 @@ public ActionResult<Void> execute() throws Exception {
installationOpts.setBackup(options.isBackupMidpointDirectory());
installationOpts.setInstallationDirectory(options.getInstallationDirectory());

UpgradeInstallationAction installationAction = new UpgradeInstallationAction();
installationAction.init(context, installationOpts);
executeAction(installationAction);
executeAction(new UpgradeInstallationAction(), installationOpts);

return null;
}

private <O, T> T executeAction(Action<O, T> action) throws Exception {
log.info("");
log.info(ConsoleFormat.formatActionStartMessage(action));
log.info("");

return action.execute();
}

private void runUpgradeSql(RunSqlOptions.Mode mode, File distributionDirectory) throws Exception {
RunSqlOptions runSqlOptions = new RunSqlOptions();
runSqlOptions.setUpgrade(true);
Expand All @@ -118,9 +100,6 @@ private void runUpgradeSql(RunSqlOptions.Mode mode, File distributionDirectory)
.map(f -> new File(distributionDirectory, f.getPath()))
.collect(Collectors.toList()));

RunSqlAction action = new RunSqlAction();
action.init(context, runSqlOptions);

executeAction(action);
executeAction(new RunSqlAction(), runSqlOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ActionResult<UpgradeObjectsItemsSummary> execute() throws Exception {
if (!options.getFiles().isEmpty()) {
if (!options.isSkipUpgradeWarning()) {
log.warn("File update will remove XML comments and change formatting. Do you wish to proceed? (Y/n)");
String result = NinjaUtils.readInput(input -> StringUtils.isEmpty(input) || input.equalsIgnoreCase("y"));
String result = NinjaUtils.readInput(log, input -> StringUtils.isEmpty(input) || input.equalsIgnoreCase("y"));

if (result.trim().equalsIgnoreCase("n")) {
log.info("Upgrade aborted");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

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

import com.beust.jcommander.Parameters;

@Parameters(resourceBundle = "messages", commandDescriptionKey = "upgrade")
public class UpgradeOptions {
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public enum Command {

PRE_UPGRADE_CHECK("pre-upgrade-check", PreUpgradeCheckOptions.class, PreUpgradeCheckAction.class),

HELP("help", HelpOptions.class, HelpAction.class);
HELP("help", HelpOptions.class, HelpAction.class),

UPGRADE("upgrade", UpgradeOptions.class, UpgradeAction.class);

private final String commandName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
import com.beust.jcommander.IUsageFormatter;
import com.beust.jcommander.JCommander;

import com.evolveum.midpoint.ninja.impl.NinjaUsageFormatter;
import com.evolveum.midpoint.ninja.impl.*;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import com.evolveum.midpoint.ninja.action.BaseOptions;
import com.evolveum.midpoint.ninja.action.ConnectionOptions;
import com.evolveum.midpoint.ninja.impl.Command;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.impl.NinjaException;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
Expand Down Expand Up @@ -281,11 +278,18 @@ private static boolean containsFileNames(String[] names, String filenameRegex) {
return Arrays.stream(names).anyMatch(s -> s.matches(filenameRegex));
}

public static String readInput(Function<String, Boolean> inputValidation) throws IOException {
public static String readInput(Log log, Function<String, Boolean> inputValidation) throws IOException {
boolean first = true;

String line = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
boolean accepted = false;
while (!accepted) {
if (!first) {
log.error("Invalid input, please try again");
}
first = false;

line = br.readLine();

accepted = inputValidation.apply(line);
Expand Down
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 @@ -146,3 +146,4 @@ upgradeObjects.skipUpgradeWarning=Skip warning about files being updated.
upgradeDistribution.distributionDirectory=Distribution directory where distribution ZIP file should be expanded as output of this command.
help=Print this help, or prints help for specific command.
help.command=[Command name]
upgrade=Upgrade midPoint
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ public static void main(String[] args) throws Exception {

// cmd = "-h run-sql";
// execute(cmd);
testJANSI();
// testJANSI();

cmd = "upgrade";
execute(cmd);
}

private static void execute(String args) {
Expand Down

0 comments on commit cecc18e

Please sign in to comment.