components/puppet/files/solaris/lib/puppet/provider/pkg_publisher/solaris.rb
changeset 1409 9db4ba32e740
child 1661 c59d67c9d1d7
equal deleted inserted replaced
1408:8bc5df437e67 1409:9db4ba32e740
       
     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(: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         if enable = @resource[:enable] and enable != nil
       
   138             if enable == :true
       
   139                 flags << "--enable"
       
   140             elsif enable == :false
       
   141                 flags << "--disable"
       
   142             end
       
   143         end
       
   144 
       
   145         if searchfirst = @resource[:searchfirst] and searchfirst != ""
       
   146             if searchfirst == :true
       
   147                 flags << "--search-first"
       
   148             end
       
   149         end
       
   150 
       
   151         if sticky = @resource[:sticky] and sticky != nil
       
   152             if sticky == :true
       
   153                 flags << "--sticky"
       
   154             elsif sticky == :false
       
   155                 flags << "--non-sticky"
       
   156             end
       
   157         end
       
   158 
       
   159         if searchafter = @resource[:searchafter] and searchafter != ""
       
   160             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   161                 { |line| line.split()[0] == searchafter }
       
   162                     flags << "--search-after" << searchafter
       
   163             else
       
   164                 Puppet.warning "Publisher #{searchafter} not found.  " \
       
   165                                "Skipping --search-after argument"
       
   166             end
       
   167         end
       
   168 
       
   169         if searchbefore = @resource[:searchbefore] and searchbefore != ""
       
   170             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   171                 { |line| line.split()[0] == searchbefore }
       
   172                     flags << "--search-before" << searchbefore
       
   173             else
       
   174                 Puppet.warning "Publisher #{searchbefore} not found.  " \
       
   175                                "Skipping --search-before argument"
       
   176             end
       
   177         end
       
   178 
       
   179         if proxy = @resource[:proxy]
       
   180             flags << "--proxy" << proxy
       
   181         end
       
   182 
       
   183         if sslkey = @resource[:sslkey]
       
   184             flags << "-k" << sslkey
       
   185         end
       
   186 
       
   187         if sslcert = @resource[:sslcert]
       
   188             flags << "-c" << sslcert
       
   189         end
       
   190         flags
       
   191     end
       
   192 
       
   193     # property setters
       
   194     def sticky=(value)
       
   195         if value == :true
       
   196             pkg("set-publisher", "--sticky", @resource[:name])
       
   197         else
       
   198             pkg("set-publisher", "--non-sticky", @resource[:name])
       
   199         end
       
   200     end
       
   201 
       
   202     def enable=(value)
       
   203         if value == :true
       
   204             pkg("set-publisher", "--enable", @resource[:name])
       
   205         else
       
   206             pkg("set-publisher", "--disable", @resource[:name])
       
   207         end
       
   208     end
       
   209 
       
   210     def searchfirst=(value)
       
   211         if value == :true
       
   212             pkg("set-publisher", "--search-first", @resource[:name])
       
   213         end
       
   214     end
       
   215 
       
   216     def searchbefore=(value)
       
   217         pkg("set-publisher", "--search-before", value, @resource[:name])
       
   218     end
       
   219 
       
   220     def searchafter=(value)
       
   221         pkg("set-publisher", "--search-after", value, @resource[:name])
       
   222     end
       
   223 
       
   224     def proxy=(value)
       
   225         pkg("set-publisher", "--proxy", value, @resource[:name])
       
   226     end
       
   227 
       
   228     def sslkey=(value)
       
   229         pkg("set-publisher", "-k", value, @resource[:name])
       
   230     end
       
   231 
       
   232     def sslcert=(value)
       
   233         pkg("set-publisher", "-c", value, @resource[:name])
       
   234     end
       
   235 
       
   236     # required puppet functions
       
   237     def create
       
   238         pkg("set-publisher", build_flags, build_origin, @resource[:name])
       
   239     end
       
   240 
       
   241     def destroy
       
   242         pkg("unset-publisher", @resource[:name])
       
   243     end
       
   244 end