New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow the script handler to wait for the script #1307
Changes from 1 commit
13c2c35
8f306eb
040d0db
bea2309
5fc5322
83f6eaa
e2b1d60
7bd3d27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,9 @@ | |
|
||
log = logging.getLogger(__name__) | ||
|
||
SCRIPT_BACKGROUND = "Run in background" | ||
SCRIPT_WAIT = "Wait for script to finish" | ||
|
||
|
||
class ScriptEventHandler(BaseEventHandler): | ||
""" | ||
|
@@ -80,6 +83,19 @@ def actions(cls): | |
actions = {} | ||
for script in scripts: | ||
actions[script] = { | ||
"background": { | ||
"type": "str", | ||
"required": True, | ||
"description": _("Wait for script to complete or run script in background. This will " | ||
"either return the HTTP request early or could also block the request."), | ||
"value": [SCRIPT_BACKGROUND, SCRIPT_WAIT] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}, | ||
"raise_error": { | ||
"type": "bool", | ||
"visibleIf": "background", | ||
"visibleValue": SCRIPT_WAIT, | ||
"description": _("On script error raise exception in HTTP request.") | ||
}, | ||
"serial": { | ||
"type": "bool", | ||
"description": _("Add '--serial <serial number>' as script " | ||
|
@@ -164,14 +180,26 @@ def do(self, action, options=None): | |
proc_args.append("--logged_in_role") | ||
proc_args.append(logged_in_user.get("role", "none")) | ||
|
||
try: | ||
p = subprocess.Popen(proc_args, cwd=self.script_directory) | ||
log.info("Started script {script!r}:" | ||
" {process!r}".format(script=script_name, process=p)) | ||
except Exception as e: | ||
log.warning("Failed to execute script {0!r}: {1!r}".format( | ||
script_name, e)) | ||
log.warning(traceback.format_exc()) | ||
if handler_options.get("background", SCRIPT_BACKGROUND) == SCRIPT_BACKGROUND: | ||
try: | ||
p = subprocess.Popen(proc_args, cwd=self.script_directory) | ||
log.info("Started script {script!r} in background:" | ||
" {process!r}".format(script=script_name, process=p)) | ||
except Exception as e: | ||
log.warning("Failed to execute script {0!r}: {1!r}".format( | ||
script_name, e)) | ||
log.warning(traceback.format_exc()) | ||
|
||
elif handler_options.get("background") == SCRIPT_WAIT: | ||
try: | ||
log.info("Starting script {script!r} in foreground.".format(script=script_name)) | ||
r = subprocess.check_call(proc_args, cwd=self.script_directory) | ||
except Exception as e: | ||
log.warning("Failed to execute script {0!r}: {1!r}".format( | ||
script_name, e)) | ||
log.warning(traceback.format_exc()) | ||
if handler_options.get("raise_error"): | ||
raise e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is mergable, but just wanted to point out potential for code deduplication here: We could get rid of the outer if-branch and instead use try:
p = subprocess.Popen(proc_args, cwd=self.script_directory)
log.info("Started script {script!r}:"
" {process!r}".format(script=script_name, process=p))
if handler_options.get("background") == SCRIPT_WAIT:
log.info("waiting for script to finish ...")
ret = p.wait()
# handle error case if ret != 0
...
except Exception as e:
log.warning("Failed to execute script {0!r}: {1!r}".format(
script_name, e))
log.warning(traceback.format_exc()) What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not aware of p.wait(). The question is, if it also raises an error accordingly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, we would need to re-raise an error. We could, however, just raise a privacyIDEA There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is, that different situations would be handled differently. The next problem is, that for some reason the tests do not work anymore - since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add an else:
log.warning("Invalid value: {!r}".format(handler_options.get("background"))) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but I will add an else branch, if raise_error is not set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return ret | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
exit 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about changing the values of
SCRIPT_BACKGROUND
andSCRIPT_WAIT
to "background" and "wait", respectively? We cannot localize the phrases "Run in background" and "Wait for script to finish" anyway, which would look weird in German.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 5fc5322