Skip to content

Commit 871e823

Browse files
committed
refactor + minor issues
1 parent 0f9ab1d commit 871e823

24 files changed

Lines changed: 169 additions & 128 deletions

File tree

deploy/services/cli/dtaas_services/commands/service_ops.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
check_running_services_for_clean,
1414
print_clean_status,
1515
build_clean_status_message,
16+
_handle_operation_result,
1617
)
1718

1819

@@ -165,10 +166,7 @@ def clean(service_names, certs):
165166
with console.status(status_msg, spinner="dots"):
166167
err, msg = setup_obj.clean_services(service_list, include_certs=certs)
167168

168-
if err is not None:
169-
raise click.ClickException(msg)
170-
171-
console.print(f"[green]✅ {msg}[/green]")
169+
_handle_operation_result(console, err, msg)
172170

173171
except FileNotFoundError as e:
174172
raise click.ClickException(str(e)) from e

deploy/services/cli/dtaas_services/commands/setup_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _install_thingsboard(console: Console, service_obj: Service) -> None:
146146
"It typically takes 5\u201310 minutes after first start.[/yellow]\n"
147147
"[cyan]Next steps:[/cyan]\n"
148148
" 1. Check status: dtaas-services status -s gitlab\n"
149-
"It will be 'starting' while it's initlizing, then 'not ready' while it's installing."
149+
"It will be 'starting' while it's initializing, then 'not ready' while it's installing."
150150
" 2. When status shows 'running', re-run: "
151151
"dtaas-services install -s gitlab"
152152
)

