Skip to content

Instantly share code, notes, and snippets.

@sbailliez
Created February 8, 2014 04:20
Show Gist options
  • Save sbailliez/8876660 to your computer and use it in GitHub Desktop.
Save sbailliez/8876660 to your computer and use it in GitHub Desktop.
Provision vagrant with ssh keys, pem file, git config, aws config, etc...
#
# Allows copying ssh keys, directly to vagrant.. quick hack for my needs
#
# It is basically reading the file, encoding it in base64, pass it in the shell, decode it and write it
# in the file with the same name in /home/vagrant, chown it and set 600 permissions
def provision_home(config, from, to = nil, user = 'vagrant', permission = "600")
if File.exists?(File.join(Dir.home, from))
require "base64"
to = to.nil? ? "/home/vagrant/#{from}" : to
from = "#{Dir.home}/#{from}"
enc = Base64.encode64(File.read("#{from}")).delete!("\n")
cmd = "mkdir -p `dirname #{from}` && openssl base64 -d -A <<< '#{enc}' > #{to} && chown #{user}:#{user} #{to} && chmod #{permission} #{to}"
config.vm.provision :shell, :inline => cmd
end
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# ...
['.gitconfig', '.ssh/id_rsa', '.ssh/id_dsa', '.ssh/somekey.pem', '.aws/config'].each { |file|
provision_home(config, file)
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment