Class: Ipecache::Plugins::ATSChef

Inherits:
Plugin
  • Object
show all
Defined in:
lib/ipecache/plugins/ats_chef.rb

Instance Method Summary collapse

Methods inherited from Plugin

#continue_on_error, #enabled?, hook, hooks, #initialize, #log_file, name, #name, #plugin_puts, #plugin_puts_error, #quiet_mode, #urls

Constructor Details

This class inherits a constructor from Ipecache::Plugins::Plugin

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ipecache/plugins/ats_chef.rb', line 9

def perform
  safe_require 'chef'
  safe_require 'uri'

  knife_file = config.knife_config || ""
  chef_role = config.chef_role

  if knife_file.empty?
    plugin_puts "No knife config file specified. Exiting..."
    exit 1
  elsif File.exists?(knife_file)
    Chef::Config.from_file(knife_file)
    rest_api = Chef::ServerAPI.new(Chef::Config[:chef_server_url])
  else
    plugin_puts "Knife config file #{knife_file} doesn't exist."
    exit 1
  end

  if !chef_role
    plugin_puts "Chef role not specified, Exiting..."
    exit 1
  end

  plugin_puts "Beginning URL Purge from ATS..."
  plugin_puts "Finding ATS Servers..."
  nodes_ats_fqdns = []
  nodes_ats = rest_api.get_rest("/search/node?q=role:#{chef_role}" )
  nodes_ats["rows"].each do |n|
    nodes_ats_fqdns <<  n.fqdn unless n.nil?
  end

  urls.each do |u|
    url = u.chomp
    plugin_puts "Purging #{url}"
    nodes_ats_fqdns.each do |ats|
      hostname = URI.parse(url).host
      path = URI.parse(url).path
      result = `ssh #{ats} 'curl -X PURGE -s -o /dev/null -w \"%{http_code}\" --header \"Host: #{hostname}\" \"http://localhost#{path}\"'`
      if result.include?("200")
        plugin_puts "--Purged from #{ats} sucessfully"
      elsif result.include?("404")
        plugin_puts "--Purge from #{ats} not needed, asset not found"
      else
        plugin_puts_error(url,"--Purge from #{ats} failed")
      end

    end
  end
end