Search in sources :

Example 1 with DownloadManager

use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.

the class JavaContainers method getJavaContainerArtifacts.

public static Map<String, Parser> getJavaContainerArtifacts(FabricService fabric, List<Profile> profileList, DownloadManager downloadManager) throws Exception {
    Map<String, Parser> artifacts = new TreeMap<String, Parser>();
    for (Profile profile : profileList) {
        Map<String, Parser> profileArtifacts = AgentUtils.getProfileArtifacts(fabric, downloadManager, profile);
        artifacts.putAll(profileArtifacts);
        appendMavenDependencies(artifacts, profile);
    }
    return artifacts;
}
Also used : TreeMap(java.util.TreeMap) Profile(io.fabric8.api.Profile) Parser(io.fabric8.maven.util.Parser)

Example 2 with DownloadManager

use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.

the class ProjectDeployerImpl method resolveProfileDeployments.

private DeployResults resolveProfileDeployments(ProjectRequirements requirements, FabricService fabric, Profile profile, ProfileBuilder builder, boolean mergeWithOldProfile) throws Exception {
    DependencyDTO rootDependency = requirements.getRootDependency();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    if (!mergeWithOldProfile) {
        // If we're not merging with the old profile, then create a dummy empty profile to merge with instead.
        ProfileBuilder emptyProfile = ProfileBuilder.Factory.create(profile.getVersion(), profile.getId()).setOverlay(true);
        profile = emptyProfile.getProfile();
    }
    if (rootDependency != null) {
        // as a hack lets just add this bundle in
        LOG.info("Got root: " + rootDependency);
        List<String> parentIds = profile.getParentIds();
        Profile overlay = profileService.getOverlayProfile(profile);
        String bundleUrl = rootDependency.toBundleUrlWithType();
        LOG.info("Using resolver to add extra features and bundles on " + bundleUrl);
        List<String> features = new ArrayList<String>(profile.getFeatures());
        List<String> bundles = new ArrayList<String>(profile.getBundles());
        List<String> optionals = new ArrayList<String>(profile.getOptionals());
        if (requirements.getFeatures() != null) {
            features.addAll(requirements.getFeatures());
        }
        if (requirements.getBundles() != null) {
            bundles.addAll(requirements.getBundles());
        }
        bundles.add(bundleUrl);
        LOG.info("Adding bundle: " + bundleUrl);
        // TODO we maybe should detect a karaf based container in a nicer way than this?
        boolean isKarafContainer = parentIds.contains("karaf") || parentIds.contains("containers-karaf");
        boolean addBundleDependencies = Objects.equal("bundle", rootDependency.getType()) || isKarafContainer;
        if (addBundleDependencies && requirements.isUseResolver()) {
            // lets build up a list of all current active features and bundles along with all discovered features
            List<Feature> availableFeatures = new ArrayList<Feature>();
            addAvailableFeaturesFromProfile(availableFeatures, fabric, overlay);
            Set<String> currentBundleLocations = new HashSet<>();
            currentBundleLocations.addAll(bundles);
            // lets add the current features
            DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabric, executorService);
            Set<Feature> currentFeatures = AgentUtils.getFeatures(fabric, downloadManager, overlay);
            addBundlesFromProfile(currentBundleLocations, overlay);
            List<String> parentProfileIds = requirements.getParentProfiles();
            if (parentProfileIds != null) {
                for (String parentProfileId : parentProfileIds) {
                    Profile parentProfile = profileService.getProfile(profile.getVersion(), parentProfileId);
                    Profile parentOverlay = profileService.getOverlayProfile(parentProfile);
                    Set<Feature> parentFeatures = AgentUtils.getFeatures(fabric, downloadManager, parentOverlay);
                    currentFeatures.addAll(parentFeatures);
                    addAvailableFeaturesFromProfile(availableFeatures, fabric, parentOverlay);
                    addBundlesFromProfile(currentBundleLocations, parentOverlay);
                }
            }
            // lets add all known features from the known repositories
            for (DependencyDTO dependency : rootDependency.getChildren()) {
                if ("test".equals(dependency.getScope()) || "provided".equals(dependency.getScope())) {
                    continue;
                }
                if ("jar".equals(dependency.getType())) {
                    String match = getAllServiceMixBundles().get(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion());
                    if (match != null) {
                        LOG.info("Replacing artifact " + dependency + " with servicemix bundle " + match);
                        String[] parts = match.split(":");
                        dependency.setGroupId(parts[0]);
                        dependency.setArtifactId(parts[1]);
                        dependency.setVersion(parts[2]);
                        dependency.setType("bundle");
                    }
                }
                String prefix = dependency.toBundleUrlWithoutVersion();
                Feature feature = findFeatureWithBundleLocationPrefix(currentFeatures, prefix);
                if (feature != null) {
                    LOG.info("Feature is already is in the profile " + feature.getId() + " for " + dependency.toBundleUrl());
                } else {
                    feature = findFeatureWithBundleLocationPrefix(availableFeatures, prefix);
                    if (feature != null) {
                        String name = feature.getName();
                        if (features.contains(name)) {
                            LOG.info("Feature is already added " + name + " for " + dependency.toBundleUrl());
                        } else {
                            LOG.info("Found a matching feature for bundle " + dependency.toBundleUrl() + ": " + feature.getId());
                            features.add(name);
                        }
                    } else {
                        String bundleUrlWithType = dependency.toBundleUrlWithType();
                        String foundBundleUri = findBundleUri(currentBundleLocations, prefix);
                        if (foundBundleUri != null) {
                            LOG.info("Bundle already included " + foundBundleUri + " for " + bundleUrlWithType);
                        } else {
                            boolean ignore = false;
                            String bundleWithoutMvnPrefix = getMavenCoords(bundleUrlWithType);
                            for (String ignoreBundlePrefix : RESOLVER_IGNORE_BUNDLE_PREFIXES) {
                                if (bundleWithoutMvnPrefix.startsWith(ignoreBundlePrefix)) {
                                    ignore = true;
                                    break;
                                }
                            }
                            if (ignore) {
                                LOG.info("Ignoring bundle: " + bundleUrlWithType);
                            } else {
                                boolean optional = dependency.isOptional();
                                LOG.info("Adding " + (optional ? "optional " : "") + " bundle: " + bundleUrlWithType);
                                if (optional) {
                                    optionals.add(bundleUrlWithType);
                                } else {
                                    bundles.add(bundleUrlWithType);
                                }
                            }
                        }
                    }
                }
            }
            // Modify the profile through the {@link ProfileBuilder}
            builder.setOptionals(optionals).setFeatures(features);
        }
        builder.setBundles(bundles);
    }
    profile = profileService.updateProfile(builder.getProfile());
    Integer minimumInstances = requirements.getMinimumInstances();
    if (minimumInstances != null) {
        FabricRequirements fabricRequirements = fabricService.get().getRequirements();
        ProfileRequirements profileRequirements = fabricRequirements.getOrCreateProfileRequirement(profile.getId());
        profileRequirements.setMinimumInstances(minimumInstances);
        fabricRequirements.setVersion(requirements.getVersion());
        fabricService.get().setRequirements(fabricRequirements);
    }
    // lets find a hawtio profile and version
    String profileUrl = findHawtioUrl(fabric);
    if (profileUrl == null) {
        profileUrl = "/";
    }
    if (!profileUrl.endsWith("/")) {
        profileUrl += "/";
    }
    String profilePath = Profiles.convertProfileIdToPath(profile.getId());
    profileUrl += "index.html#/wiki/branch/" + profile.getVersion() + "/view/fabric/profiles/" + profilePath;
    return new DeployResults(profile, profileUrl);
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) DeployResults(io.fabric8.deployer.dto.DeployResults) ArrayList(java.util.ArrayList) DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) ProfileBuilder(io.fabric8.api.ProfileBuilder) Feature(io.fabric8.agent.model.Feature) DownloadManager(io.fabric8.agent.download.DownloadManager) Profile(io.fabric8.api.Profile) ProfileService(io.fabric8.api.ProfileService) FabricRequirements(io.fabric8.api.FabricRequirements) HashSet(java.util.HashSet)

