|
2 | 2 |
|
3 | 3 | import contextlib
|
4 | 4 | import os
|
| 5 | +import sys |
5 | 6 | import stat
|
6 | 7 | import subprocess
|
7 | 8 | from collections import defaultdict
|
8 |
| -from typing import Dict, Type |
| 9 | +from typing import Dict, Type, List |
9 | 10 |
|
10 | 11 | import jinja2 as jinja2
|
11 | 12 | from config.config_mgmt import ConfigMgmt
|
@@ -96,22 +97,22 @@ def remove_if_exists(path):
|
96 | 97 | os.remove(path)
|
97 | 98 | log.info(f'removed {path}')
|
98 | 99 |
|
| 100 | +def is_list_of_strings(command): |
| 101 | + return isinstance(command, list) and all(isinstance(item, str) for item in command) |
99 | 102 |
|
100 |
| -def run_command(command: str): |
| 103 | +def run_command(command: List[str]): |
101 | 104 | """ Run arbitrary bash command.
|
102 | 105 | Args:
|
103 |
| - command: String command to execute as bash script |
| 106 | + command: List of Strings command to execute as bash script |
104 | 107 | Raises:
|
105 | 108 | ServiceCreatorError: Raised when the command return code
|
106 | 109 | is not 0.
|
107 | 110 | """
|
108 |
| - |
| 111 | + if not is_list_of_strings(command): |
| 112 | + sys.exit("Input command should be a list of strings") |
109 | 113 | log.debug(f'running command: {command}')
|
110 | 114 |
|
111 |
| - proc = subprocess.Popen(command, |
112 |
| - shell=True, |
113 |
| - executable='/bin/bash', |
114 |
| - stdout=subprocess.PIPE) |
| 115 | + proc = subprocess.Popen(command, stdout=subprocess.PIPE) |
115 | 116 | (_, _) = proc.communicate()
|
116 | 117 | if proc.returncode != 0:
|
117 | 118 | raise ServiceCreatorError(f'Failed to execute "{command}"')
|
@@ -647,4 +648,4 @@ def _post_operation_hook(self):
|
647 | 648 | """ Common operations executed after service is created/removed. """
|
648 | 649 |
|
649 | 650 | if not in_chroot():
|
650 |
| - run_command('systemctl daemon-reload') |
| 651 | + run_command(['systemctl', 'daemon-reload']) |
0 commit comments