use of com.yahoo.vespa.hosted.dockerapi.ContainerName in project vespa by vespa-engine.
the class DockerOperationsImplTest method processResultFromNodeProgramWhenNonZeroExitCode.
@Test(expected = RuntimeException.class)
public void processResultFromNodeProgramWhenNonZeroExitCode() {
final ContainerName containerName = new ContainerName("container-name");
final ProcessResult actualResult = new ProcessResult(3, "output", "errors");
final String programPath = "/bin/command";
final String[] command = new String[] { programPath, "arg" };
when(docker.executeInContainerAsRoot(any(), anyVararg())).thenReturn(// output from node program
actualResult);
dockerOperations.executeCommandInContainer(containerName, command);
}
use of com.yahoo.vespa.hosted.dockerapi.ContainerName in project vespa by vespa-engine.
the class DockerFailTest method dockerFailTest.
@Test
public void dockerFailTest() throws Exception {
try (DockerTester dockerTester = new DockerTester()) {
ContainerNodeSpec containerNodeSpec = new ContainerNodeSpec.Builder().hostname("host1.test.yahoo.com").wantedDockerImage(new DockerImage("dockerImage")).nodeState(Node.State.active).nodeType("tenant").nodeFlavor("docker").wantedRestartGeneration(1L).currentRestartGeneration(1L).minCpuCores(1).minMainMemoryAvailableGb(1).minDiskAvailableGb(1).build();
dockerTester.addContainerNodeSpec(containerNodeSpec);
// Wait for node admin to be notified with node repo state and the docker container has been started
while (dockerTester.nodeAdmin.getListOfHosts().size() == 0) {
Thread.sleep(10);
}
dockerTester.callOrderVerifier.assertInOrder(1200, "createContainerCommand with DockerImage { imageId=dockerImage }, HostName: host1.test.yahoo.com, ContainerName { name=host1 }", "executeInContainerAsRoot with ContainerName { name=host1 }, args: [" + DockerTester.NODE_PROGRAM + ", resume]");
dockerTester.dockerMock.deleteContainer(new ContainerName("host1"));
dockerTester.callOrderVerifier.assertInOrder("deleteContainer with ContainerName { name=host1 }", "createContainerCommand with DockerImage { imageId=dockerImage }, HostName: host1.test.yahoo.com, ContainerName { name=host1 }", "executeInContainerAsRoot with ContainerName { name=host1 }, args: [" + DockerTester.NODE_PROGRAM + ", resume]");
}
}
use of com.yahoo.vespa.hosted.dockerapi.ContainerName in project vespa by vespa-engine.
the class DockerOperationsImpl method removeContainer.
@Override
public void removeContainer(final Container existingContainer, ContainerNodeSpec nodeSpec) {
final ContainerName containerName = existingContainer.name;
PrefixLogger logger = PrefixLogger.getNodeAgentLogger(DockerOperationsImpl.class, containerName);
if (existingContainer.state.isRunning()) {
logger.info("Stopping container " + containerName.asString());
docker.stopContainer(containerName);
}
logger.info("Deleting container " + containerName.asString());
docker.deleteContainer(containerName);
}
use of com.yahoo.vespa.hosted.dockerapi.ContainerName in project vespa by vespa-engine.
the class DockerOperationsImpl method executeCommandInNetworkNamespace.
@Override
public void executeCommandInNetworkNamespace(ContainerName containerName, String... command) {
final PrefixLogger logger = PrefixLogger.getNodeAgentLogger(DockerOperationsImpl.class, containerName);
final Integer containerPid = docker.getContainer(containerName).filter(container -> container.state.isRunning()).map(container -> container.pid).orElseThrow(() -> new RuntimeException("PID not found for container with name: " + containerName.asString()));
Path procPath = environment.getPathResolver().getPathToRootOfHost().resolve("proc");
final String[] wrappedCommand = Stream.concat(Stream.of("sudo", "nsenter", String.format("--net=%s/%d/ns/net", procPath, containerPid), "--"), Stream.of(command)).toArray(String[]::new);
try {
Pair<Integer, String> result = processExecuter.exec(wrappedCommand);
if (result.getFirst() != 0) {
String msg = String.format("Failed to execute %s in network namespace for %s (PID = %d), exit code: %d, output: %s", Arrays.toString(wrappedCommand), containerName.asString(), containerPid, result.getFirst(), result.getSecond());
logger.error(msg);
throw new RuntimeException(msg);
}
} catch (IOException e) {
logger.warning(String.format("IOException while executing %s in network namespace for %s (PID = %d)", Arrays.toString(wrappedCommand), containerName.asString(), containerPid), e);
throw new RuntimeException(e);
}
}
use of com.yahoo.vespa.hosted.dockerapi.ContainerName in project vespa by vespa-engine.
the class AclMaintainer method applyAcl.
private void applyAcl(ContainerName containerName, Acl acl) {
if (isAclActive(containerName, acl)) {
return;
}
final Command flush = new FlushCommand(Chain.INPUT);
final Command rollback = new PolicyCommand(Chain.INPUT, Action.ACCEPT);
try {
String commands = Stream.concat(Stream.of(flush), acl.toCommands().stream()).map(command -> command.asString(IPTABLES_COMMAND)).collect(Collectors.joining("; "));
log.debug("Running ACL command '" + commands + "' in " + containerName.asString());
dockerOperations.executeCommandInNetworkNamespace(containerName, "/bin/sh", "-c", commands);
containerAcls.put(containerName, acl);
} catch (Exception e) {
log.error("Exception occurred while configuring ACLs for " + containerName.asString() + ", attempting rollback", e);
try {
dockerOperations.executeCommandInNetworkNamespace(containerName, rollback.asArray(IPTABLES_COMMAND));
} catch (Exception ne) {
log.error("Rollback of ACLs for " + containerName.asString() + " failed, giving up", ne);
}
}
}
Aggregations