Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 160 lines (141 sloc) 4.206 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
# Alternative Augeas-based provider for sysctl type
#
# Copyright (c) 2012 Dominic Cleal
# Licensed under the Apache License, Version 2.0

require 'augeasproviders/provider'

Puppet::Type.type(:sysctl).provide(:augeas) do
  desc "Uses Augeas API to update sysctl settings"

  def self.file(resource = nil)
    file = "/etc/sysctl.conf"
    file = resource[:target] if resource and resource[:target]
    file.chomp("/")
  end

  confine :feature => :augeas
  confine :exists => file

  def self.augopen(resource = nil)
    AugeasProviders::Provider.augopen("Sysctl.lns", file(resource))
  end

  def self.instances
    aug = nil
    path = "/files#{file}"
    begin
      resources = []
      aug = augopen
      aug.match("#{path}/*").each do |spath|
        resource = {:ensure => :present}

        basename = spath.split("/")[-1]
        resource[:name] = basename.split("[")[0]
        next unless resource[:name]
        next if resource[:name] == "#comment"

        resource[:value] = aug.get("#{spath}")

        # Only match comments immediately before the entry and prefixed with
        # the sysctl name
        cmtnode = aug.match("#{path}/#comment[following-sibling::*[1][self::#{basename}]]")
        unless cmtnode.empty?
          comment = aug.get(cmtnode[0])
          if comment.match(/#{resource[:name]}:/)
            resource[:comment] = comment.sub(/^#{resource[:name]}:\s*/, "")
          end
        end

        resources << new(resource)
      end
      resources
    ensure
      aug.close if aug
    end
  end

  def exists?
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      not aug.match("#{path}/#{resource[:name]}").empty?
    ensure
      aug.close if aug
    end
  end

  def create
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      aug.set("#{path}/#{resource[:name]}", resource[:value])
      if resource[:comment]
        aug.insert("#{path}/#{resource[:name]}", "#comment", true)
        aug.set("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]]",
                "#{resource[:name]}: #{resource[:comment]}")
      end
      aug.save!
    ensure
      aug.close if aug
    end
  end

  def destroy
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      aug.rm("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]")
      aug.rm("#{path}/#{resource[:name]}")
      aug.save!
    ensure
      aug.close if aug
    end
  end

  def target
    self.class.file(resource)
  end

  def value
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      aug.get("#{path}/#{resource[:name]}")
    ensure
      aug.close if aug
    end
  end

  def value=(value)
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      aug.set("#{path}/#{resource[:name]}", value)
      aug.save!
    ensure
      aug.close if aug
    end
  end

  def comment
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      comment = aug.get("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]")
      comment.sub!(/^#{resource[:name]}:\s*/, "") if comment
      comment || ""
    ensure
      aug.close if aug
    end
  end

  def comment=(value)
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      cmtnode = "#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]"
      if value.empty?
        aug.rm(cmtnode)
      else
        if aug.match(cmtnode).empty?
          aug.insert("#{path}/#{resource[:name]}", "#comment", true)
        end
        aug.set("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]]",
                "#{resource[:name]}: #{resource[:comment]}")
      end
      aug.save!
    ensure
      aug.close if aug
    end
  end
end
Something went wrong with that request. Please try again.