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

Update process should check version and not allow reversion through update #765

Closed
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,8 @@ default OperationException cancelledByConfilcts() {
bundle.getString("prospero.updates.apply.candidate.cancel_conflicts"),
CliConstants.NO_CONFLICTS_ONLY));
}

default String downgradeDetected(){
return bundle.getString("prospero.perform.update.downgrade.detected");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.eclipse.aether.artifact.Artifact;
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.diff.FsDiff;
import org.jboss.galleon.diff.FsEntry;
Expand All @@ -39,6 +40,7 @@
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.Repository;
import org.wildfly.channel.version.VersionMatcher;
import org.wildfly.prospero.ProsperoLogger;
import org.wildfly.prospero.actions.ApplyCandidateAction;
import org.wildfly.prospero.actions.SubscribeNewServerAction;
Expand All @@ -60,6 +62,7 @@
import org.wildfly.prospero.api.TemporaryFilesManager;
import org.wildfly.prospero.galleon.FeaturePackLocationParser;
import org.wildfly.prospero.galleon.GalleonUtils;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;
import org.wildfly.prospero.model.InstallationProfile;
import org.wildfly.prospero.updates.UpdateSet;
Expand Down Expand Up @@ -120,7 +123,6 @@ public Integer call() throws Exception {
log.tracef("Perform full update");

console.println(CliMessages.MESSAGES.updateHeader(installationDir));

try (UpdateAction updateAction = actionFactory.update(installationDir, mavenOptions, console, repositories)) {
performUpdate(updateAction, yes, console, installationDir, noConflictsOnly);
}
Expand All @@ -136,6 +138,17 @@ private boolean performUpdate(UpdateAction updateAction, boolean yes, CliConsole
Path targetDir = null;
try {
targetDir = Files.createTempDirectory("update-candidate");
try (InstallationMetadata installationMetadata = updateAction.getInstallationMetadata()) {
if (installationMetadata.getManifestVersions().isPresent()) {
List<ManifestVersionRecord.MavenManifest> mavenManifests = installationMetadata.getManifestVersions().get().getMavenManifests();
final List<Artifact> manifestUpdates = updateAction.findCurrentChannelSessionManifests();
if (!isManifestDowngraded(mavenManifests, manifestUpdates)) {
console.println(CliMessages.MESSAGES.downgradeDetected());
return false; // Terminate the method if a downgrade is detected
}
}
}

if (buildUpdate(updateAction, targetDir, yes, console, () -> console.confirmUpdates())) {
console.println("");
console.buildUpdatesComplete();
Expand Down Expand Up @@ -444,17 +457,17 @@ private FeaturePackLocation getFpl(InstallationProfile knownFeaturePack, String
public UpdateCommand(CliConsole console, ActionFactory actionFactory) {
super(console, actionFactory, CliConstants.Commands.UPDATE,
List.of(
new UpdateCommand.PrepareCommand(console, actionFactory),
new UpdateCommand.ApplyCommand(console, actionFactory),
new UpdateCommand.PerformCommand(console, actionFactory),
new UpdateCommand.ListCommand(console, actionFactory),
new PrepareCommand(console, actionFactory),
new ApplyCommand(console, actionFactory),
new PerformCommand(console, actionFactory),
new ListCommand(console, actionFactory),
new SubscribeCommand(console, actionFactory))
);

}

private static boolean buildUpdate(UpdateAction updateAction, Path updateDirectory, boolean yes, CliConsole console, Supplier<Boolean> confirmation) throws OperationException, ProvisioningException {
final UpdateSet updateSet = updateAction.findUpdates();

console.updatesFound(updateSet.getArtifactUpdates());
if (updateSet.isEmpty()) {
return false;
Expand Down Expand Up @@ -493,4 +506,37 @@ public static Path detectProsperoInstallationPath() throws ArgumentParsingExcept
return Paths.get(modulePath).toAbsolutePath().getParent();
}


public static boolean isManifestDowngraded(List<ManifestVersionRecord.MavenManifest> mavenManifests, List<Artifact> manifestUpdates ) {

for (ManifestVersionRecord.MavenManifest installedManifest : mavenManifests) {
Artifact updateArtifact = null;

// Find the corresponding update artifact for the installedManifest
for (Artifact manifestUpdate : manifestUpdates) {
if (manifestUpdate.getGroupId().equals(installedManifest.getGroupId()) &&
manifestUpdate.getArtifactId().equals(installedManifest.getArtifactId())) {
updateArtifact = manifestUpdate;
break;
}
}

if (updateArtifact != null) {
// Compare versions
String installedVersion = installedManifest.getVersion();
String availableVersion = updateArtifact.getVersion();
if (VersionMatcher.COMPARATOR.compare(installedVersion, availableVersion) < 0) {
log.debugf("Upgrade available for %s:%s: %s -> %s",
installedManifest.getGroupId(), installedManifest.getArtifactId(), installedVersion, availableVersion);
return true;
} else if (VersionMatcher.COMPARATOR.compare(installedVersion, availableVersion) > 0) {
log.debugf("Downgrade detected for " + installedManifest.getArtifactId() + ": " + installedVersion + " -> " + availableVersion);
return false;
} else {
System.out.println(installedManifest.getArtifactId() + " is up to date.");
}
}
}
return true;
}
}
3 changes: 2 additions & 1 deletion prospero-cli/src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,5 @@ prospero.install.list.profile.subscribe.channels=Subscribed channels:\u0020
prospero.install.list.profile.featurePacks=Installed feature packs:\u0020

prospero.candidate.apply.error.rolled_back.desc=The incomplete update changes have been rolled back. Please resolve above error and try to perform update again.
prospero.candidate.apply.error.rollback_error.desc=Unable to restore the incomplete update changes. The server might have been left in a corrupted state, please check the backup of the server at %s.
prospero.candidate.apply.error.rollback_error.desc=Unable to restore the incomplete update changes. The server might have been left in a corrupted state, please check the backup of the server at %s.
prospero.perform.update.downgrade.detected=This operation would result in a manifest being downgraded. Use revert command instead if you want to return to an older installation state.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.wildfly.prospero.actions.UpdateAction;
import org.wildfly.prospero.api.ArtifactChange;
import org.wildfly.prospero.api.FileConflict;
import org.wildfly.prospero.api.InstallationMetadata;
import org.wildfly.prospero.api.MavenOptions;
import org.wildfly.prospero.cli.ActionFactory;
import org.wildfly.prospero.cli.CliMessages;
Expand Down Expand Up @@ -98,6 +99,7 @@ public void setUp() throws Exception {

MetadataTestUtils.createInstallationMetadata(installationDir);
MetadataTestUtils.createGalleonProvisionedState(installationDir, A_PROSPERO_FP);
when(updateAction.getInstallationMetadata()).thenReturn(InstallationMetadata.loadInstallation(installationDir));
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.jboss.galleon.util.PathsUtils;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.Channel;
import org.wildfly.channel.Repository;
import org.wildfly.prospero.ProsperoLogger;
Expand Down Expand Up @@ -171,4 +175,18 @@ private ProsperoConfig addTemporaryRepositories(List<Repository> repositories) {

return new ProsperoConfig(channels, prosperoConfig.getMavenOptions());
}

public List<Artifact> findCurrentChannelSessionManifests() throws ProvisioningException, OperationException {
try (GalleonEnvironment galleonEnv = getGalleonEnv(installDir);
ChannelSession channelSession = galleonEnv.getChannelSession()) {

return channelSession.getRuntimeChannels().stream()
.map(runtimeChannel -> runtimeChannel.getChannelDefinition().getManifestCoordinate())
.map(coordinate -> new DefaultArtifact(coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getExtension(), coordinate.getVersion()))
.collect(Collectors.toList());
}
}
public InstallationMetadata getInstallationMetadata() {
return metadata;
}
}
Loading