Example 3 with DownloadManager

use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.

the class DeploymentUpdater method updateDeployment.

public void updateDeployment(Git git, File baseDir, CredentialsProvider credentials) throws Exception {
    Set<String> bundles = new LinkedHashSet<String>();
    Set<Feature> features = new LinkedHashSet<Feature>();
    Profile overlayProfile = container.getOverlayProfile();
    Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, overlayProfile);
    bundles.addAll(effectiveProfile.getBundles());
    AgentUtils.addFeatures(features, fabricService, downloadManager, effectiveProfile);
    if (copyFilesIntoGit) {
        copyDeploymentsIntoGit(git, baseDir, bundles, features);
    } else {
        addDeploymentsIntoPom(git, baseDir, effectiveProfile, bundles, features);
    }
    // now lets do a commit
    String message = "updating deployment";
    git.commit().setMessage(message).call();
    enableDeployDirectory(git, baseDir);
    String branch = GitHelpers.currentBranch(git);
    LOG.info("Pushing deployment changes to branch " + branch + " credentials " + credentials + " for container " + container.getId());
    try {
        Iterable<PushResult> results = git.push().setCredentialsProvider(credentials).setRefSpecs(new RefSpec(branch)).setOutputStream(new LoggingOutputStream(LOG)).call();
        /*
            for (PushResult result : results) {
                LOG.info(result.getMessages());
            }
*/
        LOG.info("Pushed deployment changes to branch " + branch + " for container " + container.getId());
    } catch (GitAPIException e) {
        LOG.error("Failed to push deployment changes to branch " + branch + " for container " + container.getId() + ". Reason: " + e, e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec) LoggingOutputStream(io.fabric8.common.util.LoggingOutputStream) PushResult(org.eclipse.jgit.transport.PushResult) Feature(io.fabric8.agent.model.Feature) Profile(io.fabric8.api.Profile)

Example 4 with DownloadManager

use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.

the class OpenShiftDeployAgent method createDeployTask.

private DeploymentUpdater createDeployTask(Container container, Map<String, String> openshiftConfiguration) throws MalformedURLException {
    String webappDir = relativePath(container, openshiftConfiguration, OpenShiftConstants.PROPERTY_DEPLOY_WEBAPPS);
    String deployDir = relativePath(container, openshiftConfiguration, OpenShiftConstants.PROPERTY_DEPLOY_JARS);
    if (webappDir != null || deployDir != null) {
        FabricService fabric = fabricService.get();
        DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabric, downloadExecutor);
        DeploymentUpdater deploymentUpdater = new DeploymentUpdater(downloadManager, fabric, container, webappDir, deployDir);
        deploymentUpdater.setRepositories(Maps.stringValue(openshiftConfiguration, OpenShiftConstants.PROPERTY_REPOSITORIES, OpenShiftConstants.DEFAULT_REPOSITORIES));
        deploymentUpdater.setCopyFilesIntoGit(Maps.booleanValue(openshiftConfiguration, OpenShiftConstants.PROPERTY_COPY_BINARIES_TO_GIT, false));
        return deploymentUpdater;
    }
    return null;
}
Also used : FabricService(io.fabric8.api.FabricService) DownloadManager(io.fabric8.agent.download.DownloadManager)

