Checkpoint / the simplest test looks to be working now (~/.vimrc)
authorJude N <juden@pwan.org>
Mon, 19 Nov 2018 03:47:16 +0000 (22:47 -0500)
committerJude N <juden@pwan.org>
Mon, 19 Nov 2018 03:47:16 +0000 (22:47 -0500)
24 files changed:
.gitignore
Makefile [new file with mode: 0644]
README.md
TODO.md [new file with mode: 0644]
Vagrantfile [new symlink]
copy_my_conf.gemspec [deleted file]
example/Vagrantfile
example/dotfiles [new file with mode: 0644]
lib/config.rb [new file with mode: 0644]
lib/copy_my_conf.rb [deleted file]
lib/copy_my_conf/config.rb [deleted file]
lib/copy_my_conf/git.rb [deleted file]
lib/copy_my_conf/provisioner.rb [deleted file]
lib/copy_my_conf/ssh.rb [deleted file]
lib/copy_my_conf/vim.rb [deleted file]
lib/provisioner.rb [new file with mode: 0644]
lib/vagrant-dotfiles.rb [new file with mode: 0644]
spec/copy_my_conf/config_spec.rb [deleted file]
spec/copy_my_conf/vim_spec.rb [deleted file]
spec/copy_my_conf_spec.rb [deleted file]
spec/spec_helper.rb
spec/vagrant-dotfiles.rb [new file with mode: 0644]
spec/vagrant-dotfiles/config_spec.rb [new file with mode: 0644]
vagrant-dotfiles.gemspec [new file with mode: 0644]

index 0e32a2c..fd6f153 100644 (file)
@@ -12,3 +12,6 @@
 
 #ctags
 tags
+
+vagrant-up.txt
+
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..edd2211
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+build:
+       vagrant destroy -f
+       rm -rf .vagrant/plugins*
+       rm -rf *.gem
+       gem build vagrant-dotfiles.gemspec
+       vagrant plugin install --local ./vagrant-dotfiles-1.0.0.gem
+       (vagrant up --debug 2>&1 | tee vagrant-up.txt)
index 4292537..8914b1d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,31 +1,33 @@
-# CopyMyConf
+# vagrant-dotfiles
 
 Now you can copy your dotfiles into your vagrant box, so that you don't feel like visiting Mars whenever you are in vagrant shell.
 
+This project from forked from https://github.com/akshaymankar/copy_my_conf on 9/28/2018.
+
 ## How to Use
 
-It is a simple 3 step process
+It is a simple 2 step process
 
 ### Add gem in your vagrant
 
-    $ vagrant plugin install copy_my_conf
+    $ vagrant plugin install vagrant-dotfiles
 
-### Add provisioner in your Vagrantfile
+### Create a ~/.vagrant/dotfiles files
 
-Add these lines in your vagrant file inside the `Vagrant::Config.run` block
+Each line in the file should be a dotfile in your home directory of your workstations.
 
-    config.vm.provision :copy_my_conf do |copy_conf|
-      copy_conf.git
-      copy_conf.vim
-      copy_conf.ssh
-    end
+The plugin will copy this file into your /home/vagrant directory during the 'vagrant up' action.
 
 Don't worry if you have any other provisioners, vagrant can work with multiple provisioners. Yay \o/  
-As you might have guessed, If you make any of these false, the corresponding files won't be copied
 
 If home directory of the user is not `/home/vagrant` then you can specify that using the `user_home` option in above code
 
-      copy_conf.user_home = '/home/some_other_user'
+      config.vm.provision :vagrant-dotfiles do |dotfiles_config|
+          dotfiles_conf.user_home = '/home/some_other_user'
+      emd
+
+Note there are some edge cases with trying to copy your .ssh and .ssh/authorized_keys and .tmpssh* files over so the plugin doesn't clobber
+the vagrant key stored in .ssh/authorized_keys.
 
 ### Fire !
 
@@ -33,11 +35,19 @@ If home directory of the user is not `/home/vagrant` then you can specify that u
 
 And you'll be good to go.
 
