From 7adf63265cfc7d78bf36412e0ac0f480dd689678 Mon Sep 17 00:00:00 2001 From: Akshay Mankar Date: Sat, 6 Jul 2013 20:11:21 +0530 Subject: [PATCH] Now supporting vagrant 1.2. Simplified config class. SSH not working --- copy_my_conf.gemspec | 2 +- example/Vagrantfile | 12 ++------- lib/boot.rb | 4 --- lib/copy_my_conf.rb | 46 ++++++++------------------------- lib/copy_my_conf/config.rb | 45 +++++++++++++------------------- lib/copy_my_conf/git.rb | 26 ++++++++----------- lib/copy_my_conf/provisioner.rb | 35 +++++++++++++++++++++++++ lib/copy_my_conf/ssh.rb | 28 +++++++++----------- lib/copy_my_conf/vim.rb | 30 ++++++++++----------- 9 files changed, 103 insertions(+), 125 deletions(-) delete mode 100644 lib/boot.rb create mode 100644 lib/copy_my_conf/provisioner.rb diff --git a/copy_my_conf.gemspec b/copy_my_conf.gemspec index 90e3572..7d5a576 100644 --- a/copy_my_conf.gemspec +++ b/copy_my_conf.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'copy_my_conf' - s.version = '0.0.1' + s.version = '0.1.0' s.date = '2013-01-08' s.summary = "Vagrant Provisioner to copy your configuration files into vagrant box" s.description = "Copy your configurations easily into vagrant box" diff --git a/example/Vagrantfile b/example/Vagrantfile index 07b89ee..5fb1f37 100644 --- a/example/Vagrantfile +++ b/example/Vagrantfile @@ -1,19 +1,11 @@ # -*- mode: ruby -*- # vi: set ft=ruby : -require 'copy_my_conf' - -#This is a sample vagrant file -Vagrant::Config.run do |config| +Vagrant.configure("2") do |config| config.vm.box = "precise64" - config.vm.provision Vagrant::Provisioners::CopyMyConf do |copy_conf| - copy_conf.ssh + config.vm.provision :copy_my_conf do |copy_conf| copy_conf.vim copy_conf.git copy_conf.user_home = "/home/vagrant" end - - config.vm.customize ["modifyvm", :id, "--memory", 2048] - config.vm.customize ["modifyvm", :id, "--cpus", 4] end - diff --git a/lib/boot.rb b/lib/boot.rb deleted file mode 100644 index b81a7a0..0000000 --- a/lib/boot.rb +++ /dev/null @@ -1,4 +0,0 @@ -require "copy_my_conf/config" -require "copy_my_conf/vim" -require "copy_my_conf/ssh" -require "copy_my_conf/git" diff --git a/lib/copy_my_conf.rb b/lib/copy_my_conf.rb index 7cde153..fa7fec1 100644 --- a/lib/copy_my_conf.rb +++ b/lib/copy_my_conf.rb @@ -1,40 +1,16 @@ -require 'boot' -module Vagrant - module Provisioners - class CopyMyConf < Base - def initialize *args - `rm -rf /tmp/copy_my_conf` - super *args - end +module CopyMyConf + class Plugin < Vagrant.plugin("2") + name "copy_my_conf" - def prepare - @to_be_copied = [] - config.all_enabled_attributes.each do |conf| - @to_be_copied << conf - conf.prepare env[:vm].config.vm, tmp_root - end - end - - def provision! - channel = env[:vm].channel - @to_be_copied.each do |conf| - conf.provision channel, user_home, tmp_root - end - end - - def self.config_class - Config - end - - private - - def tmp_root - "/tmp/copy_my_conf" - end + config(:copy_my_conf, :provisioner) do + require File.expand_path('../copy_my_conf/config', __FILE__) + Config + end - def user_home - config.user_home || "/home/vagrant" - end + provisioner :copy_my_conf do + require File.expand_path('../copy_my_conf/provisioner', __FILE__) + Provisioner end + end end diff --git a/lib/copy_my_conf/config.rb b/lib/copy_my_conf/config.rb index 23f150e..a1e51e6 100644 --- a/lib/copy_my_conf/config.rb +++ b/lib/copy_my_conf/config.rb @@ -1,34 +1,25 @@ -module Vagrant - module Provisioners - class CopyMyConf < Base - class Config < Vagrant::Config::Base - def self.all_attributes - [:ssh, :vim, :git] - end - attr_accessor :user_home +require_relative "git" +require_relative "ssh" +require_relative "vim" - all_attributes.each do |attr| - define_method(attr) do - instance_variable_get_or_set(attr, CopyMyConf.const_get("#{attr.capitalize}").new) - end - end +module CopyMyConf + class Config < Vagrant.plugin("2", :config) + attr_accessor :user_home - def all_enabled_attributes - all_attributes.collect do |attr| - instance_variable_get "@#{attr}" - end.compact - [@ssh, @vim, @git].compact - end + def git + @git ||= CopyMyConf::Git.new + end + + def vim + @vim ||= CopyMyConf::Vim.new + end - private - def all_attributes - self.class.all_attributes - end + def ssh + @ssh ||=CopyMyConf::Ssh.new + end - def instance_variable_get_or_set(attr, value) - instance_variable_get("@#{attr}") || instance_variable_set("@#{attr}", value) - end - end + def all_enabled_attributes + [@ssh, @vim, @git].compact end end end diff --git a/lib/copy_my_conf/git.rb b/lib/copy_my_conf/git.rb index eea3b86..4ee5240 100644 --- a/lib/copy_my_conf/git.rb +++ b/lib/copy_my_conf/git.rb @@ -1,18 +1,14 @@ -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 +module CopyMyConf + class Git + def prepare vm, tmp_root + `mkdir -p #{tmp_root}/git` + `cp ~/.gitconfig #{tmp_root}/git/` + vm.synced_folder("#{tmp_root}/git/", "#{tmp_root}/git", :id => "git") + end - def provision channel, user_home, tmp_root - puts "Copying your gitconfig" - channel.execute("cp #{tmp_root}/git/.gitconfig #{user_home}") - end - end + def provision channel, user_home, tmp_root + puts "Copying your gitconfig" + channel.execute("cp #{tmp_root}/git/.gitconfig #{user_home}") end end -end \ No newline at end of file +end diff --git a/lib/copy_my_conf/provisioner.rb b/lib/copy_my_conf/provisioner.rb new file mode 100644 index 0000000..f089200 --- /dev/null +++ b/lib/copy_my_conf/provisioner.rb @@ -0,0 +1,35 @@ +module CopyMyConf + class Provisioner < Vagrant.plugin("2", :provisioner) + + def configure(root_config) + `rm -rf /tmp/copy_my_conf` + @to_be_copied = [] + config.all_enabled_attributes.each do |conf| + @to_be_copied << conf + conf.prepare root_config.vm, tmp_root + end + end + + def provision + channel = @machine.communicate + @to_be_copied.each do |conf| + conf.provision channel, user_home, tmp_root + end + end + + def self.config_class + Config + end + + private + + def tmp_root + "/tmp/copy_my_conf" + end + + def user_home + config.user_home || "/home/vagrant" + end + end +end + diff --git a/lib/copy_my_conf/ssh.rb b/lib/copy_my_conf/ssh.rb index bbe722d..c45aba5 100644 --- a/lib/copy_my_conf/ssh.rb +++ b/lib/copy_my_conf/ssh.rb @@ -1,20 +1,16 @@ -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 +module CopyMyConf + class Ssh + def prepare vm, tmp_root + vm.synced_folder("#{tmp_root}/ssh", "~/.ssh", :id => "ssh") + end - 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 diff --git a/lib/copy_my_conf/vim.rb b/lib/copy_my_conf/vim.rb index f8935ed..b7c55bd 100644 --- a/lib/copy_my_conf/vim.rb +++ b/lib/copy_my_conf/vim.rb @@ -1,21 +1,17 @@ -module Vagrant - module Provisioners - class CopyMyConf < Base - class Vim - def prepare vm, tmp_root - `mkdir -p #{tmp_root}/vim` - ["~/.vimrc", "~/.vim"].each do |file| - `cp -r #{file} #{tmp_root}/vim` - end - vm.share_folder("vim", "#{tmp_root}/vim", "#{tmp_root}/vim") - end - - def provision channel, user_home, tmp_root - puts "Copying your vim configuratios" - channel.execute("rm -rf #{user_home}/.vim*") - channel.execute("cp -r #{tmp_root}/vim/.??* ~/") - end +module CopyMyConf + class Vim + def prepare vm, tmp_root + `mkdir -p #{tmp_root}/vim` + ["~/.vimrc", "~/.vim"].each do |file| + `cp -r #{file} #{tmp_root}/vim` end + vm.synced_folder("#{tmp_root}/vim", "#{tmp_root}/vim", :id => "vim") + end + + def provision channel, user_home, tmp_root + puts "Copying your vim configuratios" + channel.execute("rm -rf #{user_home}/.vim*") + channel.execute("cp -r #{tmp_root}/vim/.??* ~/") end end end -- 2.39.2