nohup: add option to run command with nohup

NOTE: not guaranteed that shell python class will understand
the connection was reset in time. Also redirects stdout/stderr
to a seperate file.
This commit is contained in:
Matthew G McGovern 2022-10-03 09:52:25 -07:00 коммит произвёл Chi Song
Родитель 9f757500fb
Коммит 0f616568f2
4 изменённых файлов: 27 добавлений и 4 удалений

Просмотреть файл

@ -189,6 +189,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd: str,
shell: bool = False,
sudo: bool = False,
nohup: bool = False,
no_error_log: bool = False,
no_info_log: bool = True,
no_debug_log: bool = False,
@ -202,6 +203,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd,
shell=shell,
sudo=sudo,
nohup=nohup,
no_error_log=no_error_log,
no_info_log=no_info_log,
no_debug_log=no_debug_log,
@ -219,6 +221,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd: str,
shell: bool = False,
sudo: bool = False,
nohup: bool = False,
no_error_log: bool = False,
no_info_log: bool = True,
no_debug_log: bool = False,
@ -236,6 +239,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd,
shell=shell,
sudo=sudo,
nohup=nohup,
no_error_log=no_error_log,
no_info_log=no_info_log,
no_debug_log=no_debug_log,
@ -374,6 +378,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd: str,
shell: bool = False,
sudo: bool = False,
nohup: bool = False,
no_error_log: bool = False,
no_info_log: bool = False,
no_debug_log: bool = False,
@ -386,6 +391,7 @@ class Node(subclasses.BaseClassWithRunbookMixin, ContextMixin, InitializableMixi
cmd,
shell=shell,
sudo=sudo,
nohup=nohup,
no_error_log=no_error_log,
no_info_log=no_info_log,
no_debug_log=no_debug_log,

Просмотреть файл

@ -89,6 +89,7 @@ class Ip(Tool):
),
shell=True,
sudo=True,
nohup=True,
expected_exit_code=0,
expected_exit_code_failure_message=(
f"fail to restart [down then up] the nic {nic_name}"

Просмотреть файл

@ -26,6 +26,7 @@ class Modprobe(Tool):
"dhclient -r eth0; dhclient eth0",
sudo=True,
shell=True,
nohup=True,
)
def is_module_loaded(

Просмотреть файл

@ -80,6 +80,7 @@ class Process:
self._process: Optional[spur.local.LocalProcess] = None
self._result: Optional[ExecutableResult] = None
self._sudo: bool = False
self._nohup: bool = False
# add a string stream handler to the logger
self._log_buffer = io.StringIO()
@ -91,6 +92,7 @@ class Process:
command: str,
shell: bool = False,
sudo: bool = False,
nohup: bool = False,
cwd: Optional[pathlib.PurePath] = None,
update_envs: Optional[Dict[str, str]] = None,
no_error_log: bool = False,
@ -117,6 +119,7 @@ class Process:
self._stderr_writer = LogWriter(logger=self.stderr_logger, level=stderr_level)
self._sudo = sudo
self._nohup = nohup
if update_envs is None:
update_envs = {}
@ -127,7 +130,7 @@ class Process:
shell = True
update_envs = update_envs.copy()
split_command = self._process_command(command, sudo, shell, update_envs)
split_command = self._process_command(command, sudo, shell, nohup, update_envs)
cwd_path: Optional[str] = None
if cwd:
@ -141,6 +144,7 @@ class Process:
f"cwd: {cwd_path}, "
f"shell: {shell}, "
f"sudo: {sudo}, "
f"nohup: {nohup}"
f"posix: {self._is_posix}, "
f"remote: {self._shell.is_remote}"
)
@ -170,7 +174,12 @@ class Process:
self._log.log(stderr_level, f"not found command: {identifier}")
def _process_command(
self, command: str, sudo: bool, shell: bool, update_envs: Dict[str, str]
self,
command: str,
sudo: bool,
shell: bool,
nohup: bool,
update_envs: Dict[str, str],
) -> List[str]:
# command may be Path object, convert it to str
command = str(command)
@ -180,17 +189,23 @@ class Process:
split_command = ["cmd", "/c", command]
else:
split_command = []
if nohup:
split_command += ["nohup"]
if sudo:
split_command += ["sudo"]
envs = _create_exports(update_envs=update_envs)
if envs:
command = f"{envs} {command}"
split_command += ["sh", "-c", command]
# expand variables in posix mode
update_envs.clear()
else:
if sudo and self._is_posix:
command = f"sudo {command}"
if self._is_posix:
if sudo:
command = f"sudo {command}"
if nohup:
command = f"nohup {command}"
try:
split_command = shlex.split(command, posix=self._is_posix)
except Exception as identifier: