Search in sources :

Example 11 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.

the class DockerConnection method removeContainer.

@Override
public void removeContainer(final String id) throws DockerException, InterruptedException {
    try {
        // kill container
        client.removeContainer(id);
        // update container list
        listContainers();
    } catch (ContainerNotFoundException e) {
        throw new DockerContainerNotFoundException(e);
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Example 12 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.

the class DockerConnection method pullImage.

@Override
public void pullImage(final String id, final IDockerProgressHandler handler) throws DockerException, InterruptedException {
    try {
        DockerProgressHandler d = new DockerProgressHandler(handler);
        client.pull(id, d);
        listImages();
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        DockerException f = new DockerException(e);
        throw f;
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerProgressHandler(org.eclipse.linuxtools.docker.core.IDockerProgressHandler)

Example 13 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.

the class DockerConnection method buildImage.

@Override
public String buildImage(final IPath path, final String name, final IDockerProgressHandler handler) throws DockerException, InterruptedException {
    try {
        DockerProgressHandler d = new DockerProgressHandler(handler);
        java.nio.file.Path p = FileSystems.getDefault().getPath(path.makeAbsolute().toOSString());
        String res = getClientCopy().build(p, name, d, // $NON-NLS-1$ $NON-NLS-2$
        BuildParam.create("forcerm", "true"));
        return res;
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException | IOException e) {
        DockerException f = new DockerException(e);
        throw f;
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerProgressHandler(org.eclipse.linuxtools.docker.core.IDockerProgressHandler) IOException(java.io.IOException)

Example 14 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.

the class DockerConnection method readContainerDirectory.

@SuppressWarnings("unused")
public List<ContainerFileProxy> readContainerDirectory(final String id, final String path) throws DockerException {
    List<ContainerFileProxy> childList = new ArrayList<>();
    try {
        DockerClient copyClient = getClientCopy();
        final ExecCreation execCreation = copyClient.execCreate(id, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        new String[] { "/bin/sh", "-c", "ls -l -F -L -Q " + path }, ExecCreateParam.attachStdout(), ExecCreateParam.attachStderr());
        final String execId = execCreation.id();
        final LogStream pty_stream = copyClient.execStart(execId);
        try {
            while (pty_stream.hasNext()) {
                ByteBuffer b = pty_stream.next().content();
                byte[] buffer = new byte[b.remaining()];
                b.get(buffer);
                String s = new String(buffer);
                // $NON-NLS-1$
                String[] lines = s.split("\\r?\\n");
                for (String line : lines) {
                    if (// $NON-NLS-1$
                    line.trim().startsWith("total"))
                        // ignore the total line
                        continue;
                    // $NON-NLS-1$
                    String[] token = line.split("\\s+");
                    // $NON-NLS-1$
                    boolean isDirectory = token[0].startsWith("d");
                    // $NON-NLS-1$
                    boolean isLink = token[0].startsWith("l");
                    if (token.length > 8) {
                        // last token depends on whether we have a link or not
                        String link = null;
                        if (isLink) {
                            String linkname = token[token.length - 1];
                            if (linkname.endsWith("/")) {
                                // $NON-NLS-1$
                                linkname = linkname.substring(0, linkname.length() - 1);
                                isDirectory = true;
                            }
                            IPath linkPath = new Path(path);
                            linkPath = linkPath.append(linkname);
                            link = linkPath.toString();
                            String name = token[token.length - 3];
                            childList.add(new ContainerFileProxy(path, name, isDirectory, isLink, link));
                        } else {
                            String name = token[token.length - 1];
                            // remove quotes and any indicator char
                            name = name.substring(1, name.length() - (name.endsWith("\"") ? 1 : 2));
                            childList.add(new ContainerFileProxy(path, name, isDirectory));
                        }
                    }
                }
            }
        } finally {
            if (pty_stream != null)
                pty_stream.close();
            if (copyClient != null)
                copyClient.close();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    return childList;
}
Also used : Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) DockerClient(com.spotify.docker.client.DockerClient) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) LogStream(com.spotify.docker.client.LogStream) ByteBuffer(java.nio.ByteBuffer) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ProcessingException(javax.ws.rs.ProcessingException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StorageException(org.eclipse.equinox.security.storage.StorageException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException) ExecCreation(com.spotify.docker.client.messages.ExecCreation)

Example 15 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.

the class DockerConnection method createContainer.

@Override
public String createContainer(final IDockerContainerConfig c, IDockerHostConfig hc, final String containerName) throws DockerException, InterruptedException {
    try {
        HostConfig.Builder hbuilder = HostConfig.builder().containerIdFile(hc.containerIDFile()).publishAllPorts(hc.publishAllPorts()).privileged(hc.privileged()).networkMode(hc.networkMode()).readonlyRootfs(((DockerHostConfig) hc).readonlyRootfs());
        if (((DockerHostConfig) hc).tmpfs() != null) {
            hbuilder.tmpfs(ImmutableMap.copyOf(((DockerHostConfig) hc).tmpfs()));
        }
        if (((DockerHostConfig) hc).capAdd() != null) {
            hbuilder.capAdd(((DockerHostConfig) hc).capAdd());
        }
        if (((DockerHostConfig) hc).capDrop() != null) {
            hbuilder.capDrop(((DockerHostConfig) hc).capDrop());
        }
        if (hc.binds() != null)
            hbuilder.binds(hc.binds());
        if (hc.dns() != null)
            hbuilder.dns(hc.dns());
        if (hc.dnsSearch() != null)
            hbuilder.dnsSearch(hc.dnsSearch());
        if (hc.links() != null)
            hbuilder.links(hc.links());
        if (hc.lxcConf() != null) {
            List<IDockerConfParameter> lxcconf = hc.lxcConf();
            ArrayList<LxcConfParameter> lxcreal = new ArrayList<>();
            for (IDockerConfParameter param : lxcconf) {
            // TODO: Fix This
            }
            hbuilder.lxcConf(lxcreal);
        }
        if (hc.portBindings() != null) {
            Map<String, List<IDockerPortBinding>> bindings = hc.portBindings();
            HashMap<String, List<PortBinding>> realBindings = new HashMap<>();
            for (Entry<String, List<IDockerPortBinding>> entry : bindings.entrySet()) {
                String key = entry.getKey();
                List<IDockerPortBinding> bindingList = entry.getValue();
                ArrayList<PortBinding> newList = new ArrayList<>();
                for (IDockerPortBinding binding : bindingList) {
                    newList.add(PortBinding.of(binding.hostIp(), binding.hostPort()));
                }
                realBindings.put(key, newList);
            }
            hbuilder.portBindings(realBindings);
        }
        if (hc.volumesFrom() != null) {
            hbuilder.volumesFrom(hc.volumesFrom());
        }
        if (hc.securityOpt() != null) {
            hbuilder.securityOpt(hc.securityOpt());
        }
        // interface
        if (((DockerHostConfig) hc).memory() != null) {
            hbuilder.memory(((DockerHostConfig) hc).memory());
        }
        // interface
        if (((DockerHostConfig) hc).cpuShares() != null && ((DockerHostConfig) hc).cpuShares().longValue() > 0) {
            hbuilder.cpuShares(((DockerHostConfig) hc).cpuShares());
        }
        ContainerConfig.Builder builder = ContainerConfig.builder().hostname(c.hostname()).domainname(c.domainname()).user(c.user()).attachStdin(c.attachStdin()).attachStdout(c.attachStdout()).attachStderr(c.attachStderr()).tty(c.tty()).openStdin(c.openStdin()).stdinOnce(c.stdinOnce()).cmd(c.cmd()).image(c.image()).hostConfig(hbuilder.build()).workingDir(c.workingDir()).labels(c.labels()).networkDisabled(c.networkDisabled());
        // don't set those fields in the builder.
        if (c.portSpecs() != null) {
            builder = builder.portSpecs(c.portSpecs());
        }
        if (c.exposedPorts() != null) {
            builder = builder.exposedPorts(c.exposedPorts());
        }
        if (c.env() != null) {
            builder = builder.env(c.env());
        }
        if (c.volumes() != null) {
            builder = builder.volumes(c.volumes());
        }
        if (c.entrypoint() != null) {
            builder = builder.entrypoint(c.entrypoint());
        }
        if (c.onBuild() != null) {
            builder = builder.onBuild(c.onBuild());
        }
        // create container with default random name if an empty/null
        // containerName argument was passed
        final ContainerCreation creation = client.createContainer(builder.build(), (containerName != null && !containerName.isEmpty()) ? containerName : null);
        final String id = creation.id();
        // force a refresh of the current containers to include the new one
        listContainers();
        return id;
    } catch (ContainerNotFoundException e) {
        throw new DockerContainerNotFoundException(e);
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) IDockerContainerConfig(org.eclipse.linuxtools.docker.core.IDockerContainerConfig) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) LxcConfParameter(com.spotify.docker.client.messages.HostConfig.LxcConfParameter) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) List(java.util.List) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) IDockerConfParameter(org.eclipse.linuxtools.docker.core.IDockerConfParameter) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) PortBinding(com.spotify.docker.client.messages.PortBinding)

Aggregations

DockerException (org.eclipse.linuxtools.docker.core.DockerException)80 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)27 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)19 Job (org.eclipse.core.runtime.jobs.Job)17 IOException (java.io.IOException)16 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)16 DockerContainerNotFoundException (org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)16 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)15 ArrayList (java.util.ArrayList)14 DockerClient (com.spotify.docker.client.DockerClient)13 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)10 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)9 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)8 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)7 ProcessingException (javax.ws.rs.ProcessingException)7 IPath (org.eclipse.core.runtime.IPath)7 RunConsole (org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole)7 LogStream (com.spotify.docker.client.LogStream)6 File (java.io.File)6 HashMap (java.util.HashMap)6