Last active
June 18, 2018 09:19
-
-
Save cmbankester/0ca3d103a1f5e49b8d37 to your computer and use it in GitHub Desktop.
Sync a directory into a CoreOS node using the unison container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# add `export COREOS_DOCKER_HOST_IP=some-ip-address` to your .bashrc or .zshrc | |
function echo_usage_and_exit() | |
{ | |
echo "Usage: coreos-sync local_path_to_sync remote_dir_to_sync_into [options]" | |
echo "Options:" | |
echo "\t-w\t\t(watches/resyncs on changes)" | |
echo "\t-v\t\t(enable verbose mode)" | |
echo "\t-h hostname\t(ip or hostname of CoreOS host)" | |
echo "\t-p port\t\t(port of CoreOS host; default: 5000)" | |
echo "\t-- arg [arg...]\t(args to pass to unison)" | |
exit | |
} | |
function echo_set_coreos_docker_host_ip_and_exit() | |
{ | |
echo "Error: COREOS_DOCKER_HOST_IP not set" | |
echo "Either set COREOS_DOCKER_HOST_IP or use -h hostname" | |
exit | |
} | |
function get_opts() | |
{ | |
if [ -n "$1" ]; then | |
case "$1" in | |
"-w") | |
watch="true" | |
;; | |
"-v") | |
verbose="true" | |
;; | |
"-h") | |
if [ -n "$2" ]; then | |
hostname=$2 | |
shift | |
else | |
echo_usage_and_exit | |
fi | |
;; | |
"-p") | |
if [ -n "$2" ]; then | |
port=$2 | |
shift | |
else | |
echo_usage_and_exit | |
fi | |
;; | |
"--") | |
shift # shift away the '--' arg | |
unisonargs=$@ # assign all the rest of the args to unisonargs | |
shift $# # shift all the args that are left | |
;; | |
*) | |
echo_usage_and_exit | |
;; | |
esac | |
[ -n "$unisonargs" ] || shift # shift the arg, unless $unisonargs is set | |
get_opts $@ | |
fi | |
} | |
[ -n "$1" ] && localdir=$1 && [ -n "$2" ] && remotedir=$2 && shift && shift || echo_usage_and_exit | |
[ -n "$COREOS_DOCKER_HOST_IP" ] && hostname=$COREOS_DOCKER_HOST_IP | |
port=5000 | |
get_opts $@ | |
[ -n "$unisonargs" ] || unisonargs="" | |
[ -n "$hostname" ] || echo_set_coreos_docker_host_ip_and_exit | |
[ -n "$verbose" ] && terseness="false" || terseness="true" | |
prog="unison $localdir socket://$hostname:$port/$remotedir -auto -batch -terse=$terseness -times -nodeletion=$localdir $unisonargs" | |
$prog && [ -n "$watch" ] && fswatch -o $localdir | xargs -n1 -I{} $prog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment