Class: Vagrant::Action::VM::ShareFolders

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/vm/share_folders.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ShareFolders

Returns a new instance of ShareFolders.



9
10
11
12
# File 'lib/vagrant/action/vm/share_folders.rb', line 9

def initialize(app, env)
  @logger = Log4r::Logger.new("vagrant::action::vm::share_folders")
  @app    = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant/action/vm/share_folders.rb', line 14

def call(env)
  @env = env

  prepare_folders
  

  @app.call(env)

  mount_shared_folders
end

#create_metadataObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant/action/vm/share_folders.rb', line 59

def 
  @env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating")

  folders = []
  shared_folders.each do |name, data|
    folders << {
      :name => name,
      :hostpath => File.expand_path(data[:hostpath], @env[:root_path]),
      :transient => data[:transient]
    }
  end

  @env[:vm].driver.share_folders(folders)
end

#mount_shared_foldersObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant/action/vm/share_folders.rb', line 74

def mount_shared_folders
  @env[:ui].info I18n.t("vagrant.actions.vm.share_folders.mounting")

  # short guestpaths first, so we don't step on ourselves
  folders = shared_folders.sort_by do |name, data|
    if data[:guestpath]
      data[:guestpath].length
    else
      # A long enough path to just do this at the end.
      10000
    end
  end

  # Go through each folder and mount
  folders.each do |name, data|
    if data[:guestpath]
      # Guest path specified, so mount the folder to specified point
      @env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
                            :name => name,
                            :guest_path => data[:guestpath]))

      # Dup the data so we can pass it to the guest API
      data = data.dup

      # Calculate the owner and group
      data[:owner] ||= @env[:vm].config.ssh.username
      data[:group] ||= @env[:vm].config.ssh.username

      # Mount the actual folder
      @env[:vm].guest.mount_shared_folder(name, data[:guestpath], data)
    else
      # If no guest path is specified, then automounting is disabled
      @env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",
                            :name => name))
    end
  end
end

#prepare_foldersObject

Prepares the shared folders by verifying they exist and creating them if they don't.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vagrant/action/vm/share_folders.rb', line 42

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}")

      begin
        hostpath.mkpath
      rescue Errno::EACCES
        raise Errors::SharedFolderCreateFailed, :path => hostpath.to_s
      end
    end
  end
end

#shared_foldersObject

This method returns an actual list of VirtualBox shared folders to create and their proper path.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant/action/vm/share_folders.rb', line 27

def shared_folders
  @env[:vm].config.vm.shared_folders.inject({}) do |acc, data|
    key, value = data

    next acc if value[:disabled]

    # This to prevent overwriting the actual shared folders data
    value = value.dup
    acc[key] = value
    acc
  end
end