Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: sync individual file (instead of entire folder) #3021

Closed
odigity opened this issue Feb 25, 2014 · 16 comments

Comments

@odigity
Copy link
Contributor

commented Feb 25, 2014

(I hope this isn't a dupe. I tried searching for 'sync' in issues, but the search functionality is very limited.)

I would like to see a feature to sync a single file, rather than an entire folder. A common use case for this would be syncing your local ~/.gitconfig and ~/.netrc files on the host to /home/vagrant/.gitconfig and /home/vagrant/.netrc on the guest, in order to have your GitHub and Heroku config present and identical both inside and out.

I'm currently using the following hack in my Vagrantfile to make a copy at provision time which accomplishes 90% of what I need:

gitconfig = Pathname.new("#{Dir.home}/.gitconfig")
config.vm.provision :shell, :inline => "echo -e '#{gitconfig.read()}' > '/home/vagrant/.gitconfig'", privileged: false if gitconfig.exist?

However, besides being ugly, it has the following problems:

  1. A single quote inside the file will break this implementation unless I add some kind of escaping.
  2. It doesn't keep the files in sync with changes that happen to the local copy after the provisioning command has run.

PS-In the meantime, I may use this as an opportunity to try my hand at implementing a basic Vagrant plugin that accomplishes this copying functionality in a cleaner fashion (not going to tackle syncing).

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

Vagrant actually has a "file" provisioner built-in, it just isn't documented. :) Take a look at the source for now, and I"ll try to document it now.

@mitchellh mitchellh closed this Feb 25, 2014

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

Ok, I think I've got it figured out. Do you want me to try documenting it for you?

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

@odigity That's be great.

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

Arg. Doesn't seem to work for me:

https://gist.github.com/odigity/9211935

I can still start working on the docs, since it's clear how it's meant to be used, but I'm not sure how to resolve the actual problem.

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

I finished the docs, even though the feature still doesn't work for me. :)

#3022

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

@odigity looking into your bug

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

@odigity You can't use '~' for SCP, use the full path and it'll work

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

Although, not sure why it didn't expand it. Looking into it.

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 25, 2014

@odigity I tried your exact provisoin line and it worked for me!

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

Weird.

I know Vagrant has it's own ruby/gems installation embedded within it, so it can't be an Net::SCP incompatibility. And I'm using the default Vagrant precise64.box.

For what it's worth, I'm currently using Vagrant 1.4.3, and my host is Ubuntu 13.10.

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

Pre-resolving the remote path seems to be enough to solve the problem. This works:

config.vm.provision :file, source: '~/.gitconfig', destination: '/home/vagrant/.gitconfig'

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

I suppose if other people experience the same problem as me, you'll eventually hear about it once the new docs get pushed, at which point either someone will have to fix it, or someone will have to amend the docs to suggest avoiding using '~' in the destination string.

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 25, 2014

One last thing:

It'd be nice to add a third option specifying that it's ok if the file doesn't exist. In the meantime, I'm going to work around it in the Vagrantfile:

config.vm.provision :file, source: '/.gitconfig', destination: '/home/vagrant/.gitconfig' if File.exist?('/.gitconfig')

If you like the idea, and know what form you want the option to take, I can try to implement it and send another PR.

@odigity

This comment has been minimized.

Copy link
Contributor Author

commented Feb 26, 2014

Sorry to keep spamming the thread, but I need to make one more correction. This doesn't work:

if File.exist?('~/.gitconfig')

Because exist? (and most other File methods) doesn't do tilde expansion. Therefore, I intend to change it to if File.exist?(ENV['HOME'] + '/.gitconfig'), as per the suggestion here:

http://stackoverflow.com/questions/2504646/ruby-opening-files-relative-to-home-directory

@tmatilai

This comment has been minimized.

Copy link
Collaborator

commented Feb 28, 2014

The expanding is fixed in master by #2922.

@mitchellh

This comment has been minimized.

Copy link
Member

commented Feb 28, 2014

@tmatilai Ah, that's it. I always run master so no wonder I didn't notice. :)

trickv added a commit to trickv/vyos-build-ami that referenced this issue Oct 9, 2014
Use vagrant file provisioner for dotfiles
This is amazing - many thanks to contributors on this thread:
hashicorp/vagrant#3021

Copies in .gitconfig and .vimrc from the host, and only if they
exist.

Also chdir to /vagrant and enable apt-get update on start.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.