Skip to content

Commit

Permalink
chore: remove shell usage in shutdown subprocess
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Usiskin <[email protected]>
  • Loading branch information
jusiskin committed Sep 27, 2023
1 parent 5a39c25 commit f40eb9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/deadline_worker_agent/startup/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ def _system_shutdown(config: Configuration) -> None:

_logger.info("Shutting down the instance")

shutdown_command = None
shutdown_command: list[str]

if sys.platform == "win32":
shutdown_command = "shutdown -s"
shutdown_command = ["shutdown", "-s"]
else:
shutdown_command = "sudo shutdown now"
shutdown_command = ["sudo", "shutdown", "now"]

if config.no_shutdown:
_logger.debug(
Expand All @@ -227,7 +227,6 @@ def _system_shutdown(config: Configuration) -> None:
shutdown_command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
stdout, _ = process.communicate()
command_output = stdout.decode("utf-8")
Expand Down
14 changes: 6 additions & 8 deletions test/unit/startup/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ def test_system_shutdown_called(
@pytest.mark.parametrize(
("expected_platform", "expected_command"),
(
pytest.param("win32", "shutdown -s", id="windows"),
pytest.param("linux", "sudo shutdown now", id="linux"),
pytest.param("win32", ["shutdown", "-s"], id="windows"),
pytest.param("linux", ["sudo", "shutdown", "now"], id="linux"),
),
)
@patch.object(entrypoint_mod._logger, "info")
Expand Down Expand Up @@ -449,7 +449,6 @@ def test_system_shutdown(
logger_info_mock.assert_any_call("Shutting down the instance")
subprocess_popen_mock.assert_called_once_with(
expected_command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
Expand All @@ -458,8 +457,8 @@ def test_system_shutdown(
@pytest.mark.parametrize(
("expected_platform", "expected_command"),
(
pytest.param("win32", "shutdown -s", id="windows"),
pytest.param("linux", "sudo shutdown now", id="linux"),
pytest.param("win32", ["shutdown", "-s"], id="windows"),
pytest.param("linux", ["sudo", "shutdown", "now"], id="linux"),
),
)
@patch.object(entrypoint_mod, "_logger")
Expand Down Expand Up @@ -495,7 +494,6 @@ def test_system_shutdown_failure(
logger_mock.info.assert_any_call("Shutting down the instance")
subprocess_popen_mock.assert_called_once_with(
expected_command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
Expand All @@ -509,8 +507,8 @@ def test_system_shutdown_failure(
@pytest.mark.parametrize(
("expected_platform", "expected_command"),
(
pytest.param("win32", "shutdown -s", id="windows"),
pytest.param("linux", "sudo shutdown now", id="linux"),
pytest.param("win32", ["shutdown", "-s"], id="windows"),
pytest.param("linux", ["sudo", "shutdown", "now"], id="linux"),
),
)
@patch.object(entrypoint_mod._logger, "debug")
Expand Down

0 comments on commit f40eb9a

Please sign in to comment.