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

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

Direct Known Subclasses

Aix, Linux

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider::Mount

#unmount_retries

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::Mount

#device_unchanged?

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec, #powershell_exec!

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, #direct_descendants, direct_descendants, #find_descendants_by_name, find_descendants_by_name, #inherited, store_inherited

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

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

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

#default_mount_optionsObject

Return appropriate default mount options according to the given os.


155
156
157
# File 'lib/chef/provider/mount/mount.rb', line 155

def default_mount_options
  linux? ? "defaults" : "rw"
end

#device_should_exist?Boolean

Returns:

  • (Boolean)

186
187
188
189
190
# File 'lib/chef/provider/mount/mount.rb', line 186

def device_should_exist?
  ( @new_resource.device != "none" ) &&
    ( not network_device? ) &&
    ( not %w{ cgroup tmpfs fuse vboxsf zfs }.include? @new_resource.fstype )
end

#disable_fsObject


178
179
180
# File 'lib/chef/provider/mount/mount.rb', line 178

def disable_fs
  edit_fstab(remove: true)
end

#enable_fsObject


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/chef/provider/mount/mount.rb', line 159

def enable_fs
  if @current_resource.enabled && mount_options_unchanged? && device_unchanged?
    logger.debug("#{@new_resource} is already enabled - nothing to do")
    return nil
  end

  if @current_resource.enabled
    # The current options don't match what we have, so
    # update the last matching entry with current option
    # and order will remain the same.
    edit_fstab
  else
    ::File.open("/etc/fstab", "a") do |fstab|
      fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}")
      logger.trace("#{@new_resource} is enabled at #{@new_resource.mount_point}")
    end
  end
end

#enabled?Boolean

Returns:

  • (Boolean)

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/provider/mount/mount.rb', line 54

def enabled?
  # Check to see if there is a entry in /etc/fstab. Last entry for a volume wins.
  enabled = false
  unless ::File.exist?("/etc/fstab")
    logger.debug "/etc/fstab not found, treating mount as not-enabled"
    return
  end
  ::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.device($1)
      @current_resource.fstype($2)
      @current_resource.options($3)
      @current_resource.dump($4.to_i)
      @current_resource.pass($5.to_i)
      logger.trace("Found mount #{device_fstab} to #{@new_resource.mount_point} in /etc/fstab")
    end
  end
  @current_resource.enabled(enabled)
end

#load_current_resourceObject


35
36
37
38
39
40
41
# 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)
  mounted?
  enabled?
end

#mount_fsObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chef/provider/mount/mount.rb', line 103

def mount_fs
  unless @current_resource.mounted
    mountable?
    command = [ "mount", "-t", @new_resource.fstype ]
    unless @new_resource.options.nil? || @new_resource.options.empty?
      command << "-o"
      command << @new_resource.options.join(",")
    end
    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)
    logger.trace("#{@new_resource} is mounted at #{@new_resource.mount_point}")
  else
    logger.debug("#{@new_resource} is already mounted at #{@new_resource.mount_point}")
  end
end

#mountable?Boolean

Returns:

  • (Boolean)

43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/provider/mount/mount.rb', line 43

def mountable?
  # 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 @new_resource.mount_point != "none" && !::File.exists?(@new_resource.mount_point)
    raise Chef::Exceptions::Mount, "Mount point #{@new_resource.mount_point} does not exist"
  end

  true
end

#mounted?Boolean

Returns:

  • (Boolean)

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/chef/provider/mount/mount.rb', line 78

def mounted?
  mounted = false

  # "mount" outputs the mount points as real paths. Convert
  # the mount_point of the resource to a real path in case it
  # contains symlinks in its parents dirs.
  real_mount_point = if ::File.exists? @new_resource.mount_point
                       ::File.realpath(@new_resource.mount_point)
                     else
                       @new_resource.mount_point
                     end

  shell_out!("mount").stdout.each_line do |line|
    case line
    when /^#{device_mount_regex}\s+on\s+#{Regexp.escape(real_mount_point)}\s/
      mounted = true
      logger.trace("Special device #{device_logstring} mounted as #{real_mount_point}")
    when %r{^([/\w])+\son\s#{Regexp.escape(real_mount_point)}\s+}
      mounted = false
      logger.trace("Special device #{$~[1]} mounted as #{real_mount_point}")
    end
  end
  @current_resource.mounted(mounted)
end

#network_device?Boolean

Returns:

  • (Boolean)

182
183
184
# File 'lib/chef/provider/mount/mount.rb', line 182

def network_device?
  @new_resource.device.include?(":") || @new_resource.device.include?("//")
end

#remount_commandObject


136
137
138
# File 'lib/chef/provider/mount/mount.rb', line 136

def remount_command
  [ "mount", "-o", "remount,#{@new_resource.options.join(",")}", @new_resource.mount_point ]
end

#remount_fsObject


140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/provider/mount/mount.rb', line 140

def remount_fs
  if @current_resource.mounted && @new_resource.supports[:remount]
    shell_out!(*remount_command)
    @new_resource.updated_by_last_action(true)
    logger.trace("#{@new_resource} is remounted at #{@new_resource.mount_point}")
  elsif @current_resource.mounted
    umount_fs
    sleep 1
    mount_fs
  else
    logger.debug("#{@new_resource} is not mounted at #{@new_resource.mount_point} - nothing to do")
  end
end

#umount_fsObject


127
128
129
130
131
132
133
134
# File 'lib/chef/provider/mount/mount.rb', line 127

def umount_fs
  if @current_resource.mounted
    shell_out!("umount", @new_resource.mount_point)
    logger.trace("#{@new_resource} is no longer mounted at #{@new_resource.mount_point}")
  else
    logger.debug("#{@new_resource} is not mounted at #{@new_resource.mount_point}")
  end
end