components/puppet/files/solaris/lib/puppet/provider/protocol_properties/solaris.rb
branchs11-update
changeset 3458 4912663e9858
parent 3455 6bba35ecb6b8
child 3459 e1b247c39c22
equal deleted inserted replaced
3455:6bba35ecb6b8 3458:4912663e9858
     1 #
       
     2 # CDDL HEADER START
       
     3 #
       
     4 # The contents of this file are subject to the terms of the
       
     5 # Common Development and Distribution License (the "License").
       
     6 # You may not use this file except in compliance with the License.
       
     7 #
       
     8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9 # or http://www.opensolaris.org/os/licensing.
       
    10 # See the License for the specific language governing permissions
       
    11 # and limitations under the License.
       
    12 #
       
    13 # When distributing Covered Code, include this CDDL HEADER in each
       
    14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15 # If applicable, add the following below this CDDL HEADER, with the
       
    16 # fields enclosed by brackets "[]" replaced with your own identifying
       
    17 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    18 #
       
    19 # CDDL HEADER END
       
    20 #
       
    21 
       
    22 #
       
    23 # Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 Puppet::Type.type(:protocol_properties).provide(:protocol_properties) do
       
    27     desc "Provider for managing Oracle Solaris protocol object properties"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12']
       
    30     commands :ipadm => '/usr/sbin/ipadm'
       
    31 
       
    32     def initialize(value={})
       
    33         super(value)
       
    34         @protoprops = {}
       
    35     end
       
    36 
       
    37     def self.instances
       
    38         props = {}
       
    39 
       
    40         ipadm("show-prop", "-c", "-o", "PROTO,PROPERTY,CURRENT").split(
       
    41               "\n").collect do |line|
       
    42             protocol, property, value = line.strip().split(":")
       
    43             next if value == nil
       
    44             if not props.has_key? protocol
       
    45                 props[protocol] = {}
       
    46             end
       
    47             props[protocol][property] = value
       
    48         end
       
    49 
       
    50         protocols = []
       
    51         props.each do |key, value|
       
    52             protocols << new(:name => key,
       
    53                              :ensure => :present,
       
    54                              :properties => value)
       
    55         end
       
    56         protocols
       
    57     end
       
    58 
       
    59     def self.prefetch(resources)
       
    60         # pull the instances on the system
       
    61         props = instances
       
    62 
       
    63         # set the provider for the resource to set the property_hash
       
    64         resources.keys.each do |name|
       
    65             if provider = props.find{ |prop| prop.name == name}
       
    66                 resources[name].provider = provider
       
    67             end
       
    68         end
       
    69     end
       
    70 
       
    71     def properties
       
    72         @property_hash[:properties]
       
    73     end
       
    74 
       
    75     def properties=(value)
       
    76         value.each do |key, value|
       
    77             ipadm("set-prop", "-p", "#{key}=#{value}", @resource[:name])
       
    78         end
       
    79     end
       
    80 
       
    81     def exists?
       
    82         if @resource[:properties] == nil
       
    83             return :false
       
    84         end
       
    85 
       
    86         @resource[:properties].each do |key, value|
       
    87             p = exec_cmd(command(:ipadm), "show-prop","-c", "-p", key,
       
    88                          "-o", "CURRENT", @resource[:protocol])
       
    89             if p[:exit] == 1
       
    90                 Puppet.warning "Property '#{key}' not found for protocol " \
       
    91                                "#{@resource[:protocol]}"
       
    92                 next
       
    93             end
       
    94 
       
    95             if p[:out].strip != value
       
    96                 @protoprops[key] = value
       
    97             end
       
    98         end
       
    99 
       
   100         return @protoprops.empty?
       
   101     end
       
   102 
       
   103     def create
       
   104         @protoprops.each do |key, value|
       
   105             ipadm("set-prop", "-p", "#{key}=#{value}", @resource[:name])
       
   106         end
       
   107     end
       
   108 
       
   109     def exec_cmd(*cmd)
       
   110         output = Puppet::Util::Execution.execute(cmd, :failonfail => false)
       
   111         {:out => output, :exit => $CHILD_STATUS.exitstatus}
       
   112     end
       
   113 end