Search in sources :

Example 1 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements 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 2 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class ProfileScaleAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    try {
        FabricValidations.validateProfileName(name);
    } catch (IllegalArgumentException e) {
        // we do not want exception in the server log, so print the error message to the console
        System.out.println(e.getMessage());
        return 1;
    }
    fabricService.scaleProfile(name, count);
    ProfileRequirements profileRequirements = fabricService.getRequirements().getOrCreateProfileRequirement(name);
    Integer minimumInstances = profileRequirements.getMinimumInstances();
    int size = Containers.containersForProfile(fabricService.getContainers(), name).size();
    PrintStream output = System.out;
    output.println("Profile " + name + " " + (minimumInstances != null ? "now requires " + minimumInstances + " container(s)" : "does not require any containers") + " currently has " + size + " container(s) running");
    return null;
}
Also used : PrintStream(java.io.PrintStream) ProfileRequirements(io.fabric8.api.ProfileRequirements)

Example 3 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class RequireProfileListAction method printRequirements.

@Override
protected void printRequirements(PrintStream out, FabricRequirements requirements) {
    TablePrinter table = new TablePrinter();
    table.columns("profile", "# minimum", "# maximum", "depends on");
    List<ProfileRequirements> profileRequirements = requirements.getProfileRequirements();
    for (ProfileRequirements profile : profileRequirements) {
        table.row(profile.getProfile(), getStringOrBlank(profile.getMinimumInstances()), getStringOrBlank(profile.getMaximumInstances()), getStringOrBlank(profile.getDependentProfiles()));
    }
    table.print();
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) TablePrinter(io.fabric8.utils.TablePrinter)

Example 4 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class FabricServiceImpl method createContainerAutoScaler.

@Override
public ContainerAutoScaler createContainerAutoScaler(FabricRequirements requirements, ProfileRequirements profileRequirements) {
    Collection<ContainerProvider> providerCollection = getProviders().values();
    for (ContainerProvider containerProvider : providerCollection) {
        // lets pick the highest weighted autoscaler (e.g. to prefer openshift to docker to child
        SortedMap<Integer, ContainerAutoScaler> sortedAutoScalers = new TreeMap<Integer, ContainerAutoScaler>();
        if (containerProvider instanceof ContainerAutoScalerFactory) {
            ContainerAutoScalerFactory provider = (ContainerAutoScalerFactory) containerProvider;
            ContainerAutoScaler autoScaler = provider.createAutoScaler(requirements, profileRequirements);
            if (autoScaler != null) {
                int weight = autoScaler.getWeight();
                sortedAutoScalers.put(weight, autoScaler);
            }
        }
        if (!sortedAutoScalers.isEmpty()) {
            Integer key = sortedAutoScalers.lastKey();
            if (key != null) {
                return sortedAutoScalers.get(key);
            }
        }
    }
    return null;
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) ContainerAutoScalerFactory(io.fabric8.api.ContainerAutoScalerFactory) ContainerAutoScaler(io.fabric8.api.ContainerAutoScaler) TreeMap(java.util.TreeMap)

Example 5 with ProfileRequirements

use of io.fabric8.api.ProfileRequirements in project fabric8 by jboss-fuse.

the class FabricServiceImpl method scaleProfile.

@Override
public boolean scaleProfile(String profile, int numberOfInstances) throws IOException {
    if (numberOfInstances == 0) {
        throw new IllegalArgumentException("numberOfInstances should be greater or less than zero");
    }
    FabricRequirements requirements = getRequirements();
    ProfileRequirements profileRequirements = requirements.getOrCreateProfileRequirement(profile);
    Integer minimumInstances = profileRequirements.getMinimumInstances();
    List<Container> containers = Containers.containersForProfile(getContainers(), profile);
    int containerCount = containers.size();
    int newCount = containerCount + numberOfInstances;
    if (newCount < 0) {
        newCount = 0;
    }
    boolean update = minimumInstances == null || newCount != minimumInstances;
    if (update) {
        profileRequirements.setMinimumInstances(newCount);
        setRequirements(requirements);
    }
    return update;
}
Also used : Container(io.fabric8.api.Container) ProfileRequirements(io.fabric8.api.ProfileRequirements) FabricRequirements(io.fabric8.api.FabricRequirements)

Aggregations

ProfileRequirements (io.fabric8.api.ProfileRequirements)15 FabricRequirements (io.fabric8.api.FabricRequirements)8 Container (io.fabric8.api.Container)5 ArrayList (java.util.ArrayList)4 FabricService (io.fabric8.api.FabricService)3 Profile (io.fabric8.api.Profile)3 AutoScaleProfileStatus (io.fabric8.api.AutoScaleProfileStatus)2 AutoScaleRequest (io.fabric8.api.AutoScaleRequest)2 ContainerAutoScaler (io.fabric8.api.ContainerAutoScaler)2 ProfileService (io.fabric8.api.ProfileService)2 Version (io.fabric8.api.Version)2 MQBrokerConfigDTO (io.fabric8.api.jmx.MQBrokerConfigDTO)2 MQBrokerStatusDTO (io.fabric8.api.jmx.MQBrokerStatusDTO)2 CountingMap (io.fabric8.utils.CountingMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 DownloadManager (io.fabric8.agent.download.DownloadManager)1 Feature (io.fabric8.agent.model.Feature)1 AutoScaleStatus (io.fabric8.api.AutoScaleStatus)1 ChildScalingRequirements (io.fabric8.api.ChildScalingRequirements)1