Search in sources :

Example 26 with Stop

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

the class AutoScaleController method stopContainers.

protected void stopContainers(List<Container> containers, ContainerAutoScaler autoScaler, FabricRequirements requirements, ProfileRequirements profileRequirement, AutoScaleStatus status, int delta) {
    final String profile = profileRequirement.getProfile();
    AutoScaleProfileStatus profileStatus = status.profileStatus(profile);
    // TODO sort the containers using some kind of requirements sorting order
    List<Container> sorted = new ArrayList<>(containers);
    // lets stop the ones at the end of the list by default
    Collections.reverse(sorted);
    List<String> stoppingContainerIds = new ArrayList<>();
    for (int i = 0; i < delta; i++) {
        if (i >= sorted.size()) {
            break;
        }
        Container container = sorted.get(i);
        stoppingContainerIds.add(container.getId());
        profileStatus.stoppingContainers(stoppingContainerIds);
        container.stop(true);
    }
}
Also used : Container(io.fabric8.api.Container) AutoScaleProfileStatus(io.fabric8.api.AutoScaleProfileStatus) ArrayList(java.util.ArrayList)

Example 27 with Stop

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

the class Activator method stop.

/**
 * Performs cleanup:<br/>
 * * Unregister handler;<br/>
 * * Unregister managed service;<br/>
 * * Release bundle context.
 *
 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
 */
public void stop(final BundleContext bundleContext) {
    if (m_handlerReg != null) {
        m_handlerReg.unregister();
        m_handlerReg = null;
    }
    if (m_managedServiceReg != null) {
        m_managedServiceReg.unregister();
        m_managedServiceReg = null;
    }
    ServiceRegistration<MavenResolver> registration = m_resolverReg.getAndSet(null);
    if (registration != null) {
        registration.unregister();
    }
    MavenResolver resolver = m_resolver.getAndSet(null);
    if (resolver != null) {
        try {
            resolver.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    m_bundleContext = null;
    LOG.debug("Handler for protocols " + PROTOCOL + " stopped");
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) IOException(java.io.IOException)

Example 28 with Stop

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

the class ContainerBuilder method stop.

/**
 * Stop the given containers.
 * The container directory will not get deleted.
 */
public static void stop(Set<? extends Container> containers) {
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(getBundleContext(), FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        for (Container aux : containers) {
            try {
                // We want to use the latest metadata
                Container updated = fabricService.getContainer(aux.getId());
                updated.stop(true);
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            // noop
            }
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) FabricException(io.fabric8.api.FabricException)

Example 29 with Stop

use of io.fabric8.arquillian.kubernetes.event.Stop in project fabric8-maven-plugin by fabric8io.

the class PodLogService method tailAppPodsLogs.

public void tailAppPodsLogs(final KubernetesClient kubernetes, final String namespace, final Set<HasMetadata> entities, boolean watchAddedPodsOnly, String onExitOperation, boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
    LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(entities);
    if (selector != null) {
        String ctrlCMessage = "stop tailing the log";
        if (Strings.isNotBlank(onExitOperation)) {
            final String onExitOperationLower = onExitOperation.toLowerCase().trim();
            if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                ctrlCMessage = "undeploy the app";
            } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                ctrlCMessage = "scale down the app and stop tailing the log";
            } else {
                log.warn("Unknown on-exit command: `%s`", onExitOperationLower);
            }
            resizeApp(kubernetes, namespace, entities, 1, log);
            Runtime.getRuntime().addShutdownHook(new Thread("pod log service shutdown hook") {

                @Override
                public void run() {
                    if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                        log.info("Undeploying the app:");
                        deleteEntities(kubernetes, namespace, entities, context.getS2iBuildNameSuffix(), log);
                    } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                        log.info("Stopping the app:");
                        resizeApp(kubernetes, namespace, entities, 0, log);
                    }
                    if (podWatcher != null) {
                        podWatcher.close();
                    }
                    closeLogWatcher();
                }
            });
        }
        waitAndLogPods(kubernetes, namespace, selector, watchAddedPodsOnly, ctrlCMessage, followLog, ignorePodsOlderThan, waitInCurrentThread);
    } else {
        log.warn("No selector in deployment so cannot watch pods!");
    }
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector)

Example 30 with Stop

use of io.fabric8.arquillian.kubernetes.event.Stop in project fabric8-maven-plugin by fabric8io.

the class PodLogService method waitAndLogPods.

private void waitAndLogPods(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final boolean watchAddedPodsOnly, final String ctrlCMessage, final boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
    if (context.getPodName() != null) {
        log.info("Watching pod with selector %s, and name %s waiting for a running pod...", selector, context.getPodName());
        pods = pods.withField("metadata.name", context.getPodName());
    } else {
        log.info("Watching pods with selector %s waiting for a running pod...", selector);
    }
    Pod latestPod = null;
    boolean runningPod = false;
    PodList list = pods.list();
    if (list != null) {
        List<Pod> items = list.getItems();
        if (items != null) {
            for (Pod pod : items) {
                PodStatusType status = getPodStatus(pod);
                switch(status) {
                    case WAIT:
                    case OK:
                        if (latestPod == null || KubernetesResourceUtil.isNewerResource(pod, latestPod)) {
                            if (ignorePodsOlderThan != null) {
                                Date podCreateTime = KubernetesResourceUtil.getCreationTimestamp(pod);
                                if (podCreateTime != null && podCreateTime.compareTo(ignorePodsOlderThan) > 0) {
                                    latestPod = pod;
                                }
                            } else {
                                latestPod = pod;
                            }
                        }
                        runningPod = true;
                        break;
                    case ERROR:
                    default:
                        continue;
                }
            }
        }
    }
    // we may have missed the ADDED event so lets simulate one
    if (latestPod != null) {
        onPod(Watcher.Action.ADDED, latestPod, kubernetes, namespace, ctrlCMessage, followLog);
    }
    if (!watchAddedPodsOnly) {
        // lets watch the current pods then watch for changes
        if (!runningPod) {
            log.warn("No pod is running yet. Are you sure you deployed your app via `fabric8:deploy`?");
            log.warn("Or did you stop it via `fabric8:stop`? If so try running the `fabric8:start` goal");
        }
    }
    podWatcher = pods.watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod pod) {
            onPod(action, pod, kubernetes, namespace, ctrlCMessage, followLog);
        }

        @Override
        public void onClose(KubernetesClientException e) {
        // ignore
        }
    });
    if (waitInCurrentThread) {
        while (terminateLatch.getCount() > 0) {
            try {
                terminateLatch.await();
            } catch (InterruptedException e) {
            // ignore
            }
        }
    }
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) Watcher(io.fabric8.kubernetes.client.Watcher) Date(java.util.Date) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) PodStatusType(io.fabric8.kubernetes.api.PodStatusType) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

Container (io.fabric8.api.Container)7 FabricService (io.fabric8.api.FabricService)7 IOException (java.io.IOException)7 File (java.io.File)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 BundleException (org.osgi.framework.BundleException)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)4 URISyntaxException (java.net.URISyntaxException)4 Bundle (org.osgi.framework.Bundle)4 FabricException (io.fabric8.api.FabricException)3 HashMap (java.util.HashMap)3 Session (com.jcraft.jsch.Session)2 Downloader (io.fabric8.agent.download.Downloader)2 StreamProvider (io.fabric8.agent.download.StreamProvider)2 Watch (io.fabric8.kubernetes.client.Watch)2 Watcher (io.fabric8.kubernetes.client.Watcher)2 MavenResolver (io.fabric8.maven.MavenResolver)2 Parser (io.fabric8.maven.util.Parser)2 BundleUpdate (io.fabric8.patch.management.BundleUpdate)2