use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class VerifyFeatureResolutionMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
Field field = URL.class.getDeclaredField("factory");
field.setAccessible(true);
field.set(null, null);
} catch (Exception e) {
e.printStackTrace();
}
URL.setURLStreamHandlerFactory(new CustomBundleURLStreamHandlerFactory());
System.setProperty("karaf.home", "target/karaf");
System.setProperty("karaf.data", "target/karaf/data");
ScheduledExecutorService executor = Executors.newScheduledThreadPool(8);
Hashtable<String, String> properties = new Hashtable<>();
if (additionalMetadata != null) {
try (Reader reader = new FileReader(additionalMetadata)) {
Properties metadata = new Properties();
metadata.load(reader);
for (Enumeration<?> e = metadata.propertyNames(); e.hasMoreElements(); ) {
Object key = e.nextElement();
Object val = metadata.get(key);
properties.put(key.toString(), val.toString());
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to load additional metadata from " + additionalMetadata, e);
}
}
DownloadManager manager;
MavenResolver resolver;
final Map<String, Repository> repositories;
Map<String, Feature[]> repos = new HashMap<>();
Map<String, Feature> allFeatures = new HashMap<>();
try {
resolver = MavenResolvers.createMavenResolver(null, properties, "org.ops4j.pax.url.mvn", repositorySystem);
manager = DownloadManagers.createDownloadManager(resolver, executor);
repositories = downloadRepositories(manager, descriptors).call();
for (String repoUri : repositories.keySet()) {
Feature[] features = repositories.get(repoUri).getFeatures();
// Ack features to inline configuration files urls
for (Feature feature : features) {
for (BundleInfo bi : feature.getBundles()) {
String loc = bi.getLocation();
String nloc = null;
if (loc.contains("file:")) {
for (ConfigFile cfi : feature.getConfigurationFiles()) {
if (cfi.getFinalname().substring(1).equals(loc.substring(loc.indexOf("file:") + "file:".length()))) {
nloc = cfi.getLocation();
}
}
}
if (nloc != null) {
bi.setLocation(loc.substring(0, loc.indexOf("file:")) + nloc);
}
}
allFeatures.put(feature.getId(), feature);
}
repos.put(repoUri, features);
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to load features descriptors", e);
}
List<Feature> featuresToTest = new ArrayList<>();
if (verifyTransitive) {
for (Feature[] features : repos.values()) {
featuresToTest.addAll(Arrays.asList(features));
}
} else {
for (String uri : descriptors) {
featuresToTest.addAll(Arrays.asList(repos.get(uri)));
}
}
if (features != null && !features.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String feature : features) {
if (sb.length() > 0) {
sb.append("|");
}
String p = feature.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
sb.append(p);
if (!feature.contains("/")) {
sb.append("/.*");
}
}
Pattern pattern = Pattern.compile(sb.toString());
for (Iterator<Feature> iterator = featuresToTest.iterator(); iterator.hasNext(); ) {
Feature feature = iterator.next();
String id = feature.getName() + "/" + feature.getVersion();
if (!pattern.matcher(id).matches()) {
iterator.remove();
}
}
}
for (String fmk : framework) {
properties.put("feature.framework." + fmk, fmk);
}
List<Throwable> failures = new ArrayList<>();
for (Feature feature : featuresToTest) {
try {
String id = feature.getName() + "/" + feature.getVersion();
manager = DownloadManagers.createDownloadManager(resolver, executor);
verifyResolution(manager, allFeatures, id, properties);
getLog().info("Verification of feature " + id + " succeeded");
} catch (Exception e) {
getLog().warn(e.getMessage());
failures.add(e);
if ("first".equals(fail)) {
throw e;
}
}
}
if ("end".equals(fail) && !failures.isEmpty()) {
throw new MojoExecutionException("Verification failures", new MultiException("Verification failures", failures));
}
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class ContainerResolverListAction method doExecute.
@Override
protected Object doExecute() throws Exception {
if (containerIds == null || containerIds.isEmpty()) {
containerIds = new ArrayList<String>();
for (Container container : fabricService.getContainers()) {
containerIds.add(container.getId());
}
}
TablePrinter table = new TablePrinter();
table.columns("id", "resolver", "local hostname", "local ip", "public hostname", "public ip", "manual ip");
for (String containerId : containerIds) {
Container container = fabricService.getContainer(containerId);
String localHostName = container.getLocalHostname();
String localIp = container.getLocalIp();
String publicHostName = container.getPublicHostname();
String publicIp = container.getPublicIp();
String manualIp = container.getManualIp();
localHostName = localHostName != null ? localHostName : "";
localIp = localIp != null ? localIp : "";
publicHostName = publicHostName != null ? publicHostName : "";
publicIp = publicIp != null ? publicIp : "";
manualIp = manualIp != null ? manualIp : "";
String resolver = container.getResolver();
table.row(containerId, resolver, localHostName, localIp, publicHostName, publicIp, manualIp);
}
table.print();
return null;
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class ContainerInfoAction method doExecute.
@Override
protected Object doExecute() throws Exception {
containerName = Strings.isNotBlank(containerName) ? containerName : runtimeProperties.getRuntimeIdentity();
validateContainerName(containerName);
if (!containerExists(containerName)) {
System.out.println("Container " + containerName + " does not exists!");
return 1;
}
Container container = fabricService.getContainer(containerName);
System.out.println(String.format(FORMAT, "Name:", container.getId()));
System.out.println(String.format(FORMAT, "Version:", container.getVersionId()));
System.out.println(String.format(FORMAT, "Connected:", container.isAlive()));
System.out.println(String.format(FORMAT, "Type:", emptyIfNull(container.getType())));
System.out.println(String.format(FORMAT, "Root:", container.isRoot()));
System.out.println(String.format(FORMAT, "Ensemble Server:", container.isEnsembleServer()));
System.out.println(String.format(FORMAT, "Managed:", container.isManaged()));
Long processId = container.getProcessId();
System.out.println(String.format(FORMAT, "Process ID:", ((processId != null) ? processId.toString() : "")));
if (Strings.isNotBlank(container.getLocation())) {
System.out.println(String.format(FORMAT, "Location:", emptyIfNull(container.getLocation())));
}
System.out.println(String.format(FORMAT, "Resolver:", emptyIfNull(container.getResolver())));
System.out.println(String.format(FORMAT, "Network Address:", emptyIfNull(container.getIp())));
System.out.println(String.format(FORMAT, "Local Network Address:", emptyIfNull(container.getLocalIp())));
System.out.println(String.format(FORMAT, "Public Network Address:", emptyIfNull(container.getPublicIp())));
System.out.println(String.format(FORMAT, "Local Hostname:", emptyIfNull(container.getLocalHostname())));
System.out.println(String.format(FORMAT, "Public Hostname:", emptyIfNull(container.getPublicHostname())));
System.out.println(String.format(FORMAT, "SSH Url:", emptyIfNull(container.getSshUrl())));
System.out.println(String.format(FORMAT, "JMX Url:", emptyIfNull(container.getJmxUrl())));
System.out.println(String.format(FORMAT, "Http Url:", emptyIfNull(container.getHttpUrl())));
System.out.println(String.format(FORMAT, "Jolokia Url:", emptyIfNull(container.getJolokiaUrl())));
String debugPort = container.getDebugPort();
if (Strings.isNotBlank(debugPort)) {
System.out.println(String.format(FORMAT, "Debug Port:", debugPort));
}
if (verbose != null && verbose) {
// we want each profile on a separate line
Profile[] profiles = container.getProfiles();
for (int i = 0; i < profiles.length; i++) {
String id = profiles[i].getId();
if (i == 0) {
System.out.println(String.format(FORMAT, "Profiles:", id));
} else {
System.out.println(String.format(FORMAT, "", id));
}
}
Container parent = container.getParent();
if (parent != null) {
System.out.println(String.format(FORMAT, "Parent:", parent.getId()));
}
// we want each child on a separate line
Container[] children = container.getChildren();
for (int i = 0; i < children.length; i++) {
String id = children[i].getId();
if (i == 0) {
System.out.println(String.format(FORMAT, "Children:", id));
} else {
System.out.println(String.format(FORMAT, "", id));
}
}
}
String blueprintStatus = dataStore.getContainerAttribute(containerName, DataStore.ContainerAttribute.BlueprintStatus, "", false, false);
String springStatus = dataStore.getContainerAttribute(containerName, DataStore.ContainerAttribute.SpringStatus, "", false, false);
if (!blueprintStatus.isEmpty()) {
System.out.println(String.format(FORMAT, "Blueprint Status:", blueprintStatus.toLowerCase()));
}
if (!springStatus.isEmpty()) {
System.out.println(String.format(FORMAT, "Spring Status:", springStatus.toLowerCase()));
}
System.out.println(String.format(FORMAT, "Provision Status:", container.getProvisionStatus()));
if (container.getProvisionException() != null) {
System.out.println(String.format(FORMAT, "Provision Error:", container.getProvisionException()));
}
return null;
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class ServiceImpl method featureUpdatesInPatch.
/**
* Returns a list of {@link FeatureUpdate} for single patch, taking into account already discovered updates
* @param patch
* @param updatesForFeatureKeys
* @param kind
* @return
*/
private List<FeatureUpdate> featureUpdatesInPatch(Patch patch, Map<String, FeatureUpdate> updatesForFeatureKeys, PatchKind kind) throws Exception {
Set<String> addedRepositoryNames = new HashSet<>();
HashMap<String, Repository> after = null;
try {
List<FeatureUpdate> updatesInThisPatch = new LinkedList<>();
/*
* Two pairs of features makes feature names not enough to be a key:
* <feature name="openjpa" description="Apache OpenJPA 2.2.x persistent engine support" version="2.2.2" resolver="(obr)">
* <feature name="openjpa" description="Apache OpenJPA 2.3.x persistence engine support" version="2.3.0" resolver="(obr)">
* and
* <feature name="activemq-camel" version="5.11.0.redhat-621039" resolver="(obr)" start-level="50">
* <feature name="activemq-camel" version="1.2.0.redhat-621039" resolver="(obr)">
*/
// install the new feature repos, tracking the set the were
// installed before and after
// (e.g, "karaf-enterprise-2.4.0.redhat-620133" -> Repository)
Map<String, Repository> before = new HashMap<>(getAvailableFeatureRepositories());
for (String url : patch.getPatchData().getFeatureFiles()) {
featuresService.addRepository(new URI(url));
}
after = getAvailableFeatureRepositories();
// track which old repos provide which features to find out if we have new repositories for those features
// key is name|version (don't expect '|' to be part of name...)
// assume that [feature-name, feature-version{major,minor,0,0}] is defined only in single repository
Map<String, String> featuresInOldRepositories = new HashMap<>();
// key is only name, without version - used when there's single feature in old and in new repositories
MultiMap<String, String> singleFeaturesInOldRepositories = new MultiMap<>();
Map<String, Version> actualOldFeatureVersions = new HashMap<>();
for (Repository existingRepository : before.values()) {
for (Feature feature : existingRepository.getFeatures()) {
Version v = Utils.getOsgiVersion(feature.getVersion());
Version lowestUpdateableVersion = new Version(v.getMajor(), v.getMinor(), 0);
// assume that we can update feature XXX-2.2.3 to XXX-2.2.142, but not to XXX-2.3.0.alpha-1
String key = String.format("%s|%s", feature.getName(), lowestUpdateableVersion.toString());
featuresInOldRepositories.put(key, existingRepository.getURI().toString());
singleFeaturesInOldRepositories.put(feature.getName(), existingRepository.getURI().toString());
actualOldFeatureVersions.put(key, v);
}
}
// Use the before and after set to figure out which repos were added.
addedRepositoryNames = new HashSet<>(after.keySet());
addedRepositoryNames.removeAll(before.keySet());
// track the new repositories where we can find old features
Map<String, String> featuresInNewRepositories = new HashMap<>();
MultiMap<String, String> singleFeaturesInNewRepositories = new MultiMap<>();
Map<String, String> actualNewFeatureVersions = new HashMap<>();
MultiMap<String, String> singleActualNewFeatureVersions = new MultiMap<>();
// Figure out which old repos were updated: Do they have feature
// with the same name as one contained in a repo being added?
// and do they have update'able version? (just like with bundles)
Set<String> oldRepositoryNames = new HashSet<String>();
for (String addedRepositoryName : addedRepositoryNames) {
Repository added = after.get(addedRepositoryName);
for (Feature feature : added.getFeatures()) {
Version v = Utils.getOsgiVersion(feature.getVersion());
Version lowestUpdateableVersion = new Version(v.getMajor(), v.getMinor(), 0);
String key = String.format("%s|%s", feature.getName(), lowestUpdateableVersion.toString());
featuresInNewRepositories.put(key, addedRepositoryName);
singleFeaturesInNewRepositories.put(feature.getName(), addedRepositoryName);
actualNewFeatureVersions.put(key, v.toString());
singleActualNewFeatureVersions.put(feature.getName(), v.toString());
String oldRepositoryWithUpdateableFeature = featuresInOldRepositories.get(key);
if (oldRepositoryWithUpdateableFeature == null && singleFeaturesInOldRepositories.get(feature.getName()) != null && singleFeaturesInOldRepositories.get(feature.getName()).size() == 1) {
oldRepositoryWithUpdateableFeature = singleFeaturesInOldRepositories.get(feature.getName()).get(0);
}
if (oldRepositoryWithUpdateableFeature != null) {
// track the old repository to be removed
oldRepositoryNames.add(oldRepositoryWithUpdateableFeature);
}
}
}
// We need to uninstall them. Before we uninstall, track which features were installed.
for (String oldRepositoryName : oldRepositoryNames) {
Repository repository = before.get(oldRepositoryName);
for (Feature feature : repository.getFeatures()) {
if (featuresService.isInstalled(feature)) {
Version v = Utils.getOsgiVersion(feature.getVersion());
Version lowestUpdateableVersion = new Version(v.getMajor(), v.getMinor(), 0);
String key = String.format("%s|%s", feature.getName(), lowestUpdateableVersion.toString());
String newRepositoryName = featuresInNewRepositories.get(key);
String newVersion = actualNewFeatureVersions.get(key);
if (newRepositoryName == null) {
// feature from 1.1.1 to 1.3.0
if (singleFeaturesInOldRepositories.get(feature.getName()) != null && singleFeaturesInOldRepositories.get(feature.getName()).size() == 1 && singleFeaturesInNewRepositories.get(feature.getName()) != null && singleFeaturesInNewRepositories.get(feature.getName()).size() == 1) {
newRepositoryName = singleFeaturesInNewRepositories.get(feature.getName()).get(0);
}
}
if (newVersion == null) {
if (singleActualNewFeatureVersions.get(feature.getName()) != null && singleActualNewFeatureVersions.get(feature.getName()).size() == 1) {
newVersion = singleActualNewFeatureVersions.get(feature.getName()).get(0);
}
}
FeatureUpdate featureUpdate = null;
if (newVersion != null && newRepositoryName != null) {
featureUpdate = new FeatureUpdate(feature.getName(), after.get(oldRepositoryName).getURI().toString(), feature.getVersion(), after.get(newRepositoryName).getURI().toString(), newVersion);
} else {
// we didn't find an update for installed features among feature repositories from patch
// which means we have to preserve both the feature and the repository - this may
// be user's feature
featureUpdate = new FeatureUpdate(feature.getName(), after.get(oldRepositoryName).getURI().toString(), feature.getVersion(), null, null);
}
updatesInThisPatch.add(featureUpdate);
// Merge result
FeatureUpdate oldUpdate = updatesForFeatureKeys.get(key);
if (oldUpdate != null) {
Version upv = null, newV = null;
if (oldUpdate.getNewVersion() != null) {
upv = VersionTable.getVersion(oldUpdate.getNewVersion());
}
if (newVersion != null) {
newV = VersionTable.getVersion(newVersion);
}
if (upv == null && newV == null) {
// weird...
} else {
if (upv == null || (newV != null && upv.compareTo(newV) < 0)) {
// other patch contains newer update for the feature
updatesForFeatureKeys.put(key, featureUpdate);
}
}
} else {
// this is the first update of the bundle
updatesForFeatureKeys.put(key, featureUpdate);
}
}
}
}
// now let's see if there are repositories that are NOT updated (either they're not available in patch
// (like user feature repositories) or simply didn't change (like jclouds 1.8.1 between Fuse 6.2 and 6.2.1)
Set<String> unchangedRepositoryNames = new HashSet<>(before.keySet());
unchangedRepositoryNames.removeAll(oldRepositoryNames);
for (String unchangedRepositoryName : unchangedRepositoryNames) {
Repository repository = before.get(unchangedRepositoryName);
boolean hasInstalledFeatures = false;
for (Feature feature : repository.getFeatures()) {
if (featuresService.isInstalled(feature)) {
FeatureUpdate featureUpdate = new FeatureUpdate(feature.getName(), after.get(unchangedRepositoryName).getURI().toString(), feature.getVersion(), null, null);
hasInstalledFeatures = true;
// preserve unchanged/user feature - install after restart
updatesInThisPatch.add(featureUpdate);
// the key doesn't matter
updatesForFeatureKeys.put(String.format("%s|%s", feature.getName(), feature.getVersion()), featureUpdate);
}
}
if (!hasInstalledFeatures) {
// we have to preserve unchanged/user feature repository - even if it had no installed features
// this featureUpdate means - "restore feature repository only"
FeatureUpdate featureUpdate = new FeatureUpdate(null, after.get(unchangedRepositoryName).getURI().toString(), null, null, null);
updatesInThisPatch.add(featureUpdate);
updatesForFeatureKeys.put(String.format("REPOSITORY_TO_ADD:%s", after.get(unchangedRepositoryName).getURI().toString()), featureUpdate);
}
}
return updatesInThisPatch;
} catch (Exception e) {
throw new PatchException(e.getMessage(), e);
} finally {
// we'll add new feature repositories again later. here we've added them only to track the updates
if (after != null) {
for (String repo : addedRepositoryNames) {
if (after.get(repo) != null) {
featuresService.removeRepository(after.get(repo).getURI(), false);
}
}
}
}
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class FabricServiceImpl method createContainers.
@Override
public CreateContainerMetadata[] createContainers(CreateContainerOptions options, CreationStateListener listener) {
assertValid();
try {
final ContainerProvider provider = getProvider(options.getProviderType());
if (provider == null) {
throw new FabricException("Unable to find a container provider supporting '" + options.getProviderType() + "'");
}
if (!provider.isValidProvider()) {
throw new FabricException("The provider '" + options.getProviderType() + "' is not valid in current environment");
}
String originalName = options.getName();
if (originalName == null || originalName.length() == 0) {
throw new FabricException("A name must be specified when creating containers");
}
// Only allow containers with valid names to get created.
FabricValidations.validateContainerName(originalName);
if (listener == null) {
listener = new NullCreationStateListener();
}
validateProfileDependencies(options);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class);
String versionId = options.getVersion() != null ? options.getVersion() : dataStore.get().getDefaultVersion();
Set<String> profileIds = options.getProfiles();
if (profileIds == null || profileIds.isEmpty()) {
profileIds = new LinkedHashSet<String>();
profileIds.add("default");
}
optionsMap.put("version", versionId);
optionsMap.put("profiles", profileIds);
optionsMap.put("number", 0);
// assign parent resolver if it's a child container, else assign global resolver policy
if (bootstrapConfig != null) {
String configuredGlobalResolver = bootstrapConfig.getGlobalResolver();
if (!Strings.isNullOrEmpty(configuredGlobalResolver)) {
optionsMap.put("globalResolver", configuredGlobalResolver);
if (optionsMap.get("resolver") == null) {
if (optionsMap.get("parent") == null) {
optionsMap.put("resolver", configuredGlobalResolver);
}
}
}
}
final List<CreateContainerMetadata> metadatas = new CopyOnWriteArrayList<CreateContainerMetadata>();
int orgNumber = options.getNumber();
int number = Math.max(orgNumber, 1);
if (orgNumber > 1) {
originalName = originalName + "1";
}
final CountDownLatch latch = new CountDownLatch(number);
Set<String> ignoreContainerNames = new HashSet<>();
Container[] containers = getContainers();
// check that there is no container with the given name
for (Container container : containers) {
if (container.getId().equals(options.getName())) {
throw new IllegalArgumentException("A container with name " + options.getName() + " already exists.");
}
}
for (int i = 1; i <= number; i++) {
NameValidator validator = Containers.createNameValidator(containers, ignoreContainerNames);
final String containerName = Containers.createUniqueContainerName(containers, originalName, validator);
ignoreContainerNames.add(containerName);
optionsMap.put("name", containerName);
// Check if datastore configuration has been specified and fallback to current container settings.
if (!hasValidDataStoreProperties(optionsMap)) {
optionsMap.put("dataStoreProperties", profileRegistry.get().getDataStoreProperties());
}
Class cl = options.getClass().getClassLoader().loadClass(options.getClass().getName() + "$Builder");
CreateContainerBasicOptions.Builder builder = (CreateContainerBasicOptions.Builder) mapper.readValue(mapper.writeValueAsString(optionsMap), cl);
// We always want to pass the obfuscated version of the password to the container provider.
builder = (CreateContainerBasicOptions.Builder) builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword()));
final CreateContainerOptions containerOptions = builder.build();
final CreationStateListener containerListener = listener;
final FabricService fabricService = this;
new Thread("Creating container " + containerName) {
public void run() {
try {
if (dataStore.get().hasContainer(containerName)) {
CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
metadata.setContainerName(containerName);
metadata.setCreateOptions(containerOptions);
metadata.setFailure(new IllegalArgumentException("A container with name " + containerName + " already exists."));
metadatas.add(metadata);
return;
}
dataStore.get().createContainerConfig(containerOptions);
CreateContainerMetadata metadata = provider.create(containerOptions, containerListener);
if (metadata.isSuccess()) {
Container parent = containerOptions.getParent() != null ? getContainer(containerOptions.getParent()) : null;
// TODO: We need to make sure that this entries are somehow added even to ensemble servers.
if (!containerOptions.isEnsembleServer()) {
dataStore.get().createContainerConfig(metadata);
}
ContainerImpl container = new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this);
metadata.setContainer(container);
LOGGER.info("The container " + metadata.getContainerName() + " has been successfully created");
} else {
LOGGER.warn("The creation of the container " + metadata.getContainerName() + " has failed", metadata.getFailure());
dataStore.get().deleteContainer(fabricService, containerOptions.getName());
}
metadatas.add(metadata);
} catch (Throwable t) {
CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
metadata.setContainerName(containerName);
metadata.setCreateOptions(containerOptions);
metadata.setFailure(t);
metadatas.add(metadata);
dataStore.get().deleteContainer(fabricService, containerOptions.getName());
} finally {
latch.countDown();
}
}
}.start();
}
if (!latch.await(30, TimeUnit.MINUTES)) {
throw new FabricException("Timeout waiting for container creation");
}
return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]);
} catch (Exception e) {
LOGGER.error("Failed to create containers " + e, e);
throw FabricException.launderThrowable(e);
}
}
Aggregations