Merge lp:~javier.collado/utah/install_sigterm_handler into lp:utah

Proposed by Javier Collado
Status: Merged
Approved by: Max Brustkern
Approved revision: 819
Merged at revision: 819
Proposed branch: lp:~javier.collado/utah/install_sigterm_handler
Merge into: lp:utah
Diff against target: 70 lines (+25/-2)
3 files modified
examples/run_test_vm.py (+5/-1)
examples/run_utah_tests.py (+1/-1)
utah/run.py (+19/-0)
To merge this branch: bzr merge lp:~javier.collado/utah/install_sigterm_handler
Reviewer Review Type Date Requested Status
Max Brustkern (community) Approve
Javier Collado (community) Needs Resubmitting
Review via email: mp+148457@code.launchpad.net

Description of the change

The changes in this branch add a handler for SIGTERM that just raises an
exception. This is intended to terminate the process gracefully and call all
the cleanup callbacks in the Machine object.

On one hand, I've verified that jenkins sends the SIGTERM signal when a job is
cancelled in the we interface. On the other hand, I've tested this
specificially with `run_test_cobbler.py` to make sure that the image is
unmounted and the loop device freed when the SIGTERM signal is received.

To post a comment you must log in.
Revision history for this message
Andy Doan (doanac) wrote :

nice - all the changes to the run_*.py's are annoying. Could we do that from run.py safely, or is this best for now until we have a better way of managing these different script variations?

Revision history for this message
Javier Collado (javier.collado) wrote :

I agree on that all that duplication is annoying. `install_sigterm_handler` is
already in `run.py` and is only a function call in all the scripts that we use
to run test cases.

One way I can think of to improve the situation would be to refactor both the
logging configuration and this into a single function that could be used to
perform common setup. However, we'd still have the problem of having multiple
entry points and calling something multiple times. Hence, in my opinion, the
real solution would be to refactor all the scripts we have into a single one
and really have a single entry point. Please, let me know your thoughs on this.

Revision history for this message
Andy Doan (doanac) wrote :

On 02/14/2013 12:24 PM, Javier Collado wrote:
> Hence, in my opinion, the
> real solution would be to refactor all the scripts we have into a single one
> and really have a single entry point. Please, let me know your thoughs on this.

yeah, I read the code a bit after my original response and came to the
same conclusion.

Revision history for this message
Max Brustkern (nuclearbob) wrote :

I've got two thoughts that don't require action.

It looks like install_sigterm_handler is always called right before run_tests. Any thoughts about moving it into there?

Also, some of the imports are not in alphabetical order. That doesn't matter at all, but if I'm working in those files in the future, I'll probably change it, because I'm a little weird about that. :)

819. By Javier Collado

Moved install_sigterm_handler to run_tests

Revision history for this message
Javier Collado (javier.collado) wrote :

Max, I think your suggestion is a good one, so I've updated the changes
accordingly.

review: Needs Resubmitting
Revision history for this message
Javier Collado (javier.collado) wrote :

Regarding the imports ordering, it would be nice to have that in the coding
guidelines as well.

Revision history for this message
Max Brustkern (nuclearbob) wrote :

Good point about the coding guidelines. We can discuss that the next time we have a standup or other meeting. The changes look good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'examples/run_test_vm.py'
2--- examples/run_test_vm.py 2013-01-23 12:26:38 +0000
3+++ examples/run_test_vm.py 2013-02-15 08:41:19 +0000
4@@ -21,7 +21,11 @@
5 from utah.exceptions import UTAHException
6 from utah.group import check_user_group, print_group_error_message
7 from utah.provisioning.vm.vm import TinySQLiteInventory
8-from utah.run import common_arguments, run_tests, configure_logging
9+from utah.run import (
10+ common_arguments,
11+ run_tests,
12+ configure_logging,
13+)
14
15
16 def get_parser():
17
18=== modified file 'examples/run_utah_tests.py'
19--- examples/run_utah_tests.py 2013-02-05 16:36:34 +0000
20+++ examples/run_utah_tests.py 2013-02-15 08:41:19 +0000
21@@ -27,7 +27,7 @@
22 name_argument,
23 virtual_arguments,
24 configure_logging,
25- run_tests
26+ run_tests,
27 )
28 from utah.timeout import timeout, UTAHTimeout
29 from run_install_test import run_install_test
30
31=== modified file 'utah/run.py'
32--- utah/run.py 2013-02-05 17:22:28 +0000
33+++ utah/run.py 2013-02-15 08:41:19 +0000
34@@ -22,6 +22,7 @@
35 import urllib
36 import traceback
37 import shutil
38+import signal
39
40 from utah import config
41 from utah.exceptions import UTAHException
42@@ -112,6 +113,8 @@
43 """
44 Run some tests and retrieve results.
45 """
46+ install_sigterm_handler()
47+
48 if args.json:
49 report_type = 'json'
50 else:
51@@ -291,3 +294,19 @@
52 logger = logging.getLogger('paramiko')
53 logger.propagate = False
54 logger.addHandler(ssh_file_handler)
55+
56+
57+def install_sigterm_handler():
58+ """Capture SIGTERM signal to avoid abrupt process termination.
59+
60+ This function registers a handler that raises an exception when SIGTERM is
61+ received to ensure that machine object cleanup methods are called.
62+ Otherwise, cleanup methods aren't called when the process is abruptly
63+ terminated.
64+
65+ """
66+ def handler(signum, frame):
67+ """Raise exception when signal is received."""
68+ raise UTAHException('Signal received: {}'.format(signum))
69+
70+ signal.signal(signal.SIGTERM, handler)

Subscribers

People subscribed via source and target branches