X-Git-Url: https://pwan.org/git/?p=vagrant-dotfiles.git;a=blobdiff_plain;f=lib%2Fprovisioner.rb;fp=lib%2Fprovisioner.rb;h=a1df473381ed6349acda71e2a8ba2c78745c4a7b;hp=0000000000000000000000000000000000000000;hb=e83a68ac49cf7bf169e58b5706605b072b4966a3;hpb=fa35bc36a5430811c454bc6f93aca0f6bdf20348 diff --git a/lib/provisioner.rb b/lib/provisioner.rb new file mode 100644 index 0000000..a1df473 --- /dev/null +++ b/lib/provisioner.rb @@ -0,0 +1,73 @@ +require 'pathname' + +CP_SSH_DIR=<<-end_cp_ssh_dir + mkdir -p %{user_home}/.tmpssh + touch %{user_home}/.tmpssh/authorized_keys; + cat %{user_home}/.tmpssh/authorized_keys {user_home}/.ssh/authorized_keys > %{user_home}/.tmpssh/authorized_keys; + cp -rf %{user_home)/.tmpssh/* %{user_home}/.ssh/; + rm -rf %{user_name}/.tmpssh" +end_cp_ssh_dir + +module VagrantPlugins + module VagrantDotfiles + class Provisioner < Vagrant.plugin("2", :provisioner) + + def mkdir_and_copy_file(filename, user_home, root_config) + dirname = File.dirname(filename) + if dirname != "." then + shell_cmd = "mkdir -p " + user_home + "/" + dirname + @machine.communicate.sudo("mkdir -p " + user.home + "/" + dirname) + end + file_source = "#{Dir.home}/" + filename + file_destination = user_home + "/" + filename + @machine.communicate.upload(file_source, file_destination) + end # mkdir_and_copy_file + + def provision + root_config = @machine.config + + # read ~/.vagrant/dotfiles + dotfiles = "#{Dir.home}/.vagrant/dotfiles" + tmpssh_list = [] + if File.file?(dotfiles) then + File.readlines(dotfiles, chomp: true).each do |unstripped_file| + + # No newlines please + file = unstripped_file.strip + + # Copying the local .ssh directory will clobber the vagrant key in /home/vagrant/.ssh/authorized_keys + # Instead copy the .ssh directory to .tmpssh, and concat the authorized_keys files + # then copy the .tmpssh to .ssh and delete .tmpssh + if file == ".ssh" then + @machine.communicate.upload("#{Dir.home}/" + file, @config.user_home + "/.tmpssh") + @machine.communicate.sudo(CP_SSH_DIR % {user_home: @config.user_home}) + + # If just asking for the .ssh/authorized_keys file to be copied over, put it in .tmpssh alone, and send it as above + elsif file == ".ssh/authorized_keys" then + @machine.communicate.sudo("mkdir -p " + @config.user_home + "/.tmpssh") + @machine.communicate.upload("#{Dir.home}/.ssh/authorized_keys", @config.user_home + "/.tmpssh/authorized_keys") + @machine.communicate.sudo(CP_SSH_DIR % {user_home: @config.user_home}) + + # Oh my - what if the user actually wants to copy a .tmpssh file to the vagrant box and the .ssh special cases have + # already trampled on that directoy ? Push any .tmpssh files to a list that's handled after any .ssh files + elsif file.start_with?(".tmpssh") then + tmpssh_list.append(file) + else + mkdir_and_copy_file(file, @config.user_home, root_config) + end # if + end # read file + + ## Handle any .tmpssh files that have have been postponed until after the .ssh files special processing + tmpssh_list.each do |tmpssh_filename| + mkdir_and_copy_file(tmpssh_filename, @config.user_home, root_config) + end #end tmpssh + else + @machine.ui.warning("vagrant-dotfiles: Missing ~/.vagrant/dotfiles") + @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.") + @to_be_copied = [] + end #if + end # configure + + end # class + end # module vdf +end # module vp