diff options
Diffstat (limited to 'lib/puppet/provider/interface')
-rw-r--r-- | lib/puppet/provider/interface/redhat.rb | 261 | ||||
-rw-r--r-- | lib/puppet/provider/interface/sunos.rb | 133 |
2 files changed, 0 insertions, 394 deletions
diff --git a/lib/puppet/provider/interface/redhat.rb b/lib/puppet/provider/interface/redhat.rb deleted file mode 100644 index 71f2a6327..000000000 --- a/lib/puppet/provider/interface/redhat.rb +++ /dev/null @@ -1,261 +0,0 @@ -require 'puppet/provider/parsedfile' -require 'erb' - -Puppet::Type.type(:interface).provide(:redhat) do - desc "Manage network interfaces on Red Hat operating systems. This provider - parses and generates configuration files in ``/etc/sysconfig/network-scripts``." - - INTERFACE_DIR = "/etc/sysconfig/network-scripts" - confine :exists => INTERFACE_DIR - defaultfor :operatingsystem => [:fedora, :centos, :redhat] - - # Create the setter/gettor methods to match the model. - mk_resource_methods - - @templates = {} - - # Register a template. - def self.register_template(name, string) - @templates[name] = ERB.new(string) - end - - # Retrieve a template by name. - def self.template(name) - @templates[name] - end - - register_template :alias, <<-ALIAS -DEVICE=<%= self.device %> -ONBOOT=<%= self.on_boot %> -BOOTPROTO=none -IPADDR=<%= self.name %> -NETMASK=<%= self.netmask %> -BROADCAST= -ALIAS - - - register_template :normal, <<-LOOPBACKDUMMY -DEVICE=<%= self.device %> -ONBOOT=<%= self.on_boot %> -BOOTPROTO=static -IPADDR=<%= self.name %> -NETMASK=<%= self.netmask %> -BROADCAST= -LOOPBACKDUMMY - - # maximum number of dummy interfaces - @max_dummies = 10 - - # maximum number of aliases per interface - @max_aliases_per_iface = 10 - - @@dummies = [] - @@aliases = Hash.new { |hash, key| hash[key] = [] } - - # calculate which dummy interfaces are currently already in - # use prior to needing to call self.next_dummy later on. - def self.instances - # parse all of the config files at once - Dir.glob("%s/ifcfg-*" % INTERFACE_DIR).collect do |file| - instance = parse(file) - - # store the existing dummy interfaces - @@dummies << instance.ifnum if (instance.interface_type == :dummy and ! @@dummies.include?(instance.ifnum)) - - @@aliases[instance.interface] << instance.ifnum if instance.interface_type == :alias - - instance - end - end - - # return the next avaliable dummy interface number, in the case where - # ifnum is not manually specified - def self.next_dummy - @max_dummies.times do |i| - unless @@dummies.include?(i.to_s) - @@dummies << i.to_s - return i.to_s - end - end - end - - # return the next available alias on a given interface, in the case - # where ifnum if not manually specified - def self.next_alias(interface) - @max_aliases_per_iface.times do |i| - unless @@aliases[interface].include?(i.to_s) - @@aliases[interface] << i.to_s - return i.to_s - end - end - end - - # base the ifnum, for dummy / loopback interface in linux - # on the last octect of the IP address - - # Parse the existing file. - def self.parse(file) - unless file =~ /-([^-]+)$/ - Puppet.warning "Could not extract interface name from %s; skipping" % file - return nil - end - name = $1 - instance = new(:name => name) - return instance unless FileTest.exist?(file) - - File.readlines(file).each do |line| - if line =~ /^(\w+)=(.+)$/ - method = $1.downcase + "=" - instance.send(method, $2) if instance.respond_to?(method) - end - end - - return instance - end - - # Prefetch our interface list, yo. - def self.prefetch(resources) - instances.each do |prov| - if resource = resources[prov.name] - resource.provider = prov - end - end - end - - def create - self.class.resource_type.validproperties.each do |property| - if value = @resource.should(property) - @property_hash[property] = value - end - end - @property_hash[:name] = @resource.name - - return (@resource.class.name.to_s + "_created").intern - end - - def destroy - File.unlink(file_path) - end - - def exists? - FileTest.exist?(file_path) - end - - # generate the content for the interface file, so this is dependent - # on whether we are adding an alias to a real interface, or a loopback - # address (also dummy) on linux. For linux it's quite involved, and we - # will use an ERB template - def generate - itype = self.interface_type == :alias ? :alias : :normal - self.class.template(itype).result(binding) - end - - # Where should the file be written out? - # This defaults to INTERFACE_DIR/ifcfg-<namevar>, but can have a - # more symbolic name by setting interface_desc in the type. - def file_path - if resource and val = resource[:interface_desc] - desc = val - else - desc = self.name - end - - self.fail("Could not get name for interface") unless desc - - if self.interface_type == :alias - return File.join(INTERFACE_DIR, "ifcfg-" + self.interface + ":" + desc) - else - return File.join(INTERFACE_DIR, "ifcfg-" + desc) - end - end - - # Use the device value to figure out all kinds of nifty things. - def device=(value) - case value - when /:/: - @property_hash[:interface], @property_hash[:ifnum] = value.split(":") - @property_hash[:interface_type] = :alias - when /^dummy/: - @property_hash[:interface_type] = :loopback - @property_hash[:interface] = "dummy" - - # take the number of the dummy interface, as this is used - # when working out whether to call next_dummy when dynamically - # creating these - @property_hash[:ifnum] = value.sub("dummy",'') - - @@dummies << @property_hash[:ifnum].to_s unless @@dummies.include?(@property_hash[:ifnum].to_s) - else - @property_hash[:interface_type] = :normal - @property_hash[:interface] = value - end - end - - # create the device name, so this based on the IP, and interface + type - def device - case @resource.should(:interface_type) - when :loopback - @property_hash[:ifnum] ||= self.class.next_dummy - return "dummy" + @property_hash[:ifnum] - when :alias - @property_hash[:ifnum] ||= self.class.next_alias(@resource[:interface]) - return @resource[:interface] + ":" + @property_hash[:ifnum] - end - end - - # LAK:NOTE This method is why this resource type has been disabled. - # The model is ridiculous -- the device name should be the interface - # name. We're 90% of the way toward making this possible, but I'm not - # taking the time to fix it. - # Set the name to our ip address. - def ipaddr=(value) - @property_hash[:name] = value - end - - - # whether the device is to be brought up on boot or not. converts - # the true / false of the type, into yes / no values respectively - # writing out the ifcfg-* files - def on_boot - case @property_hash[:onboot].to_s - when "true" - return "yes" - when "false" - return "no" - else - return "neither" - end - end - - # Mark whether the interface should be started on boot. - def on_boot=(value) - # translate whether we come up on boot to true/false - case value.downcase - when "yes": - @property_hash[:onboot] = :true - else - @property_hash[:onboot] = :false - end - end - - # Write the new file out. - def flush - # Don't flush to disk if we're removing the config. - return if self.ensure == :absent - - @property_hash.each do |name, val| - if val == :absent - raise ArgumentError, "Propety %s must be provided" % val - end - end - - File.open(file_path, "w") do |f| - f.puts generate() - end - end - - def prefetch - @property_hash = self.class.parse(file_path) - end -end - diff --git a/lib/puppet/provider/interface/sunos.rb b/lib/puppet/provider/interface/sunos.rb deleted file mode 100644 index eda21ca3d..000000000 --- a/lib/puppet/provider/interface/sunos.rb +++ /dev/null @@ -1,133 +0,0 @@ -require 'puppet/provider/parsedfile' -require 'erb' - -Puppet::Type.type(:interface).provide(:sunos) do - confine :kernel => "SunOS" - - # Add accessor/getter methods for each property/parameter; these methods - # modify @property_hash. - mk_resource_methods - - # Get a list of interface instances. - def self.instances - Dir.glob("/etc/hostname.*").collect do |file| - device = File.basename(file).split(".").pop - - instance = new(:interface => device) - instance.parse - instance - end - end - - def self.match(hash) - # see if we can match the has against an existing object - if model.find { |obj| obj.value(:name) == hash[:name] } - return obj - else - return false - end - end - - # Prefetch our interface list, yo. - def self.prefetch(resources) - instances.each do |prov| - if resource = resources[prov.name] - resource.provider = prov - end - end - end - - def initialize(*args) - @property_hash = {} - super - end - - def create - self.class.resource_type.validproperties.each do |property| - if value = resource.should(property) - @property_hash[property] = value - end - end - @property_hash[:name] = resource.name - - return (@resource.class.name.to_s + "_created").intern - end - - def destroy - File.unlink(file_path) - @property_hash[:ensure] = :absent - end - - def exists? - FileTest.exist?(file_path) - end - - # Where should the file be written out? Can be overridden by setting - # :target in the model. - def file_path - self.fail("Could not determine interface") unless interface = @property_hash[:interface] || (resource and resource[:interface]) - return File.join("/etc", "hostname." + interface) - end - - def flush - return if self.ensure == :absent - File.open(file_path, "w") { |f| f.print generate() + "\n" } - end - - # Turn our record into a line. - def generate - ret = [] - if self.interface_type == :alias - ret << "addif" - end - ret << self.name - - if self.ifopts != :absent - if @property_hash[:ifopts].is_a?(Array) - ret << @property_hash[:ifopts].join(" ") - else - ret << @property_hash[:ifopts] - end - end - - if self.onboot and ! [:absent, :false].include?(self.onboot) - ret << "up" - end - - return ret.join(" ") - end - - # Parse our interface file. - def parse - (@property_hash = {:ensure => :absent} and return) unless FileTest.exist?(file_path) - - values = File.read(file_path).chomp.split(/\s+/) - - @property_hash[:ensure] = :present - #@property_hash = {:ensure => :present} - - # Are we the primary interface? - if values[0] == "addif" - @property_hash[:interface_type] = :alias - values.shift - else - @property_hash[:interface_type] = :normal - end - - # Should the interface be up by default? - if values[-1] == "up" - @property_hash[:onboot] = :true - values.pop - else - @property_hash[:onboot] = :false - end - - # Set the interface name. - @property_hash[:name] = values.shift - - # Handle any interface options - unless values.empty? - @property_hash[:ifopts] = values.join(" ") - end - end -end |