From 9cb4597a27544e2ea25469dc1c60e95cf641f2cd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 8 Jan 2012 11:23:43 -0800 Subject: [PATCH] :create flag on shared folders will create on host if it doesnt exist [GH-604] --- CHANGELOG.md | 2 ++ lib/vagrant/action/vm/share_folders.rb | 23 +++++++++++++++++++++-- lib/vagrant/config/vm.rb | 11 ++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8453eb96c..d3574fbdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ - Added a "--plain" flag to `vagrant ssh` which will cause Vagrant to not perform any authentication. It will simply `ssh` into the proper IP and port of the virtual machine. + - If a shared folder now has a `:create` flag set to `true`, the path on the + host will be created if it doesn't exist. - Removed Thor as a dependency for the command line interfaces. This resulted in general speed increases across all command line commands. - Linux uses `shutdown -h` instead of `halt` to hopefully more consistently diff --git a/lib/vagrant/action/vm/share_folders.rb b/lib/vagrant/action/vm/share_folders.rb index 98363da6d..33807c9de 100644 --- a/lib/vagrant/action/vm/share_folders.rb +++ b/lib/vagrant/action/vm/share_folders.rb @@ -1,15 +1,20 @@ +require 'pathname' + +require 'log4r' + module Vagrant module Action module VM class ShareFolders def initialize(app, env) - @app = app - @env = env + @logger = Log4r::Logger.new("vagrant::action::vm::share_folders") + @app = app end def call(env) @env = env + prepare_folders create_metadata @app.call(env) @@ -32,6 +37,20 @@ module Vagrant end end + # Prepares the shared folders by verifying they exist and creating them + # if they don't. + def prepare_folders + shared_folders.each do |name, options| + hostpath = Pathname.new(options[:hostpath]).expand_path(@env[:root_path]) + + if !hostpath.directory? && options[:create] + # Host path doesn't exist, so let's create it. + @logger.debug("Host path doesn't exist, creating: #{hostpath}") + hostpath.mkpath + end + end + end + def create_metadata @env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating") diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 047dc4171..677c05d65 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -1,3 +1,5 @@ +require 'pathname' + require 'vagrant/config/vm/sub_vm' require 'vagrant/config/vm/provisioner' @@ -52,8 +54,9 @@ Please change your configurations to match this new syntax. def share_folder(name, guestpath, hostpath, opts=nil) @shared_folders[name] = { - :guestpath => guestpath, - :hostpath => hostpath, + :guestpath => guestpath.to_s, + :hostpath => hostpath.to_s, + :create => false, :owner => nil, :group => nil, :nfs => false @@ -133,7 +136,9 @@ do before is certainly still possible with `VBoxManage` as well. errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.boxes.find(box) && !base_mac shared_folders.each do |name, options| - if !File.directory?(File.expand_path(options[:hostpath].to_s, env.root_path)) + hostpath = Pathname.new(options[:hostpath]).expand_path(env.root_path) + + if !hostpath.directory? && !options[:create] errors.add(I18n.t("vagrant.config.vm.shared_folder_hostpath_missing", :name => name, :path => options[:hostpath]))