c10b8143ff8f6ef60b6d18043537abf9fc34645b
[vagrant-dotfiles.git] / lib / copy_my_conf.rb
1 require 'boot'
2 module Vagrant
3 module Provisioners
4 class CopyMyConf < Base
5
6 def prepare
7 prepare_vim if config.vim
8 prepare_git if config.git
9 prepare_ssh if config.ssh
10 end
11
12 def provision!
13 channel = env[:vm].channel
14 provision_ssh(channel) if config.ssh
15 provision_vim(channel) if config.vim
16 provision_git(channel) if config.git
17 end
18
19 def self.config_class
20 Config
21 end
22
23 private
24
25 def tmp_root
26 "/tmp/copy_my_conf"
27 end
28
29 def prepare_ssh
30 env[:vm].config.vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh")
31 end
32
33 def prepare_git
34 `mkdir -p #{tmp_root}/git`
35 `cp ~/.gitconfig #{tmp_root}/git/`
36 env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git")
37 end
38
39 def prepare_vim
40 `mkdir -p #{tmp_root}/vim`
41 ["~/.vimrc", "~/.vim"].each do |file|
42 `cp -r #{file} #{tmp_root}/vim`
43 end
44 env[:vm].config.vm.share_folder("vim", "#{tmp_root}/vim/", "#{tmp_root}/vim")
45 end
46
47 def provision_git(channel)
48 puts "Copying your gitconfig"
49 channel.execute("cp #{tmp_root}/git/.gitconfig ~/")
50 end
51
52 def provision_vim(channel)
53 puts "Copying your vim configuratios"
54 channel.execute("rm -rf #{user_home}/.vim*")
55 channel.execute("cp -r #{tmp_root}/vim/.??* ~/")
56 end
57
58 def provision_ssh(channel)
59 puts "Copying your ssh keys and config"
60 channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached")
61 channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached")
62 channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh")
63 channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password
64 end
65
66 def user_home
67 config.user_home || "/home/vagrant"
68 end
69 end
70 end
71 end