a1df473381ed6349acda71e2a8ba2c78745c4a7b
[vagrant-dotfiles.git] / lib / provisioner.rb
1 require 'pathname'
2
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"
9 end_cp_ssh_dir
10
11 module VagrantPlugins
12 module VagrantDotfiles
13 class Provisioner < Vagrant.plugin("2", :provisioner)
14
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)
20 end
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
25
26 def provision
27 root_config = @machine.config
28
29 # read ~/.vagrant/dotfiles
30 dotfiles = "#{Dir.home}/.vagrant/dotfiles"
31 tmpssh_list = []
32 if File.file?(dotfiles) then
33 File.readlines(dotfiles, chomp: true).each do |unstripped_file|
34
35 # No newlines please
36 file = unstripped_file.strip
37
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})
44
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})
50
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)
55 else
56 mkdir_and_copy_file(file, @config.user_home, root_config)
57 end # if
58 end # read file
59
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)
63 end #end tmpssh
64 else
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.")
67 @to_be_copied = []
68 end #if
69 end # configure
70
71 end # class
72 end # module vdf
73 end # module vp