protected internal LocalWrapperScriptBuilder(DockerContainerExecutor _enclosing,
                                              Path containerWorkDir)
 {
     this._enclosing        = _enclosing;
     this.wrapperScriptPath = new Path(containerWorkDir, Shell.AppendScriptExtension(DockerContainerExecutor
                                                                                     .DockerContainerExecutorScript));
 }
Exemple #2
0
        /// <summary>
        /// Creates a script to run a container that will run forever unless
        /// stopped by external means.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private static FilePath CreateUnhaltingScriptFile(ContainerId cId, FilePath scriptFileDir
                                                          , FilePath processStartFile)
        {
            FilePath    scriptFile = Shell.AppendScriptExtension(scriptFileDir, "scriptFile");
            PrintWriter fileWriter = new PrintWriter(scriptFile);

            if (Shell.Windows)
            {
                fileWriter.WriteLine("@echo \"Running testscript for delayed kill\"");
                fileWriter.WriteLine("@echo \"Writing pid to start file\"");
                fileWriter.WriteLine("@echo " + cId + ">> " + processStartFile);
                fileWriter.WriteLine("@pause");
            }
            else
            {
                fileWriter.Write("#!/bin/bash\n\n");
                fileWriter.Write("echo \"Running testscript for delayed kill\"\n");
                fileWriter.Write("hello=\"Got SIGTERM\"\n");
                fileWriter.Write("umask 0\n");
                fileWriter.Write("trap \"echo $hello >> " + processStartFile + "\" SIGTERM\n");
                fileWriter.Write("echo \"Writing pid to start file\"\n");
                fileWriter.Write("echo $$ >> " + processStartFile + "\n");
                fileWriter.Write("while true; do\ndate >> /dev/null;\n done\n");
            }
            fileWriter.Close();
            return(scriptFile);
        }
 protected internal LocalWrapperScriptBuilder(DefaultContainerExecutor _enclosing,
                                              Path containerWorkDir)
 {
     this._enclosing        = _enclosing;
     this.wrapperScriptPath = new Path(containerWorkDir, Shell.AppendScriptExtension("default_container_executor"
                                                                                     ));
 }
 public UnixLocalWrapperScriptBuilder(DefaultContainerExecutor _enclosing, Path containerWorkDir
                                      )
     : base(_enclosing)
 {
     this._enclosing        = _enclosing;
     this.sessionScriptPath = new Path(containerWorkDir, Shell.AppendScriptExtension("default_container_executor_session"
                                                                                     ));
 }
 public UnixLocalWrapperScriptBuilder(DockerContainerExecutor _enclosing, Path containerWorkDir
                                      , string dockerCommand, string dockerPidScript)
     : base(_enclosing)
 {
     this._enclosing        = _enclosing;
     this.dockerCommand     = dockerCommand;
     this.dockerPidScript   = dockerPidScript;
     this.sessionScriptPath = new Path(containerWorkDir, Shell.AppendScriptExtension(DockerContainerExecutor
                                                                                     .DockerContainerExecutorSessionScript));
 }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        private void TestContainerLaunchAndExit(int exitCode)
        {
            FilePath    scriptFile       = Shell.AppendScriptExtension(tmpDir, "scriptFile");
            PrintWriter fileWriter       = new PrintWriter(scriptFile);
            FilePath    processStartFile = new FilePath(tmpDir, "start_file.txt").GetAbsoluteFile
                                               ();
            // ////// Construct the Container-id
            ContainerId cId = CreateContainerId(0);

            if (Shell.Windows)
            {
                fileWriter.WriteLine("@echo Hello World!> " + processStartFile);
                fileWriter.WriteLine("@echo " + cId + ">> " + processStartFile);
                if (exitCode != 0)
                {
                    fileWriter.WriteLine("@exit " + exitCode);
                }
            }
            else
            {
                fileWriter.Write("\numask 0");
                // So that start file is readable by the test
                fileWriter.Write("\necho Hello World! > " + processStartFile);
                fileWriter.Write("\necho $$ >> " + processStartFile);
                // Have script throw an exit code at the end
                if (exitCode != 0)
                {
                    fileWriter.Write("\nexit " + exitCode);
                }
            }
            fileWriter.Close();
            ContainerLaunchContext containerLaunchContext = recordFactory.NewRecordInstance <ContainerLaunchContext
                                                                                             >();
            URL resource_alpha = ConverterUtils.GetYarnUrlFromPath(localFS.MakeQualified(new
                                                                                         Path(scriptFile.GetAbsolutePath())));
            LocalResource rsrc_alpha = recordFactory.NewRecordInstance <LocalResource>();

            rsrc_alpha.SetResource(resource_alpha);
            rsrc_alpha.SetSize(-1);
            rsrc_alpha.SetVisibility(LocalResourceVisibility.Application);
            rsrc_alpha.SetType(LocalResourceType.File);
            rsrc_alpha.SetTimestamp(scriptFile.LastModified());
            string destinationFile = "dest_file";
            IDictionary <string, LocalResource> localResources = new Dictionary <string, LocalResource
                                                                                 >();

            localResources[destinationFile] = rsrc_alpha;
            containerLaunchContext.SetLocalResources(localResources);
            IList <string> commands = Arrays.AsList(Shell.GetRunScriptCommand(scriptFile));

            containerLaunchContext.SetCommands(commands);
            StartContainerRequest scRequest = StartContainerRequest.NewInstance(containerLaunchContext
                                                                                , CreateContainerToken(cId, DummyRmIdentifier, context.GetNodeId(), user, context
                                                                                                       .GetContainerTokenSecretManager()));
            IList <StartContainerRequest> list = new AList <StartContainerRequest>();

            list.AddItem(scRequest);
            StartContainersRequest allRequests = StartContainersRequest.NewInstance(list);

            containerManager.StartContainers(allRequests);
            BaseContainerManagerTest.WaitForContainerState(containerManager, cId, ContainerState
                                                           .Complete);
            IList <ContainerId> containerIds = new AList <ContainerId>();

            containerIds.AddItem(cId);
            GetContainerStatusesRequest gcsRequest = GetContainerStatusesRequest.NewInstance(
                containerIds);
            ContainerStatus containerStatus = containerManager.GetContainerStatuses(gcsRequest
                                                                                    ).GetContainerStatuses()[0];

            // Verify exit status matches exit state of script
            NUnit.Framework.Assert.AreEqual(exitCode, containerStatus.GetExitStatus());
        }
        public virtual void TestContainerLaunchAndStop()
        {
            containerManager.Start();
            FilePath    scriptFile       = Shell.AppendScriptExtension(tmpDir, "scriptFile");
            PrintWriter fileWriter       = new PrintWriter(scriptFile);
            FilePath    processStartFile = new FilePath(tmpDir, "start_file.txt").GetAbsoluteFile
                                               ();
            // ////// Construct the Container-id
            ContainerId cId = CreateContainerId(0);

            if (Shell.Windows)
            {
                fileWriter.WriteLine("@echo Hello World!> " + processStartFile);
                fileWriter.WriteLine("@echo " + cId + ">> " + processStartFile);
                fileWriter.WriteLine("@ping -n 100 127.0.0.1 >nul");
            }
            else
            {
                fileWriter.Write("\numask 0");
                // So that start file is readable by the test
                fileWriter.Write("\necho Hello World! > " + processStartFile);
                fileWriter.Write("\necho $$ >> " + processStartFile);
                fileWriter.Write("\nexec sleep 100");
            }
            fileWriter.Close();
            ContainerLaunchContext containerLaunchContext = recordFactory.NewRecordInstance <ContainerLaunchContext
                                                                                             >();
            URL resource_alpha = ConverterUtils.GetYarnUrlFromPath(localFS.MakeQualified(new
                                                                                         Path(scriptFile.GetAbsolutePath())));
            LocalResource rsrc_alpha = recordFactory.NewRecordInstance <LocalResource>();

            rsrc_alpha.SetResource(resource_alpha);
            rsrc_alpha.SetSize(-1);
            rsrc_alpha.SetVisibility(LocalResourceVisibility.Application);
            rsrc_alpha.SetType(LocalResourceType.File);
            rsrc_alpha.SetTimestamp(scriptFile.LastModified());
            string destinationFile = "dest_file";
            IDictionary <string, LocalResource> localResources = new Dictionary <string, LocalResource
                                                                                 >();

            localResources[destinationFile] = rsrc_alpha;
            containerLaunchContext.SetLocalResources(localResources);
            IList <string> commands = Arrays.AsList(Shell.GetRunScriptCommand(scriptFile));

            containerLaunchContext.SetCommands(commands);
            StartContainerRequest scRequest = StartContainerRequest.NewInstance(containerLaunchContext
                                                                                , CreateContainerToken(cId, DummyRmIdentifier, context.GetNodeId(), user, context
                                                                                                       .GetContainerTokenSecretManager()));
            IList <StartContainerRequest> list = new AList <StartContainerRequest>();

            list.AddItem(scRequest);
            StartContainersRequest allRequests = StartContainersRequest.NewInstance(list);

            containerManager.StartContainers(allRequests);
            int timeoutSecs = 0;

            while (!processStartFile.Exists() && timeoutSecs++ < 20)
            {
                Sharpen.Thread.Sleep(1000);
                Log.Info("Waiting for process start-file to be created");
            }
            NUnit.Framework.Assert.IsTrue("ProcessStartFile doesn't exist!", processStartFile
                                          .Exists());
            // Now verify the contents of the file
            BufferedReader reader = new BufferedReader(new FileReader(processStartFile));

            NUnit.Framework.Assert.AreEqual("Hello World!", reader.ReadLine());
            // Get the pid of the process
            string pid = reader.ReadLine().Trim();

            // No more lines
            NUnit.Framework.Assert.AreEqual(null, reader.ReadLine());
            // Now test the stop functionality.
            // Assert that the process is alive
            NUnit.Framework.Assert.IsTrue("Process is not alive!", DefaultContainerExecutor.ContainerIsAlive
                                              (pid));
            // Once more
            NUnit.Framework.Assert.IsTrue("Process is not alive!", DefaultContainerExecutor.ContainerIsAlive
                                              (pid));
            IList <ContainerId> containerIds = new AList <ContainerId>();

            containerIds.AddItem(cId);
            StopContainersRequest stopRequest = StopContainersRequest.NewInstance(containerIds
                                                                                  );

            containerManager.StopContainers(stopRequest);
            BaseContainerManagerTest.WaitForContainerState(containerManager, cId, ContainerState
                                                           .Complete);
            GetContainerStatusesRequest gcsRequest = GetContainerStatusesRequest.NewInstance(
                containerIds);
            ContainerStatus containerStatus = containerManager.GetContainerStatuses(gcsRequest
                                                                                    ).GetContainerStatuses()[0];
            int expectedExitCode = ContainerExitStatus.KilledByAppmaster;

            NUnit.Framework.Assert.AreEqual(expectedExitCode, containerStatus.GetExitStatus()
                                            );
            // Assert that the process is not alive anymore
            NUnit.Framework.Assert.IsFalse("Process is still alive!", DefaultContainerExecutor
                                           .ContainerIsAlive(pid));
        }
        public virtual void TestContainerLaunch()
        {
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";
            string testImage    = "\"sequenceiq/hadoop-docker:2.4.1\"";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >(Org.Mockito.Mockito.ReturnsDeepStubs);
            ContainerId cId = Org.Mockito.Mockito.Mock <ContainerId>(Org.Mockito.Mockito.ReturnsDeepStubs
                                                                     );
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            env[YarnConfiguration.NmDockerContainerExecutorImageName] = testImage;
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path pidFile    = new Path(workDir, "pid");

            dockerContainerExecutor.ActivateContainer(cId, pidFile);
            int ret = dockerContainerExecutor.LaunchContainer(container, scriptPath, tokensPath
                                                              , appSubmitter, appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs
                                                                  ());

            NUnit.Framework.Assert.AreEqual(0, ret);
            //get the script
            Path sessionScriptPath = new Path(workDir, Shell.AppendScriptExtension(DockerContainerExecutor
                                                                                   .DockerContainerExecutorSessionScript));
            LineNumberReader lnr = new LineNumberReader(new FileReader(sessionScriptPath.ToString
                                                                           ()));
            bool           cmdFound     = false;
            IList <string> localDirs    = DirsToMount(dirsHandler.GetLocalDirs());
            IList <string> logDirs      = DirsToMount(dirsHandler.GetLogDirs());
            IList <string> workDirMount = DirsToMount(Sharpen.Collections.SingletonList(workDir
                                                                                        .ToUri().GetPath()));
            IList <string> expectedCommands = new AList <string>(Arrays.AsList(DockerLaunchCommand
                                                                               , "run", "--rm", "--net=host", "--name", containerId));

            Sharpen.Collections.AddAll(expectedCommands, localDirs);
            Sharpen.Collections.AddAll(expectedCommands, logDirs);
            Sharpen.Collections.AddAll(expectedCommands, workDirMount);
            string shellScript = workDir + "/launch_container.sh";

            Sharpen.Collections.AddAll(expectedCommands, Arrays.AsList(testImage.ReplaceAll("['\"]"
                                                                                            , string.Empty), "bash", "\"" + shellScript + "\""));
            string expectedPidString = "echo `/bin/true inspect --format {{.State.Pid}} " + containerId
                                       + "` > " + pidFile.ToString() + ".tmp";
            bool pidSetterFound = false;

            while (lnr.Ready())
            {
                string line = lnr.ReadLine();
                Log.Debug("line: " + line);
                if (line.StartsWith(DockerLaunchCommand))
                {
                    IList <string> command = new AList <string>();
                    foreach (string s in line.Split("\\s+"))
                    {
                        command.AddItem(s.Trim());
                    }
                    NUnit.Framework.Assert.AreEqual(expectedCommands, command);
                    cmdFound = true;
                }
                else
                {
                    if (line.StartsWith("echo"))
                    {
                        NUnit.Framework.Assert.AreEqual(expectedPidString, line);
                        pidSetterFound = true;
                    }
                }
            }
            NUnit.Framework.Assert.IsTrue(cmdFound);
            NUnit.Framework.Assert.IsTrue(pidSetterFound);
        }