components/puppet/files/solaris/lib/puppet/provider/pkg_publisher/solaris.rb
branchs11u2-sru
changeset 3460 5c5af6e58474
parent 3457 6358358b4186
child 3461 1240b4c4e38d
equal deleted inserted replaced
3457:6358358b4186 3460:5c5af6e58474
     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, 2014, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 Puppet::Type.type(:pkg_publisher).provide(:pkg_publisher) do
       
    27     desc "Provider for Solaris publishers"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12']
       
    30     commands :pkg => '/usr/bin/pkg'
       
    31 
       
    32     def self.instances
       
    33         publishers = {}
       
    34         publisher_order = []
       
    35         pkg(:publisher, "-H", "-F", "tsv").split("\n").collect do |line|
       
    36             name, sticky, syspub, enabled, type, status, origin, proxy = \
       
    37                 line.split("\t")
       
    38 
       
    39             # set the order of the publishers
       
    40             if not publisher_order.include?("name")
       
    41                 publisher_order << name
       
    42             end
       
    43 
       
    44             # strip off any trailing "/" characters
       
    45             if origin.end_with?("/")
       
    46                 origin = origin[0..-2]
       
    47             end
       
    48 
       
    49             if publishers.has_key?(name)
       
    50                 # if we've seen this publisher before, simply update the origin
       
    51                 # array
       
    52                 publishers[name]["origin"] << origin
       
    53             else
       
    54                 # create a new hash entry for this publisher
       
    55                 publishers[name] = {'sticky' => sticky, 'enable' => enabled,
       
    56                                     'origin' => Array[origin]}
       
    57                 publishers[name]["proxy"] = proxy if proxy != "-"
       
    58 
       
    59                 index = publisher_order.index(name)
       
    60                 if index == 0
       
    61                     publishers[name]["searchfirst"] = true
       
    62                     publishers[name]["searchafter"] = nil
       
    63                 else
       
    64                     publishers[name]["searchfirst"] = nil
       
    65                     publishers[name]["searchafter"] = publisher_order[index-1]
       
    66                 end
       
    67             end
       
    68         end
       
    69 
       
    70         # create new entries for the system
       
    71         publishers.keys.collect do |publisher|
       
    72             new(:name => publisher,
       
    73                 :ensure => :present,
       
    74                 :sticky => publishers[publisher]["sticky"],
       
    75                 :enable => publishers[publisher]["enable"],
       
    76                 :origin => publishers[publisher]["origin"],
       
    77                 :proxy => publishers[publisher]["proxy"],
       
    78                 :searchfirst => publishers[publisher]["searchfirst"],
       
    79                 :searchafter => publishers[publisher]["searchafter"],
       
    80                 :searchbefore => nil,
       
    81                 :sslkey => nil,
       
    82                 :sslcert => nil)
       
    83         end
       
    84     end
       
    85 
       
    86     def self.prefetch(resources)
       
    87         # pull the instances on the system
       
    88         publishers = instances
       
    89 
       
    90         # set the provider for the resource to set the property_hash
       
    91         resources.keys.each do |name|
       
    92             if provider = publishers.find{ |publisher| publisher.name == name}
       
    93                 resources[name].provider = provider
       
    94             end
       
    95         end
       
    96     end
       
    97 
       
    98     # property getters - each getter does exactly the same thing so create
       
    99     # dynamic methods to handle them
       
   100     [:sticky, :enable, :origin, :proxy, :searchfirst, :searchafter,
       
   101      :searchbefore, :sslkey, :sslcert].each do |property|
       
   102         define_method(property) do
       
   103             begin
       
   104                 @property_hash[property]
       
   105             rescue
       
   106             end
       
   107         end
       
   108     end
       
   109 
       
   110     def exists?
       
   111         # only compare against @resource if one is provided via manifests
       
   112         if @property_hash[:ensure] == :present and @resource[:origin] != nil
       
   113             return @property_hash[:origin].sort() == @resource[:origin].sort()
       
   114         end
       
   115         @property_hash[:ensure] == :present
       
   116     end
       
   117 
       
   118     def build_origin
       
   119         origins = []
       
   120         
       
   121         # add all the origins from the manifest
       
   122         for o in @resource[:origin] do
       
   123             origins << "-g" << o
       
   124         end
       
   125 
       
   126         # remove all the existing origins
       
   127         if @property_hash != {}
       
   128             for o in @property_hash[:origin] do
       
   129                 origins << "-G" << o
       
   130             end
       
   131         end
       
   132         origins
       
   133     end
       
   134 
       
   135     def build_flags
       
   136         flags = []
       
   137 
       
   138         if searchfirst = @resource[:searchfirst] and searchfirst != ""
       
   139             if searchfirst == :true
       
   140                 flags << "--search-first"
       
   141             end
       
   142         end
       
   143 
       
   144         if sticky = @resource[:sticky] and sticky != nil
       
   145             if sticky == :true
       
   146                 flags << "--sticky"
       
   147             elsif sticky == :false
       
   148                 flags << "--non-sticky"
       
   149             end
       
   150         end
       
   151 
       
   152         if searchafter = @resource[:searchafter] and searchafter != ""
       
   153             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   154                 { |line| line.split()[0] == searchafter }
       
   155                     flags << "--search-after" << searchafter
       
   156             else
       
   157                 Puppet.warning "Publisher #{searchafter} not found.  " \
       
   158                                "Skipping --search-after argument"
       
   159             end
       
   160         end
       
   161 
       
   162         if searchbefore = @resource[:searchbefore] and searchbefore != ""
       
   163             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   164                 { |line| line.split()[0] == searchbefore }
       
   165                     flags << "--search-before" << searchbefore
       
   166             else
       
   167                 Puppet.warning "Publisher #{searchbefore} not found.  " \
       
   168                                "Skipping --search-before argument"
       
   169             end
       
   170         end
       
   171 
       
   172         if proxy = @resource[:proxy]
       
   173             flags << "--proxy" << proxy
       
   174         end
       
   175 
       
   176         if sslkey = @resource[:sslkey]
       
   177             flags << "-k" << sslkey
       
   178         end
       
   179 
       
   180         if sslcert = @resource[:sslcert]
       
   181             flags << "-c" << sslcert
       
   182         end
       
   183         flags
       
   184     end
       
   185 
       
   186     # property setters
       
   187     def sticky=(value)
       
   188         if value == :true
       
   189             pkg("set-publisher", "--sticky", @resource[:name])
       
   190         else
       
   191             pkg("set-publisher", "--non-sticky", @resource[:name])
       
   192         end
       
   193     end
       
   194 
       
   195     def enable=(value)
       
   196         if value == :true
       
   197             pkg("set-publisher", "--enable", @resource[:name])
       
   198         else
       
   199             pkg("set-publisher", "--disable", @resource[:name])
       
   200         end
       
   201     end
       
   202 
       
   203     def searchfirst=(value)
       
   204         if value == :true
       
   205             pkg("set-publisher", "--search-first", @resource[:name])
       
   206         end
       
   207     end
       
   208 
       
   209     def searchbefore=(value)
       
   210         pkg("set-publisher", "--search-before", value, @resource[:name])
       
   211     end
       
   212 
       
   213     def searchafter=(value)
       
   214         pkg("set-publisher", "--search-after", value, @resource[:name])
       
   215     end
       
   216 
       
   217     def proxy=(value)
       
   218         pkg("set-publisher", "--proxy", value, @resource[:name])
       
   219     end
       
   220 
       
   221     def sslkey=(value)
       
   222         pkg("set-publisher", "-k", value, @resource[:name])
       
   223     end
       
   224 
       
   225     def sslcert=(value)
       
   226         pkg("set-publisher", "-c", value, @resource[:name])
       
   227     end
       
   228 
       
   229     # required puppet functions
       
   230     def create
       
   231         pkg("set-publisher", build_flags, build_origin, @resource[:name])
       
   232         # pkg(5) does not allow for a new publisher to be set with the disabled
       
   233         # flag, so check for it after setting the publisher
       
   234         if enable = @resource[:enable] and enable != nil
       
   235             if enable == :false
       
   236                 pkg("set-publisher", "--disable", @resource[:name])
       
   237             end
       
   238         end
       
   239     end
       
   240 
       
   241     def destroy
       
   242         pkg("unset-publisher", @resource[:name])
       
   243     end
       
   244 end