Skip to content

Commit f22de18

Browse files
authored
Merge pull request #63 from stackql/feature/ja-updates
Logging update
2 parents 916e111 + 6b106f2 commit f22de18

File tree

8 files changed

+83
-35
lines changed

8 files changed

+83
-35
lines changed

setup.py

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

1111
setup(
1212
name='stackql-deploy',
13-
version='1.8.6',
13+
version='1.8.7',
1414
description='Model driven resource provisioning and deployment framework using StackQL.',
1515
long_description=readme,
1616
long_description_content_type='text/x-rst',

stackql_deploy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.8.6'
1+
__version__ = '1.8.7'

stackql_deploy/cli.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import os
44
import sys
55
import subprocess
6+
67
from . import __version__ as deploy_version
78
from .lib.bootstrap import logger
9+
from .lib.utils import print_unicode_box, BorderColor
810
from .cmd.build import StackQLProvisioner
911
from .cmd.test import StackQLTestRunner
1012
from .cmd.teardown import StackQLDeProvisioner
@@ -16,20 +18,6 @@
1618
# utility functions
1719
#
1820

19-
def print_unicode_box(message):
20-
border_color = '\033[93m' # Yellow color
21-
reset_color = '\033[0m'
22-
23-
lines = message.split('\n')
24-
max_length = max(len(line) for line in lines)
25-
top_border = border_color + '┌' + '─' * (max_length + 2) + '┐' + reset_color
26-
bottom_border = border_color + '└' + '─' * (max_length + 2) + '┘' + reset_color
27-
28-
click.echo(top_border)
29-
for line in lines:
30-
click.echo(border_color + '│ ' + line.ljust(max_length) + ' │' + reset_color)
31-
click.echo(bottom_border)
32-
3321
def get_stackql_instance(custom_registry=None, download_dir=None):
3422
"""Initializes StackQL with the given options."""
3523
stackql_kwargs = {}
@@ -190,7 +178,7 @@ def build(ctx, stack_dir, stack_env, log_level, env_file,
190178
)
191179
message = (f"Deploying stack: [{stack_name_display}] "
192180
f"to environment: [{stack_env}]")
193-
print_unicode_box(message)
181+
print_unicode_box(message, BorderColor.YELLOW)
194182

195183
provisioner.run(dry_run, show_queries, on_failure)
196184
click.echo("🎯 dry-run build complete" if dry_run
@@ -222,7 +210,7 @@ def teardown(ctx, stack_dir, stack_env, log_level, env_file,
222210
)
223211
message = (f"Tearing down stack: [{stack_name_display}] "
224212
f"in environment: [{stack_env}]")
225-
print_unicode_box(message)
213+
print_unicode_box(message, BorderColor.YELLOW)
226214

227215
deprovisioner.run(dry_run, show_queries, on_failure)
228216
click.echo(f"🚧 teardown complete (dry run: {dry_run})")
@@ -253,7 +241,7 @@ def test(ctx, stack_dir, stack_env, log_level, env_file,
253241
)
254242
message = (f"Testing stack: [{stack_name_display}] "
255243
f"in environment: [{stack_env}]")
256-
print_unicode_box(message)
244+
print_unicode_box(message, BorderColor.YELLOW)
257245

258246
test_runner.run(dry_run, show_queries, on_failure)
259247
click.echo(f"🔍 tests complete (dry run: {dry_run})")

stackql_deploy/cmd/base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ def process_exports(
119119
show_query(True, exports_query, self.logger)
120120
catch_error_and_exit(f"exports query failed for {resource['name']}", self.logger)
121121

122+
# Check if we received an error from the query execution
123+
if (len(exports) >= 1 and isinstance(exports[0], dict)):
124+
# Check for our custom error wrapper
125+
if '_stackql_deploy_error' in exports[0]:
126+
error_msg = exports[0]['_stackql_deploy_error']
127+
show_query(True, exports_query, self.logger)
128+
catch_error_and_exit(
129+
f"exports query failed for {resource['name']}\n\nError details:\n{error_msg}",
130+
self.logger
131+
)
132+
# Check for direct error in result
133+
elif 'error' in exports[0]:
134+
error_msg = exports[0]['error']
135+
show_query(True, exports_query, self.logger)
136+
catch_error_and_exit(
137+
f"exports query failed for {resource['name']}\n\nError details:\n{error_msg}",
138+
self.logger
139+
)
140+
122141
if len(exports) > 1:
123142
catch_error_and_exit(
124143
f"exports should include one row only, received {str(len(exports))} rows",

stackql_deploy/cmd/build.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
catch_error_and_exit,
55
export_vars,
66
run_ext_script,
7-
get_type
7+
get_type,
8+
print_unicode_box,
9+
BorderColor
810
)
911
from ..lib.config import get_full_context, render_value
1012
from ..lib.templating import get_queries, render_inline_template
@@ -43,6 +45,8 @@ def run(self, dry_run, show_queries, on_failure):
4345

4446
for resource in self.manifest.get('resources', []):
4547

48+
print_unicode_box(f"Processing resource: [{resource['name']}]", BorderColor.BLUE)
49+
4650
type = get_type(resource, self.logger)
4751

4852
self.logger.info(f"processing resource [{resource['name']}], type: {type}")

stackql_deploy/cmd/teardown.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import datetime
33
from ..lib.utils import (
44
catch_error_and_exit,
5-
get_type
5+
get_type,
6+
print_unicode_box,
7+
BorderColor
68
)
79
from ..lib.config import get_full_context, render_value
810
from ..lib.templating import get_queries, render_inline_template
@@ -70,6 +72,9 @@ def run(self, dry_run, show_queries, on_failure):
7072
self.collect_exports(show_queries, dry_run)
7173

7274
for resource in reversed(self.manifest['resources']):
75+
76+
print_unicode_box(f"Processing resource: [{resource['name']}]", BorderColor.RED)
77+
7378
# process resources in reverse order
7479
type = get_type(resource, self.logger)
7580

stackql_deploy/cmd/test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import datetime
33
from ..lib.utils import (
44
catch_error_and_exit,
5-
get_type
5+
get_type,
6+
print_unicode_box,
7+
BorderColor
68
)
79
from ..lib.config import get_full_context
810
from ..lib.templating import get_queries, render_inline_template
@@ -19,6 +21,8 @@ def run(self, dry_run, show_queries, on_failure):
1921

2022
for resource in self.manifest.get('resources', []):
2123

24+
print_unicode_box(f"Processing resource: [{resource['name']}]", BorderColor.BLUE)
25+
2226
type = get_type(resource, self.logger)
2327

2428
if type == 'query':

stackql_deploy/lib/utils.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
# lib/utils.py
2+
import click
3+
from enum import Enum
24
import time
35
import json
46
import sys
57
import subprocess
68
import re
79

10+
class BorderColor(Enum):
11+
YELLOW = '\033[93m' # Bright yellow
12+
BLUE = '\033[94m' # Bright blue
13+
RED = '\033[91m' # Bright red
14+
15+
def print_unicode_box(message: str, color: BorderColor = BorderColor.YELLOW):
16+
border_color = color.value
17+
reset_color = '\033[0m'
18+
19+
lines = message.split('\n')
20+
max_length = max(len(line) for line in lines)
21+
top_border = border_color + '┌' + '─' * (max_length + 2) + '┐' + reset_color
22+
bottom_border = border_color + '└' + '─' * (max_length + 2) + '┘' + reset_color
23+
24+
click.echo(top_border)
25+
for line in lines:
26+
click.echo(border_color + '│ ' + line.ljust(max_length) + ' │' + reset_color)
27+
click.echo(bottom_border)
28+
829
def catch_error_and_exit(errmsg, logger):
930
logger.error(errmsg)
1031
sys.exit("stackql-deploy operation failed 🚫")
@@ -18,6 +39,7 @@ def get_type(resource, logger):
1839

1940
def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None, env_vars=None, retries=0, delay=5):
2041
attempt = 0
42+
last_error = None
2143
while attempt <= retries:
2244
try:
2345
logger.debug(f"(utils.run_stackql_query) executing stackql query on attempt {attempt + 1}:\n\n{query}\n")
@@ -29,20 +51,22 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
2951
if len(result) == 0:
3052
logger.debug("(utils.run_stackql_query) stackql query executed successfully, retrieved 0 items.")
3153
pass
32-
elif not suppress_errors and result and 'error' in result[0]:
54+
elif result and 'error' in result[0]:
3355
error_message = result[0]['error']
34-
if attempt == retries:
35-
# If retries are exhausted, log the error and exit
36-
catch_error_and_exit(
37-
(
38-
f"(utils.run_stackql_query) error occurred during stackql query execution:\n\n"
39-
f"{error_message}\n"
40-
),
41-
logger
42-
)
43-
else:
44-
# Log the error and prepare for another attempt
45-
logger.error(f"attempt {attempt + 1} failed:\n\n{error_message}\n")
56+
last_error = error_message # Store the error for potential return
57+
if not suppress_errors:
58+
if attempt == retries:
59+
# If retries are exhausted, log the error and exit
60+
catch_error_and_exit(
61+
(
62+
f"(utils.run_stackql_query) error occurred during stackql query execution:\n\n"
63+
f"{error_message}\n"
64+
),
65+
logger
66+
)
67+
else:
68+
# Log the error and prepare for another attempt
69+
logger.error(f"attempt {attempt + 1} failed:\n\n{error_message}\n")
4670
elif 'count' in result[0]:
4771
# If the result is a count query, return the count
4872
logger.debug(
@@ -74,6 +98,7 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
7498

7599
except Exception as e:
76100
# Log the exception and check if retry attempts are exhausted
101+
last_error = str(e) # Store the exception for potential return
77102
if attempt == retries:
78103
catch_error_and_exit(
79104
f"(utils.run_stackql_query) an exception occurred during stackql query execution:\n\n{str(e)}\n",
@@ -87,6 +112,9 @@ def run_stackql_query(query, stackql, suppress_errors, logger, custom_auth=None,
87112
attempt += 1
88113

89114
logger.debug(f"(utils.run_stackql_query) all attempts ({retries + 1}) to execute the query completed.")
115+
# If suppress_errors is True and we have an error, return an empty list with error info as a special dict
116+
if suppress_errors and last_error:
117+
return [{'_stackql_deploy_error': last_error}]
90118
# return None
91119
return []
92120

0 commit comments

Comments
 (0)