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 200 lines (175 sloc) 4.793 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
# Alternative Augeas-based provider for host type (Puppet builtin)
#
# Copyright (c) 2012 Dominic Cleal
# Licensed under the Apache License, Version 2.0

require 'augeasproviders/provider'

Puppet::Type.type(:host).provide(:augeas) do
  desc "Uses Augeas API to update hosts file"

  def self.file(resource = nil)
    file = nil
    case Facter.value(:operatingsystem)
    when "Solaris"
      file = "/etc/inet/hosts"
    else
      file = "/etc/hosts"
    end
    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("Hosts.lns", file(resource))
  end

  def self.instances
    aug = nil
    path = "/files#{file}"
    begin
      resources = []
      aug = augopen
      aug.match("#{path}/*").each do |hpath|
        host = {:ensure => :present}
        host[:name] = aug.get("#{hpath}/canonical")
        next unless host[:name]
        host[:ip] = aug.get("#{hpath}/ipaddr")

        aliases = []
        aug.match("#{hpath}/alias").each do |apath|
          aliases << aug.get(apath)
        end
        host[:host_aliases] = aliases unless aliases.empty?

        comment = aug.get("#{hpath}/#comment")
        host[:comment] = comment if comment

        resources << new(host)
      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}/*[canonical = '#{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}/01/ipaddr", resource[:ip])
      aug.set("#{path}/01/canonical", resource[:name])

      if resource[:host_aliases]
        values = resource[:host_aliases]
        values = values.split unless values.is_a? Array
        values.each do |halias|
          aug.set("#{path}/01/alias[last()+1]", halias)
        end
      end

      # comment property only available in Puppet 2.7+
      if Puppet::Type.type(:host).validattr? :comment and resource[:comment]
        aug.set("#{path}/01/#comment", 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}/*[canonical = '#{resource[:name]}']")
      aug.save!
    ensure
      aug.close if aug
    end
  end

  def target
    self.class.file(resource)
  end

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

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

  def host_aliases
    aug = nil
    path = "/files#{self.class.file(resource)}"
    begin
      aug = self.class.augopen(resource)
      aliases = []
      aug.match("#{path}/*[canonical = '#{resource[:name]}']/alias").each do |apath|
        aliases << aug.get(apath)
      end
      aliases.join(" ")
    ensure
      aug.close if aug
    end
  end

  def host_aliases=(values)
    aug = nil
    path = "/files#{self.class.file(resource)}"
    entry = "#{path}/*[canonical = '#{resource[:name]}']"
    begin
      aug = self.class.augopen(resource)
      aug.rm("#{entry}/alias")

      insafter = "canonical"
      values = values.split unless values.is_a? Array
      values.each do |halias|
        aug.insert("#{entry}/#{insafter}", "alias", false)
        aug.set("#{entry}/alias[last()]", halias)
        insafter = "alias[last()]"
      end

      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)
      aug.get("#{path}/*[canonical = '#{resource[:name]}']/#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)
      if value.empty?
        aug.rm("#{path}/*[canonical = '#{resource[:name]}']/#comment")
      else
        aug.set("#{path}/*[canonical = '#{resource[:name]}']/#comment", value)
      end
      aug.save!
    ensure
      aug.close if aug
    end
  end
end
Something went wrong with that request. Please try again.