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

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

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider::Mount

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

Methods inherited from Chef::Provider

#action_nothing, build_from_file

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Constructor Details

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Mount

Returns a new instance of Mount.



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

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)
  @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.



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

def real_device
  @real_device
end

Instance Method Details

#disable_fsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/provider/mount/mount.rb', line 132

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+#{@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



121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/provider/mount/mount.rb', line 121

def enable_fs
  unless @current_resource.enabled
    ::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
  else
    Chef::Log.debug("#{@new_resource.mount_point} is already enabled.")
  end
end

#load_current_resourceObject



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
# File 'lib/chef/provider/mount/mount.rb', line 36

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(@new_resource.device !~ /:/ && !::File.exists?(@new_resource.device) )
    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
  popen4("mount") do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      case line
      when /^#{device_mount_regex}\s+on\s+#{@new_resource.mount_point}/
        mounted = true
        Chef::Log.debug("Special device #{device_logstring} mounted as #{@new_resource.mount_point}")
      when /^([\/\w])+\son\s#{@new_resource.mount_point}\s+/
        mounted = false
        Chef::Log.debug("Special device #{$~[1]} mounted as #{@new_resource.mount_point}")
      end
    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.read("/etc/fstab").each do |line|
    case line
    when /^[#\s]/
      next
    when /^#{device_fstab_regex}\s+#{@new_resource.mount_point}/
      enabled = true
      Chef::Log.debug("Found mount #{device_fstab} to #{@new_resource.mount_point} in /etc/fstab")
    when /^[\/\w]+\s+#{@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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/provider/mount/mount.rb', line 82

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 << " #{device_real}"
    command << " #{@new_resource.mount_point}"
    run_command(:command => command)
    Chef::Log.info("Mounted #{@new_resource.mount_point}")
  else
    Chef::Log.debug("#{@new_resource.mount_point} is already mounted.")
  end
end

#remount_fsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/provider/mount/mount.rb', line 105

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

    @new_resource.updated = 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



95
96
97
98
99
100
101
102
103
# File 'lib/chef/provider/mount/mount.rb', line 95

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