Module: VagrantPlugins::Parallels::Util::UnixMountHelpers

Included in:
GuestLinuxCap::MountParallelsSharedFolder, SyncedFolderCap::MountOptions
Defined in:
lib/vagrant-parallels/util/unix_mount_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



9
10
11
12
13
14
# File 'lib/vagrant-parallels/util/unix_mount_helpers.rb', line 9

def self.extended(klass)
  if !klass.class_variable_defined?(:@@logger)
    klass.class_variable_set(:@@logger, Log4r::Logger.new(klass.name.downcase))
  end
  klass.extend Vagrant::Util::Retryable
end

Instance Method Details

#detect_owner_group_ids(machine, guest_path, mount_options, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vagrant-parallels/util/unix_mount_helpers.rb', line 16

def detect_owner_group_ids(machine, guest_path, mount_options, options)
  mount_uid = find_mount_options_id("uid", mount_options)
  mount_gid = find_mount_options_id("gid", mount_options)

  if mount_uid.nil?
    if options[:owner].to_i.to_s == options[:owner].to_s
      mount_uid = options[:owner]
      self.class_variable_get(:@@logger).debug("Owner user ID (provided): #{mount_uid}")
    else
      output = {stdout: '', stderr: ''}
      uid_command = "id -u #{options[:owner]}"
      machine.communicate.execute(uid_command,
        error_class: Errors::ParallelsMountFailed,
        error_key: :parallels_mount_failed,
        command: uid_command,
        output: output[:stderr]
      ) { |type, data| output[type] << data if output[type] }
      mount_uid = output[:stdout].chomp
      self.class_variable_get(:@@logger).debug("Owner user ID (lookup): #{options[:owner]} -> #{mount_uid}")
    end
  else
    machine.ui.warn "Detected mount owner ID within mount options. (uid: #{mount_uid} guestpath: #{guest_path})"
  end

  if mount_gid.nil?
    if options[:group].to_i.to_s == options[:group].to_s
      mount_gid = options[:group]
      self.class_variable_get(:@@logger).debug("Owner group ID (provided): #{mount_gid}")
    else
      begin
        output = {stdout: '', stderr: ''}
        gid_command = "getent group #{options[:group]}"
        machine.communicate.execute(gid_command,
          error_class: Errors::ParallelsMountFailed,
          error_key: :parallels_mount_failed,
          command: gid_command,
          output: output[:stderr]
        ) { |type, data| output[type] << data if output[type] }
        mount_gid = output[:stdout].split(':').at(2).to_s.chomp
        self.class_variable_get(:@@logger).debug("Owner group ID (lookup): #{options[:group]} -> #{mount_gid}")
      rescue Vagrant::Errors::ParallelsMountFailed
        if options[:owner] == options[:group]
          self.class_variable_get(:@@logger).debug("Failed to locate group `#{options[:group]}`. Group name matches owner. Fetching effective group ID.")
          output = {stdout: ''}
          result = machine.communicate.execute("id -g #{options[:owner]}",
            error_check: false
          ) { |type, data| output[type] << data if output[type] }
          mount_gid = output[:stdout].chomp if result == 0
          self.class_variable_get(:@@logger).debug("Owner group ID (effective): #{mount_gid}")
        end
        raise unless mount_gid
      end
    end
  else
    machine.ui.warn "Detected mount group ID within mount options. (gid: #{mount_gid} guestpath: #{guest_path})"
  end
  {:gid => mount_gid, :uid => mount_uid}
end

#emit_upstart_notification(machine, guest_path) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/vagrant-parallels/util/unix_mount_helpers.rb', line 95

def emit_upstart_notification(machine, guest_path)
  # Emit an upstart event if we can
  machine.communicate.sudo <<-EOH.gsub(/^ {12}/, "")
      if command -v /sbin/init && /sbin/init 2>/dev/null --version | grep upstart; then
        /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
      fi
    EOH
end

#find_mount_options_id(id_name, mount_options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vagrant-parallels/util/unix_mount_helpers.rb', line 75

def find_mount_options_id(id_name, mount_options)
  id_line = mount_options.detect{|line| line.include?("#{id_name}=")}
  if id_line
    match = id_line.match(/,?#{Regexp.escape(id_name)}=(?<option_id>\d+),?/)
    found_id = match["option_id"]
    updated_id_line = [
      match.pre_match,
      match.post_match
    ].find_all{|string| !string.empty?}.join(',')
    if updated_id_line.empty?
      mount_options.delete(id_line)
    else
      idx = mount_options.index(id_line)
      mount_options.delete(idx)
      mount_options.insert(idx, updated_id_line)
    end
  end
  found_id
end

#merge_mount_options(base, overrides) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vagrant-parallels/util/unix_mount_helpers.rb', line 104

def merge_mount_options(base, overrides)
  base = base.join(",").split(",")
  overrides = overrides.join(",").split(",")
  b_kv = Hash[base.map{|item| item.split("=", 2) }]
  o_kv = Hash[overrides.map{|item| item.split("=", 2) }]
  merged = {}.tap do |opts|
    (b_kv.keys + o_kv.keys).uniq.each do |key|
      opts[key] = o_kv.fetch(key, b_kv[key])
    end
  end
  merged.map do |key, value|
    [key, value].compact.join("=")
  end
end