Class: VagrantPlugins::Bindfs::Action::Bind

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-bindfs/bind.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Bind

Returns a new instance of Bind.



5
6
7
8
# File 'lib/vagrant-bindfs/bind.rb', line 5

def initialize(app, env)
  @app = app
  @env = env
end

Instance Method Details

#additional_optionsObject



169
170
171
172
173
174
175
176
# File 'lib/vagrant-bindfs/bind.rb', line 169

def additional_options
  @additional_options ||= {
    # only for old versions, this will result in an error
    # if you try that within current bindfs versions!
    "owner" => "vagrant",
    "group" => "vagrant"
  }.freeze
end

#additional_shortcutsObject



184
185
186
187
188
189
190
191
192
# File 'lib/vagrant-bindfs/bind.rb', line 184

def additional_shortcuts
  @additional_shortcuts ||= {
    "u" => nil, # overwrites the value of force-user
    "g" => nil, # overwrites the value of force-group
    "m" => nil, # overwrites the value of mirror
    "M" => nil, # overwrites the value of mirror-only
    "p" => nil  # overwrites the value of perms
  }.freeze
end

#available_flagsObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/vagrant-bindfs/bind.rb', line 194

def available_flags
  @available_flags ||= {
    "create-as-user" => false,
    "create-as-mounter" => false,
    "chown-normal" => false,
    "chown-ignore" => false,
    "chown-deny" => false,
    "chgrp-normal" => false,
    "chgrp-ignore" => false,
    "chgrp-deny" => false,
    "chmod-normal" => false,
    "chmod-ignore" => false,
    "chmod-deny" => false,
    "chmod-allow-x" => false,
    "xattr-none" => false,
    "xattr-ro" => false,
    "xattr-rw" => false,
    "no-allow-other" => false,
    "realistic-permissions" => false,
    "ctime-from-mtime" => false,
    "hide-hard-links" => false,
    "multithreaded" => false
  }.freeze
end

#available_optionsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vagrant-bindfs/bind.rb', line 152

def available_options
  @available_options ||= {
    "force-user" => "vagrant",
    "force-group" => "vagrant",
    "perms" => "u=rwX:g=rD:o=rD",
    "mirror" => nil,
    "mirror-only" => nil,
    "map" => nil,
    "create-for-user" => nil,
    "create-for-group" => nil,
    "create-with-perms" => nil,
    "chmod-filter" => nil,
    "read-rate" => nil,
    "write-rate" => nil
  }.freeze
end

#available_shortcutsObject



178
179
180
181
182
# File 'lib/vagrant-bindfs/bind.rb', line 178

def available_shortcuts
  @available_shortcuts ||= {
    "o" => nil
  }.freeze
end

#bind_foldersObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vagrant-bindfs/bind.rb', line 93

def bind_folders
  @env[:ui].info I18n.t("vagrant.config.bindfs.status.binding_all")

  binded_folders.each do |id, options|
    source, dest, args = normalize_options(options)

    bind_command = [
      "bindfs",
      args,
      source,
      dest
    ].compact

    unless @machine.communicate.test("test -d #{source}")
      @env[:ui].error I18n.t(
        "vagrant.config.bindfs.errors.source_path_not_exist",
        path: source
      )

      next
    end

    if @machine.communicate.test("mount | grep bindfs | grep #{dest}")
      @env[:ui].info I18n.t(
        "vagrant.config.bindfs.already_mounted",
        dest: dest
      )

      next
    end

    @env[:ui].info I18n.t(
      "vagrant.config.bindfs.status.binding_entry",
      dest: dest,
      source: source
    )

    @machine.communicate.tap do |comm|
      comm.sudo("mkdir -p #{dest}")

      comm.sudo(
        bind_command.join(" "),
        error_class: Error,
        error_key: :binding_failed
      )
    end
  end
end

#binded_foldersObject



22
23
24
# File 'lib/vagrant-bindfs/bind.rb', line 22

def binded_folders
  @machine.config.bindfs.bind_folders
end

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vagrant-bindfs/bind.rb', line 10

def call(env)
  @app.call(env)
  @env = env

  @machine = env[:machine]

  unless binded_folders.empty?
    handle_bindfs_installation
    bind_folders
  end
end

#default_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant-bindfs/bind.rb', line 26

def default_options
  current_defaults = @machine.config.bindfs.default_options

  new_defaults = {}.tap do |new_default|
    current_defaults.each do |key, value|
      new_default[key.to_s.gsub("_", "-")] = value
    end
  end

  available_options.merge(
    available_shortcuts
  ).merge(
    available_flags
  ).merge(
    new_defaults
  )
end

#handle_bindfs_installationObject



142
143
144
145
146
147
148
149
150
# File 'lib/vagrant-bindfs/bind.rb', line 142

def handle_bindfs_installation
  unless @machine.guest.capability(:bindfs_installed)
    @env[:ui].warn(I18n.t("vagrant.config.bindfs.not_installed"))

    unless @machine.guest.capability(:bindfs_install)
      raise Vagrant::Bindfs::Error, :cannot_install
    end
  end
end

#normalize_options(current_options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-bindfs/bind.rb', line 44

def normalize_options(current_options)
  new_options = {}.tap do |new_option|
    current_options.each do |key, value|
      new_option[key.to_s.gsub("_", "-")] = value
    end
  end

  source = new_options.delete("source-path")
  dest = new_options.delete("dest-path")
  options = default_options.merge(new_options)

  args = [].tap do |arg|
    options.each do |key, value|
      next if key == "force-user" and options.keys.include? "owner"
      next if key == "force-user" and options.keys.include? "u"

      next if key == "force-group" and options.keys.include? "group"
      next if key == "force-group" and options.keys.include? "g"

      next if key == "mirror" and options.keys.include? "m"
      next if key == "mirror-only" and options.keys.include? "M"
      next if key == "perms" and options.keys.include? "p"

      if available_flags.keys.include? key
        arg.push "--#{key}" if value
        next
      end

      next if value.nil?

      if available_shortcuts.keys.include?(key) or additional_shortcuts.keys.include?(key)
        arg.push "-#{key} '#{value}'"
        next
      end

      if available_options.keys.include?(key) or additional_options.keys.include?(key)
        arg.push "--#{key}='#{value}'"
        next
      end
    end
  end

  [
    source,
    dest,
    args.join(" ")
  ]
end