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

[#476] If the manifest cannot be resolved from maven, fallback to a c… #550

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static void deployUpgrade() throws Exception {
.setOffline(false)
.build());
final RepositorySystem system = msm.newRepositorySystem();
final DefaultRepositorySystemSession session = msm.newRepositorySystemSession(system, false);
final DefaultRepositorySystemSession session = msm.newRepositorySystemSession(system);

/* mock a wildfly-core feature pack that requires a channel resolve
* the mocked artifact lives in {@code testRepo}
Expand Down Expand Up @@ -141,7 +141,7 @@ public void setUp() throws Exception {
installation = new ProvisioningAction(outputPath, mavenOptions, new CliConsole());
}

private static Artifact deployIfMissing(RepositorySystem system, DefaultRepositorySystemSession session, String groupId, String artifactId, String classifier, String extension) throws ArtifactResolutionException, DeploymentException {
protected static Artifact deployIfMissing(RepositorySystem system, DefaultRepositorySystemSession session, String groupId, String artifactId, String classifier, String extension) throws ArtifactResolutionException, DeploymentException {
final ArtifactRequest artifactRequest = new ArtifactRequest();
Artifact updateCli = new DefaultArtifact(groupId, artifactId, classifier, extension, UPGRADE_VERSION);
artifactRequest.setArtifact(updateCli);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.prospero.it.utils;

import org.apache.commons.io.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.RemoteRepository;
import org.jboss.galleon.ProvisioningException;
import org.wildfly.prospero.api.MavenOptions;
import org.wildfly.prospero.wfchannel.MavenSessionManager;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

public class TestRepositoryUtils {

private final String repositoryUrl;
private final RepositorySystem repoSystem;
private final DefaultRepositorySystemSession repoSession;

public TestRepositoryUtils(URL repository) throws ProvisioningException {
this.repositoryUrl = repository.toExternalForm();
final MavenSessionManager msm = new MavenSessionManager(MavenOptions.OFFLINE_NO_CACHE);
repoSystem = msm.newRepositorySystem();
repoSession = msm.newRepositorySystemSession(repoSystem);
}

public TestRepositoryUtils(Path repository) throws ProvisioningException, MalformedURLException {
this(repository.toUri().toURL());
}

public void deployArtifact(Artifact artifact) throws MalformedURLException, DeploymentException {
final DeployRequest request = new DeployRequest();
request.addArtifact(artifact);
request.setRepository(new RemoteRepository.Builder("test-repo", "default", repositoryUrl).build());
repoSystem.deploy(repoSession, request);
}

public void removeArtifact(String groupId, String artifactId) throws IOException {
try {
FileUtils.deleteDirectory(Path.of(new URI(repositoryUrl)).resolve(groupId.replace('.', File.separatorChar)).resolve(artifactId).toFile());
} catch (URISyntaxException e) {
// already validated
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,7 @@ public interface ProsperoLogger extends BasicLogger {

@Message(id = 264, value = "Bad artifact record format in the cache descriptor, line %d: '%s'")
IOException unableToReadArtifactCache(int row, String line, @Cause Exception e);

@Message(id = 265, value = "Unable to create temporary file")
ProvisioningException unableToCreateTemporaryFile(@Cause Throwable t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@
import org.wildfly.prospero.api.exceptions.MetadataException;
import org.wildfly.prospero.api.SavedState;
import org.wildfly.prospero.galleon.GalleonEnvironment;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.model.ProsperoConfig;
import org.wildfly.prospero.updates.UpdateSet;
import org.wildfly.prospero.wfchannel.MavenSessionManager;
import org.jboss.galleon.ProvisioningException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import static org.wildfly.prospero.galleon.GalleonUtils.MAVEN_REPO_LOCAL;

Expand Down Expand Up @@ -110,7 +108,7 @@ public void prepareRevert(SavedState savedState, MavenOptions mavenOptions, List
try (GalleonEnvironment galleonEnv = GalleonEnvironment
.builder(targetDir, prosperoConfig.getChannels(), mavenSessionManager)
.setConsole(console)
.setRestoreManifest(revertMetadata.getManifest())
.setRestoreManifest(revertMetadata.getManifest(), revertMetadata.getManifestVersions().orElse(null))
.setSourceServerPath(installation)
.build();
PrepareCandidateAction prepareCandidateAction = new PrepareCandidateAction(installation,
Expand All @@ -125,36 +123,17 @@ public void prepareRevert(SavedState savedState, MavenOptions mavenOptions, List
}

prepareCandidateAction.buildCandidate(targetDir, galleonEnv,
ApplyCandidateAction.Type.REVERT, provisioningConfig);
ApplyCandidateAction.Type.REVERT, provisioningConfig,
UpdateSet.EMPTY, (channels) -> revertMetadata.getManifestVersions());
}

revertCurrentVersions(targetDir, revertMetadata);

ProsperoLogger.ROOT_LOGGER.revertCandidateCompleted(targetDir);
} finally {
System.clearProperty(MAVEN_REPO_LOCAL);
}
}
}

private static void revertCurrentVersions(Path targetDir, InstallationMetadata revertMetadata) throws MetadataException {
try {
final Optional<ManifestVersionRecord> manifestHistory = revertMetadata.getManifestVersions();
if (manifestHistory.isPresent()) {
ProsperoMetadataUtils.writeVersionRecord(
targetDir.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.CURRENT_VERSION_FILE),
manifestHistory.get());
} else {
Path versionsFile = targetDir.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.CURRENT_VERSION_FILE);
if (Files.exists(versionsFile)) {
Files.delete(versionsFile);
}
}
} catch (IOException e) {
throw ProsperoLogger.ROOT_LOGGER.unableToWriteFile(targetDir.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.CURRENT_VERSION_FILE), e);
}
}

private static void verifyStateExists(SavedState savedState, InstallationMetadata metadata) throws MetadataException {
if (metadata.getRevisions().stream().noneMatch(s->s.getName().equals(savedState.getName()))) {
throw ProsperoLogger.ROOT_LOGGER.savedStateNotFound(savedState.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jboss.galleon.ProvisioningException;
import org.jboss.galleon.ProvisioningManager;
import org.jboss.galleon.config.ProvisioningConfig;
import org.jboss.logging.Logger;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.UnresolvedMavenArtifactException;
Expand All @@ -31,11 +32,11 @@
import org.wildfly.prospero.api.exceptions.ArtifactResolutionException;
import org.wildfly.prospero.api.exceptions.MetadataException;
import org.wildfly.prospero.api.exceptions.OperationException;
import org.wildfly.prospero.galleon.ArtifactCache;
import org.wildfly.prospero.galleon.GalleonEnvironment;
import org.wildfly.prospero.galleon.GalleonFeaturePackAnalyzer;
import org.wildfly.prospero.galleon.GalleonUtils;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.metadata.ManifestVersionResolver;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;
import org.wildfly.prospero.model.ProsperoConfig;
import org.wildfly.prospero.updates.CandidateProperties;
Expand All @@ -49,10 +50,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

class PrepareCandidateAction implements AutoCloseable{
class PrepareCandidateAction implements AutoCloseable {

private static final Logger LOG = Logger.getLogger(PrepareCandidateAction.class.getName());
private final InstallationMetadata metadata;
private final ProsperoConfig prosperoConfig;
private final MavenSessionManager mavenSessionManager;
Expand All @@ -70,7 +74,8 @@ boolean buildCandidate(Path targetDir, GalleonEnvironment galleonEnv, ApplyCandi
}

/**
* Builds an update/revert candidate server in {@code targetDir}.
* Builds an update/revert candidate server in {@code targetDir}. Uses the manifests resolved during
* provisioning of the candidate to generate metadata.
*
* @param targetDir
* @param galleonEnv
Expand All @@ -83,7 +88,29 @@ boolean buildCandidate(Path targetDir, GalleonEnvironment galleonEnv, ApplyCandi
*/
boolean buildCandidate(Path targetDir, GalleonEnvironment galleonEnv, ApplyCandidateAction.Type operation,
ProvisioningConfig config, UpdateSet updateSet) throws ProvisioningException, OperationException {
doBuildUpdate(targetDir, galleonEnv, config);
return this.buildCandidate(targetDir, galleonEnv, operation, config, updateSet, this::getManifestVersionRecord);
}

/**
* Builds an update/revert candidate server in {@code targetDir}.
*
* @param targetDir
* @param galleonEnv
* @param operation
* @param config
* @param updateSet
* @param manifestVersionRecordSupplier - provides information about manifest versions that should be used to generate
* metadata and cache after provisioning
* @return
* @throws ProvisioningException
* @throws OperationException
*/
boolean buildCandidate(Path targetDir, GalleonEnvironment galleonEnv, ApplyCandidateAction.Type operation,
ProvisioningConfig config, UpdateSet updateSet,
Function<List<Channel>, Optional<ManifestVersionRecord>> manifestVersionRecordSupplier) throws ProvisioningException, OperationException {
Objects.requireNonNull(manifestVersionRecordSupplier);

doBuildUpdate(targetDir, galleonEnv, config, manifestVersionRecordSupplier);

try {
final SavedState savedState = metadata.getRevisions().get(0);
Expand All @@ -96,7 +123,8 @@ boolean buildCandidate(Path targetDir, GalleonEnvironment galleonEnv, ApplyCandi
return true;
}

private void doBuildUpdate(Path targetDir, GalleonEnvironment galleonEnv, ProvisioningConfig provisioningConfig)
private void doBuildUpdate(Path targetDir, GalleonEnvironment galleonEnv, ProvisioningConfig provisioningConfig,
Function<List<Channel>, Optional<ManifestVersionRecord>> manifestVersionResolver)
throws ProvisioningException, OperationException {
final ProvisioningManager provMgr = galleonEnv.getProvisioningManager();
try {
Expand All @@ -110,33 +138,50 @@ private void doBuildUpdate(Path targetDir, GalleonEnvironment galleonEnv, Provis
e.getAttemptedRepositories(), mavenSessionManager.isOffline());
}

try {
final ManifestVersionRecord manifestRecord =
new ManifestVersionResolver(mavenSessionManager.getProvisioningRepo(), mavenSessionManager.newRepositorySystem())
.getCurrentVersions(galleonEnv.getChannels());
writeProsperoMetadata(targetDir, galleonEnv.getChannelSession().getRecordedChannel(), prosperoConfig.getChannels(),
manifestRecord);
} catch (IOException ex) {
throw ProsperoLogger.ROOT_LOGGER.unableToDownloadFile(ex);

final Optional<ManifestVersionRecord> manifestRecord = manifestVersionResolver.apply(galleonEnv.getChannels());

if (LOG.isTraceEnabled()) {
LOG.tracef("Recording manifests: %s", manifestRecord.orElse(new ManifestVersionRecord()));
}
manifestRecord.ifPresent(rec -> cacheManifests(rec, targetDir));
writeProsperoMetadata(targetDir, galleonEnv.getChannelSession().getRecordedChannel(), prosperoConfig.getChannels(),
manifestRecord);

try {
final GalleonFeaturePackAnalyzer galleonFeaturePackAnalyzer = new GalleonFeaturePackAnalyzer(galleonEnv.getChannels(), mavenSessionManager);

galleonFeaturePackAnalyzer.cacheGalleonArtifacts(targetDir, provisioningConfig);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Optional<ManifestVersionRecord> getManifestVersionRecord(List<Channel> channels) {
final ProsperoManifestVersionResolver manifestResolver = new ProsperoManifestVersionResolver(mavenSessionManager);
try {
return Optional.of(manifestResolver.getCurrentVersions(channels));
} catch (IOException e) {
ProsperoLogger.ROOT_LOGGER.debug("Unable to retrieve current manifest versions", e);
return Optional.empty();
}
}

private void cacheManifests(ManifestVersionRecord manifestRecord, Path installDir) {
try {
ArtifactCache.getInstance(installDir).cache(manifestRecord, mavenSessionManager.getResolvedArtifactVersions());
} catch (IOException e) {
ProsperoLogger.ROOT_LOGGER.debug("Unable to record manifests in the internal cache", e);
}
}

@Override
public void close() {
metadata.close();
}

private void writeProsperoMetadata(Path home, ChannelManifest manifest, List<Channel> channels, ManifestVersionRecord manifestVersions) throws MetadataException {
private void writeProsperoMetadata(Path home, ChannelManifest manifest, List<Channel> channels, Optional<ManifestVersionRecord> manifestVersions) throws MetadataException {
try (InstallationMetadata installationMetadata = InstallationMetadata.newInstallation(home, manifest,
new ProsperoConfig(channels), Optional.of(manifestVersions))) {
new ProsperoConfig(channels), manifestVersions)) {
installationMetadata.recordProvision(true, false);
}
}
Expand All @@ -158,6 +203,5 @@ private void writeCandidateProperties(UpdateSet updateSet, Path installationDir)
} catch (IOException e) {
ProsperoLogger.ROOT_LOGGER.unableToWriteChannelNamesToFile(candidateFile.toFile().getAbsolutePath(),e);
}

}
}
Loading
Loading