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());
}
}
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;
}
}
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;
}
}
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;
}
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);
}
}
Aggregations