|
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(:interface_properties).provide(:interface_properties) do |
|
27 desc "Provider for managing Oracle Solaris interface 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 @ifprops = {} |
|
35 end |
|
36 |
|
37 def self.instances |
|
38 props = {} |
|
39 |
|
40 ipadm("show-ifprop", "-c", "-o", |
|
41 "IFNAME,PROPERTY,PROTO,CURRENT").split("\n").collect do |line| |
|
42 ifname, property, proto, value = line.strip().split(":") |
|
43 fullname = ifname + "/" + proto |
|
44 if not props.has_key? fullname |
|
45 props[fullname] = {} |
|
46 end |
|
47 props[fullname][property] = value |
|
48 end |
|
49 |
|
50 interfaces = [] |
|
51 props.each do |key, value| |
|
52 interfaces << new(:name => key, |
|
53 :ensure => :present, |
|
54 :properties => value) |
|
55 end |
|
56 interfaces |
|
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-ifprop", "-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 ifname, protocol = @resource[:interface].split("/") |
|
87 |
|
88 @resource[:properties].each do |key, value| |
|
89 p = exec_cmd(command(:ipadm), "show-ifprop", "-m", protocol, |
|
90 "-p", key, "-c", "-o", "CURRENT", ifname) |
|
91 |
|
92 if p[:exit] == 1 |
|
93 Puppet.warning "Property '#{key}' not found for interface |
|
94 #{ifname}" |
|
95 next |
|
96 end |
|
97 |
|
98 if p[:out].strip != value |
|
99 @ifprops[key] = value |
|
100 end |
|
101 end |
|
102 |
|
103 return @ifprops.empty? |
|
104 end |
|
105 |
|
106 def create |
|
107 name, proto = @resource[:interface].split("/") |
|
108 @ifprops.each do |key, value| |
|
109 ipadm("set-ifprop", "-m", proto, "-p", "#{key}=#{value}", name) |
|
110 end |
|
111 end |
|
112 |
|
113 def exec_cmd(*cmd) |
|
114 output = Puppet::Util::Execution.execute(cmd, :failonfail => false) |
|
115 {:out => output, :exit => $CHILD_STATUS.exitstatus} |
|
116 end |
|
117 end |