Created
October 7, 2013 00:28
-
-
Save uu59/6860888 to your computer and use it in GitHub Desktop.
This file contains 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/bash | |
set -ue | |
usage() { | |
cat <<EOT >&2 | |
vagrant-watch.sh [vagrantfile path] [host dir] [guest dir] | |
Example: | |
host$ vagrant-watch.sh ./Vagrantfile /tmp/foo /home/vagrant | |
rsync host:/tmp/foo -> guest:/home/vagrant | |
guest$ ls /home/vagrant | |
foo | |
guest$ ls /home/vagrant/foo | |
.. foo contents .. | |
EOT | |
} | |
# should be set these | |
vagrantfile="$1" | |
hostdir="$2" | |
guestdir="$3" | |
hostname=`date +%s%N` # なんでもいい | |
ssh_config_file=`mktemp` | |
trap "rm -f $ssh_config_file" EXIT | |
hostdir=$(readlink -e -v $hostdir) | |
vagrantfile=$(readlink -e -v $vagrantfile) | |
rsync_command=$(cat <<CMD | |
rsync -aq --delete -e "ssh -F $ssh_config_file" $hostdir $hostname:$guestdir | |
CMD | |
) | |
js=$(cat <<JS | |
var fs = require('fs'); | |
var exec = require('child_process').exec; | |
fs.watch('$hostdir', function (event, filename) { | |
exec('$rsync_command', function(error, stdout, stderr){ | |
if (error === null) { | |
console.log(new Date(), "modified"); | |
}else{ | |
console.log('exec error: ' + error); | |
} | |
}); | |
}); | |
JS | |
) | |
echo Initializing... | |
echo Watching files $hostdir to vm:$guestdir | |
( | |
cd $(dirname $vagrantfile) | |
vagrant ssh-config --host "$hostname" > $ssh_config_file | |
cat <<SSH >> $ssh_config_file | |
Ciphers arcfour,arcfour256 | |
ControlMaster auto | |
ControlPath ~/.ssh/vagrant-rsync-%r@%h:%p | |
ControlPersist 10 | |
SSH | |
echo Ready. | |
node -e "$js" | |
) | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment