Class: VagrantPlugins::ProviderIijGp::Action::SyncFolders

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::ScopedHashOverride
Defined in:
lib/vagrant-iijgp/action/sync_folders.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SyncFolders

Returns a new instance of SyncFolders.



10
11
12
13
# File 'lib/vagrant-iijgp/action/sync_folders.rb', line 10

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("vagrant_iijgp::action::sync_folders")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/vagrant-iijgp/action/sync_folders.rb', line 15

def call(env)
  @env = env

  prepare_folders

  @app.call(env)

  install_rsync_guest
  rsync
end

#install_rsync_guestObject



26
27
28
29
# File 'lib/vagrant-iijgp/action/sync_folders.rb', line 26

def install_rsync_guest
  @env[:ui].info I18n.t("vagrant_iijgp.install_rsync")
  @env[:machine].communicate.execute("yum install -y rsync")
end

#prepare_foldersObject

Prepares the synced folders by verifying they exist and creating them if they don’t.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-iijgp/action/sync_folders.rb', line 76

def prepare_folders
  synced_folders.each do |id, 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 Vagrant::Errors::SharedFolderCreateFailed,
          :path => hostpath.to_s
      end
    end
  end
end

#rsyncObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant-iijgp/action/sync_folders.rb', line 31

def rsync
  ssh_info = @env[:machine].ssh_info

  synced_folders.each do |id, data|
    hostpath  = File.expand_path(data[:hostpath], @env[:root_path])
    guestpath = data[:guestpath]

    # append tailing slash for rsync
    hostpath = hostpath + '/' if hostpath !~ /\/$/
    guestpath = guestpath + '/' if guestpath !~ /\/$/

    @env[:machine].communicate.execute(%Q[mkdir -p "#{guestpath}" && chown "#{ssh_info[:username]}" "#{guestpath}"])

    ssh_command = %Q[ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i "#{ssh_info[:private_key_path]}"]
    command = %w[rsync -avz --exclude .vagrant/ -e] +
      [ssh_command, hostpath, "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"]

    @logger.info ("-- rsync command: #{command.inspect}")
    @env[:ui].info I18n.t("vagrant_iijgp.rsync_folder", :hostpath => hostpath, :guestpath => guestpath)
    ret = Vagrant::Util::Subprocess.execute(*command)
    if ret.exit_code != 0
      raise Errors::RsyncError, :guestpath => guestpath, :hostpath => hostpath, :stderr => ret.stderr
    end
  end
end

#synced_foldersObject

Returns an actual list of IIJGP shared folders



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

def synced_folders
  {}.tap do |result|
    @env[:machine].config.vm.synced_folders.each do |id, data|
      data = scoped_hash_override(data, :iijgp)

      # Ignore NFS shared folders
      next if data[:nfs]

      # Ignore disabled shared folders
      next if data[:disabled]

      result[id] = data.dup
    end
  end
end