From e83a68ac49cf7bf169e58b5706605b072b4966a3 Mon Sep 17 00:00:00 2001 From: Jude N Date: Sun, 18 Nov 2018 22:47:16 -0500 Subject: [PATCH] Checkpoint / the simplest test looks to be working now (~/.vimrc) --- .gitignore | 3 ++ Makefile | 7 +++ README.md | 36 +++++++++----- TODO.md | 5 ++ Vagrantfile | 1 + copy_my_conf.gemspec | 11 ----- example/Vagrantfile | 10 ++-- example/dotfiles | 8 +++ lib/config.rb | 18 +++++++ lib/copy_my_conf.rb | 16 ------ lib/copy_my_conf/config.rb | 26 ---------- lib/copy_my_conf/git.rb | 14 ------ lib/copy_my_conf/provisioner.rb | 35 ------------- lib/copy_my_conf/ssh.rb | 16 ------ lib/copy_my_conf/vim.rb | 17 ------- lib/provisioner.rb | 73 ++++++++++++++++++++++++++++ lib/vagrant-dotfiles.rb | 21 ++++++++ spec/copy_my_conf/config_spec.rb | 17 ------- spec/copy_my_conf/vim_spec.rb | 20 -------- spec/copy_my_conf_spec.rb | 39 --------------- spec/spec_helper.rb | 2 +- spec/vagrant-dotfiles.rb | 39 +++++++++++++++ spec/vagrant-dotfiles/config_spec.rb | 12 +++++ vagrant-dotfiles.gemspec | 12 +++++ 24 files changed, 227 insertions(+), 231 deletions(-) create mode 100644 Makefile create mode 100644 TODO.md create mode 120000 Vagrantfile delete mode 100644 copy_my_conf.gemspec create mode 100644 example/dotfiles create mode 100644 lib/config.rb delete mode 100644 lib/copy_my_conf.rb delete mode 100644 lib/copy_my_conf/config.rb delete mode 100644 lib/copy_my_conf/git.rb delete mode 100644 lib/copy_my_conf/provisioner.rb delete mode 100644 lib/copy_my_conf/ssh.rb delete mode 100644 lib/copy_my_conf/vim.rb create mode 100644 lib/provisioner.rb create mode 100644 lib/vagrant-dotfiles.rb delete mode 100644 spec/copy_my_conf/config_spec.rb delete mode 100644 spec/copy_my_conf/vim_spec.rb delete mode 100644 spec/copy_my_conf_spec.rb create mode 100644 spec/vagrant-dotfiles.rb create mode 100644 spec/vagrant-dotfiles/config_spec.rb create mode 100644 vagrant-dotfiles.gemspec diff --git a/.gitignore b/.gitignore index 0e32a2c..fd6f153 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ #ctags tags + +vagrant-up.txt + diff --git a/Makefile b/Makefile new file mode 100644 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) diff --git a/README.md b/README.md index 4292537..8914b1d 100644 --- 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 +Copyright(c) 2018 Jude Nagurney License: [MIT License](http://mit-license.org/) diff --git a/TODO.md b/TODO.md new file mode 100644 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 index 0000000..4bc0529 --- /dev/null +++ b/Vagrantfile @@ -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 index f5452ba..0000000 --- a/copy_my_conf.gemspec +++ /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 diff --git a/example/Vagrantfile b/example/Vagrantfile index 74c2de0..d5f99bf 100644 --- a/example/Vagrantfile +++ b/example/Vagrantfile @@ -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 index 0000000..02ca45c --- /dev/null +++ b/example/dotfiles @@ -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 index 0000000..27257a7 --- /dev/null +++ b/lib/config.rb @@ -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 index fa7fec1..0000000 --- a/lib/copy_my_conf.rb +++ /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 index a1e51e6..0000000 --- a/lib/copy_my_conf/config.rb +++ /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 index 4ee5240..0000000 --- a/lib/copy_my_conf/git.rb +++ /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 index f089200..0000000 --- a/lib/copy_my_conf/provisioner.rb +++ /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 index 38e7643..0000000 --- a/lib/copy_my_conf/ssh.rb +++ /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 index b7c55bd..0000000 --- a/lib/copy_my_conf/vim.rb +++ /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 index 0000000..a1df473 --- /dev/null +++ b/lib/provisioner.rb @@ -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 index 0000000..84aecbc --- /dev/null +++ b/lib/vagrant-dotfiles.rb @@ -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 index 5e1334d..0000000 --- a/spec/copy_my_conf/config_spec.rb +++ /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 index fb0a56e..0000000 --- a/spec/copy_my_conf/vim_spec.rb +++ /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 index 10b0247..0000000 --- a/spec/copy_my_conf_spec.rb +++ /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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 876582f..50351c4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,4 +2,4 @@ require 'rspec' require "fixtures" $:< 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 index 0000000..dd5fe26 --- /dev/null +++ b/spec/vagrant-dotfiles/config_spec.rb @@ -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 index 0000000..d03a2d3 --- /dev/null +++ b/vagrant-dotfiles.gemspec @@ -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 -- 2.39.2