Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Committed provider, type, and markdown to redhat-routes branch

  • Loading branch information...
commit 86b9517a657ddce7a9e392ec4f534d4eb3e4958d 1 parent 900026d
@ericsakowski ericsakowski authored
View
19 README.markdown
@@ -37,6 +37,8 @@ Interface configuration
Route configuration
+ For Debian:
+
network_route { '172.17.67.0':
ensure => 'present',
gateway => '172.18.6.2',
@@ -44,6 +46,23 @@ Route configuration
netmask => '255.255.255.0',
}
+ For RedHat Enterprise:
+
+ network_route { '172.17.67.0/24':
+ ensure => 'present',
+ gateway => '10.0.2.2',
+ interface => 'eth0',
+ netmask => '24',
+ network => '172.17.67.0'
+ }
+ network_route { 'default':
+ ensure => 'present',
+ gateway => '10.0.2.2',
+ interface => 'eth0',
+ netmask => '',
+ network => 'default'
+ }
+
Create resources on the fly with the `puppet resource` command:
root@debian-6:~# puppet resource network_config eth1 ensure=present family=inet method=static ipaddress=169.254.0.1 netmask=255.255.0.0
View
115 lib/puppet/provider/network_route/redhat.rb
@@ -0,0 +1,115 @@
+require 'ipaddr'
+require 'puppetx/filemapper'
+
+Puppet::Type.type(:network_route).provide(:redhat) do
+ # RHEL network_route routes provider.
+ #
+ # This provider uses the filemapper mixin to map the routes file to a
+ # collection of network_route providers, and back.
+ #
+ # @see https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html
+
+ include PuppetX::FileMapper
+
+ desc "RHEL style routes provider"
+
+ confine :osfamily => :redhat
+
+ defaultfor :osfamily => :redhat
+
+ has_feature :provider_options
+
+ def select_file
+ "/etc/sysconfig/network-scripts/route-#{@resource[:interface]}"
+ end
+
+ def self.target_files
+ Dir["/etc/sysconfig/network-scripts/route-*"]
+ end
+
+ class MalformedRoutesError < Puppet::Error
+ def initialize(msg = nil)
+ msg = 'Malformed redhat route file; cannot instantiate network_route resources' if msg.nil?
+ super
+ end
+ end
+
+ def self.raise_malformed
+ @failed = true
+ raise MalformedRoutesError
+ end
+
+ def self.parse_file(filename, contents)
+ # Build out an empty hash for new routes for storing their configs.
+ route_hash = Hash.new do |hash, key|
+ hash[key] = {}
+ hash[key][:name] = key
+ hash[key]
+ end
+
+ lines = contents.split("\n")
+ lines.each do |line|
+ # Strip off any trailing comments
+ line.sub!(/#.*$/, '')
+
+ if line =~ /^\s*#|^\s*$/
+ # Ignore comments and blank lines
+ next
+ end
+
+ route = line.split
+ if route.length < 4
+ raise_malformed
+ end
+ if route[0] != "default"
+ # use the CIDR version of the target as :name
+ network, netmask = route[0].split("/")
+ cidr_target = "#{network}/#{IPAddr.new(netmask).to_i.to_s(2).count('1')}"
+
+ route_hash[cidr_target][:network] = network
+ route_hash[cidr_target][:netmask] = netmask
+ route_hash[cidr_target][:gateway] = route[2]
+ route_hash[cidr_target][:interface] = route[4]
+ else
+ cidr_target = "default"
+ route_hash[cidr_target][:network] = "default"
+ route_hash[cidr_target][:netmask] = ''
+ route_hash[cidr_target][:gateway] = route[2]
+ route_hash[cidr_target][:interface] = route[4]
+ end
+
+ end
+
+ route_hash.values
+ end
+
+ # Generate an array of sections
+ def self.format_file(filename, providers)
+ contents = []
+ contents << header
+ # Build routes
+ providers.sort_by(&:name).each do |provider|
+ raise Puppet::Error, "#{provider.name} does not have a network." if provider.network.nil?
+ raise Puppet::Error, "#{provider.name} does not have a netmask." if provider.netmask.nil?
+ raise Puppet::Error, "#{provider.name} does not have a gateway." if provider.gateway.nil?
+ raise Puppet::Error, "#{provider.name} does not have an interface" if provider.interface.nil?
+ if provider.network == "default"
+ contents << "#{provider.network} via #{provider.gateway} dev #{provider.interface}\n"
+ else
+ contents << "#{provider.network}/#{provider.netmask} via #{provider.gateway} dev #{provider.interface}\n"
+ end
+ end
+ contents.join
+ end
+
+ def self.header
+ str = <<-HEADER
+# HEADER: This file is is being managed by puppet. Changes to
+# HEADER: routes that are not being managed by puppet will persist;
+# HEADER: however changes to routes that are being managed by puppet will
+# HEADER: be overwritten. In addition, file order is NOT guaranteed.
+# HEADER: Last generated at: #{Time.now}
+HEADER
+ str
+ end
+end
View
5 lib/puppet/type/network_route.rb
@@ -13,10 +13,9 @@
newproperty(:network) do
isrequired
desc "The target network address"
-
validate do |value|
begin
- t = IPAddr.new(value)
+ t = IPAddr.new(value) unless value == "default"
rescue ArgumentError
fail("Invalid value for network: #{value}")
end
@@ -55,4 +54,4 @@
isrequired
desc "The interface to use for the route"
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.