Sha256: 25dec47b95254700bdfcb931e18a5bf3006275652fc221719f91bdb1d1ee4b2b

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

# The logic goes like this:
#  Smartproxy name must be unique
#   smartproxy hostname is used instead of name

require 'resolv'
# TRANSLATORS: do not translate
desc <<-END_DESC
  Migrate each host's textual puppetmaster value over to a reference to a smart proxy

  The smart proxies should be declared using a FQDN before this operation. The procedure is as follows
    find all smart-proxies that support puppetca and their aliases
    for each host match the fqdn of the puppetmaster with the list
END_DESC
namespace :smartproxy do
  task :migrate => :environment do

    proxies = Feature.find_by_name("Puppet").smart_proxies
    proxies.map! do |proxy|
      class << proxy
        attr_accessor :names
        def include? hostname
          names.include? hostname
        end
        # # This creates a list of names that include the proxy's fqdn plus pupppet.domain, if puppet is an alias for host
        def load_aliases
          @names = [hostname]
          resolv = Resolv.new()
          domain = hostname.match(/^[^\.]+(.*)/)[1]
          begin
            pm_ip    = resolv.getaddress("puppet" + domain)
            proxy_ip = resolv.getaddress(hostname)
            @names.unshift("puppet" + domain) if pm_ip == proxy_ip
          rescue
          end
          self
        end
      end
      proxy.load_aliases
    end
    puts "Checking for the use of :-"
    for proxy in proxies
      puts proxy.names.join ", "
    end
    for host in Host.all
      next if host.puppetca?

      for proxy in proxies
        fqpm = host.pm_fqdn
        if proxy.include?(fqpm)
          # The proxy's name or puppet alias is the same as the fully qualified puppetmaster_name
          host.update_attribute(:puppet_proxy_id, proxy.id)
          puts "Updated #{host.name} to use the #{proxy.name} smart proxy"
        end
      end
      puts "Failed to map #{host.name}'s puppetmaster(#{host.pm_fqdn}) to a smart proxy" if host.puppet_proxy.nil?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_discovery-1.0.0 test/foreman_app/lib/tasks/smartproxy.rake
foreman_discovery-1.0.0.rc4 test/foreman_app/lib/tasks/smartproxy.rake
foreman_discovery-1.0.0.rc3 test/foreman_app/lib/tasks/smartproxy.rake
foreman_discovery-1.0.0.rc2 test/foreman_app/lib/tasks/smartproxy.rake
foreman_discovery-1.0.0.rc1 test/foreman_app/lib/tasks/smartproxy.rake