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