Class: VagrantBindfs::Middleware

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

Overview

A Vagrant middleware which use bindfs to relocate directories inside a VM and change their owner, group and permission.

Constant Summary collapse

@@options =

Options

{
  :owner                => 'vagrant',
  :group                => 'vagrant',
  :perms                => 'u=rwX:g=rD:o=rD',
  :mirror               => nil,
  :'mirror-only'        => nil,
  :'create-for-user'    => nil,
  :'create-for-group'   => nil,
  :'create-with-perms'  => nil,
}
@@flags =

Flags

{
  :'no-allow-other'     => false,
  :'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,
  :'ctime-from-mtime'   => false,
}

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Middleware

Returns a new instance of Middleware.



39
40
41
42
# File 'lib/vagrant-bindfs/middleware.rb', line 39

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

Instance Method Details

#bind_foldersObject



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
92
93
94
95
96
97
98
99
100
101
# File 'lib/vagrant-bindfs/middleware.rb', line 58

def bind_folders
  @env["vm"].ssh.execute do |ssh|
    
    # Check if bindfs is installed on VM
    begin
      ssh.exec!("sudo bindfs --help")
    rescue Vagrant::Errors::VagrantError => e
      @env.ui.error e
      @env.ui.error I18n.t("vagrant.actions.vm.bind_folders.bindfs_not_installed")
      return
    end
    
    @env.ui.info I18n.t("vagrant.actions.vm.bind_folders.binding")
    binded_folders.each do |opts|

      path     = opts.delete(:path)
      bindpath = opts.delete(:bindpath)
      opts     = default_options.merge(opts)

      args = []
      opts.each do |key, value|
        if @@flags.key?(key)
          args << "--#{key.to_s}" if !!value 
        else
          args << "--#{key.to_s}=#{value}" if !value.nil?
        end
      end
      args = " #{args.join(" ")}"
      
      begin
        ssh.exec!("sudo mkdir -p #{bindpath}")
        ssh.exec!("sudo bindfs#{args} #{path} #{bindpath}")
        @env.ui.info I18n.t("vagrant.actions.vm.bind_folders.binding_entry",
          :path     => path,
          :bindpath => bindpath
          )
      rescue Vagrant::Errors::VagrantError => e
        @env.ui.error e
        @env.ui.error I18n.t("vagrant.actions.vm.bind_folders.bindfs_command_fail")
      end
      
    end
  end
end

#binded_foldersObject



50
51
52
# File 'lib/vagrant-bindfs/middleware.rb', line 50

def binded_folders
  @env.env.config.bindfs.binded_folders
end

#call(env) ⇒ Object



44
45
46
47
48
# File 'lib/vagrant-bindfs/middleware.rb', line 44

def call(env)
  @env = env
  @app.call(env)
  bind_folders unless binded_folders.empty?
end

#default_optionsObject



54
55
56
# File 'lib/vagrant-bindfs/middleware.rb', line 54

def default_options
  @@options.merge(@@flags).merge(@env.env.config.bindfs.default_options || {})
end