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"
# -*- 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
-
+++ /dev/null
-require "copy_my_conf/config"
-require "copy_my_conf/vim"
-require "copy_my_conf/ssh"
-require "copy_my_conf/git"
-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
-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
-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
--- /dev/null
+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
+
-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
-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