Skip to content

Instantly share code, notes, and snippets.

@benspaulding
Last active December 1, 2017 22:05
Show Gist options
  • Save benspaulding/32e68795f3458e9a549a7d961e1cceac to your computer and use it in GitHub Desktop.
Save benspaulding/32e68795f3458e9a549a7d961e1cceac to your computer and use it in GitHub Desktop.
Help scripts for moving data on and off of Nanobox environments

These are works in progress, but they function for me and my uses.

Note that I wrote one in sh and one in fish just for exercise — the sh one will work fine if you use fish. The only time you need to pick one to match your own shell is if the files will be sourced, which is not yet supported.

Ideas

  • Add a script to simply drop you into a shell on host machine with all these environment variables set. That would make it easier to run multiple commands.
  • Add ability to source the files so the individual functions could be used in other places.
#!/usr/bin/env fish
# use_nbox_vars.fish
# This accepts three arguments:
#
# 1. The name of a nanobox environment, e.g., `local` or `dry-run`
# 2. The name of a command to run, e.g., `mysql`
# 3. Arguments for the command, e.g., `--host='$DATA_MYSQL_HOST' ...`
#
# For example, to load a database dump into your dry-run environment:
#
# ./use_nbox_vars.fish dry-run mysql \
# --host='$DATA_MYSQL_HOST' \
# --user='$DATA_MYSQL_USER' \
# --password='$DATA_MYSQL_PASS' \
# --database=gonano \
# < ../mydbdump.sql
#
# Note that the arguments which use the nanobox environment variables are in
# single quotes `''`; this keeps them from expanding until the command and its
# arguments are eval'd.
#
# This can be made more useful if you create your own commands for common
# tasks, e.g. a script called `load_dbdump`:
#
# #!/usr/bin/env fish
# mysql --host='$DATA_MYSQL_HOST' --user='$DATA_MYSQL_USER' --password='$DATA_MYSQL_PASS' --database=gonano $argv
#
# Then it is easier to type and remember:
#
# ./use_nbox_vars.fish dry-run ./load_dbdump < mydbdump.sql
# Currently this only uses `DATA_*` vars; it could be modified to use
# all vars, or allow an option to be passed in to th sed pattern.
function use_nbox_vars -a env \
-d "Set local environment variables for this command from nanobox environment variables."
# Parse the output of `nanobox info <env>` and eval it to set environment variables.
eval (nanobox info $env | sed -nE "s/[[:space:]]*(DATA_[^=[:space:]]+)[=[:space:]]+(.*)/set -gx \1 '\2';/p")
end
function main -a env -a cmd \
-d "Evaluate `cmd` and its arguments with the given nanobox `env`'s variables."
set -l code "$cmd $argv[3..-1]"
use_nbox_vars $env
printf "=> %s\n" "Running with nanobox $env env vars..." "$code" (eval "echo $code")
eval $code
end
# TODO: Wrap this call to main() in an if statement checking on the script name
# to allow for sourcing this as well as calling it. That may feel more natural
# as arguments would not need to be single quoted E.g.:
#
# source use_nbox_vars.fish
# use_nbox_vars dry-run; echo $DATA_MYSQL_HOST
main $argv
#!/bin/bash
# use_nbox_vars.sh
# This accepts three arguments:
#
# 1. The name of a nanobox environment, e.g., `local` or `dry-run`
# 2. The name of a command to run, e.g., `mysql`
# 3. Arguments for the command, e.g., `--host='$DATA_MYSQL_HOST' ...`
#
# For example, to load a database dump into your dry-run environment:
#
# ./use_nbox_vars.sh dry-run mysql \
# --host='$DATA_MYSQL_HOST' \
# --user='$DATA_MYSQL_USER' \
# --password='$DATA_MYSQL_PASS' \
# --database=gonano \
# < ../mydbdump.sql
#
# Note that the arguments which use the nanobox environment variables are in
# single quotes `''`; this keeps them from expanding until the command and its
# arguments are eval'd.
#
# This can be made more useful if you create your own commands for common
# tasks, e.g. a script called `load_dbdump`:
#
# #!/bin/sh
# mysql --host='$DATA_MYSQL_HOST' --user='$DATA_MYSQL_USER' --password='$DATA_MYSQL_PASS' --database=gonano "$@"
#
# Then it is easier to type and remember:
#
# ./use_nbox_vars.sh dry-run ./load_dbdump < mydbdump.sql
# Currently this only uses `DATA_*` vars; it could be modified to use
# all vars, or allow an option to be passed in to th sed pattern.
function use_nbox_vars() {
eval $(nanobox info $1 | sed -nE "s/[[:space:]]*(DATA_[^=[:space:]]+)[=[:space:]]+(.*)/\1='\2'/p")
}
function main() {
env=$1
shift
cmd=$1
shift
code="$cmd $@"
use_nbox_vars $env
printf "=> %s\n" "Running with nanobox $env env vars..." "$code" "$(eval echo $code)"
eval $code
}
# TODO: Wrap this call to main() in an if statement checking on the script name
# to allow for sourcing this as well as calling it. That may feel more natural
# as arguments would not need to be single quoted E.g.:
#
# source use_nbox_vars.sh
# use_nbox_vars dry-run; echo $DATA_MYSQL_HOST
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment