use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class ResourcesTest method testClient.
@Test
public void testClient() throws Exception {
// Doesn't work on CircleCI because their lxc-driver can't set cpus
// See output of `docker run --cpuset-cpus 0-1 spotify/busybox:latest true`
assumeFalse(isCircleCi());
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// Wait for agent to come up
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Deploy the job on the agent
final Deployment deployment = Deployment.of(jobId, START);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
// Wait for the job to run
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
assertJobEquals(job, taskStatus.getJob());
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(CPU_SHARES, hostConfig.cpuShares());
assertEquals(CPUSET_CPUS, hostConfig.cpusetCpus());
final Info info = docker.info();
final Iterable<String> split = Splitter.on(".").split(docker.version().apiVersion());
//noinspection ConstantConditions
final int major = Integer.parseInt(Iterables.get(split, 0, "0"));
//noinspection ConstantConditions
final int minor = Integer.parseInt(Iterables.get(split, 1, "0"));
// TODO (dxia) This doesn't work on docker < 1.7 ie docker API < 1.19 for some reason.
if (major >= 1 && minor >= 19) {
if (info.memoryLimit()) {
assertEquals(MEMORY, hostConfig.memory());
}
if (info.swapLimit()) {
assertEquals(MEMORY_SWAP, hostConfig.memorySwap());
}
}
}
}
use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.
the class SyslogRedirectionTest method test.
@Test
public void test() throws Exception {
final String syslogOutput = "should-be-redirected";
try (final DockerClient docker = getNewDockerClient()) {
// Start a container that will be our "syslog" endpoint (just run netcat and print whatever
// we receive).
final String port = "4711";
final String expose = port + "/udp";
docker.pull(ALPINE);
final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
final String syslogContainerId = creation.id();
docker.startContainer(syslogContainerId);
final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
assertThat(containerInfo.state().running(), equalTo(true));
final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
// Run a Helios job that logs to syslog.
startDefaultMaster();
startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final List<String> command = Lists.newArrayList();
final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
{
// Verify the log for the task container
LogStream logs = null;
try {
logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
final String log = logs.readFully();
// for old docker versions should be nothing in the docker output log, either error text
// or our message
assertEquals("", log);
} catch (DockerRequestException e) {
// for new docker versions, trying to read logs should throw an error but the syslog
// option should be set
final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
assertEquals("syslog", logType);
} finally {
if (logs != null) {
logs.close();
}
}
}
// Verify the log for the syslog container
{
final String log;
try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
log = logs.readFully();
}
// the output message from the command should appear in the syslog container
assertThat(log, containsString(syslogOutput));
}
}
}
use of com.spotify.docker.client.messages.HostConfig in project TOSCAna by StuPro-TOSCAna.
the class KubernetesPushingGopherIT method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
DockerClient client = DefaultDockerClient.fromEnv().build();
logger.info("Downloading registry image");
client.pull("registry:2");
this.registryPort = 5000;
final Map<String, List<PortBinding>> ports = singletonMap("5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", this.registryPort)));
final HostConfig hostConfig = HostConfig.builder().portBindings(ports).build();
logger.info("Creating Local Registry Container");
ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image("registry:2").build();
String id = client.createContainer(config).id();
logger.info("Registry container id: {}", id);
logger.info("Starting registry container");
client.startContainer(id);
this.runningContainers.add(id);
}
use of com.spotify.docker.client.messages.HostConfig 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);
}
}
use of com.spotify.docker.client.messages.HostConfig in project kie-wb-common by kiegroup.
the class DockerRuntimeExecExecutor method create.
private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
if (runtimeConfig.isPull()) {
try {
LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
}
}
final String[] ports = { runtimeConfig.getPort() };
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
if (!_dockerProvider.isPresent()) {
return Optional.empty();
}
final DockerProvider dockerProvider = _dockerProvider.get();
final List<PortBinding> randomPort = new ArrayList<>();
final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
randomPort.add(randomPortBinding);
portBindings.put(runtimeConfig.getPort(), randomPort);
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
final ContainerCreation creation;
try {
creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
}
final String id = creation.id();
String shortId = id.substring(0, 12);
String host = "";
try {
docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
} catch (DockerException | InterruptedException ex) {
throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
}
DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
dockerRuntimeEndpoint.setHost(host);
dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
dockerRuntimeEndpoint.setContext("");
return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
Aggregations