Skip to content

Commit eeeda22

Browse files
megkidobsanders
authored andcommitted
FEATURE: log pallet hook activity to a specific file, /var/log/run-pallet.log
1 parent 0844c2b commit eeeda22

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

common/nodes/logrotate.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Append rules to logrotate to prune files in /var/log
55
</stack:description>
66

77
<stack:copyright>
8-
Copyright (c) 2006 - 2019 Teradata
8+
Copyright (c) 2006 - 2020 Teradata
99
All rights reserved. Stacki(r) v5.x stacki.com
1010
https://github.com/Teradata/stacki/blob/master/LICENSE.txt
1111
</stack:copyright>
@@ -34,6 +34,10 @@ https://github.com/Teradata/stacki/blob/master/LICENSE-ROCKS.txt
3434
2&gt; /dev/null || true
3535
endscript
3636
}
37+
/var/log/pallet-hooks.log {
38+
size=1M
39+
missingok
40+
}
3741
</stack:file>
3842
</stack:script>
3943

common/src/stack/command/stack/argument_processors/pallet.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
from stack.commands import Log
88
from stack.exception import ArgNotFound
9+
import logging
910

1011
# Pallet info from Probepal and the database columns do not have the same names.
1112
# They do have a mapping that ends up being the same values as one another. So
@@ -138,34 +139,45 @@ def _get_pallet_hooks(self, operation, pallet_info):
138139
except FileNotFoundError as exception:
139140
self.notify(f"{script_file} specified in {hook_file} not found:\n\n{exception}")
140141

142+
def log_routine(self, msg, log_methods):
143+
[ _logger(msg) for _logger in log_methods ]
144+
141145
def run_pallet_hooks(self, operation, pallet_info):
146+
# Setup the logger
147+
logging.basicConfig(
148+
filename="/var/log/run-pallet.log",
149+
format="%(asctime)s: %(message)s",
150+
datefmt="%Y-%m-%d %H:%M:%S",
151+
level=logging.DEBUG
152+
)
153+
logger = logging.getLogger(__name__)
154+
142155
"""Run the hooks for the pallet operation in progress."""
143156
pallet_info = self._normalize_pallet_info(pallet_info)
144-
self.notify(f'checking for hooks in {PALLET_HOOK_ROOT} for {"-".join(pallet_info_getter(pallet_info))}')
145-
157+
self.log_routine(f'checking for hooks in {PALLET_HOOK_ROOT} for {"-".join(pallet_info_getter(pallet_info))}', [self.notify, logger.info])
146158
# Find all executable files within the script directory and sort them by name.
147159
hooks = sorted(
148160
self._get_pallet_hooks(operation=operation, pallet_info=pallet_info),
149161
key=lambda script_path: script_path.name,
150162
)
151163
# Execute the hooks in name sorted order.
152164
for hook in hooks:
153-
self.notify(f'running hook: {hook}')
154-
Log(f'Running {operation} pallet hook for pallet {"-".join(pallet_info_getter(pallet_info))}: {hook} ')
165+
self.log_routine(f'running hook: {hook}', [self.notify])
166+
self.log_routine(f'Running {operation} pallet hook for pallet {"-".join(pallet_info_getter(pallet_info))}: {hook} ', [Log, logger.info])
155167
try:
156168
# subprocess's env mapping must be strings to strings!
157169
environ = dict((str(k), str(v)) for k, v in asdict(pallet_info).items())
158170
result = self._exec(['/usr/bin/env', str(hook)], cwd=hook.parent, check=True, env=environ)
159-
Log(f'Result of running {hook} (rc=={result.returncode}):')
160-
Log(f'Env:\n{environ}')
161-
Log(f'Stdout:\n{result.stdout}')
162-
Log(f'Stderr:\n{result.stderr}')
171+
self.log_routine(f'Result of running {hook} (rc=={result.returncode}):', [Log, logger.info])
172+
self.log_routine(f'Env:\n{environ}', [Log, logger.info])
173+
self.log_routine(f'Stdout:\n{result.stdout}', [Log, logger.info])
174+
self.log_routine(f'Stderr:\n{result.stderr}', [Log, logger.info])
163175
except (PermissionError, subprocess.CalledProcessError) as exception:
164176
msg = f'Unable to run hook {hook}:\n\n{exception}\n'
165177
# CalledProcessError has additional info...
166178
if hasattr(exception, 'stdout'):
167179
msg += f'\nstdout:\n{exception.stdout}\n\nstderr:\n{exception.stderr}'
168-
self.notify(msg)
180+
self.log_routine(msg, [self.notify, logger.warning])
169181

170182
def get_pallet_hook_directory(self, pallet_info):
171183
"""Calculate the hook directory for a given pallet."""

0 commit comments

Comments
 (0)