+## Development Notes
+
+Reminders for the occasional/accidental rubyist
+
+Rebuilding the gem: gem build vagrant-dotfiles.gemspec
+Running the tests: rspec spec
+Loading the gem to the local dev environment: vagrant plugin install --local ./vagrant-dotfiles-1.0.0.gem
+
 ## Feedback
 This is my first gem so any kind of feedback would be appreciated.  
 Feel free fork, edit and send pull requests.
 
 ## Copyrights & Author
 
-Copyright(c) 2013 Akshay Mankar <itsakshaymankar@gmail.com>  
+Copyright(c) 2018 Jude Nagurney <jude@pwan.org>
 License: [MIT License](http://mit-license.org/)
diff --git a/TODO.md b/TODO.md
new file mode 100644 (file)
index 0000000..41693f6
--- /dev/null
+++ b/TODO.md
@@ -0,0 +1,5 @@
+# Get rspec tests working
+## https://github.com/hashicorp/vagrant-spec
+
+# Need to loop over all VMs being upped, for when the Vagrantfile defines multiple VMs
+# 'with_target_vms(argv[0]) do |vm|` from https://github.com/clintoncwolfe/vagrant-rspec-ci/blob/master/lib/vagrant-rspec-ci/command.rb ??
diff --git a/Vagrantfile b/Vagrantfile
new file mode 120000 (symlink)
index 0000000..4bc0529
--- /dev/null
@@ -0,0 +1 @@
+./example/Vagrantfile
\ No newline at end of file
diff --git a/copy_my_conf.gemspec b/copy_my_conf.gemspec
deleted file mode 100644 (file)
index f5452ba..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-Gem::Specification.new do |s|
-  s.name        = 'copy_my_conf'
-  s.version     = '0.1.1'
-  s.date        = '2013-07-06'
-  s.summary     = "Vagrant Provisioner to copy your configuration files into vagrant box"
-  s.description = "Copy your configurations easily into vagrant box"
-  s.authors     = ["Akshay Mankar"]
-  s.email       = 'itsakshaymankar@gmail.com'
-  s.files       = Dir["lib/**/*.rb"]
-  s.homepage    = 'http://github.com/akshaymankar/copy_my_conf'
-end
index 74c2de0..d5f99bf 100644 (file)
@@ -1,12 +1,10 @@
 # -*- mode: ruby -*-
 # vi: set ft=ruby :
 Vagrant.configure("2") do |config|
-  config.vm.box = "precise64"
+  config.vm.box = "ubuntu/trusty64"
 
-  config.vm.provision :copy_my_conf do |copy_conf|
-    copy_conf.vim
-    copy_conf.git
-    copy_conf.ssh
-    copy_conf.user_home = "/home/vagrant"
+  config.vm.provision 'vagrant-dotfiles' do |dotfile_conf|
+    dotfile_conf.user_home = "/home/vagrant"
   end
+
 end
diff --git a/example/dotfiles b/example/dotfiles
new file mode 100644 (file)
index 0000000..02ca45c
--- /dev/null
@@ -0,0 +1,8 @@
+.gitconfig
+.tmpssh
+.gitattributes
+.ssh/authorized_keys
+.vim
+.vimrc
+not_a_dotfile_dont_care
+subdir/also_not_a_dotfile
diff --git a/lib/config.rb b/lib/config.rb
new file mode 100644 (file)
index 0000000..27257a7
--- /dev/null
@@ -0,0 +1,18 @@
+
+module VagrantPlugins
+  module VagrantDotfiles
+    class Config < Vagrant.plugin("2", :config)
+      attr_accessor :user_home
+
+      def initialize
+        @user_home = UNSET_VALUE
+      end
+
+      def finalize!
+        @user_home = '/home/vagrant' if @user_home == UNSET_VALUE
+      end
+
+    end # class
+  end # module vdt
+end # module vp
+
diff --git a/lib/copy_my_conf.rb b/lib/copy_my_conf.rb
deleted file mode 100644 (file)
index fa7fec1..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-module CopyMyConf
-  class Plugin < Vagrant.plugin("2")
-    name "copy_my_conf"
-
-    config(:copy_my_conf, :provisioner) do
-      require File.expand_path('../copy_my_conf/config', __FILE__)
-      Config
-    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
deleted file mode 100644 (file)
index a1e51e6..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-require_relative "git"
-require_relative "ssh"
-require_relative "vim"
-
-module CopyMyConf
-  class Config < Vagrant.plugin("2", :config)
-    attr_accessor :user_home
-
-    def git
-      @git ||= CopyMyConf::Git.new
-    end
-
-    def vim
-      @vim ||= CopyMyConf::Vim.new
-    end
-
-    def ssh
-      @ssh ||=CopyMyConf::Ssh.new
-    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
deleted file mode 100644 (file)
index 4ee5240..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-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
-end
diff --git a/lib/copy_my_conf/provisioner.rb b/lib/copy_my_conf/provisioner.rb
deleted file mode 100644 (file)
index f089200..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-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
deleted file mode 100644 (file)
index 38e7643..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-module CopyMyConf
-  class Ssh
-    def prepare vm, tmp_root
-      vm.synced_folder("#{ENV['HOME']}/.ssh", "#{tmp_root}/ssh",:id => "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
diff --git a/lib/copy_my_conf/vim.rb b/lib/copy_my_conf/vim.rb
deleted file mode 100644 (file)
index b7c55bd..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-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
diff --git a/lib/provisioner.rb b/lib/provisioner.rb
new file mode 100644 (file)
index 0000000..a1df473
--- /dev/null
@@ -0,0 +1,73 @@
+require 'pathname'
+
+CP_SSH_DIR=<<-end_cp_ssh_dir
+   mkdir -p %{user_home}/.tmpssh
+   touch %{user_home}/.tmpssh/authorized_keys; 
+   cat %{user_home}/.tmpssh/authorized_keys {user_home}/.ssh/authorized_keys > %{user_home}/.tmpssh/authorized_keys; 
+   cp -rf %{user_home)/.tmpssh/* %{user_home}/.ssh/; 
+   rm -rf %{user_name}/.tmpssh"
+end_cp_ssh_dir
+
+module VagrantPlugins
+  module VagrantDotfiles
+    class Provisioner < Vagrant.plugin("2", :provisioner)
+
+      def mkdir_and_copy_file(filename, user_home, root_config)
+        dirname = File.dirname(filename)
+        if dirname != "." then
+          shell_cmd = "mkdir -p " + user_home + "/" + dirname
+          @machine.communicate.sudo("mkdir -p "  + user.home + "/" + dirname)
+        end
+        file_source = "#{Dir.home}/" + filename
+        file_destination = user_home + "/" + filename
+        @machine.communicate.upload(file_source, file_destination)
+      end # mkdir_and_copy_file
+
+      def provision
+        root_config = @machine.config
+
+        # read ~/.vagrant/dotfiles
+        dotfiles = "#{Dir.home}/.vagrant/dotfiles"
+        tmpssh_list = []
+        if File.file?(dotfiles) then
+          File.readlines(dotfiles, chomp: true).each do |unstripped_file|
+
+            # No newlines please
+            file = unstripped_file.strip
+
+            # Copying the local .ssh directory will clobber the vagrant key in /home/vagrant/.ssh/authorized_keys
+            # Instead copy the .ssh directory to .tmpssh, and concat the authorized_keys files 
+            # then copy the .tmpssh to .ssh and delete .tmpssh
+            if file == ".ssh" then
+              @machine.communicate.upload("#{Dir.home}/" + file, @config.user_home + "/.tmpssh")
+              @machine.communicate.sudo(CP_SSH_DIR % {user_home: @config.user_home})
+
+            # If just asking for the .ssh/authorized_keys file to be copied over, put it in .tmpssh alone, and send it as above
+            elsif file == ".ssh/authorized_keys" then
+              @machine.communicate.sudo("mkdir -p " + @config.user_home  + "/.tmpssh")
+              @machine.communicate.upload("#{Dir.home}/.ssh/authorized_keys", @config.user_home + "/.tmpssh/authorized_keys")
+              @machine.communicate.sudo(CP_SSH_DIR % {user_home: @config.user_home})
+
+            # Oh my - what if the user actually wants to copy a .tmpssh file to the vagrant box and the .ssh special cases have
+            # already trampled on that directoy ?  Push any .tmpssh files to a list that's handled after any .ssh files
+            elsif file.start_with?(".tmpssh") then
+              tmpssh_list.append(file)
+            else
+              mkdir_and_copy_file(file, @config.user_home, root_config)
+            end # if
+          end # read file
+
+          ## Handle any .tmpssh files that have have been postponed until after the .ssh files special processing
+          tmpssh_list.each do |tmpssh_filename|
+            mkdir_and_copy_file(tmpssh_filename, @config.user_home, root_config)
+          end #end tmpssh
+        else
+          @machine.ui.warning("vagrant-dotfiles: Missing ~/.vagrant/dotfiles")
+          @machine.ui.warning("Files in your home directory mentioned in ~/.vagrant/dotfiles (one per line) will be copied to /home/vagrant on your vagrant box.")
+          @to_be_copied = []
+        end #if
+      end # configure
+
+    end # class
+  end # module vdf
+end # module vp
diff --git a/lib/vagrant-dotfiles.rb b/lib/vagrant-dotfiles.rb
new file mode 100644 (file)
index 0000000..84aecbc
--- /dev/null
@@ -0,0 +1,21 @@
+module VagrantPlugins
+  module VagrantDotfiles
+    class Plugin < Vagrant.plugin("2")
+      name "vagrant-dotfiles"
+      description <<-DESC
+      Provide support for copying home directory files mentioned in ~/.vagrant/dotfiles
+      to the /vagrant directory on the vagrant box.
+      DESC
+
+      config('vagrant-dotfiles', :provisioner) do
+        require File.expand_path("../config", __FILE__)
+        Config
+      end
+
+      provisioner('vagrant-dotfiles') do
+        require File.expand_path("../provisioner", __FILE__)
+        Provisioner
+      end
+    end
+  end
+end
diff --git a/spec/copy_my_conf/config_spec.rb b/spec/copy_my_conf/config_spec.rb
deleted file mode 100644 (file)
index 5e1334d..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-require "spec_helper"
-module Vagrant::Provisioners
-  class CopyMyConf < Base
-    describe Config do
-      it "should list all the true attributes" do
-        config = Config.new
-
-        config.vim
-        config.ssh
-
-        all_enabled_attributes = config.all_enabled_attributes
-        all_enabled_attributes.map(&:class) =~ [Vim, Ssh]
-      end
-    end
-  end
-end
-
diff --git a/spec/copy_my_conf/vim_spec.rb b/spec/copy_my_conf/vim_spec.rb
deleted file mode 100644 (file)
index fb0a56e..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-require "spec_helper"
-
-module Vagrant
-  module Provisioners
-    class CopyMyConf < Base
-      describe Vim do
-        it "should copy dotfiles to temporary location in vagrant box" do
-          Vim.any_instance.stub(:`).and_return(nil)
-          vm = Object.new
-          tmp_root = "tmp_root"
-
-          vm.should_receive(:share_folder).with(anything, "#{tmp_root}/vim", "#{tmp_root}/vim")
-          vim = Vim.new
-
-          vim.prepare vm, tmp_root
-        end
-      end
-    end
-  end
-end
\ No newline at end of file
diff --git a/spec/copy_my_conf_spec.rb b/spec/copy_my_conf_spec.rb
deleted file mode 100644 (file)
index 10b0247..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-require "spec_helper"
-
-module Vagrant
-  module Provisioners
-    describe CopyMyConf do
-      before(:each) do
-        env_vm = Object.new
-        env_vm_config = Object.new
-        @mock_vm = Object.new
-        @config = CopyMyConf.config_class.new
-        @env_channel = Object.new
-
-        CopyMyConf.any_instance.stub(:env).and_return({:vm => env_vm})
-        env_vm.stub(:config).and_return(env_vm_config)
-        env_vm.stub(:channel).and_return(@env_channel)
-        env_vm_config.stub(:vm).and_return(@mock_vm)
-        CopyMyConf.any_instance.stub(:config).and_return(@config)
-      end
-
-      it "should prepare provisioning process" do
-        @config.should_receive(:all_enabled_attributes).and_return([CopyMyConf::Vim.new])
-        CopyMyConf::Vim.any_instance.should_receive(:prepare).with(@mock_vm, anything)
-
-        CopyMyConf.new.prepare
-      end
-
-      it "should provision the vm" do
-        @config.stub(:all_enabled_attributes).and_return([CopyMyConf::Vim.new])
-        copy_my_conf = CopyMyConf.new
-
-        CopyMyConf::Vim.any_instance.stub(:prepare)
-        copy_my_conf.prepare
-
-        CopyMyConf::Vim.any_instance.should_receive(:provision).with(@env_channel, anything, anything)
-        copy_my_conf.provision!
-      end
-    end
-  end
-end
index 876582f..50351c4 100644 (file)
@@ -2,4 +2,4 @@ require 'rspec'
 require "fixtures"
 $:<<File.dirname(__FILE__)+"/../lib"
 
-require "copy_my_conf"
\ No newline at end of file
+require "vagrant-dotfiles"
diff --git a/spec/vagrant-dotfiles.rb b/spec/vagrant-dotfiles.rb
new file mode 100644 (file)
index 0000000..4c8c440
--- /dev/null
@@ -0,0 +1,39 @@
+require "spec_helper"
+
+module Vagrant
+  module Provisioners
+    describe VagrantDotfiles do
+      before(:each) do
+        env_vm = Object.new
+        env_vm_config = Object.new
+        @mock_vm = Object.new
+        @config = VagrantDotfiles.config_class.new
+        @env_channel = Object.new
+
+        VagrantDotfiles.any_instance.stub(:env).and_return({:vm => env_vm})
+        env_vm.stub(:config).and_return(env_vm_config)
+        env_vm.stub(:channel).and_return(@env_channel)
+        env_vm_config.stub(:vm).and_return(@mock_vm)
+        VagrantDotfiles.any_instance.stub(:config).and_return(@config)
+      end
+
+      it "should prepare provisioning process" do
+        @config.should_receive(:all_enabled_attributes).and_return([VagrantDotfiles::Vim.new])
+        VagrantDotfiles::Vim.any_instance.should_receive(:prepare).with(@mock_vm, anything)
+
+        VagrantDotfiles.new.prepare
+      end
+
+      it "should provision the vm" do
+        @config.stub(:all_enabled_attributes).and_return([VagrantDotfiles::Vim.new])
+        vagrant_dotfiles = VagrantDotfiles.new
+
+        VagrantDotfiles::Vim.any_instance.stub(:prepare)
+        vagrant_dotfiles.prepare
+
+        VagrantDotfiles::Vim.any_instance.should_receive(:provision).with(@env_channel, anything, anything)
+        vagrant_dotfiles.provision!
+      end
+    end
+  end
+end
diff --git a/spec/vagrant-dotfiles/config_spec.rb b/spec/vagrant-dotfiles/config_spec.rb
new file mode 100644 (file)
index 0000000..dd5fe26
--- /dev/null
@@ -0,0 +1,12 @@
+require "spec_helper"
+module Vagrant::Provisioners
+  class VagrantDotfiles < Base
+    describe Config do
+      it "should list all the true attributes" do
+        config = Config.new
+
+      end
+    end
+  end
+end
+
diff --git a/vagrant-dotfiles.gemspec b/vagrant-dotfiles.gemspec
new file mode 100644 (file)
index 0000000..d03a2d3
--- /dev/null
@@ -0,0 +1,12 @@
+Gem::Specification.new do |s|
+  s.name        = 'vagrant-dotfiles'
+  s.version     = '1.0.0'
+  s.date        = '2018-09-28'
+  s.summary     = "Vagrant Provisioner to copy your configuration files into vagrant box"
+  s.description = "Copy your configurations easily into vagrant box"
+  s.authors     = ["Jude Nagurney"]
+  s.email       = 'juden@pwan.org'
+  s.files       = Dir["lib/**/*.rb"]
+  s.homepage    = 'http://github.com/jude/vagrant-dotfiles'
+  s.license     = 'MIT'
+end