Add a provider called mount that uses the output of mount to prefetch
current mounted devices and uses mount/umount to perform changes.

It may not work on any other System than Linux and even on this platform
it is not really well tested so far. Especially prefetching options and
the fstype may fail.

Signed-off-by: Stefan Schulte <[email protected]>
---
Local-branch: feature/next/mounttab
 lib/puppet/provider/mountpoint/mount.rb |  135 +++++++++++++++++++++++++++++++
 1 files changed, 135 insertions(+), 0 deletions(-)
 create mode 100755 lib/puppet/provider/mountpoint/mount.rb

diff --git a/lib/puppet/provider/mountpoint/mount.rb 
b/lib/puppet/provider/mountpoint/mount.rb
new file mode 100755
index 0000000..c9ffea1
--- /dev/null
+++ b/lib/puppet/provider/mountpoint/mount.rb
@@ -0,0 +1,135 @@
+Puppet::Type.type(:mountpoint).provide(:mount) do
+
+  commands :mount  => '/bin/mount'
+  commands :umount => '/bin/umount'
+
+  self::REGEX = case Facter.value(:operatingsystem)
+    when "Darwin"
+      /^(.*?) on (?:\/private\/var\/automount)?(\S*) 
\(([A-Za-z]*),?([^\)]*)\)$/
+    when "Solaris"
+      # when run mount -p: device blockdevice name fstype pass atboot option
+      /^(\S*)\s*\S*\s*(\S*)\s*(\S*)\s*\S*\s*\S*\s*(\S*)/
+    when "HP-UX"
+      # when run mount -p: device name fstype options dump pass
+      /^(\S*)\s*(\S*)\s*(\S*)\s*(\S*)/
+    else
+      /^(\S*) on (\S*) type (\S*) \((.*)\)$/
+  end
+  self::FIELDS = [:device,:name,:fstype,:options]
+
+  def self.parse_line(line)
+    if match = self::REGEX.match(line)
+      hash = {}
+      self::FIELDS.zip(match.captures) do |field,value|
+        if field == :options and Facter.value(:operatingsystem) == 'Darwin'
+          value.gsub!(/,\s*/,',')  # Darwin adds spaces between options
+        end
+        hash[field] = value
+        hash[:ensure] = :mounted
+        Puppet.debug hash.inspect
+      end
+      hash
+    else
+      Puppet.warning "Failed to match mount line #{line}"
+      nil
+    end
+  end
+
+  def self.instances
+    args = []
+    if ['HP-UX','Solaris'].include?(Facter.value(:operatingsystem))
+      args << '-p' # output like fstab to be able to parse fstype
+    end
+    mountpoints = []
+    mount(*args).split("\n").each do |line|
+      if mountpoint = parse_line(line)
+        mountpoints << new(mountpoint)
+      end
+    end
+    mountpoints
+  end
+
+  def self.prefetch(mountpoints)
+    instances.each do |prov|
+      if mountpoint = mountpoints[prov.name]
+        mountpoint.provider = prov
+      end
+    end
+  end
+
+  def perform_mount
+    args = []
+    args << resource[:device] if resource[:device]
+    case Facter.value(:operatingsystem)
+    when 'HP-UX', 'Solaris'
+      args << '-F' << resource[:fstype] if resource[:fstype]
+    else
+      args << '-t' << resource[:fstype] if resource[:fstype]
+    end
+    args << '-o' << resource[:options] if resource[:options]
+    args << resource[:name]
+    mount *args
+  end
+
+  def perform_remount
+    options = "remount"
+    options += ",#{resource[:options]}" if resource[:options]
+    args = []
+    args << '-o' << options
+    args << resource[:name]
+    mount *args
+  end
+
+  def perform_umount
+    umount resource[:name]
+  end
+
+  remount_sufficient = [:options]
+  [:device, :fstype, :options].each do |property|
+    define_method(property) do
+      @property_hash[property] || :absent
+    end
+    define_method("#{property}=") do |val|
+      @property_hash[property] = val
+      if remount_sufficient.include?(property)
+        @flush_need_remount = true
+      else
+        @flush_need_umount_mount = true
+      end
+    end
+  end
+
+  def remount(tryremount = true)
+    info "Remounting"
+    if resource[:remounts] == :true and tryremount
+      perform_remount
+    else
+      perform_umount
+      perform_mount
+    end
+  end
+
+  def ensure
+    @property_hash[:ensure] || :unmounted
+  end
+
+  def ensure=(value)
+    if value == :mounted
+      perform_mount
+    else
+      perform_umount
+    end
+    @property_hash[:ensure] == value
+  end
+
+  def flush
+    if @flush_need_umount_mount
+      remount(false)
+    elsif @flush_need_remount
+      remount(true)
+    end
+    @flush_need_umount_mount = false
+    @flush_need_remount = false
+  end
+
+end
-- 
1.7.5.rc3

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to