Search in sources :

Example 46 with Feature

use of io.fabric8.agent.model.Feature in project fabric8 by jboss-fuse.

the class ExtendedUpgradeAndRollbackTest method testContainerAfterVersionUpgradeAndDowngrade.

/**
 * The purpose of this test is that everything works ok, even if the container is created after the version.
 * This is a test for the issue: http://fusesource.com/issues/browse/FABRIC-363
 */
@SuppressWarnings("unchecked")
@Test
public void testContainerAfterVersionUpgradeAndDowngrade() throws Exception {
    System.out.println(CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    BundleContext moduleContext = ServiceLocator.getSystemContext();
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        System.out.println(CommandSupport.executeCommand("fabric:version-create --parent 1.0 1.1"));
        Set<Container> containers = ContainerBuilder.create().withName("basic_camel_a").withProfiles("feature-camel").assertProvisioningResult().build(fabricService);
        try {
            // Make sure that the profile change has been applied before changing the version
            CountDownLatch latch = WaitForConfigurationChange.on(fabricService);
            System.out.println(CommandSupport.executeCommand("fabric:profile-edit --feature camel-script --feature camel-hazelcast feature-camel 1.1"));
            Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            System.out.println(CommandSupport.executeCommand("fabric:container-upgrade --all 1.1"));
            ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
            System.out.println(CommandSupport.executeCommand("fabric:container-list"));
            for (Container container : containers) {
                Assert.assertEquals("Container should have version 1.1", "1.1", container.getVersion().getId());
                String bundles = CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " osgi:list -s | grep camel-hazelcast");
                Assert.assertNotNull(bundles);
                System.out.println(bundles);
                Assert.assertFalse("Expected camel-hazelcast installed on container:" + container.getId() + ".", bundles.isEmpty());
            }
            System.out.println(CommandSupport.executeCommand("fabric:container-rollback --all 1.0"));
            ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
            System.out.println(CommandSupport.executeCommand("fabric:container-list"));
            for (Container container : containers) {
                Assert.assertEquals("Container should have version 1.0", "1.0", container.getVersion().getId());
                String bundles = CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " osgi:list -s | grep camel-hazelcast");
                Assert.assertNotNull(bundles);
                System.out.println(bundles);
                Assert.assertTrue("Expected no camel-hazelcast installed on container:" + container.getId() + ".", bundles.isEmpty());
            }
        } finally {
            ContainerBuilder.stop(fabricService, containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) CountDownLatch(java.util.concurrent.CountDownLatch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 47 with Feature

use of io.fabric8.agent.model.Feature in project fabric8 by jboss-fuse.

the class ProjectDeployerImpl method addAvailableFeaturesFromProfile.

protected void addAvailableFeaturesFromProfile(Collection<Feature> allFeatures, FabricService fabric, Profile overlay) throws Exception {
    for (String repoUriWithExpressions : overlay.getRepositories()) {
        String repoUri = VersionPropertyPointerResolver.replaceVersions(fabric, overlay.getConfigurations(), repoUriWithExpressions);
        Repository repo = new Repository(URI.create(repoUri));
        repo.load();
        allFeatures.addAll(Arrays.asList(repo.getFeatures()));
    }
}
Also used : Repository(io.fabric8.agent.model.Repository)

Example 48 with Feature

use of io.fabric8.agent.model.Feature in project fabric8 by jboss-fuse.

the class Agent method provision.

public void provision(Set<String> repositories, Set<String> features, Set<String> bundles, Set<String> reqs, Set<String> overrides, Set<String> optionals, Map<String, Map<VersionRange, Map<String, String>>> metadata) throws Exception {
    updateStatus("downloading");
    Callable<Map<String, Repository>> repos = downloadRepositories(manager, repositories);
    Map<String, Feature> allFeatures = new HashMap<>();
    for (Repository repository : repos.call().values()) {
        for (Feature f : repository.getFeatures()) {
            String id = f.getId();
            if (allFeatures.put(id, f) != null) {
                throw new IllegalStateException("Duplicate feature found: " + id);
            }
        }
    }
    provision(allFeatures, features, bundles, reqs, overrides, optionals, metadata);
}
Also used : StaticRepository(io.fabric8.agent.repository.StaticRepository) Repository(io.fabric8.agent.model.Repository) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Feature(io.fabric8.agent.model.Feature)

Example 49 with Feature

use of io.fabric8.agent.model.Feature in project fabric8 by jboss-fuse.

the class Agent method provision.

public void provision(Map<String, Feature> allFeatures, Set<String> features, Set<String> bundles, Set<String> reqs, Set<String> overrides, Set<String> optionals, Map<String, Map<VersionRange, Map<String, String>>> metadata) throws Exception {
    Callable<Map<String, Resource>> res = loadResources(manager, metadata, optionals);
    // TODO: requirements should be able to be assigned to a region
    Map<String, Set<String>> requirements = new HashMap<>();
    for (String feature : features) {
        addToMapSet(requirements, ROOT_REGION, "feature:" + feature);
    }
    for (String bundle : bundles) {
        addToMapSet(requirements, ROOT_REGION, "bundle:" + bundle);
    }
    for (String req : reqs) {
        addToMapSet(requirements, ROOT_REGION, "req:" + req);
    }
    Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
    request.updateSnaphots = updateSnaphots;
    request.bundleUpdateRange = bundleUpdateRange;
    request.featureResolutionRange = featureResolutionRange;
    request.globalRepository = new StaticRepository(res.call().values());
    request.overrides = overrides;
    request.requirements = requirements;
    request.stateChanges = Collections.emptyMap();
    request.options = options;
    request.metadata = metadata;
    request.bundleStartTimeout = bundleStartTimeout;
    Deployer.DeploymentState dstate = new Deployer.DeploymentState();
    // Service bundle
    dstate.serviceBundle = serviceBundle;
    // Start level
    FrameworkStartLevel fsl = systemBundleContext.getBundle().adapt(FrameworkStartLevel.class);
    dstate.initialBundleStartLevel = fsl.getInitialBundleStartLevel();
    dstate.currentStartLevel = fsl.getStartLevel();
    // Bundles
    dstate.bundles = new HashMap<>();
    for (Bundle bundle : systemBundleContext.getBundles()) {
        dstate.bundles.put(bundle.getBundleId(), bundle);
    }
    // Features
    dstate.features = allFeatures;
    // Region -> bundles mapping
    // Region -> policy mapping
    dstate.bundlesPerRegion = new HashMap<>();
    dstate.filtersPerRegion = new HashMap<>();
    if (digraph == null) {
        for (Long id : dstate.bundles.keySet()) {
            addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, id);
        }
    } else {
        RegionDigraph clone = digraph.copy();
        for (Region region : clone.getRegions()) {
            // Get bundles
            dstate.bundlesPerRegion.put(region.getName(), new HashSet<>(region.getBundleIds()));
            // Get policies
            Map<String, Map<String, Set<String>>> edges = new HashMap<>();
            for (RegionDigraph.FilteredRegion fr : clone.getEdges(region)) {
                Map<String, Set<String>> policy = new HashMap<>();
                Map<String, Collection<String>> current = fr.getFilter().getSharingPolicy();
                for (String ns : current.keySet()) {
                    for (String f : current.get(ns)) {
                        addToMapSet(policy, ns, f);
                    }
                }
                edges.put(fr.getRegion().getName(), policy);
            }
            dstate.filtersPerRegion.put(region.getName(), edges);
        }
    }
    final State state = new State();
    try {
        storage.load(state);
    } catch (IOException e) {
        LOGGER.warn("Error loading agent state", e);
    }
    if (state.managedBundles.isEmpty()) {
        for (Bundle b : systemBundleContext.getBundles()) {
            if (b.getBundleId() != 0) {
                addToMapSet(state.managedBundles, ROOT_REGION, b.getBundleId());
            }
        }
    }
    // corresponding jar url and use that one to compute the checksum of the bundle.
    for (Map.Entry<Long, Bundle> entry : dstate.bundles.entrySet()) {
        long id = entry.getKey();
        Bundle bundle = entry.getValue();
        if (id > 0 && isUpdateable(bundle) && !state.bundleChecksums.containsKey(id)) {
            try {
                URL url = bundle.getResource("META-INF/MANIFEST.MF");
                URLConnection con = url.openConnection();
                Method method = con.getClass().getDeclaredMethod("getLocalURL");
                method.setAccessible(true);
                String jarUrl = ((URL) method.invoke(con)).toExternalForm();
                if (jarUrl.startsWith("jar:")) {
                    String jar = jarUrl.substring("jar:".length(), jarUrl.indexOf("!/"));
                    jar = new URL(jar).getFile();
                    long checksum = ChecksumUtils.checksumFile(new File(jar));
                    state.bundleChecksums.put(id, checksum);
                }
            } catch (Throwable t) {
                LOGGER.debug("Error calculating checksum for bundle: %s", bundle, t);
            }
        }
    }
    dstate.state = state;
    Set<String> prereqs = new HashSet<>();
    while (true) {
        try {
            Deployer.DeployCallback callback = new BaseDeployCallback() {

                @Override
                public void phase(String message) {
                    Agent.this.updateStatus(message);
                }

                @Override
                public void saveState(State newState) {
                    state.replace(newState);
                    try {
                        Agent.this.saveState(newState);
                    } catch (IOException e) {
                        LOGGER.warn("Error storing agent state", e);
                    }
                }

                @Override
                public void provisionList(Set<Resource> resources) {
                    Agent.this.provisionList(resources);
                }

                @Override
                public void restoreConfigAdminIfNeeded() {
                    if (configInstaller != null) {
                        configInstaller.restoreConfigAdminIfNeeded();
                    }
                }

                @Override
                public boolean done(boolean agentStarted, List<String> urls) {
                    return Agent.this.done(agentStarted, urls);
                }
            };
            // FABRIC-790, FABRIC-981 - wait for ProfileUrlHandler before attempting to load bundles (in subsystem.resolve())
            // (which may be the case with bundle.xxx=blueprint:profile:xxx URLs in io.fabric8.agent PID)
            // https://developer.jboss.org/message/920681 - 30 seconds is too low sometimes
            // there was "url.handler.timeouts" option for agent, but it was removed during migration to karaf 4.x resolver
            // LOGGER.debug("Waiting for ProfileUrlHandler");
            // awaitService(URLStreamHandlerService.class, "(url.handler.protocol=profile)", 30, TimeUnit.SECONDS);
            // LOGGER.debug("Waiting for ProfileUrlHandler finished");
            Deployer deployer = new Deployer(manager, callback);
            deployer.setDeploymentAgentId(deploymentAgentId);
            deployer.deploy(dstate, request);
            break;
        } catch (Deployer.PartialDeploymentException e) {
            if (!prereqs.containsAll(e.getMissing())) {
                prereqs.addAll(e.getMissing());
            } else {
                throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
            }
        }
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) MapUtils.addToMapSet(io.fabric8.agent.internal.MapUtils.addToMapSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) URL(java.net.URL) List(java.util.List) HashSet(java.util.HashSet) RegionDigraph(org.eclipse.equinox.region.RegionDigraph) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) Method(java.lang.reflect.Method) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) URLConnection(java.net.URLConnection) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MultiException(io.fabric8.common.util.MultiException) Region(org.eclipse.equinox.region.Region) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) StaticRepository(io.fabric8.agent.repository.StaticRepository)

