Moved code to prepare and provision git and ssh into seperate classes
authorAkshay Mankar <akshaym@thoughtworks.com>
Tue, 8 Jan 2013 19:25:22 +0000 (00:55 +0530)
committerAkshay Mankar <akshaym@thoughtworks.com>
Tue, 8 Jan 2013 19:25:22 +0000 (00:55 +0530)
example/Vagrantfile
lib/boot.rb
lib/copy_my_conf.rb
lib/copy_my_conf/git.rb [new file with mode: 0644]
lib/copy_my_conf/ssh.rb [new file with mode: 0644]

index a766e2b..8aa5f3f 100644 (file)
@@ -8,9 +8,9 @@ Vagrant::Config.run do |config|
   config.ssh.forward_agent = true
   
   config.vm.provision Vagrant::Provisioners::CopyMyConf do |copy_conf|
-    copy_conf.ssh = false
+    copy_conf.ssh = true
     copy_conf.vim = true
-    copy_conf.git = false
+    copy_conf.git = true
     copy_conf.user_home = "/home/vagrant"
   end
 
index b12274b..b81a7a0 100644 (file)
@@ -1,2 +1,4 @@
 require "copy_my_conf/config"
 require "copy_my_conf/vim"
+require "copy_my_conf/ssh"
+require "copy_my_conf/git"
index 60d14d9..8f2b889 100644 (file)
@@ -2,6 +2,10 @@ require 'boot'
 module Vagrant
   module Provisioners
     class CopyMyConf < Base
+      def initialize *args
+        `rm -rf /tmp/copy_my_conf`
+        super *args
+      end
 
       def prepare
         @to_be_copied = []
@@ -17,9 +21,6 @@ module Vagrant
         @to_be_copied.each do |conf|
           conf.provision channel, user_home, tmp_root
         end
-        provision_ssh(channel) if config.ssh
-        provision_vim(channel) if config.vim
-        provision_git(channel) if config.git
       end
 
       def self.config_class
@@ -32,29 +33,6 @@ module Vagrant
         "/tmp/copy_my_conf"
       end
 
-      def prepare_ssh
-        env[:vm].config.vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh")
-      end
-
-      def prepare_git
-        `mkdir -p #{tmp_root}/git`
-        `cp ~/.gitconfig #{tmp_root}/git/`
-        env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git")
-      end
-
-      def provision_git(channel)
-        puts "Copying your gitconfig"
-        channel.execute("cp #{tmp_root}/git/.gitconfig ~/")
-      end
-
-      def provision_ssh(channel)
-        puts "Copying your ssh keys and config"
-        channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached")
-        channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached")
-        channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh")
-        channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password
-      end
-
       def user_home
         config.user_home || "/home/vagrant"
       end
diff --git a/lib/copy_my_conf/git.rb b/lib/copy_my_conf/git.rb
new file mode 100644 (file)
index 0000000..eea3b86
--- /dev/null
@@ -0,0 +1,18 @@
+module Vagrant
+  module Provisioners
+    class CopyMyConf < Base
+      class Git
+        def prepare vm, tmp_root
+          `mkdir -p #{tmp_root}/git`
+          `cp ~/.gitconfig #{tmp_root}/git/`
+          vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git")
+        end
+
+        def provision channel, user_home, tmp_root
+          puts "Copying your gitconfig"
+          channel.execute("cp #{tmp_root}/git/.gitconfig #{user_home}")
+        end
+      end
+    end
+  end
+end
\ No newline at end of file
diff --git a/lib/copy_my_conf/ssh.rb b/lib/copy_my_conf/ssh.rb
new file mode 100644 (file)
index 0000000..bbe722d
--- /dev/null
@@ -0,0 +1,20 @@
+module Vagrant
+  module Provisioners
+    class CopyMyConf < Base
+      class Ssh
+        def prepare vm, tmp_root
+          vm.share_folder("ssh", "#{tmp_root}/ssh", "~/.ssh")
+        end
+
+        def provision channel, user_home, tmp_root
+          puts "Copying your ssh keys and config"
+          channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached")
+          channel.execute("[[ -f #{user_home}/.ssh/authorized_keys ]] && mv #{user_home}/.ssh/authorized_keys #{tmp_root}/cached")
+          channel.execute("cp #{tmp_root}/ssh/* #{user_home}/.ssh")
+          channel.execute("cat #{tmp_root}/cached/authorized_keys >> #{user_home}/.ssh/authorized_keys") # So that `vagrant ssh` doesn't ask for password
+        end
+
+      end
+    end
+  end
+end