Example 5 with DownloadManager

use of io.fabric8.agent.download.DownloadManager in project fabric8 by jboss-fuse.

the class Agent method loadResources.

// 
// State support
// 
public static Callable<Map<String, Resource>> loadResources(DownloadManager manager, Map<String, Map<VersionRange, Map<String, String>>> metadata, Set<String> uris) throws MultiException, InterruptedException, MalformedURLException {
    final Map<String, Resource> resources = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    final MetadataBuilder builder = new MetadataBuilder(metadata);
    final DownloadCallback callback = new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            String uri = provider.getUrl();
            Map<String, String> headers = builder.getMetadata(uri, provider.getFile());
            Resource resource = ResourceBuilder.build(uri, headers);
            synchronized (resources) {
                resources.put(uri, resource);
            }
        }
    };
    for (String uri : uris) {
        downloader.download(uri, callback);
    }
    return new Callable<Map<String, Resource>>() {

        @Override
        public Map<String, Resource> call() throws Exception {
            downloader.await();
            return resources;
        }
    };
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) HashMap(java.util.HashMap) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Resource(org.osgi.resource.Resource) Downloader(io.fabric8.agent.download.Downloader) Callable(java.util.concurrent.Callable)

Aggregations

HashMap (java.util.HashMap)9 DownloadManager (io.fabric8.agent.download.DownloadManager)8 Profile (io.fabric8.api.Profile)8 File (java.io.File)8 Downloader (io.fabric8.agent.download.Downloader)6 DownloadCallback (io.fabric8.agent.download.DownloadCallback)5 StreamProvider (io.fabric8.agent.download.StreamProvider)5 Feature (io.fabric8.agent.model.Feature)5 ProfileService (io.fabric8.api.ProfileService)5 MavenResolver (io.fabric8.maven.MavenResolver)5 Map (java.util.Map)5 MultiException (io.fabric8.common.util.MultiException)4 Parser (io.fabric8.maven.util.Parser)4 IOException (java.io.IOException)4 Repository (io.fabric8.agent.model.Repository)3 FabricService (io.fabric8.api.FabricService)3 ArrayList (java.util.ArrayList)3 Hashtable (java.util.Hashtable)3 BundleInfo (io.fabric8.agent.model.BundleInfo)2 Agent (io.fabric8.agent.service.Agent)2