Extracted Logic for vim in seperate class
[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 @to_be_copied = []
8 config.all_true.each do |c|
9 conf = self.class.const_get(c.capitalize).new
10 @to_be_copied << conf
11 conf.prepare env[:vm].config.vm, tmp_root
12 end
13 end
14
15 def provision!
16 channel = env[:vm].channel
17 @to_be_copied.each do |conf|
18 conf.provision channel, user_home, tmp_root
19 end
20 provision_ssh(channel) if config.ssh
21 provision_vim(channel) if config.vim
22 provision_git(channel) if config.git
23 end
24
25 def self.config_class
26 Config
27 end
28
29 private
30
31 def tmp_root
32 "/tmp/copy_my_conf"
33 end
34
35 def prepare_ssh
36 env[:vm].config.vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh")
37 end
38
39 def prepare_git
40 `mkdir -p #{tmp_root}/git`
41 `cp ~/.gitconfig #{tmp_root}/git/`
42 env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git")
43 end
44
45 def provision_git(channel)
46 puts "Copying your gitconfig"
47 channel.execute("cp #{tmp_root}/git/.gitconfig ~/")
48 end
49
50 def provision_ssh(channel)
51 puts "Copying your ssh keys and config"
52 channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached")
53 channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached")
54 channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh")
55 channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password
56 end
57
58 def user_home
59 config.user_home || "/home/vagrant"
60 end
61 end
62 end
63 end