Search in sources :

Example 76 with Start

use of io.fabric8.arquillian.kubernetes.event.Start 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 77 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class AgentTest method testAgent.

@Test
public void testAgent() throws Exception {
    System.setProperty("karaf.data", new File("target/karaf/data").getAbsolutePath());
    System.setProperty("karaf.home", new File("target/karaf").getAbsolutePath());
    Dictionary<String, String> resolverProps = new Hashtable<>();
    resolverProps.put(ServiceConstants.PROPERTY_REPOSITORIES, "http://repository.jboss.org/nexus/content/repositories/fs-public/@id=jboss.fs.public," + "https://repository.jboss.org/nexus/content/groups/ea/@id=jboss.ea.repo," + "http://repo1.maven.org/maven2@id=maven.central.repo," + "http://download.eng.bos.redhat.com/brewroot/repos/jb-fuse-6.2-build/latest/maven@id=brew");
    MavenResolver mavenResolver = MavenResolvers.createMavenResolver(resolverProps, null);
    DownloadManager manager = DownloadManagers.createDownloadManager(mavenResolver, Executors.newScheduledThreadPool(8));
    BundleContext systemBundleContext = createMock(BundleContext.class);
    TestSystemBundle systemBundle = createTestSystemBundle("/common", "system-bundle");
    systemBundle.setBundleContext(systemBundleContext);
    Bundle serviceBundle = createTestBundle(1l, Bundle.ACTIVE, "/common", "fabric-agent");
    expect(systemBundleContext.getBundle()).andReturn(systemBundle).anyTimes();
    expect(systemBundleContext.getBundles()).andReturn(new Bundle[] { systemBundle }).anyTimes();
    long nextBundleId = 2;
    List<Bundle> mockBundles = new ArrayList<>();
    String karafVersion = System.getProperty("karaf-version");
    String[] bundles = { "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.api/1.0.1", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.cm/1.1.0", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.core/1.8.0", "mvn:org.apache.aries.blueprint/org.apache.aries.blueprint.core.compatibility/1.0.0", "mvn:org.apache.aries.proxy/org.apache.aries.proxy/1.1.1", "mvn:org.apache.aries/org.apache.aries.util/1.1.3", "mvn:org.apache.felix/org.apache.felix.configadmin/1.8.12", "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.command/" + karafVersion, "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.config/" + karafVersion, "mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.modules/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.commands/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.console/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.dev/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.log/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.osgi/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.packages/" + karafVersion, "mvn:org.apache.karaf.shell/org.apache.karaf.shell.ssh/" + karafVersion, "mvn:org.apache.mina/mina-core/2.0.13", "mvn:org.apache.sshd/sshd-core/0.14.0.redhat-001", "mvn:org.ow2.asm/asm-all/5.0.4", "mvn:org.ops4j.pax.logging/pax-logging-api/1.9.1", "mvn:org.ops4j.pax.logging/pax-logging-service/1.9.1" };
    for (String bundleUri : bundles) {
        File file = mavenResolver.download(bundleUri);
        Hashtable<String, String> headers = doGetMetadata(file);
        TestBundle bundle = new TestBundle(++nextBundleId, bundleUri, Bundle.INSTALLED, headers) {

            @Override
            public void setStartLevel(int startlevel) {
            }

            @Override
            public void start() throws BundleException {
            }
        };
        expect(systemBundleContext.installBundle(EasyMock.eq(bundleUri), EasyMock.<InputStream>anyObject())).andReturn(bundle);
    }
    ServiceRegistration registration = EasyMock.createMock(ServiceRegistration.class);
    expect(systemBundleContext.registerService(EasyMock.eq(ResolverHookFactory.class), EasyMock.<ResolverHookFactory>anyObject(), EasyMock.<Dictionary>isNull())).andReturn(registration);
    registration.unregister();
    replay(systemBundleContext, registration);
    for (Bundle bundle : mockBundles) {
        replay(bundle);
    }
    Agent agent = new Agent(serviceBundle, systemBundleContext, manager) {

        @Override
        protected <T> void awaitService(Class<T> serviceClass, String filterspec, int timeout, TimeUnit timeUnit) {
        }
    };
    String karafFeaturesUrl = "mvn:org.apache.karaf.assemblies.features/standard/" + System.getProperty("karaf-version") + "/xml/features";
    agent.provision(Collections.singleton(karafFeaturesUrl), Collections.singleton("ssh"), Collections.<String>emptySet(), Collections.<String>emptySet(), Collections.<String>emptySet(), new HashSet<>(Arrays.asList("mvn:org.ops4j.pax.logging/pax-logging-api/1.9.1", "mvn:org.ops4j.pax.logging/pax-logging-service/1.9.1", "mvn:org.apache.felix/org.apache.felix.configadmin/1.8.12")), Collections.<String, Map<VersionRange, Map<String, String>>>emptyMap());
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) Bundle(org.osgi.framework.Bundle) VersionRange(org.apache.felix.utils.version.VersionRange) DownloadManager(io.fabric8.agent.download.DownloadManager) MavenResolver(io.fabric8.maven.MavenResolver) TimeUnit(java.util.concurrent.TimeUnit) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 78 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class Activator method onConnected.

