use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.
the class DeploymentAgent method doUpdate.
public boolean doUpdate(Dictionary<String, ?> props) throws Exception {
if (props == null || Boolean.parseBoolean((String) props.get("disabled"))) {
return false;
}
final Hashtable<String, String> properties = new Hashtable<>();
for (Enumeration e = props.keys(); e.hasMoreElements(); ) {
Object key = e.nextElement();
Object val = props.get(key);
if (!"service.pid".equals(key) && !FeatureConfigInstaller.FABRIC_ZOOKEEPER_PID.equals(key)) {
properties.put(key.toString(), val.toString());
}
}
updateStatus("analyzing", null);
// Building configuration
curatorCompleteService.waitForService(TimeUnit.SECONDS.toMillis(30));
String httpUrl;
List<URI> mavenRepoURIs;
// force reading of updated informations from ZK
if (!fabricService.isEmpty()) {
updateMavenRepositoryConfiguration(fabricService.getService());
}
try {
fabricServiceOperations.lock();
// no one will change the members now
httpUrl = this.httpUrl;
mavenRepoURIs = this.mavenRepoURIs;
} finally {
fabricServiceOperations.unlock();
}
addMavenProxies(properties, httpUrl, mavenRepoURIs);
final MavenResolver resolver = MavenResolvers.createMavenResolver(properties, "org.ops4j.pax.url.mvn");
final DownloadManager manager = DownloadManagers.createDownloadManager(resolver, getDownloadExecutor());
manager.addListener(new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
int pending = manager.pending();
updateStatus(pending > 0 ? "downloading (" + pending + " pending)" : "downloading", null);
}
});
// Update framework, libs, system and config props
final Object lock = new Object();
final AtomicBoolean restart = new AtomicBoolean();
final Set<String> libsToRemove = new HashSet<>(managedLibs.keySet());
final Set<String> endorsedLibsToRemove = new HashSet<>(managedEndorsedLibs.keySet());
final Set<String> extensionLibsToRemove = new HashSet<>(managedExtensionLibs.keySet());
final Set<String> sysPropsToRemove = new HashSet<>(managedSysProps.keySet());
final Set<String> configPropsToRemove = new HashSet<>(managedConfigProps.keySet());
final Set<String> etcsToRemove = new HashSet<>(managedEtcs.keySet());
final Properties configProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "config.properties"));
final Properties systemProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "system.properties"));
Downloader downloader = manager.createDownloader();
for (String key : properties.keySet()) {
if (key.equals("framework")) {
String url = properties.get(key);
if (!url.startsWith("mvn:")) {
throw new IllegalArgumentException("Framework url must use the mvn: protocol");
}
downloader.download(url, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
File file = provider.getFile();
String path = file.getPath();
if (path.startsWith(KARAF_HOME)) {
path = path.substring(KARAF_HOME.length() + 1);
}
synchronized (lock) {
if (!path.equals(configProps.get("karaf.framework.felix"))) {
configProps.put("karaf.framework", "felix");
configProps.put("karaf.framework.felix", path);
restart.set(true);
}
}
}
});
} else if (key.startsWith("config.")) {
String k = key.substring("config.".length());
String v = properties.get(key);
synchronized (lock) {
managedConfigProps.put(k, v);
configPropsToRemove.remove(k);
if (!v.equals(configProps.get(k))) {
configProps.put(k, v);
restart.set(true);
}
}
} else if (key.startsWith("system.")) {
String k = key.substring("system.".length());
synchronized (lock) {
String v = properties.get(key);
managedSysProps.put(k, v);
sysPropsToRemove.remove(k);
if (!v.equals(systemProps.get(k))) {
systemProps.put(k, v);
restart.set(true);
}
}
} else if (key.startsWith("lib.")) {
String value = properties.get(key);
downloader.download(value, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
File libFile = provider.getFile();
String libName = libFile.getName();
Long checksum = ChecksumUtils.checksum(libFile);
boolean update;
synchronized (lock) {
managedLibs.put(libName, "true");
libsToRemove.remove(libName);
update = !Long.toString(checksum).equals(libChecksums.getProperty(libName));
}
if (update) {
Files.copy(libFile, new File(LIB_PATH, libName));
restart.set(true);
}
}
});
} else if (key.startsWith("endorsed.")) {
String value = properties.get(key);
downloader.download(value, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
File libFile = provider.getFile();
String libName = libFile.getName();
Long checksum = ChecksumUtils.checksum(new FileInputStream(libFile));
boolean update;
synchronized (lock) {
managedEndorsedLibs.put(libName, "true");
endorsedLibsToRemove.remove(libName);
update = !Long.toString(checksum).equals(endorsedChecksums.getProperty(libName));
}
if (update) {
Files.copy(libFile, new File(LIB_ENDORSED_PATH, libName));
restart.set(true);
}
}
});
} else if (key.startsWith("extension.")) {
String value = properties.get(key);
downloader.download(value, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
File libFile = provider.getFile();
String libName = libFile.getName();
Long checksum = ChecksumUtils.checksum(libFile);
boolean update;
synchronized (lock) {
managedExtensionLibs.put(libName, "true");
extensionLibsToRemove.remove(libName);
update = !Long.toString(checksum).equals(extensionChecksums.getProperty(libName));
}
if (update) {
Files.copy(libFile, new File(LIB_EXT_PATH, libName));
restart.set(true);
}
}
});
} else if (key.startsWith("etc.")) {
String value = properties.get(key);
downloader.download(value, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
File etcFile = provider.getFile();
String etcName = etcFile.getName();
Long checksum = ChecksumUtils.checksum(new FileInputStream(etcFile));
boolean update;
synchronized (lock) {
managedEtcs.put(etcName, "true");
etcsToRemove.remove(etcName);
update = !Long.toString(checksum).equals(etcChecksums.getProperty(etcName));
}
if (update) {
Files.copy(etcFile, new File(KARAF_ETC, etcName));
}
}
});
}
}
downloader.await();
// Remove unused libs, system & config properties
for (String sysProp : sysPropsToRemove) {
systemProps.remove(sysProp);
managedSysProps.remove(sysProp);
System.clearProperty(sysProp);
restart.set(true);
}
for (String configProp : configPropsToRemove) {
configProps.remove(configProp);
managedConfigProps.remove(configProp);
restart.set(true);
}
for (String lib : libsToRemove) {
File libFile = new File(LIB_PATH, lib);
libFile.delete();
libChecksums.remove(lib);
managedLibs.remove(lib);
restart.set(true);
}
for (String lib : endorsedLibsToRemove) {
File libFile = new File(LIB_ENDORSED_PATH, lib);
libFile.delete();
endorsedChecksums.remove(lib);
managedEndorsedLibs.remove(lib);
restart.set(true);
}
for (String lib : extensionLibsToRemove) {
File libFile = new File(LIB_EXT_PATH, lib);
libFile.delete();
extensionChecksums.remove(lib);
managedExtensionLibs.remove(lib);
restart.set(true);
}
for (String etc : etcsToRemove) {
File etcFile = new File(KARAF_ETC, etc);
etcFile.delete();
etcChecksums.remove(etc);
managedEtcs.remove(etc);
}
libChecksums.save();
endorsedChecksums.save();
extensionChecksums.save();
etcChecksums.save();
managedLibs.save();
managedEndorsedLibs.save();
managedExtensionLibs.save();
managedConfigProps.save();
managedSysProps.save();
managedEtcs.save();
if (restart.get()) {
updateStatus("restarting", null);
configProps.save();
systemProps.save();
System.setProperty("karaf.restart", "true");
bundleContext.getBundle(0).stop();
return false;
}
FeatureConfigInstaller configInstaller = null;
ServiceReference configAdminServiceReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
if (configAdminServiceReference != null) {
ConfigurationAdmin configAdmin = (ConfigurationAdmin) bundleContext.getService(configAdminServiceReference);
configInstaller = new FeatureConfigInstaller(bundleContext, configAdmin, manager);
}
int bundleStartTimeout = Constants.BUNDLE_START_TIMEOUT;
String overriddenTimeout = properties.get(Constants.BUNDLE_START_TIMEOUT_PID_KEY);
try {
if (overriddenTimeout != null)
bundleStartTimeout = Integer.parseInt(overriddenTimeout);
} catch (Exception e) {
LOGGER.warn("Failed to set {} value: [{}], applying default value: {}", Constants.BUNDLE_START_TIMEOUT_PID_KEY, overriddenTimeout, Constants.BUNDLE_START_TIMEOUT);
}
Agent agent = new Agent(bundleContext.getBundle(), systemBundleContext, manager, configInstaller, null, DEFAULT_FEATURE_RESOLUTION_RANGE, DEFAULT_BUNDLE_UPDATE_RANGE, DEFAULT_UPDATE_SNAPSHOTS, bundleContext.getDataFile(STATE_FILE), bundleStartTimeout) {
@Override
public void updateStatus(String status) {
DeploymentAgent.this.updateStatus(status, null, false);
}
@Override
public void updateStatus(String status, boolean force) {
DeploymentAgent.this.updateStatus(status, null, force);
}
@Override
protected void saveState(State newState) throws IOException {
super.saveState(newState);
DeploymentAgent.this.state.replace(newState);
}
@Override
protected void provisionList(Set<Resource> resources) {
DeploymentAgent.this.provisionList = resources;
}
@Override
protected boolean done(boolean agentStarted, List<String> urls) {
if (agentStarted) {
// let's do patch-management "last touch" only if new agent wasn't started.
return true;
}
// agent finished provisioning, we can call back to low level patch management
ServiceReference<PatchManagement> srPm = systemBundleContext.getServiceReference(PatchManagement.class);
ServiceReference<FabricService> srFs = systemBundleContext.getServiceReference(FabricService.class);
if (srPm != null && srFs != null) {
PatchManagement pm = systemBundleContext.getService(srPm);
FabricService fs = systemBundleContext.getService(srFs);
if (pm != null && fs != null) {
LOGGER.info("Validating baseline information");
this.updateStatus("validating baseline information", true);
Profile profile = fs.getCurrentContainer().getOverlayProfile();
Map<String, String> versions = profile.getConfiguration("io.fabric8.version");
File localRepository = resolver.getLocalRepository();
if (pm.alignTo(versions, urls, localRepository, new PatchSynchronization())) {
this.updateStatus("requires full restart", true);
// let's reuse the same flag
restart.set(true);
return false;
}
if (handleRestartJvmFlag(profile, restart)) {
return false;
}
}
}
return true;
}
};
agent.setDeploymentAgentId(deploymentAgentId);
agent.provision(getPrefixedProperties(properties, "repository."), getPrefixedProperties(properties, "feature."), getPrefixedProperties(properties, "bundle."), getPrefixedProperties(properties, "req."), getPrefixedProperties(properties, "override."), getPrefixedProperties(properties, "optional."), getMetadata(properties, "metadata#"));
if (restart.get()) {
// prevent updating status to "success"
return false;
}
return true;
}
use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.
the class ProfileDownloader method downloadProfile.
/**
* Downloads the bundles, features and FABs for this profile.
*/
public void downloadProfile(Profile profile) throws Exception {
if (listener != null) {
listener.beforeDownloadProfile(profile);
}
ProfileService profileService = fabricService.adapt(ProfileService.class);
if (!profile.isOverlay()) {
profile = profileService.getOverlayProfile(profile);
}
DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabricService, executorService);
Set<String> bundles = new LinkedHashSet<String>();
Set<Feature> features = new LinkedHashSet<Feature>();
addMavenBundles(fabricService, profile, bundles, profile.getBundles());
addMavenBundles(fabricService, profile, bundles, profile.getFabs());
AgentUtils.addFeatures(features, fabricService, downloadManager, profile);
Map<String, File> files = AgentUtils.downloadBundles(downloadManager, features, bundles, Collections.<String>emptySet(), !isDownloadFilesFromProfile());
Set<Map.Entry<String, File>> entries = files.entrySet();
for (Map.Entry<String, File> entry : entries) {
String name = entry.getKey();
File file = entry.getValue();
if (processedFiles.add(file)) {
String fileName = file.getName();
String mvnCoords = getMavenCoords(name);
File destFile;
if (mvnCoords != null) {
Parser parser = new Parser(mvnCoords);
destFile = new File(target, parser.getArtifactPath());
} else {
destFile = new File(target, fileName);
}
if (force || !destFile.exists()) {
LOG.info("Copying file: " + file + " to: " + destFile.getCanonicalPath());
Files.copy(file, destFile);
if (listener != null) {
listener.onCopyDone(profile, destFile);
}
}
}
}
if (listener != null) {
listener.afterDownloadProfile(profile);
}
}
use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.
the class Subsystem method downloadBundles.
@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, MetadataBuilder builder, Set<String> overrides, String featureResolutionRange) throws Exception {
for (Subsystem child : children) {
child.downloadBundles(manager, builder, overrides, featureResolutionRange);
}
final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
final Downloader downloader = manager.createDownloader();
final Map<BundleInfo, Conditional> infos = new HashMap<>();
if (feature != null) {
for (Conditional cond : feature.getConditional()) {
for (final BundleInfo bi : cond.getBundles()) {
infos.put(bi, cond);
}
}
for (BundleInfo bi : feature.getBundles()) {
infos.put(bi, null);
}
}
ResourceBuilderCallback callback = new ResourceBuilderCallback(bundles, builder);
for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
final BundleInfo bi = entry.getKey();
final String loc = bi.getLocation();
downloader.download(loc, callback);
}
for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
final String loc = bundle.getName();
downloader.download(loc, callback);
}
for (final FabricBundle fabricBundle : fabricBundles) {
final String loc = fabricBundle.getLocation();
downloader.download(loc, callback);
}
for (String override : overrides) {
final String loc = Overrides.extractUrl(override);
downloader.download(loc, callback);
}
downloader.await();
Overrides.override(bundles, overrides);
if (feature != null) {
// Add conditionals
Map<Conditional, Resource> resConds = new HashMap<>();
for (Conditional cond : feature.getConditional()) {
FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
addIdentityRequirement(this, resCond, false);
addIdentityRequirement(resCond, this, true);
installable.add(resCond);
resConds.put(cond, resCond);
}
// Add features
FeatureResource resFeature = FeatureResource.build(feature, featureResolutionRange, bundles);
addIdentityRequirement(resFeature, this);
installable.add(resFeature);
// Add dependencies
for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
final BundleInfo bi = entry.getKey();
final String loc = bi.getLocation();
final Conditional cond = entry.getValue();
ResourceImpl res = bundles.get(loc);
int sl = bi.getStartLevel() <= 0 ? feature.getStartLevel() : bi.getStartLevel();
if (bi.isDependency()) {
addDependency(res, false, bi.isStart(), sl);
} else {
doAddDependency(res, cond == null, bi.isStart(), sl);
}
if (cond != null) {
addIdentityRequirement(res, resConds.get(cond), true);
}
}
}
for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
final String loc = bundle.getName();
boolean dependency = Boolean.parseBoolean(bundle.getAttribute("dependency"));
boolean start = bundle.getAttribute("start") == null || Boolean.parseBoolean(bundle.getAttribute("start"));
int startLevel = 0;
try {
startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
} catch (NumberFormatException e) {
// Ignore
}
if (dependency) {
addDependency(bundles.get(loc), false, start, startLevel);
} else {
doAddDependency(bundles.get(loc), true, start, startLevel);
addIdentityRequirement(this, bundles.get(loc));
}
}
for (final FabricBundle fabricBundle : fabricBundles) {
final String loc = fabricBundle.getLocation();
boolean dependency = Boolean.parseBoolean(fabricBundle.getProperty("dependency"));
boolean start = fabricBundle.getProperty("start") == null || Boolean.parseBoolean(fabricBundle.getProperty("start"));
int startLevel = 0;
try {
startLevel = Integer.parseInt(fabricBundle.getProperty("start-level"));
} catch (NumberFormatException e) {
// Ignore
}
if (dependency) {
addDependency(bundles.get(loc), false, start, startLevel);
} else {
doAddDependency(bundles.get(loc), true, start, startLevel);
addIdentityRequirement(this, bundles.get(loc));
}
}
// Compute dependencies
for (DependencyInfo info : dependencies.values()) {
installable.add(info.resource);
addIdentityRequirement(info.resource, this, info.mandatory);
}
}
use of io.fabric8.agent.download.DownloadManager 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.agent.download.DownloadManager in project fabric8 by jboss-fuse.
the class VerifyFeatureResolutionMojo method verifyResolution.
private void verifyResolution(DownloadManager manager, Map<String, Feature> allFeatures, String feature, Hashtable<String, String> properties) throws MojoExecutionException {
try {
properties.put("feature.totest", feature);
FakeSystemBundle systemBundle = getSystemBundleResource(getMetadata(properties, "metadata#"));
FakeServiceReference profileHandlerSR = new FakeServiceReference(URLStreamHandlerService.class.getName(), "(url.handler.protocol=profile2)");
systemBundle.setServiceReferences(URLStreamHandlerService.class.getName(), null, new ServiceReference[] { profileHandlerSR });
systemBundle.setService(profileHandlerSR, new Object());
Agent agent = new Agent(null, systemBundle, manager);
agent.setOptions(EnumSet.of(io.fabric8.agent.service.Constants.Option.Simulate, io.fabric8.agent.service.Constants.Option.Silent));
try {
agent.provision(allFeatures, getPrefixedProperties(properties, "feature."), getPrefixedProperties(properties, "bundle."), getPrefixedProperties(properties, "req."), getPrefixedProperties(properties, "override."), getPrefixedProperties(properties, "optional."), getMetadata(properties, "metadata#"));
} catch (Exception e) {
Set<String> resources = new TreeSet<>(manager.getProviders().keySet());
throw new MojoExecutionException("Feature resolution failed for " + feature + "\nMessage: " + e.toString() + "\nResources: " + toString(resources), e);
}
} catch (MojoExecutionException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException("Error verifying feature " + feature + "\nMessage: " + e.getMessage(), e);
}
}
Aggregations