components/ruby/puppet-modules/oracle-solaris_providers/files/etc/puppet/modules/solaris_providers/lib/puppet/provider/pkg_publisher/solaris.rb
changeset 5438 c068f8c677e8
parent 4106 ac78e6a526fe
equal deleted inserted replaced
5437:449f3459d285 5438:c068f8c677e8
       
     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, 2015, 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             # strip off any trailing "/" characters
       
    44             if origin.end_with?("/")
       
    45                 origin = origin[0..-2]
       
    46             end
       
    47 
       
    48             if publishers.has_key?(name)
       
    49                 # if we've seen this publisher before, simply update the origin
       
    50                 # array
       
    51 	        if type.eql? "mirror"
       
    52                     publishers[name]["mirror"] << origin
       
    53 		else
       
    54                     publishers[name]["origin"] << origin
       
    55 		end
       
    56 
       
    57             else
       
    58                 # create a new hash entry for this publisher
       
    59                 publishers[name] = {'sticky' => sticky, 'enable' => enabled,
       
    60                                     'origin' => Array[],
       
    61 				    'mirror' => Array[]}
       
    62 					
       
    63 		if type.eql? "mirror"
       
    64                     publishers[name]["mirror"] << origin
       
    65 		else
       
    66                     publishers[name]["origin"] << origin
       
    67 		end
       
    68 
       
    69                 publishers[name]["proxy"] = proxy if proxy != "-"
       
    70 
       
    71                 index = publisher_order.index(name)
       
    72                 if index == 0
       
    73                     publishers[name]["searchfirst"] = true
       
    74                     publishers[name]["searchafter"] = nil
       
    75                 else
       
    76                     publishers[name]["searchfirst"] = nil
       
    77                     publishers[name]["searchafter"] = publisher_order[index-1]
       
    78                 end
       
    79             end
       
    80         end
       
    81         # create new entries for the system
       
    82         publishers.keys.collect do |publisher|
       
    83             new(:name => publisher,
       
    84                 :ensure => :present,
       
    85                 :sticky => publishers[publisher]["sticky"],
       
    86                 :enable => publishers[publisher]["enable"],
       
    87                 :origin => publishers[publisher]["origin"],
       
    88                 :mirror => publishers[publisher]["mirror"],
       
    89                 :proxy => publishers[publisher]["proxy"],
       
    90                 :searchfirst => publishers[publisher]["searchfirst"],
       
    91                 :searchafter => publishers[publisher]["searchafter"],
       
    92                 :searchbefore => nil,
       
    93                 :sslkey => nil,
       
    94                 :sslcert => nil)
       
    95         end
       
    96     end
       
    97 
       
    98     def self.prefetch(resources)
       
    99         # pull the instances on the system
       
   100         publishers = instances
       
   101 
       
   102         # set the provider for the resource to set the property_hash
       
   103         resources.keys.each do |name|
       
   104             if provider = publishers.find{ |publisher| publisher.name == name}
       
   105                 resources[name].provider = provider
       
   106             end
       
   107         end
       
   108     end
       
   109 
       
   110     # property getters - each getter does exactly the same thing so create
       
   111     # dynamic methods to handle them
       
   112     [:sticky, :enable, :origin, :mirror, :proxy, :searchfirst, :searchafter,
       
   113      :searchbefore, :sslkey, :sslcert].each do |property|
       
   114         define_method(property) do
       
   115             begin
       
   116                 @property_hash[property]
       
   117             rescue
       
   118             end
       
   119         end
       
   120     end
       
   121 
       
   122     def exists?
       
   123         # only compare against @resource if one is provided via manifests
       
   124         if @property_hash[:ensure] == :present and @resource[:origin] != nil
       
   125             return @property_hash[:origin].sort() == @resource[:origin].sort()
       
   126         end
       
   127         @property_hash[:ensure] == :present
       
   128     end
       
   129 
       
   130     def build_origin
       
   131         origins = []
       
   132 	
       
   133         # add all the origins from the manifest 
       
   134         if !@resource[:origin].nil?
       
   135 	   for o in @resource[:origin] do
       
   136                origins << "-g" << o
       
   137            end
       
   138 	end
       
   139 
       
   140         # add all the mirrors from the manifest 
       
   141 	if !@resource[:mirror].nil?
       
   142 	    for o in @resource[:mirror] do
       
   143                 origins << "-m" << o
       
   144             end
       
   145 	end
       
   146         # remove all the existing origins and mirrors
       
   147         if @property_hash != {}
       
   148            if !@property_hash[:origin].nil?
       
   149               for o in @property_hash[:origin] do
       
   150                   origins << "-G" << o
       
   151               end
       
   152 	   end
       
   153            if !@property_hash[:mirror].nil?
       
   154               for o in @property_hash[:mirror] do
       
   155                   origins << "-M" << o
       
   156               end
       
   157            end
       
   158         end
       
   159         origins
       
   160     end
       
   161 
       
   162     def build_flags
       
   163         flags = []
       
   164 
       
   165         if searchfirst = @resource[:searchfirst] and searchfirst != ""
       
   166             if searchfirst == :true
       
   167                 flags << "--search-first"
       
   168             end
       
   169         end
       
   170 
       
   171         if sticky = @resource[:sticky] and sticky != nil
       
   172             if sticky == :true
       
   173                 flags << "--sticky"
       
   174             elsif sticky == :false
       
   175                 flags << "--non-sticky"
       
   176             end
       
   177         end
       
   178 
       
   179         if searchafter = @resource[:searchafter] and searchafter != ""
       
   180             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   181                 { |line| line.split()[0] == searchafter }
       
   182                     flags << "--search-after" << searchafter
       
   183             else
       
   184                 Puppet.warning "Publisher #{searchafter} not found.  " \
       
   185                                "Skipping --search-after argument"
       
   186             end
       
   187         end
       
   188 
       
   189         if searchbefore = @resource[:searchbefore] and searchbefore != ""
       
   190             if pkg(:publisher, "-H", "-F", "tsv").split("\n").detect \
       
   191                 { |line| line.split()[0] == searchbefore }
       
   192                     flags << "--search-before" << searchbefore
       
   193             else
       
   194                 Puppet.warning "Publisher #{searchbefore} not found.  " \
       
   195                                "Skipping --search-before argument"
       
   196             end
       
   197         end
       
   198 
       
   199         if proxy = @resource[:proxy]
       
   200             flags << "--proxy" << proxy
       
   201         end
       
   202 
       
   203         if sslkey = @resource[:sslkey]
       
   204             flags << "-k" << sslkey
       
   205         end
       
   206 
       
   207         if sslcert = @resource[:sslcert]
       
   208             flags << "-c" << sslcert
       
   209         end
       
   210         flags
       
   211     end
       
   212 
       
   213     # property setters
       
   214     def sticky=(value)
       
   215         if value == :true
       
   216             pkg("set-publisher", "--sticky", @resource[:name])
       
   217         else
       
   218             pkg("set-publisher", "--non-sticky", @resource[:name])
       
   219         end
       
   220     end
       
   221 
       
   222     def enable=(value)
       
   223         if value == :true
       
   224             pkg("set-publisher", "--enable", @resource[:name])
       
   225         else
       
   226             pkg("set-publisher", "--disable", @resource[:name])
       
   227         end
       
   228     end
       
   229 
       
   230     def searchfirst=(value)
       
   231         if value == :true
       
   232             pkg("set-publisher", "--search-first", @resource[:name])
       
   233         end
       
   234     end
       
   235 
       
   236     def searchbefore=(value)
       
   237         pkg("set-publisher", "--search-before", value, @resource[:name])
       
   238     end
       
   239 
       
   240     def searchafter=(value)
       
   241         pkg("set-publisher", "--search-after", value, @resource[:name])
       
   242     end
       
   243 
       
   244     def proxy=(value)
       
   245         pkg("set-publisher", "--proxy", value, @resource[:name])
       
   246     end
       
   247 
       
   248     def sslkey=(value)
       
   249         pkg("set-publisher", "-k", value, @resource[:name])
       
   250     end
       
   251 
       
   252     def sslcert=(value)
       
   253         pkg("set-publisher", "-c", value, @resource[:name])
       
   254     end
       
   255 
       
   256     # required puppet functions
       
   257     def create
       
   258         pkg("set-publisher", build_flags, build_origin, @resource[:name])
       
   259         # pkg(5) does not allow for a new publisher to be set with the disabled
       
   260         # flag, so check for it after setting the publisher
       
   261         if enable = @resource[:enable] and enable != nil
       
   262             if enable == :false
       
   263                 pkg("set-publisher", "--disable", @resource[:name])
       
   264             end
       
   265         end
       
   266     end
       
   267     
       
   268     def mirror=(value)
       
   269         create
       
   270     end
       
   271 
       
   272     def destroy
       
   273         pkg("unset-publisher", @resource[:name])
       
   274     end
       
   275 end