Skip to content

Commit a271e14

Browse files
committed
NF: SINGULARITY_CMD=shell to record (bash) history+result of interactive sessions
**Related** This is a prototype for functionality which might be of interest outside of this project, e.g. related: - regular `datalad run` to record activities in the shell. - [`run --interactive`](datalad/datalad#2158 (comment)) - [`run --shell`](datalad/datalad#2275) so here I am "implementing" it, solely for containerized environments ATM, via a "over the head" communication to the shim in environment variable - `datalad run` for better record keeping, e.g. - [saving stdout/err](datalad/datalad#3385) so here I was not bothering to establish stdout/err capture but possibly could and might - `reproman login`, or even `execute` (with or without --trace) and may be `run` where we could benefit from having an environment with a unified interface for interactive sessions which would also establish the record of activities - just a regular shell environment to make a clear record of commands which were ran - might eventually absorb/meld with the "opinionated .bashrc" proposed for the training curiculum: ReproNim/module-reproducible-basics#26 which provides assistance/docs for more efficient use of cmdline and establishes 'infinite bash history'. **reproshell???** So it feels to me like a motivation for some kind of a reproshell independent project which would be - usable indepdendently and easily installable/bindable (e.g. into a container) - parametrizeable to be invoked from the shim here and/or by datalad or reproman so could just take care about capturing all sidecar files into specified locations **Could benefit from** - knowing more about "datalad (containers-)run" invocation Implemented now within `singularity_run` shim, which could have benefited from having additional information about how exactly it was `ran` and also to instruct datalad run "upstairs" that there is now an additional file in [extra_outputs](datalad/datalad#3094). Hence there is datalad/datalad#3422 - [`datalad run` being able to 'cover' multiple commits](datalad/datalad#3265) Interactivity creates ambiguity for `rerun` semantic: - run record ATM would say "reinvoke interactive session" which might be desireable on its own (e.g. to redo something manually in that original container) - but for "automated reproducibility" we do have all information (bash history file, which is a list of commands to run) possibly recorded in another commit, which is ATM is not associated with the "run" record So may be with somehow [tagging run commits](datalad/datalad#3371) it could be possible to disambiguate/select specific run commits/records? <details> <summary>**Example**</summary> (dev) 1 13348.....................................:Wed 15 May 2019 06:12:24 PM EDT:. (git-annex)hopa:~/proj/repronim/containers[enh-shell]git-annex $> SINGULARITY_CMD=shell datalad containers-run -n repronim-reproin [INFO ] Making sure inputs are available (this may take some time) [INFO ] == Command start (output follows) ===== <ome/yoh/proj/repronim/containers$ echo "I will do something useful today" I will do something useful today singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers$ touch my-results singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers$ cd images/ singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers/images$ ls bids README.md repronim singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers/images$ cd ../ singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers$ ls binds images LICENSE my-results README.md scripts <pa:/home/yoh/proj/repronim/containers$ rm LICENSE ; echo 'nobody needs those' nobody needs those singularity:repronim-reproin > yoh@hopa:/home/yoh/proj/repronim/containers$ exit add(ok): .repronim/bash_histories/0.1-3-ge25c927-2019-05-15T18:12:37-04:00 (file) save(ok): . (dataset) action summary: add (ok: 1) save (ok: 1) [INFO ] == Command exit (modification check follows) ===== delete(ok): LICENSE (file) add(ok): my-results (file) save(ok): . (dataset) action summary: add (ok: 1) delete (ok: 1) get (notneeded: 1) save (ok: 1) SINGULARITY_CMD=shell datalad containers-run -n repronim-reproin 3.42s user 1.74s system 9% cpu 54.068 total $> git log --stat HEAD^^.. commit 89fed08617418e5ddb88ae11ee2c14db699acf31 (HEAD -> enh-shell) Author: Yaroslav Halchenko <[email protected]> Date: Wed May 15 18:13:28 2019 -0400 [DATALAD RUNCMD] ./scripts/singularity_cmd run images/rep... === Do not change lines below === { "chain": [], "cmd": "./scripts/singularity_cmd run images/repronim/repronim-reproin--0.5.4.sing ", "dsid": "b02e63c2-62c1-11e9-82b0-52540040489c", "exit": 0, "extra_inputs": [], "inputs": [ "images/repronim/repronim-reproin--0.5.4.sing" ], "outputs": [], "pwd": "." } ^^^ Do not change lines above ^^^ LICENSE | 201 --------------------------------------------------------------------------------------------- my-results | 1 + 2 files changed, 1 insertion(+), 201 deletions(-) commit 5aa3b3383c2746f7c1d07ecdcc73852eb0a30f17 Author: Yaroslav Halchenko <[email protected]> Date: Wed May 15 18:13:28 2019 -0400 [REPRONIM/CONTAINERS]: bash history for the interactive session Actual changes might (or not, depending on the invocation) get committed in the next commit .repronim/bash_histories/0.1-3-ge25c927-2019-05-15T18:12:37-04:00 | 7 +++++++ 1 file changed, 7 insertions(+) $> cat .repronim/bash_histories/0.1-3-ge25c927-2019-05-15T18:12:37-04:00 echo "I will do something useful today" touch my-results cd images/ ls cd ../ ls rm LICENSE ; echo 'nobody needs those' </details> **Additional possible features which might come here into a prototype** - color info/error messages from the shim - improve PS1 (probably multiline -- too much in a single line to still be able edit commands) - indicate being [reproman --trace](ReproNim/reproman#416 - provide 'reactive' PS1 to alert user when he/she leaves the initial directory (thus the one outside of original dataset), possibly resulting in outputs which would not be recorded
1 parent 2b6f83c commit a271e14

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

scripts/singularity_cmd

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,113 @@ function info() {
3737
: # echo -e "I: $@" >&2
3838
}
3939

40+
function error() {
41+
echo -e "E: $@" >&2
42+
exit 1
43+
}
44+
45+
function has_changes() {
46+
git status -s | grep -q .
47+
}
48+
49+
function singularity_version() {
50+
singularity --version | sed -e 's,^[^0-9]*,,g'
51+
}
52+
53+
# https://stackoverflow.com/a/24067243
54+
function version_gt() {
55+
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
56+
}
57+
4058
thisdir=$(dirname $0| xargs readlink -f)
4159
updir=$(dirname "$thisdir")
4260

4361
cmd="${SINGULARITY_CMD:-$1}"; shift
4462

63+
# We might need to expand list of arguments
64+
args=("$@")
65+
66+
#
67+
# Pass other useful variables inside the container
68+
#
4569
if [ ! -z "${DATALAD_CONTAINER_NAME:-}" ]; then
4670
export SINGULARITYENV_DATALAD_CONTAINER_NAME="$DATALAD_CONTAINER_NAME"
4771
fi
4872

73+
#
74+
# Prepare bind mounts
75+
#
76+
4977
# singularity bind mounts system /tmp, which might result in side-effects
5078
# Create a dedicated temporary directory to be removed upon completion
5179
tmpdir=$(mktemp -d --suffix=singtmp)
5280
info "created temp dir $tmpdir"
5381
trap "rm -fr '$tmpdir' && info 'removed temp dir $tmpdir'" exit
5482

55-
singularity "$cmd" -e -c -W "$tmpdir" -H "$updir/binds/HOME" -B $PWD --pwd "$PWD" "$@"
83+
#
84+
# Prepare for storing bash history in cmd='shell' mode
85+
#
86+
# Will be non-empty if some post-run handling is needed
87+
FINAL_BASH_HISTORY=
88+
TEMP_BASH_HISTORY_LOCAL=
89+
if [ "$cmd" = "shell" ]; then
90+
# should be outside of $tmpdir so we could copy it there before
91+
# trap cleans things up
92+
histstamp=$(git describe --always)-$(date -Iseconds)
93+
TEMP_BASH_HISTORY_LOCAL=$(mktemp -t bash_history.$histstamp.XXXXXXXXX)
94+
TEMP_BASH_HISTORY_FILENAME=$(basename $TEMP_BASH_HISTORY_LOCAL)
95+
TEMP_BASH_HISTORY="$tmpdir/tmp/$TEMP_BASH_HISTORY_FILENAME"
96+
# singularity 2.x seems to mess with HISTFILE - cannot pass through!
97+
if version_gt 3 "$(singularity_version)"; then
98+
error "Can manipulate bash history only with singularity >= 3"
99+
fi
100+
# Expose it to singularity environment
101+
export SINGULARITYENV_HISTFILE="/tmp/$TEMP_BASH_HISTORY_FILENAME"
102+
# We will copy it only if it was clean and new changes emerged
103+
# Handle (save) protocol of interactive sessions
104+
if ! has_changes ; then
105+
# TODO: place at the top of the dataset!?
106+
FINAL_BASH_HISTORY=".repronim/bash_histories/$histstamp"
107+
# TODO: cleanup TEMP_BASH_HISTORY in case of crash?
108+
else
109+
echo "W: uncomitted changes present, 'shell' mode will NOT commit bash history."
110+
echo " You will find stored history at $TEMP_BASH_HISTORY_LOCAL"
111+
fi
112+
if [ "$#" -gt 1 ]; then
113+
error "for 'shell' mode - do not provide any custom command. Got options: $@"
114+
fi
115+
cmd="exec"
116+
args+=(bash)
117+
fi
118+
119+
#
120+
# The actual invocation
121+
#
122+
singularity "$cmd" -e -c -W "$tmpdir" -H "$updir/binds/HOME" -B "$PWD" --pwd "$PWD" "${args[@]}"
123+
124+
125+
#
126+
# Handle possible digital objects to save/be added to be saved
127+
#
128+
if [ ! -z "$FINAL_BASH_HISTORY" ]; then
129+
if ! has_changes ; then
130+
# TODO: someone might want to just record his wonderings around, so
131+
# might be worth an option to force saving history only
132+
echo "I: no changes to the tree detected. Bash history will not be saved."
133+
echo " You will find stored history at $TEMP_BASH_HISTORY_LOCAL"
134+
else
135+
mkdir -p "$(dirname $FINAL_BASH_HISTORY)"
136+
mv "$TEMP_BASH_HISTORY" "$FINAL_BASH_HISTORY"
137+
# due to https://github.com/datalad/datalad/issues/3421 saving entire directory of histories
138+
datalad save \
139+
-m "[REPRONIM/CONTAINERS]: bash history for the interactive session
140+
141+
Actual changes might (or not, depending on the invocation) get committed in the next commit" \
142+
"$(dirname $FINAL_BASH_HISTORY)"
143+
fi
144+
fi
145+
146+
if [ ! -z "$TEMP_BASH_HISTORY_LOCAL" ] && [ -e "$TEMP_BASH_HISTORY" ]; then
147+
# So we did create it but did not move to be saved, so let's expose locally before it is wiped out
148+
mv "$TEMP_BASH_HISTORY" "$TEMP_BASH_HISTORY_LOCAL"
149+
fi

0 commit comments

Comments
 (0)