deploy/services/cli/dtaas_services/commands/utility.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ def _print_operation_status(
3737
console.print(f"[{meta.color}]{meta.name} all services....[/{meta.color}]")
3838

3939

40+
def _handle_operation_result(console: Console, err, msg: str) -> None:
41+
"""Handle the result of a service operation.
42+
43+
Args:
44+
console: Rich console for output
45+
err: Error object from operation (None if success)
46+
msg: Result message from operation
47+
48+
Raises:
49+
click.ClickException: If operation failed
50+
"""
51+
if err is not None:
52+
raise click.ClickException(msg)
53+
console.print(f"[green]✅ {msg}[/green]")
54+
55+
4056
def _handle_service_command(
4157
operation_func: Callable,
4258
service_list: Optional[list[str]],
@@ -52,10 +68,7 @@ def _handle_service_command(
5268
):
5369
err, msg = operation_func(service_list)
5470

55-
if err is not None:
56-
raise click.ClickException(msg)
57-
58-
console.print(f"[green]✅ {msg}[/green]")
71+
_handle_operation_result(console, err, msg)
5972
except FileNotFoundError as e:
6073
raise click.ClickException(str(e)) from e
6174

deploy/services/cli/dtaas_services/pkg/lib/status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Service status & inspection"""
22

3-
from typing import Any, Tuple, Optional, Set
3+
from typing import Tuple, Optional, Set
44
from python_on_whales import Container
55
from .utils import check_compose_file, DOCKER_OPERATION_EXCEPTIONS
66
from ..formatter import RemovedServiceEntry
@@ -194,7 +194,7 @@ def _categorize_container_state(
194194
)
195195

196196
def _process_single_container(
197-
self, container: Any, container_map: dict, all_services: set
197+
self, container: Container, container_map: dict, all_services: set
198198
) -> None:
199199
"""Process a single container and add to map if it matches a service."""
200200
if not self._match_container_by_name(container, container_map, all_services):

deploy/services/cli/dtaas_services/pkg/lib/utils.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
TypeError,
1616
)
1717

18+
# List of service names for data/log directory management
19+
SERVICE_DATA_SUBDIRS = [
20+
"grafana",
21+
"gitlab",
22+
"influxdb",
23+
"mongodb",
24+
"postgres",
25+
"rabbitmq",
26+
"thingsboard",
27+
]
28+
1829

1930
def try_remove_file(path: Path) -> None:
2031
"""Try to remove a file with permission handling."""
@@ -221,12 +232,4 @@ def get_data_subdirectories(service_list: Optional[list] = None) -> list:
221232
"""Get list of data subdirectories to clean."""
222233
if service_list:
223234
return service_list
224-
return [
225-
"grafana",
226-
"influxdb",
227-
"mongodb",
228-
"postgres",
229-
"rabbitmq",
230-
"thingsboard",
231-
"gitlab",
232-
]
235+
return SERVICE_DATA_SUBDIRS

deploy/services/cli/dtaas_services/pkg/password_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
logger = logging.getLogger(__name__)
99

10-
PASSWORDS_FILE = "current.passwords.env"
10+
PASSWORDS_FILE = "password.env.current"
1111

1212
# Keys belonging to each service (used by remove_service_passwords)
1313
SERVICE_KEYS = {

deploy/services/cli/dtaas_services/pkg/services/gitlab/health.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from typing import Optional
55
from python_on_whales import DockerClient
6+
from ...utils import get_container_health_status, has_running_container
67

78
logger = logging.getLogger(__name__)
89

@@ -26,23 +27,6 @@ def _get_gitlab_container(docker):
2627
return None
2728

2829

29-
def _check_container_health(container) -> str:
30-
"""Get the health status of the GitLab container.
31-
32-
Args:
33-
container: Docker container object
34-
35-
Returns:
36-
Health status string, or "unknown" if not available
37-
"""
38-
try:
39-
if hasattr(container.state, "health") and container.state.health:
40-
return container.state.health.status
41-
except (AttributeError, TypeError):
42-
logger.warning("Could not get health status for gitlab.")
43-
return "unknown state"
44-
45-
4630
def is_gitlab_healthy(docker) -> str:
4731
"""Check the current health status of the GitLab container (non-blocking).
4832
@@ -56,7 +40,7 @@ def is_gitlab_healthy(docker) -> str:
5640
container = _get_gitlab_container(docker)
5741
if container is None:
5842
return "not found"
59-
return _check_container_health(container)
43+
return get_container_health_status(container)
6044

6145

6246
def is_gitlab_running() -> bool:
@@ -71,9 +55,7 @@ def is_gitlab_running() -> bool:
7155
try:
7256
docker = DockerClient()
7357
containers = docker.container.list(filters={"name": GITLAB_CONTAINER_NAME})
74-
return any(
75-
hasattr(c, "state") and c.state.status == "running" for c in containers
76-
)
58+
return has_running_container(containers)
7759
except Exception:
7860
logger.exception("Error while checking if GitLab is running")
7961
return False

deploy/services/cli/dtaas_services/pkg/services/gitlab/users.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def _create_single_user(gl: gitlab.Gitlab, row: dict) -> Tuple[bool, str, int |
5353
return False, f"Failed to create user '{username}': {exc}", None
5454

5555

56-
def _create_user_and_pat(
57-
gl: gitlab.Gitlab, row: dict
58-
) -> Tuple[bool, str, str]:
56+
def _create_user_and_pat(gl: gitlab.Gitlab, row: dict) -> Tuple[bool, str, str]:
5957
"""Create a user and, if newly created, their Personal Access Token.
6058
6159
Args:

deploy/services/cli/dtaas_services/pkg/services/thingsboard/checker.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
import click
99
from rich.console import Console
10-
from ...utils import is_ci
10+
from ...utils import has_running_container, is_ci, is_container_running
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -36,10 +36,7 @@ def _validate_postgres_for_thingsboard_check(container_map: dict) -> bool:
3636
return False
3737

3838
postgres_container = container_map["postgres"]
39-
return (
40-
hasattr(postgres_container, "state")
41-
and postgres_container.state.status == "running"
42-
)
39+
return is_container_running(postgres_container)
4340

4441

4542
def _find_thingsboard_containers(docker) -> list:
@@ -58,30 +55,6 @@ def _find_thingsboard_containers(docker) -> list:
5855
return []
5956

6057

61-
def _is_container_running(container) -> bool:
62-
"""Check if a single container is running.
63-
64-
Args:
65-
container: Container object
66-
67-
Returns:
68-
True if container has state and is running
69-
"""
70-
return hasattr(container, "state") and container.state.status == "running"
71-
72-
73-
def _has_running_container(containers: list) -> bool:
74-
"""Check if any container in list is running.
75-
76-
Args:
77-
containers: List of container objects
78-
79-
Returns:
80-
True if any container is running
81-
"""
82-
return any(_is_container_running(container) for container in containers)
83-
84-
8558
def _is_thingsboard_container_running(docker) -> bool:
8659
"""Check if ThingsBoard container is running.
8760
@@ -90,7 +63,7 @@ def _is_thingsboard_container_running(docker) -> bool:
9063
while ThingsBoard is running since ThingsBoard depends on PostgreSQL.
9164
"""
9265
containers = _find_thingsboard_containers(docker)
93-
return _has_running_container(containers)
66+
return has_running_container(containers)
9467

9568

9669
def is_thingsboard_running() -> bool:

deploy/services/cli/dtaas_services/pkg/services/thingsboard/tb_utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run_thingsboard_install(console: Console, docker) -> None:
6767
"[bold cyan]Installing ThingsBoard schema...[/bold cyan]",
6868
spinner="dots",
6969
):
70-
timeout = int(os.getenv("THINGSBOARD_INSTALL_TIMEOUT", "300"))
70+
timeout = 400
7171

7272
try:
7373
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:

0 commit comments

Comments
 (0)