Example 50 with Feature

use of io.fabric8.agent.model.Feature in project fabric8 by jboss-fuse.

the class AgentUtils method addFeatures.

/**
 * Adds the set of features to the given set for the given profile
 *
 * @param features
 * @param fabricService
 *@param downloadManager
 * @param profile   @throws Exception
 */
public static void addFeatures(Set<Feature> features, FabricService fabricService, DownloadManager downloadManager, Profile profile) throws Exception {
    List<String> featureNames = profile.getFeatures();
    Map<String, Repository> repositories = getRepositories(fabricService, downloadManager, profile);
    for (String featureName : featureNames) {
        Feature feature = FeatureUtils.search(featureName, repositories.values());
        if (feature == null) {
            LOGGER.warn("Could not find feature " + featureName + " for profile " + profile.getId() + " in repositories " + repositories.keySet());
        } else {
            features.addAll(expandFeature(feature, repositories));
        }
    }
}
Also used : Repository(io.fabric8.agent.model.Repository) Feature(io.fabric8.agent.model.Feature)

Aggregations

Test (org.junit.Test)20 HashMap (java.util.HashMap)18 FabricService (io.fabric8.api.FabricService)15 IOException (java.io.IOException)13 Feature (io.fabric8.agent.model.Feature)12 Container (io.fabric8.api.Container)11 Profile (io.fabric8.api.Profile)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 Map (java.util.Map)9 BundleContext (org.osgi.framework.BundleContext)9 HashSet (java.util.HashSet)8 URL (java.net.URL)7 BundleException (org.osgi.framework.BundleException)7 Repository (io.fabric8.agent.model.Repository)6 Version (io.fabric8.api.Version)6 LinkedHashSet (java.util.LinkedHashSet)6 Ignore (org.junit.Ignore)6 BundleInfo (io.fabric8.agent.model.BundleInfo)5 ContainerProxy (io.fabric8.itests.paxexam.support.ContainerProxy)5