3 CP_SSH_DIR
=<<-end_cp_ssh_dir
4 mkdir -p %{user_home}/.tmpssh
5 touch %{user_home}/.tmpssh/authorized_keys;
6 cat %{user_home}/.tmpssh/authorized_keys {user_home}/.ssh/authorized_keys > %{user_home}/.tmpssh/authorized_keys;
7 cp -rf %{user_home)/.tmpssh/* %{user_home}/.ssh/;
8 rm -rf %{user_name}/.tmpssh;
12 module VagrantDotfiles
13 class Provisioner
< Vagrant
.plugin("2", :provisioner)
15 def mkdir_and_copy_file(filename
, user_home
, root_config
)
16 dirname
= File
.dirname(filename
)
17 if dirname
!= "." then
18 shell_cmd
= "mkdir -p " + user_home
+ "/" + dirname
19 @machine.communicate
.sudo("mkdir -p " + user
.home
+ "/" + dirname
)
21 file_source
= "#{Dir.home}/" + filename
22 file_destination
= user_home
+ "/" + filename
23 @machine.communicate
.upload(file_source
, file_destination
)
24 end # mkdir_and_copy_file
27 root_config
= @machine.config
29 # read ~/.vagrant/dotfiles
30 dotfiles
= "#{Dir.home}/.vagrant/dotfiles"
32 if File
.file
?(dotfiles
) then
33 File
.readlines(dotfiles
, chomp
: true).each
do |unstripped_file
|
36 file
= unstripped_file
.strip
38 # Copying the local .ssh directory will clobber the vagrant key in /home/vagrant/.ssh/authorized_keys
39 # Instead copy the .ssh directory to .tmpssh, and concat the authorized_keys files
40 # then copy the .tmpssh to .ssh and delete .tmpssh
41 if file
== ".ssh" then
42 @machine.communicate
.upload("#{Dir.home}/" + file
, @config.user_home
+ "/.tmpssh")
43 @machine.communicate
.sudo(CP_SSH_DIR
% {user_home
: @config.user_home
})
45 # If just asking for the .ssh/authorized_keys file to be copied over, put it in .tmpssh alone, and send it as above
46 elsif file
== ".ssh/authorized_keys" then
47 @machine.communicate
.sudo("mkdir -p " + @config.user_home
+ "/.tmpssh")
48 @machine.communicate
.upload("#{Dir.home}/.ssh/authorized_keys", @config.user_home
+ "/.tmpssh/authorized_keys")
49 @machine.communicate
.sudo(CP_SSH_DIR
% {user_home
: @config.user_home
})
51 # Oh my - what if the user actually wants to copy a .tmpssh file to the vagrant box and the .ssh special cases have
52 # already trampled on that directoy ? Push any .tmpssh files to a list that's handled after any .ssh files
53 elsif file
.start_with
?(".tmpssh") then
54 tmpssh_list
.append(file
)
56 mkdir_and_copy_file(file
, @config.user_home
, root_config
)
60 ## Handle any .tmpssh files that have have been postponed until after the .ssh files special processing
61 tmpssh_list
.each
do |tmpssh_filename
|
62 mkdir_and_copy_file(tmpssh_filename
, @config.user_home
, root_config
)
65 @machine.ui
.warning("vagrant-dotfiles: Missing ~/.vagrant/dotfiles")
66 @machine.ui
.warning("Files in your home directory mentioned in ~/.vagrant/dotfiles (one per line) will be copied to /home/vagrant on your vagrant box.")