public void onConnected() {
    destroyManager();
    try {
        manager = new Manager(this.bundleContext, curator, uri, exportedAddress, timeout);
        manager.init();
    } catch (Exception e) {
        throw new RuntimeException("Unable to start DOSGi service: " + e.getMessage(), e);
    }
}
Also used : Manager(io.fabric8.dosgi.impl.Manager) IOException(java.io.IOException)

Example 79 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method start.

@Override
public void start(Container container) {
    assertValid();
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
        CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
        ComputeService computeService = getOrCreateComputeService(options);
        try {
            String nodeId = jCloudsContainerMetadata.getNodeId();
            Optional<RunScriptOptions> runScriptOptions = ToRunScriptOptions.withComputeService(computeService).apply(jCloudsContainerMetadata);
            String script = buildStartScript(container.getId(), options);
            ExecResponse response;
            if (runScriptOptions.isPresent()) {
                response = computeService.runScriptOnNode(nodeId, script, runScriptOptions.get());
            } else {
                response = computeService.runScriptOnNode(nodeId, script);
            }
            if (response == null) {
                jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
            } else if (response.getOutput() != null && response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
        } catch (Throwable t) {
            jCloudsContainerMetadata.setFailure(t);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) ToRunScriptOptions(io.fabric8.service.jclouds.functions.ToRunScriptOptions) ExecResponse(org.jclouds.compute.domain.ExecResponse) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ComputeService(org.jclouds.compute.ComputeService) URISyntaxException(java.net.URISyntaxException) RunNodesException(org.jclouds.compute.RunNodesException) MalformedURLException(java.net.MalformedURLException)

Example 80 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testCallbackOnDisconnectCanClose.

@Test
public void testCallbackOnDisconnectCanClose() throws Exception {
    curator.close();
    LOG.info("....");
    SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
    final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
    proxyCurator.start();
    proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    LOG.info("curator is go: " + proxyCurator);
    final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
    final ArrayList<Runnable> members = new ArrayList<Runnable>();
    final int nThreads = 1;
    final CountDownLatch gotDisconnectEvent = new CountDownLatch(1);
    class GroupRunnable implements Runnable, GroupListener<NodeState> {

        final int id;

        private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();

        ZooKeeperGroup<NodeState> group;

        NodeState nodeState;

        public GroupRunnable(int id) {
            this.id = id;
            members.add(this);
            nodeState = new NodeState("foo" + id);
        }

        @Override
        public void run() {
            group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
            group.add(this);
            LOG.info("run: Added: " + this);
            try {
                while (true) {
                    switch(jobQueue.take()) {
                        case 0:
                            LOG.info("run: close: " + this);
                            try {
                                group.close();
                            } catch (IOException ignored) {
                            }
                            return;
                        case 1:
                            LOG.info("run: start: " + this);
                            group.start();
                            group.update(nodeState);
                            break;
                        case 2:
                            LOG.info("run: update: " + this);
                            nodeState.setId(nodeState.getId() + id);
                            group.update(nodeState);
                            break;
                    }
                }
            } catch (InterruptedException exit) {
            }
        }

        @Override
        public void groupEvent(Group<NodeState> group, GroupEvent event) {
            LOG.info("Got: event: " + event);
            if (event.equals(GroupEvent.DISCONNECTED)) {
                gotDisconnectEvent.countDown();
            }
        }
    }
    ;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    for (int i = 0; i < nThreads; i++) {
        executorService.execute(new GroupRunnable(i));
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(1);
        // wait for registration
        while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
    assertTrue("first started is master", firsStartedIsMaster);
    LOG.info("got master...");
    // lets see how long they take to notice a no responses to heart beats
    socketProxy.pause();
    // splash in an update
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(2);
    }
    boolean hasMaster = true;
    while (hasMaster) {
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            hasMaster &= groupRunnable.group.isMaster();
        }
        if (hasMaster) {
            LOG.info("Waiting for no master state on proxy pause");
            TimeUnit.SECONDS.sleep(1);
        }
    }
    try {
        boolean gotDisconnect = gotDisconnectEvent.await(15, TimeUnit.SECONDS);
        assertTrue("got disconnect event", gotDisconnect);
        LOG.info("do close");
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            groupRunnable.jobQueue.offer(0);
        }
        executorService.shutdown();
        // at a min when the session has expired
        boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
        assertTrue("all threads complete", allThreadComplete);
    } finally {
        proxyCurator.close();
        socketProxy.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Group(io.fabric8.groups.Group) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NodeState(io.fabric8.groups.NodeState) GroupListener(io.fabric8.groups.GroupListener) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketProxy(org.apache.activemq.util.SocketProxy) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)27 Test (org.junit.Test)25 File (java.io.File)18 HashMap (java.util.HashMap)16 Map (java.util.Map)10 Git (org.eclipse.jgit.api.Git)10 ArrayList (java.util.ArrayList)9 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)8 Bundle (org.osgi.framework.Bundle)8 PatchException (io.fabric8.patch.management.PatchException)7 URISyntaxException (java.net.URISyntaxException)7 BundleException (org.osgi.framework.BundleException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 Container (io.fabric8.api.Container)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)5 FabricService (io.fabric8.api.FabricService)5 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)5 HashSet (java.util.HashSet)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5