Class: Chef::Provider::Mount::Mount

Inherits:
Chef::Provider::Mount show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/mount/mount.rb

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

Methods inherited from Chef::Provider::Mount

#action_disable, #action_enable, #action_mount, #action_remount, #action_umount

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(new_resource, run_context) ⇒ Mount

Returns a new instance of Mount.



29
30
31
32
# File 'lib/chef/provider/mount/mount.rb', line 29

def initialize(new_resource, run_context)
  super
  @real_device = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#real_deviceObject

Returns the value of attribute real_device.



33
34
35
# File 'lib/chef/provider/mount/mount.rb', line 33

def real_device
  @real_device
end

Instance Method Details

#device_should_exist?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/chef/provider/mount/mount.rb', line 167

def device_should_exist?
  @new_resource.device !~ /:/ && @new_resource.device !~ /\/\// && @new_resource.device != "tmpfs"
end

#disable_fsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef/provider/mount/mount.rb', line 144

def disable_fs
  if @current_resource.enabled
    contents = []
    
    found = false
    ::File.readlines("/etc/fstab").reverse_each do |line|
      if !found && line =~ /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}/
        found = true
        Chef::Log.debug("Removing #{@new_resource.mount_point} from fstab")
        next
      else
        contents << line
      end
    end
    
    ::File.open("/etc/fstab", "w") do |fstab|
      contents.reverse_each { |line| fstab.puts line}
    end
  else
    Chef::Log.debug("#{@new_resource.mount_point} is not enabled")
  end
end

#enable_fsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/provider/mount/mount.rb', line 127

def enable_fs
  if @current_resource.enabled && mount_options_unchanged?
    Chef::Log.debug("#{@new_resource.mount_point} is already enabled.")
    return nil
  end
  
  if @current_resource.enabled
    # The current options don't match what we have, so
    # disable, then enable.
    disable_fs
  end
  ::File.open("/etc/fstab", "a") do |fstab|
    fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? "defaults" : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}")
    Chef::Log.info("Enabled #{@new_resource.mount_point}")
  end
end

#load_current_resourceObject



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
74
75
76
77
78
79
80
81
# File 'lib/chef/provider/mount/mount.rb', line 35

def load_current_resource
  @current_resource = Chef::Resource::Mount.new(@new_resource.name)
  @current_resource.mount_point(@new_resource.mount_point)
  @current_resource.device(@new_resource.device)
  Chef::Log.debug("Checking for mount point #{@current_resource.mount_point}")

  # only check for existence of non-remote devices
  if (device_should_exist? && !::File.exists?(device_real) )
    raise Chef::Exceptions::Mount, "Device #{@new_resource.device} does not exist"
  elsif( !::File.exists?(@new_resource.mount_point) )
    raise Chef::Exceptions::Mount, "Mount point #{@new_resource.mount_point} does not exist"
  end

  # Check to see if the volume is mounted. Last volume entry wins.
  mounted = false
  shell_out!("mount").stdout.each_line do |line|
    case line
    when /^#{device_mount_regex}\s+on\s+#{Regexp.escape(@new_resource.mount_point)}/
      mounted = true
      Chef::Log.debug("Special device #{device_logstring} mounted as #{@new_resource.mount_point}")
    when /^([\/\w])+\son\s#{Regexp.escape(@new_resource.mount_point)}\s+/
      mounted = false
      Chef::Log.debug("Special device #{$~[1]} mounted as #{@new_resource.mount_point}")
    end
  end
  @current_resource.mounted(mounted)

  # Check to see if there is a entry in /etc/fstab. Last entry for a volume wins.
  enabled = false
  ::File.foreach("/etc/fstab") do |line|
    case line
    when /^[#\s]/
      next
    when /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/
      enabled = true
      @current_resource.fstype($1)
      @current_resource.options($2)
      @current_resource.dump($3.to_i)
      @current_resource.pass($4.to_i)
      Chef::Log.debug("Found mount #{device_fstab} to #{@new_resource.mount_point} in /etc/fstab")
    when /^[\/\w]+\s+#{Regexp.escape(@new_resource.mount_point)}/
      enabled = false
      Chef::Log.debug("Found conflicting mount point #{@new_resource.mount_point} in /etc/fstab")
    end
  end
  @current_resource.enabled(enabled)
end

#mount_fsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/provider/mount/mount.rb', line 83

def mount_fs
  unless @current_resource.mounted
    command = "mount -t #{@new_resource.fstype}"
    command << " -o #{@new_resource.options.join(',')}" unless @new_resource.options.nil? || @new_resource.options.empty?
    command << case @new_resource.device_type
    when :device
      " #{device_real}"
    when :label
      " -L #{@new_resource.device}"
    when :uuid
      " -U #{@new_resource.device}"
    end
    command << " #{@new_resource.mount_point}"
    shell_out!(command)
    Chef::Log.info("Mounted #{@new_resource.mount_point}")
  else
    Chef::Log.debug("#{@new_resource.mount_point} is already mounted.")
  end
end

#remount_fsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chef/provider/mount/mount.rb', line 112

def remount_fs
  if @current_resource.mounted and @new_resource.supports[:remount]
    shell_out!("mount -o remount #{@new_resource.mount_point}")

    @new_resource.updated_by_last_action(true)
    Chef::Log.info("Remounted #{@new_resource.mount_point}")
  elsif @current_resource.mounted
    umount_fs
    sleep 1
    mount_fs
  else
    Chef::Log.debug("#{@new_resource.mount_point} is not mounted.")
  end
end

#umount_fsObject



103
104
105
106
107
108
109
110
# File 'lib/chef/provider/mount/mount.rb', line 103

def umount_fs
  if @current_resource.mounted
    shell_out!("umount #{@new_resource.mount_point}")
    Chef::Log.info("Unmounted #{@new_resource.mount_point}")
  else
    Chef::Log.debug("#{@new_resource.mount_point} is not mounted.")
  end
end