Skip to content

Commit 2eb1276

Browse files
feat(api-tools): add use_log and dry_run params to __execute_command function
1 parent 29727fa commit 2eb1276

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/api/services/apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ async def download_file(
655655

656656
@staticmethod
657657
async def sync_dokku_with_api_database() -> None:
658-
success, message = await run_command("apps:list")
658+
success, message = await run_command("apps:list", use_log=False)
659659

660660
if not success:
661661
logging.warning("Could not recover apps list to sync with database")
@@ -675,6 +675,6 @@ async def sync_dokku_with_api_database() -> None:
675675
logging.warning(
676676
f"[sync_dokku_w_app_database]:{app_name}::Destroying unused application..."
677677
)
678-
await run_command(f"--force apps:destroy {app_name}")
678+
await run_command(f"--force apps:destroy {app_name}", use_log=False)
679679

680680
logging.warning("[sync_dokku_w_app_database]::Sync complete.")

src/api/services/databases.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ async def sync_dokku_with_api_database() -> None:
389389
services = {}
390390

391391
for plugin_name in available_databases:
392-
success, message = await run_command(f"{plugin_name}:list")
392+
success, message = await run_command(f"{plugin_name}:list", use_log=False)
393393

394394
if not success:
395395
logging.warning(
@@ -413,6 +413,8 @@ async def sync_dokku_with_api_database() -> None:
413413
logging.warning(
414414
f"[sync_dokku_w_service_database]:{plugin_name}:{service_name}::Destroying unused service..."
415415
)
416-
await run_command(f"--force {plugin_name}:destroy {service_name}")
416+
await run_command(
417+
f"--force {plugin_name}:destroy {service_name}", use_log=False
418+
)
417419

418420
logging.warning("[sync_dokku_w_service_database]::Sync complete.")

src/api/services/networks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async def get_linked_apps(
182182

183183
@staticmethod
184184
async def sync_dokku_with_api_database() -> None:
185-
success, message = await run_command("network:list")
185+
success, message = await run_command("network:list", use_log=False)
186186

187187
if not success:
188188
logging.warning("Could not recover networks list to sync with database")
@@ -206,6 +206,6 @@ async def sync_dokku_with_api_database() -> None:
206206
logging.warning(
207207
f"[sync_dokku_w_network_database]:{network_name}::Destroying unused network..."
208208
)
209-
await run_command(f"--force network:destroy {network_name}")
209+
await run_command(f"--force network:destroy {network_name}", use_log=False)
210210

211211
logging.warning("[sync_dokku_w_network_database]::Sync complete.")

src/api/tools/ssh.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,29 @@ def get_command_history() -> list:
3434
return list(command_history)
3535

3636

37-
async def __execute_command(command: str, username: str) -> Tuple[bool, str]:
37+
async def __execute_command(
38+
command: str, username: str, use_log: bool = True, dry_run: bool = False
39+
) -> Tuple[bool, str]:
3840
"""
3941
Execute a command on the remote server via SSH.
4042
4143
Args:
4244
command (str): The command to execute.
4345
username (str): The SSH username.
46+
use_log (bool): If True, save the command at a command history (in-memory).
47+
dry_run (bool): If True, the command is not actually executed.
4448
Returns:
4549
Tuple[bool, str]: A tuple containing a boolean indicating success
4650
or failure and the output or error message.
4751
"""
4852
if username == "root":
4953
command = f"dokku {command}"
5054

51-
_log_command(command)
55+
if use_log:
56+
_log_command(command)
57+
58+
if dry_run:
59+
return True, ""
5260

5361
try:
5462
async with asyncssh.connect(
@@ -80,17 +88,23 @@ async def __execute_command(command: str, username: str) -> Tuple[bool, str]:
8088
return False, str(error)
8189

8290

83-
async def run_command(command: str) -> Tuple[bool, str]:
91+
async def run_command(
92+
command: str, use_log: bool = True, dry_run: bool = False
93+
) -> Tuple[bool, str]:
8494
"""
8595
Run a command on the remote server via SSH.
8696
8797
Args:
8898
command (str): The command to execute.
99+
use_log (bool): If True, save the command at a command history (in-memory).
100+
dry_run (bool): If True, the command is not actually executed.
89101
Returns:
90102
Tuple[bool, str]: A tuple containing a boolean indicating success
91103
or failure and the output or error message.
92104
"""
93-
success, message = await __execute_command(command, "dokku")
105+
success, message = await __execute_command(
106+
command, "dokku", use_log=use_log, dry_run=dry_run
107+
)
94108

95109
if success:
96110
logging.info(f"Command executed successfully: {command}")
@@ -99,17 +113,23 @@ async def run_command(command: str) -> Tuple[bool, str]:
99113
return success, message
100114

101115

102-
async def run_command_as_root(command: str) -> Tuple[bool, str]:
116+
async def run_command_as_root(
117+
command: str, use_log: bool = True, dry_run: bool = False
118+
) -> Tuple[bool, str]:
103119
"""
104120
Run a command on the remote server via SSH as root.
105121
106122
Args:
107123
command (str): The command to execute.
124+
use_log (bool): If True, save the command at a command history (in-memory).
125+
dry_run (bool): If True, the command is not actually executed.
108126
Returns:
109127
Tuple[bool, str]: A tuple containing a boolean indicating success
110128
or failure and the output or error message.
111129
"""
112-
success, message = await __execute_command(command, "root")
130+
success, message = await __execute_command(
131+
command, "root", use_log=use_log, dry_run=dry_run
132+
)
113133

114134
if success:
115135
logging.info(f"Command executed successfully: {command}")

0 commit comments

Comments
 (0)