Skip to content
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

Switch to using a simpler-but-still-hacky way of invoking stata #6

Merged
merged 4 commits into from
May 10, 2021

Conversation

bloodearnest
Copy link
Member

@bloodearnest bloodearnest commented May 7, 2021

We create a wrapper script around the supplied script that writes a file
if it successfully runs the script. We use the presence of this file as
indication of success or failure, and exit appropriately.

We can then use the regular stata (without -b), which gives us a much simpler streaming stdout.

We also refactor the Dockerfile to our new approach, and add a very basic test suite

@bloodearnest bloodearnest marked this pull request as ready for review May 10, 2021 15:35
We create a wrapper script around the supplied script that writes a file
if it successfully runs the script. We use the contents of this file as
indication of success or failure, and exit approriately.

We can then use the regular stata (without -b), which gives stdout much
more simply.

Also, add an experimental features to auto import ado files in the
./libraries directory of a study.
 - use buildkit with tags
 - add packages.txt with base image tooling
 - remove python3 dep
 - have the renew-license script install expect
Copy link
Contributor

@evansd evansd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is both so great and so terrible that it needs to exist. Only real question for me is over the wrapper= thing.

entrypoint.sh Outdated
test -z "${STATA_LICENSE:-}" && { echo "No STATA_LICENSE environment variable found"; exit 1; }
echo "$STATA_LICENSE" > /usr/local/stata/stata.lic

script=$1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would shellcheck tell us to use script="$1" here as, theoretically, the argument could contain spaces?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So shellcheck doesn't complain, as it knows $1 must be space-less, for it to be parsed as $1 on the cli! But it does no harm, so will add

Worth adding shellcheck to the lint command though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Args are definitely not guaranteed to be space-less, unless I'm misunderstanding you

dave@carnap:~/tmp$ cat test.sh 
#!/bin/bash
echo "\$1 is $1"

dave@carnap:~/tmp$ ./test.sh 'hello there'
$1 is hello there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fair point. I've already fixed it anyway, though.

Copy link
Member Author

@bloodearnest bloodearnest May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my point was about what actions would have as their first argument is designed to be interpreted as a path to pass to stata, but there's no harm in being explicit about it.

Copy link
Member Author

@bloodearnest bloodearnest May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also foo=$1 as assignment is fine in bash, it's if you use it in other commands its an issue (which in hindsight is I think the real reason shellcheck didn't complain).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also foo=$1 as assignment is fine in bash

Ah, so it is! I've always been so paranoid about quoting everything I never realised that

entrypoint.sh Outdated
# actual script

tmp=$(mktemp)
wrapper=wrapper.$script
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this transformation will work in general. $script will usually contain slashes so analysis/my_script.do becomes wrapper.analysis/my_script.do which will give you an error when you try to write to it. Maybe just use mktemp again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg, good point.

So, I didn't want to use temp, as that made for ugly logs for users to see (I want to link the wrapper name to their name as much as possible). It's easy enough to fix though, with ${script%.do}.wrapper.do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bash string transformations will never cease to amaze me!

I agree it makes sense to try to keep the name sensible-looking, I just wonder if there's any way to do that while keeping it out of /workspace. The trap .. rm solution is neat but there's still something a bit yucky about writing temporary stuff as root in the working directory. What about something like replacing the workspace path segment with wrapped so you'd get e.g. /wrapped/analysis/script.do? Not saying that's a great solution either but feels worth thinking through the solution space a bit further.

Copy link
Member Author

@bloodearnest bloodearnest May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to be explicit about the fact there is a wrapper, but obvious what its wrapping.

Which for me means the basename not being the same as the scriptname (e.g. I expect most people would not notice /wrapped/ above and they'd just read /workspace/ there, which potential confusion),,

e.g. if their action is stata analysis/model.do, the log files will talk about analysis/model.wrapper.do, i.e same path prefix.

Also, I'm wary of putting it in a different directory, in case there are some odd CWD semantics in stata.

We could do analysis/wrapper-model.do or something similar, but stata seems to require a .do extension.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm wary of putting it in a different directory, in case there are some odd CWD semantics in stata.

That is a fair worry. Fine to leave as is I think. Just felt worth checking there wasn't a good alternative.

entrypoint.sh Outdated
Comment on lines 12 to 13
# stata is super odd in its cli interactions and behaviour. So we wrap up the
# actual script
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon future us would be grateful for more detail here. Specifically the fact it always exits 0, that it doesn't stop on error if you pipe commands in, and that it doesn't log to stdout if you use batch mode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, already had a change pending for this, and a README update

@@ -0,0 +1,55 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests! 🎉

Also, clean up some files left over from test runs, and add shellcheck
linting
@bloodearnest bloodearnest merged commit 95d6e97 into main May 10, 2021
@bloodearnest bloodearnest deleted the taming-stata-cli branch May 10, 2021 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants