components/puppet/files/solaris/lib/puppet/provider/link_properties/solaris.rb
branchs11-update
changeset 2771 8e4227dc2fc4
child 2038 b64efc6f1fe1
equal deleted inserted replaced
2767:82fe1f1d5d8d 2771:8e4227dc2fc4
       
     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(:link_properties).provide(:link_properties) do
       
    27     desc "Provider for managing Oracle Solaris link properties"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12']
       
    30     commands :dladm => '/usr/sbin/dladm'
       
    31 
       
    32     def initialize(value={})
       
    33         super(value)
       
    34         @linkprops = {}
       
    35     end
       
    36 
       
    37     def self.instances
       
    38         dladm("show-link", "-p", "-o", "link").
       
    39         split("\n").collect do |link|
       
    40             props = {}
       
    41             dladm("show-linkprop", "-c", "-o", "property,value",
       
    42                   link.strip()).split("\n").collect do |line|
       
    43                 next if line.strip.end_with? ":"
       
    44                 data = line.split(":", 2)
       
    45                 name, value = data
       
    46                 props[name] = value.delete("\\")  # remove the escape character
       
    47             end
       
    48 
       
    49             new(:name => link.strip(),
       
    50                 :ensure => :present,
       
    51                 :properties => props)
       
    52         end
       
    53     end
       
    54 
       
    55     def properties
       
    56         @property_hash[:properties]
       
    57     end
       
    58 
       
    59     def properties=(value)
       
    60         value.each do |key, value|
       
    61             dladm("set-linkprop", "-p", "#{key}=#{value}", @resource[:name])
       
    62         end
       
    63     end
       
    64 
       
    65     def add_properties
       
    66         return [] if not @linkprops
       
    67         if @linkprops
       
    68             a = []
       
    69             @linkprops.each do |key, value|
       
    70                 a << "#{key}=#{value}"
       
    71             end
       
    72         end
       
    73         properties = Array["-p", a.join(",")]
       
    74     end
       
    75 
       
    76     def exists?
       
    77         if @resource[:properties] == nil
       
    78             return :false
       
    79         end
       
    80 
       
    81         @resource[:properties].each do |key, value|
       
    82             p = exec_cmd(command(:dladm), "show-linkprop", @resource[:link],
       
    83                          "-c", "-p", key, "-o", "value")
       
    84             if p[:exit] == 1
       
    85                 Puppet.warning "Property '#{key}' not found for link " \
       
    86                                "#{@resource[:link]}"
       
    87                 next
       
    88             end
       
    89 
       
    90             if p[:out].strip != value
       
    91                 @linkprops[key] = value
       
    92             end
       
    93         end
       
    94 
       
    95         return @linkprops.empty?
       
    96     end
       
    97 
       
    98     def create
       
    99         dladm("set-linkprop", add_properties, @resource[:link])
       
   100     end
       
   101 
       
   102     def exec_cmd(*cmd)
       
   103         output = Puppet::Util::Execution.execute(cmd, :failonfail => false)
       
   104         {:out => output, :exit => $CHILD_STATUS.exitstatus}
       
   105     end
       
   106 end