From 719dcd77bb051f9c138dd7ce6dc155f9c073705d Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 17 May 2020 14:25:40 +0200 Subject: [PATCH 1/2] examples: execute bash sessions in script environment Previously, the sessions contained only explicitly exported variables and functions. This was fragile and in part buggy due to lacking exports. Interactive features like user-defined aliases and functions are still working as before. --- examples/deploy-container.sh | 2 +- examples/deploy-nixops.sh | 2 +- examples/deploy-qemu-vm.sh | 2 +- examples/start-bash-session.sh | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 examples/start-bash-session.sh diff --git a/examples/deploy-container.sh b/examples/deploy-container.sh index 5912ba5..d48dfb4 100755 --- a/examples/deploy-container.sh +++ b/examples/deploy-container.sh @@ -81,6 +81,6 @@ echo "Bitcoind data dir:" sudo ls -al /var/lib/containers/demo-node/var/lib/bitcoind # Uncomment to start a shell session here -# export -f c; bash -li +# . start-bash-session.sh # Cleanup happens at exit (see above) diff --git a/examples/deploy-nixops.sh b/examples/deploy-nixops.sh index b204aa9..d7209e8 100755 --- a/examples/deploy-nixops.sh +++ b/examples/deploy-nixops.sh @@ -39,6 +39,6 @@ nixops deploy -d bitcoin-node nixops ssh bitcoin-node systemctl status bitcoind # Uncomment to start a shell session here -# bash -li +# . start-bash-session.sh # Cleanup happens at exit (see above) diff --git a/examples/deploy-qemu-vm.sh b/examples/deploy-qemu-vm.sh index a87547c..01ad0df 100755 --- a/examples/deploy-qemu-vm.sh +++ b/examples/deploy-qemu-vm.sh @@ -91,6 +91,6 @@ echo "Node info:" c nodeinfo # Uncomment to start a shell session here -# export -f c; bash -li +# . start-bash-session.sh # Cleanup happens at exit (see above) diff --git a/examples/start-bash-session.sh b/examples/start-bash-session.sh new file mode 100644 index 0000000..6ed16bf --- /dev/null +++ b/examples/start-bash-session.sh @@ -0,0 +1,6 @@ +# Start an interactive bash session in the current bash environment. + +# BASH_ENVIRONMENT contains definitions of read-only variables like 'BASHOPTS' that +# cause warnings on importing. Suppress these warnings during bash startup. +BASH_ENVIRONMENT=<(declare -p; declare -pf) \ + bash --rcfile <(echo 'source $BASH_ENVIRONMENT 2>/dev/null') From 0f1ee5f53323317cfbd449235f2f185d64684207 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 17 May 2020 23:46:44 +0200 Subject: [PATCH 2/2] examples: improve shell session usability - Add usage prompt when starting shell sessions - Give all examples an uniform interface ("c") for running commands or starting a shell on the node. --- examples/deploy-container.sh | 8 +++++++- examples/deploy-nixops.sh | 1 + examples/start-bash-session.sh | 13 +++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/deploy-container.sh b/examples/deploy-container.sh index d48dfb4..f9fc494 100755 --- a/examples/deploy-container.sh +++ b/examples/deploy-container.sh @@ -62,7 +62,13 @@ in { } EOF # Run command in container -c() { sudo extra-container run demo-node -- "$@" | cat; } +c() { + if [[ $# > 0 ]]; then + sudo extra-container run demo-node -- "$@" | cat; + else + sudo nixos-container root-login demo-node + fi +} echo echo "Bitcoind service:" diff --git a/examples/deploy-nixops.sh b/examples/deploy-nixops.sh index d7209e8..155cc5e 100755 --- a/examples/deploy-nixops.sh +++ b/examples/deploy-nixops.sh @@ -38,6 +38,7 @@ nixops deploy -d bitcoin-node # Connect to node nixops ssh bitcoin-node systemctl status bitcoind +c() { nixops ssh bitcoin-node "$@"; } # Uncomment to start a shell session here # . start-bash-session.sh diff --git a/examples/start-bash-session.sh b/examples/start-bash-session.sh index 6ed16bf..37ba4fc 100644 --- a/examples/start-bash-session.sh +++ b/examples/start-bash-session.sh @@ -1,6 +1,15 @@ # Start an interactive bash session in the current bash environment. +USAGE_INFO=' +Starting shell... +Run "c COMMAND" to execute a command on the bitcoin node +Run "c" to start a shell session inside the node' + # BASH_ENVIRONMENT contains definitions of read-only variables like 'BASHOPTS' that -# cause warnings on importing. Suppress these warnings during bash startup. +# cause warnings on evaluation. Suppress these warnings while sourcing. BASH_ENVIRONMENT=<(declare -p; declare -pf) \ - bash --rcfile <(echo 'source $BASH_ENVIRONMENT 2>/dev/null') +USAGE_INFO="$USAGE_INFO" \ + bash --rcfile <(echo ' + source $BASH_ENVIRONMENT 2>/dev/null + echo "$USAGE_INFO" + ')