Search in sources :

Example 16 with Container

use of io.fabric8.maven.docker.model.Container in project docker-maven-plugin by fabric8io.

the class StopMojo method getNetworksToRemove.

private Set<Network> getNetworksToRemove(QueryService queryService, PomLabel pomLabel) throws DockerAccessException {
    if (!autoCreateCustomNetworks) {
        return Collections.emptySet();
    }
    Set<Network> customNetworks = new HashSet<>();
    Set<Network> networks = queryService.getNetworks();
    for (ImageConfiguration image : getResolvedImages()) {
        final NetworkConfig config = image.getRunConfiguration().getNetworkingConfig();
        if (config.isCustomNetwork()) {
            Network network = getNetworkByName(networks, config.getCustomNetwork());
            if (network != null) {
                customNetworks.add(network);
                for (Container container : getContainersToStop(queryService, image)) {
                    if (!shouldStopContainer(container, pomLabel, image)) {
                        // it's sill in use don't collect it
                        customNetworks.remove(network);
                    }
                }
            }
        }
    }
    return customNetworks;
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) Network(io.fabric8.maven.docker.model.Network) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) HashSet(java.util.HashSet)

Example 17 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ClusterListAction method printCluster.

protected void printCluster(String dir, PrintStream out) throws Exception {
    // do we have any clusters at all?
    if (exists(getCurator(), dir) == null) {
        return;
    }
    List<String> children = getAllChildren(getCurator(), dir);
    Map<String, Map<String, ClusterNode>> clusters = new TreeMap<String, Map<String, ClusterNode>>();
    for (String child : children) {
        byte[] data = getCurator().getData().forPath(child);
        if (data != null && data.length > 0) {
            String text = new String(data).trim();
            if (!text.isEmpty()) {
                String clusterName = getClusterName(dir, child);
                Map<String, ClusterNode> cluster = clusters.get(clusterName);
                if (cluster == null) {
                    cluster = new TreeMap<String, ClusterNode>();
                    clusters.put(clusterName, cluster);
                }
                ObjectMapper mapper = new ObjectMapper();
                Map<String, Object> map = null;
                try {
                    map = mapper.readValue(data, HashMap.class);
                } catch (JsonParseException e) {
                    log.error("Error parsing JSON string: {}", text);
                    throw e;
                }
                ClusterNode node = null;
                Object id = value(map, "id", "container");
                if (id != null) {
                    Object agent = value(map, "container", "agent");
                    List services = (List) value(map, "services");
                    node = cluster.get(id);
                    if (node == null) {
                        node = new ClusterNode();
                        cluster.put(id.toString(), node);
                    }
                    if (services != null) {
                        if (!services.isEmpty()) {
                            for (Object service : services) {
                                node.services.add(getSubstitutedData(getCurator(), service.toString()));
                            }
                            node.masters.add(agent);
                        } else {
                            node.slaves.add(agent);
                        }
                    } else {
                        Object started = value(map, "started");
                        if (started == Boolean.TRUE) {
                            node.masters.add(agent);
                        } else {
                            node.slaves.add(agent);
                        }
                    }
                }
            }
        }
    }
    TablePrinter table = new TablePrinter();
    table.columns("cluster", "masters", "slaves", "services");
    for (String clusterName : clusters.keySet()) {
        Map<String, ClusterNode> nodes = clusters.get(clusterName);
        table.row(clusterName, "", "", "", "");
        for (String nodeName : nodes.keySet()) {
            ClusterNode node = nodes.get(nodeName);
            table.row("   " + nodeName, printList(node.masters), printList(node.slaves), printList(node.services));
        }
    }
    table.print();
}
Also used : HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) JsonParseException(com.fasterxml.jackson.core.JsonParseException) TablePrinter(io.fabric8.utils.TablePrinter) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 18 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ContainerAddProfileAction method doExecute.

protected Object doExecute() throws Exception {
    FabricValidations.validateContainerName(container);
    FabricValidations.validateProfileNames(profiles);
    Container cont = FabricCommand.getContainer(fabricService, container);
    // we can add existing profiles
    Profile[] profs = FabricCommand.getExistingProfiles(fabricService, cont.getVersion(), this.profiles);
    cont.addProfiles(profs);
    return null;
}
Also used : Container(io.fabric8.api.Container) Profile(io.fabric8.api.Profile)

Example 19 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ContainerChangeProfileAction method doExecute.

protected Object doExecute() throws Exception {
    FabricValidations.validateContainerName(container);
    FabricValidations.validateProfileNames(profiles);
    Container cont = FabricCommand.getContainer(fabricService, container);
    // we can only change to existing profiles
    Profile[] profs = FabricCommand.getExistingProfiles(fabricService, cont.getVersion(), profiles);
    cont.setProfiles(profs);
    return null;
}
Also used : Container(io.fabric8.api.Container) Profile(io.fabric8.api.Profile)

Example 20 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ContainerConnectAction method executSshCommand.

/**
 * Executes the ssh command.
 */
private void executSshCommand(CommandSession session, String username, String password, String hostname, String port, String cmd) throws Exception {
    if (cmd == null || cmd.length() == 0) {
        // ENTESB-6826: we're connecting in "shell" mode, which isn't wise when running from bin/client or ssh
        if (session.getKeyboard().getClass().getName().equals("org.apache.sshd.common.channel.ChannelPipedInputStream")) {
            System.err.println("When connecting to remote container using \"fabric:container-connect\" using ssh or bin/client, please establish SSH session (run bin/client) first and then run \"fabric:container-connect\"");
            return;
        }
    }
    // Create the client from prototype
    SshClient client = createClient();
    String agentSocket;
    if (this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME) != null) {
        agentSocket = this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString();
        client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, agentSocket);
    }
    try {
        ConnectFuture future = client.connect(hostname, Integer.parseInt(port));
        future.await();
        sshSession = future.getSession();
        Object oldIgnoreInterrupts = this.session.get(Console.IGNORE_INTERRUPTS);
        this.session.put(Console.IGNORE_INTERRUPTS, Boolean.TRUE);
        try {
            System.out.println("Connected");
            boolean authed = false;
            if (!authed) {
                if (username == null) {
                    throw new FabricAuthenticationException("No username specified.");
                }
                log.debug("Prompting user for password");
                String pwd = password != null ? password : ShellUtils.readLine(session, "Password: ", true);
                sshSession.authPassword(username, pwd);
                int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
                if ((ret & ClientSession.AUTHED) == 0) {
                    System.err.println("Password authentication failed");
                } else {
                    authed = true;
                }
            }
            if (!authed) {
                throw new FabricAuthenticationException("Failed to authenticate.");
            }
            // If user is authenticated credentials to session for future use.
            ShellUtils.storeFabricCredentials(session, username, password);
            ClientChannel channel;
            if (cmd != null && cmd.length() > 0) {
                channel = sshSession.createChannel("exec", cmd);
                channel.setIn(new ByteArrayInputStream(new byte[0]));
            } else {
                channel = sshSession.createChannel("shell");
                channel.setIn(new NoCloseInputStream(System.in));
                ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session));
                ((ChannelShell) channel).setupSensibleDefaultPty();
                ((ChannelShell) channel).setAgentForwarding(true);
            }
            channel.setOut(new NoCloseOutputStream(System.out));
            channel.setErr(new NoCloseOutputStream(System.err));
            channel.open();
            channel.waitFor(ClientChannel.CLOSED, 0);
        } finally {
            session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
            sshSession.close(false);
        }
    } finally {
        client.stop();
    }
}
Also used : NoCloseInputStream(org.apache.sshd.common.util.NoCloseInputStream) SshClient(org.apache.sshd.SshClient) FabricAuthenticationException(io.fabric8.api.FabricAuthenticationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) ChannelShell(org.apache.sshd.client.channel.ChannelShell) NoCloseOutputStream(org.apache.sshd.common.util.NoCloseOutputStream) ClientChannel(org.apache.sshd.ClientChannel)

Aggregations

Container (io.fabric8.api.Container)139 Test (org.junit.Test)85 FabricService (io.fabric8.api.FabricService)56 Container (io.fabric8.kubernetes.api.model.Container)56 ArrayList (java.util.ArrayList)42 IOException (java.io.IOException)40 Profile (io.fabric8.api.Profile)37 HashMap (java.util.HashMap)36 Map (java.util.Map)32 FabricException (io.fabric8.api.FabricException)27 File (java.io.File)27 Pod (io.fabric8.kubernetes.api.model.Pod)24 BundleContext (org.osgi.framework.BundleContext)24 Version (io.fabric8.api.Version)23 HashSet (java.util.HashSet)20 List (java.util.List)20 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)19 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)17 LinkedList (java.util.LinkedList)17 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)16