Search in sources :

Example 86 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class CamelAutoTestService method onConfigured.

@Override
protected void onConfigured() throws Exception {
    LOG.debug("onConfigured. mockOutputs: " + mockOutputs + " mockInputs: " + mockInputs + " messageFolder: " + messageFolder);
    FabricService fabricService = this.fabricService.getOptional();
    // lets find the camel contexts to test in this container
    MBeanServer mbeanServerValue = mbeanServer;
    if (mbeanServerValue != null && fabricService != null) {
        Profile overlayProfile = fabricService.getCurrentContainer().getOverlayProfile();
        Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, overlayProfile);
        Set<String> configurationFileNames = effectiveProfile.getConfigurationFileNames();
        for (CamelContext camelContext : camelContexts.values()) {
            String camelContextID = camelContext.getName();
            // check we only add testing stuff to each context once
            if (camelContext instanceof ModelCamelContext) {
                final ModelCamelContext modelCamelContext = (ModelCamelContext) camelContext;
                List<RouteDefinition> routeDefinitions = modelCamelContext.getRouteDefinitions();
                if (camelContextsConfigured.add(camelContextID)) {
                    NodeIdFactory nodeIdFactory = camelContext.getNodeIdFactory();
                    if (mockInputs || mockOutputs) {
                        for (RouteDefinition routeDefinition : routeDefinitions) {
                            String routeId = routeDefinition.idOrCreate(nodeIdFactory);
                            modelCamelContext.stopRoute(routeId);
                            final String routeKey = camelContextID + "." + routeId;
                            LOG.info("Mocking Camel route: " + routeKey);
                            routeDefinition.adviceWith(modelCamelContext, new AdviceWithRouteBuilder() {

                                @Override
                                public void configure() throws Exception {
                                    if (mockOutputs) {
                                        modelCamelContext.addRegisterEndpointCallback(strategy);
                                    }
                                }
                            });
                        // the advised route is automatic restarted
                        }
                    }
                    String path = messageFolder;
                    if (Strings.isNotBlank(path)) {
                        path += "/";
                    }
                    path += camelContextID;
                    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
                    try {
                        for (RouteDefinition routeDefinition : routeDefinitions) {
                            String routeId = routeDefinition.idOrCreate(nodeIdFactory);
                            String routePath = path + "/" + routeId + "/";
                            List<FromDefinition> inputs = routeDefinition.getInputs();
                            for (FromDefinition input : inputs) {
                                Endpoint endpoint = input.getEndpoint();
                                if (endpoint == null) {
                                    String uri = input.getUri();
                                    if (Strings.isNullOrBlank(uri)) {
                                        String ref = input.getRef();
                                        if (Strings.isNotBlank(ref)) {
                                            uri = "ref:" + ref;
                                        }
                                    }
                                    if (Strings.isNotBlank(uri)) {
                                        endpoint = camelContext.getEndpoint(uri);
                                    }
                                }
                                if (endpoint == null) {
                                    LOG.warn("Cannot find endpoint, uri or ref of input " + input + " on route " + routeId + " camelContext: " + camelContextID);
                                } else {
                                    for (String configFile : configurationFileNames) {
                                        if (configFile.startsWith(routePath)) {
                                            LOG.info("Sending file: " + configFile + " to " + endpoint);
                                            byte[] data = effectiveProfile.getFileConfiguration(configFile);
                                            if (data != null) {
                                                // lest send this message to this endpoint
                                                producerTemplate.sendBody(endpoint, data);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        producerTemplate.stop();
                    }
                }
            }
        }
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) FromDefinition(org.apache.camel.model.FromDefinition) Profile(io.fabric8.api.Profile) ModelCamelContext(org.apache.camel.model.ModelCamelContext) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) NodeIdFactory(org.apache.camel.spi.NodeIdFactory) RouteDefinition(org.apache.camel.model.RouteDefinition) Endpoint(org.apache.camel.Endpoint) FabricService(io.fabric8.api.FabricService) MBeanServer(javax.management.MBeanServer)

Example 87 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class AutoScaleController method autoScaleProfile.

private void autoScaleProfile(FabricService service, final ContainerAutoScaler autoScaler, FabricRequirements requirements, ProfileRequirements profileRequirement, AutoScaleStatus status) {
    final String profile = profileRequirement.getProfile();
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    Integer maximumInstances = profileRequirement.getMaximumInstances();
    String requirementsVersion = requirements.getVersion();
    final String version = Strings.isNotBlank(requirementsVersion) ? requirementsVersion : service.getDefaultVersionId();
    if (maximumInstances != null || minimumInstances != null) {
        if (maximumInstances != null) {
            List<Container> containers = Containers.aliveAndSuccessfulContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = count - maximumInstances;
            if (delta > 0) {
                stopContainers(containers, autoScaler, requirements, profileRequirement, status, delta);
            }
        }
        if (minimumInstances != null) {
            // lets check if we need to provision more
            List<Container> containers = Containers.aliveOrPendingContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = minimumInstances - count;
            try {
                AutoScaleProfileStatus profileStatus = status.profileStatus(profile);
                if (delta < 0) {
                    FabricService fs = this.fabricService.get();
                    if (fs != null) {
                        profileStatus.destroyingContainer();
                        for (int i = delta; i < 0; i++) {
                            while (!containers.isEmpty()) {
                                Container container = containers.remove(0);
                                if (container.getId().startsWith("auto_")) {
                                    fs.destroyContainer(container);
                                    break;
                                }
                            }
                        }
                    }
                } else if (delta > 0) {
                    if (AutoScalers.requirementsSatisfied(service, version, requirements, profileRequirement, status)) {
                        profileStatus.creatingContainer();
                        final AutoScaleRequest command = new AutoScaleRequest(service, version, profile, delta, requirements, profileRequirement, status);
                        new Thread("Creating container for " + command.getProfile()) {

                            @Override
                            public void run() {
                                try {
                                    autoScaler.createContainers(command);
                                } catch (Exception e) {
                                    LOGGER.error("Failed to create container of profile: " + profile + ". Caught: " + e, e);
                                }
                            }
                        }.start();
                    }
                } else {
                    profileStatus.provisioned();
                }
            } catch (Exception e) {
                LOGGER.error("Failed to auto-scale " + profile + ". Caught: " + e, e);
            }
        }
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) AutoScaleProfileStatus(io.fabric8.api.AutoScaleProfileStatus) AutoScaleRequest(io.fabric8.api.AutoScaleRequest)

Example 88 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class VersionResource method createProfile.

/**
 * Creates a new profile.
 * <br>
 * For example send this JSON to be able to create a new profile:
 *
 * <code>
 * { "id": "myNewProfile", "parents": ["containers-tomcat"] }
 * </code>
 */
@POST
public Response createProfile(ProfileDTO profileDTO) throws URISyntaxException {
    Objects.notNull(profileDTO, "profileDTO");
    FabricService fabricService = getFabricService();
    Objects.notNull(fabricService, "fabricService");
    ProfileService profileService = getProfileService();
    Objects.notNull(profileService, "profileService");
    String id = profileDTO.getId();
    if (Strings.isNullOrBlank(id)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("No id specified for the profile to be created").build();
    }
    URI location = new URI(getBaseUri() + "profile/" + id);
    // lets check it doesn't already exist
    String versionId = version.getId();
    if (profileService.hasProfile(versionId, id)) {
        return Response.seeOther(location).entity("Profile already exists for id: " + id).build();
    }
    // lets override whatever the version is set to
    profileDTO.setVersion(versionId);
    // create the profile
    ProfileBuilder builder = ProfileBuilder.Factory.create(versionId, id);
    profileDTO.populateBuilder(fabricService, profileService, builder);
    Profile profile = builder.getProfile();
    profileService.createProfile(profile);
    return Response.created(location).build();
}
Also used : ProfileService(io.fabric8.api.ProfileService) FabricService(io.fabric8.api.FabricService) URI(java.net.URI) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile) POST(javax.ws.rs.POST)

Example 89 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class Deployer method computeBundlesToRefresh.

private void computeBundlesToRefresh(Map<Bundle, String> toRefresh, Collection<Bundle> bundles, Map<Resource, Bundle> resources, Map<Resource, List<Wire>> resolution) {
    // Compute the new list of fragments
    Map<Bundle, Set<Resource>> newFragments = new HashMap<>();
    for (Bundle bundle : bundles) {
        newFragments.put(bundle, new HashSet<Resource>());
    }
    if (resolution != null) {
        for (Resource res : resolution.keySet()) {
            for (Wire wire : resolution.get(res)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    Bundle bundle = resources.get(wire.getProvider());
                    if (bundle != null) {
                        Bundle b = resources.get(wire.getRequirer());
                        Resource r = b != null ? b.adapt(BundleRevision.class) : wire.getRequirer();
                        newFragments.get(bundle).add(r);
                    }
                }
            }
        }
    }
    // Main loop
    int size;
    Map<Bundle, Resource> bndToRes = new HashMap<>();
    for (Map.Entry<Resource, Bundle> entry : resources.entrySet()) {
        bndToRes.put(entry.getValue(), entry.getKey());
    }
    do {
        size = toRefresh.size();
        main: for (Bundle bundle : bundles) {
            Resource resource = bndToRes.get(bundle);
            // This bundle is not managed
            if (resource == null) {
                continue;
            }
            // Continue if we already know about this bundle
            if (toRefresh.containsKey(bundle)) {
                continue;
            }
            // Ignore non resolved bundle
            BundleWiring wiring = bundle.adapt(BundleWiring.class);
            if (wiring == null) {
                continue;
            }
            // Ignore bundles that won't be wired
            List<Wire> newWires = resolution.get(resource);
            if (newWires == null) {
                continue;
            }
            // Check if this bundle is a host and its fragments changed
            Set<Resource> oldFragments = new HashSet<>();
            for (BundleWire wire : wiring.getProvidedWires(null)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    oldFragments.add(wire.getRequirer());
                }
            }
            if (!oldFragments.equals(newFragments.get(bundle))) {
                toRefresh.put(bundle, "Attached fragments changed: " + new ArrayList<>(newFragments.get(bundle)));
                break;
            }
            // Compare the old and new resolutions
            Set<Resource> wiredBundles = new HashSet<>();
            for (BundleWire wire : wiring.getRequiredWires(null)) {
                BundleRevision rev = wire.getProvider();
                Bundle provider = rev.getBundle();
                if (toRefresh.containsKey(provider)) {
                    // The bundle is wired to a bundle being refreshed,
                    // so we need to refresh it too
                    toRefresh.put(bundle, "Wired to " + provider.getSymbolicName() + "/" + provider.getVersion() + " which is being refreshed");
                    continue main;
                }
                Resource res = bndToRes.get(provider);
                wiredBundles.add(res != null ? res : rev);
            }
            Map<Resource, Requirement> wiredResources = new HashMap<>();
            for (Wire wire : newWires) {
                // Handle only packages, hosts, and required bundles
                String namespace = wire.getRequirement().getNamespace();
                if (!namespace.equals(BundleNamespace.BUNDLE_NAMESPACE) && !namespace.equals(PackageNamespace.PACKAGE_NAMESPACE) && !namespace.equals(HostNamespace.HOST_NAMESPACE)) {
                    continue;
                }
                // Ignore non-resolution time requirements
                String effective = wire.getRequirement().getDirectives().get(Namespace.CAPABILITY_EFFECTIVE_DIRECTIVE);
                if (effective != null && !Namespace.EFFECTIVE_RESOLVE.equals(effective)) {
                    continue;
                }
                // Ignore non bundle resources
                if (!isBundle(wire.getProvider())) {
                    continue;
                }
                if (!wiredResources.containsKey(wire.getProvider())) {
                    wiredResources.put(wire.getProvider(), wire.getRequirement());
                }
            }
            if (!wiredBundles.containsAll(wiredResources.keySet())) {
                Map<Resource, Requirement> newResources = new HashMap<>(wiredResources);
                newResources.keySet().removeAll(wiredBundles);
                StringBuilder sb = new StringBuilder();
                sb.append("Should be wired to: ");
                boolean first = true;
                for (Map.Entry<Resource, Requirement> entry : newResources.entrySet()) {
                    if (!first) {
                        sb.append(", ");
                    } else {
                        first = false;
                    }
                    Resource res = entry.getKey();
                    Requirement req = entry.getValue();
                    sb.append(getSymbolicName(res)).append("/").append(getVersion(res));
                    sb.append(" (through ");
                    sb.append(req);
                    sb.append(")");
                }
                toRefresh.put(bundle, sb.toString());
            }
        }
    } while (toRefresh.size() > size);
}
Also used : MapUtils.addToMapSet(io.fabric8.agent.internal.MapUtils.addToMapSet) MapUtils.removeFromMapSet(io.fabric8.agent.internal.MapUtils.removeFromMapSet) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Resource(org.osgi.resource.Resource) FeatureResource(io.fabric8.agent.resolver.FeatureResource) Wire(org.osgi.resource.Wire) BundleWire(org.osgi.framework.wiring.BundleWire) BundleWire(org.osgi.framework.wiring.BundleWire) Requirement(org.osgi.resource.Requirement) ZipEntry(java.util.zip.ZipEntry) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 90 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class Manager method init.

public void init() throws Exception {
    // Create client and server
    this.client = new ClientInvokerImpl(queue, timeout, serializationStrategies);
    this.server = new ServerInvokerImpl(uri, queue, serializationStrategies);
    this.client.start();
    this.server.start();
    // ZooKeeper tracking
    try {
        create(curator, DOSGI_REGISTRY, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException e) {
    // The node already exists, that's fine
    }
    this.tree = new TreeCacheExtended(curator, DOSGI_REGISTRY, true);
    this.tree.getListenable().addListener(this);
    this.tree.start();
    // UUID
    this.uuid = Utils.getUUID(this.bundleContext);
    // Service listener filter
    String filter = "(" + RemoteConstants.SERVICE_EXPORTED_INTERFACES + "=*)";
    // Initialization
    this.bundleContext.addServiceListener(this, filter);
    // Service registration
    this.registration = this.bundleContext.registerService(new String[] { ListenerHook.class.getName(), EventHook.class.getName(), FindHook.class.getName() }, this, null);
    // Check existing services
    ServiceReference[] references = this.bundleContext.getServiceReferences((String) null, filter);
    if (references != null) {
        for (ServiceReference reference : references) {
            exportService(reference);
        }
    }
}
Also used : ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) TreeCacheExtended(org.apache.curator.framework.recipes.cache.TreeCacheExtended) KeeperException(org.apache.zookeeper.KeeperException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Test (org.junit.Test)35 IOException (java.io.IOException)23 File (java.io.File)17 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)15 FabricService (io.fabric8.api.FabricService)11 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)11 Map (java.util.Map)10 Container (io.fabric8.api.Container)9 PatchException (io.fabric8.patch.management.PatchException)9 Expectations (mockit.Expectations)9 Profile (io.fabric8.api.Profile)8 TreeMap (java.util.TreeMap)7 Version (io.fabric8.api.Version)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 Check (io.fabric8.karaf.checks.Check)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 Patch (io.fabric8.patch.management.Patch)5