Search in sources :

Example 16 with ContainerName

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);
}
Also used : ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) ProcessResult(com.yahoo.vespa.hosted.dockerapi.ProcessResult) Test(org.junit.Test)

Example 17 with ContainerName

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]");
    }
}
Also used : ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) DockerImage(com.yahoo.vespa.hosted.dockerapi.DockerImage) ContainerNodeSpec(com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec) Test(org.junit.Test)

Example 18 with ContainerName

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);
}
Also used : ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) PrefixLogger(com.yahoo.vespa.hosted.node.admin.util.PrefixLogger)

Example 19 with 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);
    }
}
Also used : Arrays(java.util.Arrays) ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) Environment(com.yahoo.vespa.hosted.node.admin.component.Environment) ProcessResult(com.yahoo.vespa.hosted.dockerapi.ProcessResult) HashMap(java.util.HashMap) DockerNetworkCreator(com.yahoo.vespa.hosted.dockerapi.DockerNetworkCreator) InetAddress(java.net.InetAddress) ProcessExecuter(com.yahoo.system.ProcessExecuter) IPAddresses(com.yahoo.vespa.hosted.node.admin.task.util.network.IPAddresses) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) PrefixLogger(com.yahoo.vespa.hosted.node.admin.util.PrefixLogger) NodeType(com.yahoo.config.provision.NodeType) Container(com.yahoo.vespa.hosted.dockerapi.Container) IOException(java.io.IOException) DockerImpl(com.yahoo.vespa.hosted.dockerapi.DockerImpl) Inet4Address(java.net.Inet4Address) Collectors(java.util.stream.Collectors) Pair(com.yahoo.collections.Pair) Inet6Address(java.net.Inet6Address) List(java.util.List) ContainerNodeSpec(com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec) Stream(java.util.stream.Stream) DockerImage(com.yahoo.vespa.hosted.dockerapi.DockerImage) Docker(com.yahoo.vespa.hosted.dockerapi.Docker) Paths(java.nio.file.Paths) Optional(java.util.Optional) ContainerResources(com.yahoo.vespa.hosted.dockerapi.ContainerResources) Collections(java.util.Collections) Path(java.nio.file.Path) IOException(java.io.IOException) PrefixLogger(com.yahoo.vespa.hosted.node.admin.util.PrefixLogger)

Example 20 with ContainerName

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);
        }
    }
}
Also used : PolicyCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.PolicyCommand) ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) DockerOperations(com.yahoo.vespa.hosted.node.admin.docker.DockerOperations) FlushCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.FlushCommand) HashMap(java.util.HashMap) NodeRepository(com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeRepository) Collectors(java.util.stream.Collectors) Pair(com.yahoo.collections.Pair) List(java.util.List) Stream(java.util.stream.Stream) Chain(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.Chain) Command(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.Command) Map(java.util.Map) ContainerAclSpec(com.yahoo.vespa.hosted.node.admin.ContainerAclSpec) Optional(java.util.Optional) Action(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.Action) PrefixLogger(com.yahoo.vespa.hosted.node.admin.util.PrefixLogger) FlushCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.FlushCommand) PolicyCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.PolicyCommand) FlushCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.FlushCommand) Command(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.Command) PolicyCommand(com.yahoo.vespa.hosted.node.admin.maintenance.acl.iptables.PolicyCommand)

Aggregations

ContainerName (com.yahoo.vespa.hosted.dockerapi.ContainerName)21 Test (org.junit.Test)13 DockerImage (com.yahoo.vespa.hosted.dockerapi.DockerImage)7 ContainerNodeSpec (com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec)7 Container (com.yahoo.vespa.hosted.dockerapi.Container)4 ProcessResult (com.yahoo.vespa.hosted.dockerapi.ProcessResult)4 PrefixLogger (com.yahoo.vespa.hosted.node.admin.util.PrefixLogger)4 Map (java.util.Map)4 Docker (com.yahoo.vespa.hosted.dockerapi.Docker)3 IOException (java.io.IOException)3 List (java.util.List)3 Optional (java.util.Optional)3 Pair (com.yahoo.collections.Pair)2 NodeType (com.yahoo.config.provision.NodeType)2 ContainerResources (com.yahoo.vespa.hosted.dockerapi.ContainerResources)2 Environment (com.yahoo.vespa.hosted.node.admin.component.Environment)2 NodeRepository (com.yahoo.vespa.hosted.node.admin.configserver.noderepository.NodeRepository)2 OrchestratorException (com.yahoo.vespa.hosted.node.admin.configserver.orchestrator.OrchestratorException)2 DockerOperations (com.yahoo.vespa.hosted.node.admin.docker.DockerOperations)2 NodeAgent (com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgent)2