Repository navigation
Harden shell scripts with improved robustness & verbosity - #233
Conversation
These environment variables were added to Travis in late 2018 travis-ci/travis-ci#8935 (comment)
|
This is the template I use for shell scripts: #!/usr/bin/env bash
## script.sh: <short description here>
## Copyright 2019 Michael M. Hoffman <michael.hoffman@utoronto.ca>
set -o nounset -o pipefail -o errexit
if [[ $# != 0 || "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
echo usage: "$0"
exit 2
fiRationale/discussion:
Additional comments on shell style that jump out at me:
You may also find the Hoffman Lab application command-line user-interface checklist helpful. |
|
Thanks @michaelmhoffman for the excellent feedback and shell wisdom. I'll work on integrating your points. In the longterm, it is not clear that we will continue managing the build with a shell script, rather than say migrating the pipeline into Python. So given that consideration, I will probably avoid large changes like adding command-line arguments to the shell scripts... until at least we have a better idea of the ideal future architecture. However, many other suggestions are immediately actionable and I'm excited to implement them! |
|
|
||
| # Create PDF output (unless BUILD_PDF environment variable equals "false") | ||
| if [ "${BUILD_PDF:-true}" != "false" ] && [ -z "$DOCKER_EXISTS" ]; then | ||
| if [ "$(BUILD_PDF:-true)" != "false" ] && [ -z "$DOCKER_EXISTS" ]; then |
There was a problem hiding this comment.
This should be parameter expansion (${BUILD_PDF:-true}) not command substitution ($(command -v docker)). Same below for some variables.
There was a problem hiding this comment.
Ah that distinction was lost on me, and the root cause of all this back and forth incorrect usage!
Properly use command substitution and parameter expansion in the appropriate places. They are not the same thing. manubot#233 (comment)
Previously echo defaulted to stdout https://stackoverflow.com/a/41766832/4651668
agitter
left a comment
There was a problem hiding this comment.
I'm leaving a few comments on the changes that have been made so far. I'm not much of a shell scripting expert so I defer to @michaelmhoffman's suggestions above. There are still a few others we could implement in this pull request or a subsequent one.
|
|
||
| set -o errexit \ | ||
| -o nounset \ | ||
| -o xtrace |
There was a problem hiding this comment.
The suggestion to not set xtrace here and instead allow it optionally with bash -x script.sh makes sense to me.
There was a problem hiding this comment.
Changed in da0b397 along with adding -o pipefail. From these GNU docs
The exit status of a pipeline is the exit status of the last command in the pipeline, unless the
pipefailoption is enabled (see The Set Builtin). Ifpipefailis enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word '!' precedes the pipeline, the exit status is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.
So I am not sure why pipefail does not interfere with the error suppression in:
DOCKER_EXISTS="$(command -v docker || true)"
There was a problem hiding this comment.
command -v docker || true is a list of commands, not a pipeline.
| # Set options for extra caution & debugging | ||
| set -o errexit \ | ||
| -o nounset \ | ||
| -o xtrace |
|
|
||
| # Generate reference information | ||
| echo "Retrieving and processing reference metadata" | ||
| >&2 echo "Retrieving and processing reference metadata" |
There was a problem hiding this comment.
I'm not accustomed to seeing >&2 at the beginning of the line, but it looks like there is not single convention for this.
There was a problem hiding this comment.
I agree this seems rather odd to me but I did not comment on it earlier. More important is a consistent use of whatever convention you choose. My own preference in descending order would be:
echo >&2 "Retrieving and processing reference metadata"
echo "Retrieving and processing reference metadata" >&2
>&2 echo "Retrieving and processing reference metadata"
Normally I do redirections at the end of a command but with echo or printf it can get lost at the end.
| # Create PDF output (unless BUILD_PDF environment variable equals "false") | ||
| if [ "$BUILD_PDF" != "false" ] && [ -z "$DOCKER_EXISTS" ]; then | ||
| echo "Exporting PDF manuscript using WeasyPrint" | ||
| if [ "${BUILD_PDF:-true}" != "false" ] && [ -z "$DOCKER_EXISTS" ]; then |
There was a problem hiding this comment.
Change to use michaelmhoffman's tip 11? #233 (comment)
There was a problem hiding this comment.
You don't need
["${BUILD_PDF:-true}" != "false"], just["${BUILD_PDF:-}" != "false"]. Better yet, the usual convention is to test[ "${BUILD_PDF:-}" ]which will let a user setBUILD_PDFto anything to mean true and empty or unset to mean false. Using a command-line argument is a more expected interface in most cases though.
I don't think [ "${BUILD_PDF:-}" ] is what we wan't here because it defaults to not building the PDF if BUILD_PDF is unset. However, we want PDF building unless explicitly disabled with BUILD_PDF=false. I also think we want a solution where BUILD_PDF=false and BUILD_PDF=true perform as written, such that they can be easily toggled in CI configs. We could do ["${BUILD_PDF:-}" != "false"], which saves a few characters but is a bit less readable for the non-expert.
There was a problem hiding this comment.
You could call it SUPPRESS_PDF or better yet MANUBOT_SUPPRESS_PDF (or something semantically equivalent).
Another alternative is simply to set the variable upstream with something like:
BUILD_PDF="${BUILD_PDF:-true}"
Then you can just use "${BUILD_PDF}" from then on forward.
|
Ready for review. I addressed several of @michaelmhoffman's suggestions, but not all of them. Some I'd like to address at a future time, but don't want to hold up this PR. Would be good for reviewers to take one more pass to make sure I haven't done anything incorrect. |
agitter
left a comment
There was a problem hiding this comment.
I re-reviewed and did some light testing in my environment that does not have Docker. The build script worked as expected.
Two other recommendations that were not addressed in this pull request are:
You may want some of these variables to be set only in cases where the user hasn't set and exported them. You can do that with a formulation like TZ="${TZ:-Etc/UTC}". I find overriding user settings of TZ and LC_ALL rather aggressive. If the latter is only to change Python settings you probably want to use PYTHONIOENCODING instead or even better fix it in the Python script itself.
I would advise adding shellcheck to your editor and your CI workflow which would catch some of the errors above.
I recommend we merge and create new issues for these suggestions. The shellcheck comment raises a larger issue. The CI in this repository is being used to generate manuscripts. We don't have separate CI to test the build process and may want to consider that. We would not want those tests to propagate to downstream manuscripts though.
For the record, I satisfied all shellcheck grievances besides quoting: Lines 19 to 20 in 2fd84f1 Did not modify those lines to avoid conflicts since manuscript instances change them.
I started modifying this, but stopped because I remembered we had problems in the past and would want to make sure we're not introducing a bug regression. Also it's not entirely clear to me that we shouldn't always use UTC timezone for manuscripts. Anyways, just want to spend some more time learning how these variables and configurations work before blindly changing. |
This build is based on ddb0288. This commit was created by the following Travis CI build and job: https://travis-ci.com/manubot/rootstock/builds/117215168 https://travis-ci.com/manubot/rootstock/jobs/211660565 [ci skip] The full commit message that triggered this build is copied below: Harden shell scripts with improved robustness & verbosity (#233) Merges #233 Hat tip to Michael Hoffman (https://hoffmanlab.org/) for feedback. * Apply shell best practices References: https://kvz.io/blog/2013/11/21/bash-best-practices/ https://www.davidpashley.com/articles/writing-robust-shell-scripts/ #233 (comment) * Specify bash (not just sh). Other shellcheck suggestions Properly use command substitution and parameter expansion in the appropriate places. * echo diagnostic messages to stderr Previously echo defaulted to stdout https://stackoverflow.com/a/41766832/4651668 Placement of stderr redirect to: "echo >&2" * Update bash options: xtrace specified by calling command * Quote variable names * deploy.sh: use Travis env vars for CI build URLs These environment variables were added to Travis in late 2018 travis-ci/travis-ci#8935 (comment)
This build is based on ddb0288. This commit was created by the following Travis CI build and job: https://travis-ci.com/manubot/rootstock/builds/117215168 https://travis-ci.com/manubot/rootstock/jobs/211660565 [ci skip] The full commit message that triggered this build is copied below: Harden shell scripts with improved robustness & verbosity (#233) Merges #233 Hat tip to Michael Hoffman (https://hoffmanlab.org/) for feedback. * Apply shell best practices References: https://kvz.io/blog/2013/11/21/bash-best-practices/ https://www.davidpashley.com/articles/writing-robust-shell-scripts/ #233 (comment) * Specify bash (not just sh). Other shellcheck suggestions Properly use command substitution and parameter expansion in the appropriate places. * echo diagnostic messages to stderr Previously echo defaulted to stdout https://stackoverflow.com/a/41766832/4651668 Placement of stderr redirect to: "echo >&2" * Update bash options: xtrace specified by calling command * Quote variable names * deploy.sh: use Travis env vars for CI build URLs These environment variables were added to Travis in late 2018 travis-ci/travis-ci#8935 (comment)
|
That reasoning make sense to me. Your comment about regression testing is what I was hinting at. Because the The |
I can think of two possibilities:
No immediate easy solutions come to mind. |
Merges manubot/rootstock#233 Hat tip to Michael Hoffman (https://hoffmanlab.org/) for feedback. * Apply shell best practices References: https://kvz.io/blog/2013/11/21/bash-best-practices/ https://www.davidpashley.com/articles/writing-robust-shell-scripts/ manubot/rootstock#233 (comment) * Specify bash (not just sh). Other shellcheck suggestions Properly use command substitution and parameter expansion in the appropriate places. * echo diagnostic messages to stderr Previously echo defaulted to stdout https://stackoverflow.com/a/41766832/4651668 Placement of stderr redirect to: "echo >&2" * Update bash options: xtrace specified by calling command * Quote variable names * deploy.sh: use Travis env vars for CI build URLs These environment variables were added to Travis in late 2018 travis-ci/travis-ci#8935 (comment)
Writing good shell scripts that provide useful debugging information seems to be challenging. This PR attempts to adopt best practices as discussed here.
@dongbohu @michaelmhoffman @agitter feel free to suggest any other ways our shell scripts could be improved.