-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commits adds two new commands to sarus * `ps`: list the containers * `kill`: stops and destroy a container In addition * introduces the option `-n, --name` to the `run` command that allows to specify the name of the container * changes the default name of the container from `container-*` to `sarus-container-*` * changes the root path of `crun` from `/run/runc` to `/run/runc/<UID>` to ensure container isolation among users
- Loading branch information
1 parent
4d80be2
commit f39e22e
Showing
14 changed files
with
368 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Sarus | ||
# | ||
# Copyright (c) 2018-2023, ETH Zurich. All rights reserved. | ||
# | ||
# Please, refer to the LICENSE file in the root directory. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import common.util as util | ||
import psutil | ||
import subprocess | ||
import time | ||
import unittest | ||
|
||
from pathlib import Path | ||
|
||
|
||
class TestCommandKill(unittest.TestCase): | ||
|
||
CONTAINER_IMAGE = util.ALPINE_IMAGE | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
try: | ||
util.pull_image_if_necessary( | ||
is_centralized_repository=False, image=cls.CONTAINER_IMAGE) | ||
except Exception as e: | ||
print(e) | ||
|
||
def test_kill_command_is_defined(self): | ||
try: | ||
subprocess.check_output(["sarus", "help", "kill"]) | ||
except subprocess.CalledProcessError as _: | ||
self.fail("Can't execute command `sarus kill`") | ||
|
||
|
||
def test_kill_deletes_running_container(self): | ||
sarus_process = psutil.Popen(["sarus", "run", "--name", "test_container", self.CONTAINER_IMAGE, "sleep", "5"]) | ||
|
||
time.sleep(2) | ||
sarus_children = sarus_process.children(recursive=True) | ||
self.assertGreater(len(sarus_children), 0, "At least the sleep process must be there") | ||
|
||
psutil.Popen(["sarus", "kill", "test_container"]) | ||
time.sleep(1) | ||
|
||
self.assertFalse(any([p.is_running() for p in sarus_children]), | ||
"Sarus child processes were not cleaned up") | ||
self.assertFalse(list(Path("/sys/fs/cgroup/cpuset").glob("test_container")), | ||
"Cgroup subdir was not cleaned up") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Sarus | ||
# | ||
# Copyright (c) 2018-2023, ETH Zurich. All rights reserved. | ||
# | ||
# Please, refer to the LICENSE file in the root directory. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import common.util as util | ||
import pytest | ||
import psutil | ||
import subprocess | ||
import time | ||
import unittest | ||
|
||
|
||
class TestCommandPs(unittest.TestCase): | ||
|
||
CONTAINER_IMAGE = util.ALPINE_IMAGE | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
try: | ||
util.pull_image_if_necessary( | ||
is_centralized_repository=False, image=cls.CONTAINER_IMAGE) | ||
except Exception as e: | ||
print(e) | ||
|
||
def test_ps_command_is_defined(self): | ||
try: | ||
subprocess.check_output(["sarus", "help", "ps"]) | ||
except subprocess.CalledProcessError as _: | ||
self.fail("Can't execute command `sarus ps`") | ||
|
||
def test_ps_shows_running_container(self): | ||
sarus_process = psutil.Popen(["sarus", "run", "--name", "test_container", self.CONTAINER_IMAGE, "sleep", "5"]) | ||
time.sleep(2) | ||
output = subprocess.check_output(["sarus", "ps"]).decode() | ||
self.assertGreater(len(output.splitlines()),1) | ||
self.assertTrue(any(["test_container" in line for line in output.splitlines()])) | ||
|
||
@pytest.mark.skip("This test requires to run with a different identity") | ||
def test_ps_hides_running_container_from_other_users(self): | ||
sarus_process = psutil.Popen(["sarus", "run", "--name", "test_container", self.CONTAINER_IMAGE, "sleep", "5"]) | ||
time.sleep(2) | ||
output = subprocess.check_output(["sarus", "ps"], user="janedoe").decode() | ||
|
||
try: | ||
self.assertEqual(len(output.splitlines()), 1) | ||
except AssertionError: | ||
self.assertGreater(len(output.splitlines()), 1) | ||
self.assertFalse(any(["test_container" in line for line in output.splitlines()])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Sarus | ||
* | ||
* Copyright (c) 2018-2023, ETH Zurich. All rights reserved. | ||
* | ||
* Please, refer to the LICENSE file in the root directory. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
*/ | ||
|
||
#ifndef cli_CommandStop_hpp | ||
#define cli_CommandStop_hpp | ||
|
||
#include "cli/Command.hpp" | ||
#include "cli/HelpMessage.hpp" | ||
#include "cli/Utility.hpp" | ||
|
||
#include <runtime/Runtime.hpp> | ||
#include <runtime/Utility.hpp> | ||
|
||
#include <boost/format.hpp> | ||
#include <boost/program_options.hpp> | ||
|
||
namespace sarus { | ||
namespace cli { | ||
|
||
class CommandKill : public Command { | ||
public: | ||
CommandKill() { } | ||
|
||
CommandKill(const libsarus::CLIArguments &args, std::shared_ptr<common::Config> conf) | ||
: conf{std::move(conf)} { | ||
parseCommandArguments(args); | ||
} | ||
|
||
void execute() override { | ||
libsarus::logMessage(boost::format("kill container: %s") % containerName, libsarus::LogLevel::INFO); | ||
|
||
auto runcPath = conf->json["runcPath"].GetString(); | ||
auto args = libsarus::CLIArguments{runcPath, | ||
"--root", "/run/runc/" + std::to_string(conf->userIdentity.uid), | ||
"kill", containerName, "SIGHUP"}; | ||
|
||
// execute runc | ||
auto status = libsarus::forkExecWait(args); | ||
|
||
if (status != 0) { | ||
auto message = boost::format("%s exited with code %d") % args % status; | ||
libsarus::logMessage(message, libsarus::LogLevel::WARN); | ||
exit(status); | ||
} | ||
}; | ||
|
||
bool requiresRootPrivileges() const override { return true; }; | ||
std::string getBriefDescription() const override { return "Kill a running container"; }; | ||
void printHelpMessage() const override { | ||
auto printer = cli::HelpMessage() | ||
.setUsage("sarus kill [NAME]\n") | ||
.setDescription(getBriefDescription()); | ||
std::cout << printer; | ||
}; | ||
|
||
private: | ||
|
||
void parseCommandArguments(const libsarus::CLIArguments &args) { | ||
cli::utility::printLog("parsing CLI arguments of kill command", libsarus::LogLevel::DEBUG); | ||
|
||
libsarus::CLIArguments nameAndOptionArgs, positionalArgs; | ||
std::tie(nameAndOptionArgs, positionalArgs) = cli::utility::groupOptionsAndPositionalArguments(args, boost::program_options::options_description{}); | ||
|
||
// the kill command expects exactly one positional argument (the container name) | ||
cli::utility::validateNumberOfPositionalArguments(positionalArgs, 1, 1, "kill"); | ||
|
||
try { | ||
containerName = positionalArgs.argv()[0]; | ||
} catch (std::exception &e) { | ||
auto message = boost::format("%s\nSee 'sarus help kill'") % e.what(); | ||
cli::utility::printLog(message, libsarus::LogLevel::GENERAL, std::cerr); | ||
SARUS_THROW_ERROR(message.str(), libsarus::LogLevel::INFO); | ||
} | ||
} | ||
|
||
std::string containerName; | ||
std::shared_ptr<common::Config> conf; | ||
}; | ||
|
||
} // namespace cli | ||
} // namespace sarus | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.