Class: Ipecache::Plugins::Varnish

Inherits:
Plugin
  • Object
show all
Defined in:
lib/ipecache/plugins/varnish.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
# File 'lib/ipecache/plugins/varnish.rb', line 9

def perform
  safe_require 'uri'

  hosts = config.hosts
  use_ssh = config.use_ssh
  action = (config.action.upcase == "PURGE") ? "PURGE" : "BAN"
  key = config.auth_key

  if !hosts
    plugin_puts "No hosts in config file specified. Exiting..."
    exit 1
  end

  request = (action == "BAN") ? "Banning" : "Purging"

  with = (use_ssh) ? "ssh curl" : "curl";

  urls.each do |u|
    url = u.chomp.gsub(/\*$/,'').gsub(/\*/,'.*')
    plugin_puts ("#{request} #{url} through #{with}")
    hosts.each do |varnish|
      hostname = URI.parse(url).host
      path = URI.parse(url).path
      if use_ssh
        result = `ssh #{varnish} 'curl -X #{action} -s -o /dev/null -w \"%{http_code}\" --header \"X-BAN-Auth: #{key}\" --header \"Host: #{hostname}\" \"http://localhost#{path}\"'`
      else
        result = `curl -X #{action} -s -o /dev/null -w "%{http_code}" --header "X-BAN-Auth: #{key}" --header "Host: #{hostname}" "http://#{varnish}#{path}"`
      end
      if result.include?("200")
        plugin_puts "--#{request} from #{varnish} sucessfully"
      elsif result.include?("404")
        plugin_puts "--#{action} from #{varnish} not needed, asset not found"
      else
        plugin_puts_error(url,"--#{action} from #{varnish} failed with http_code = #{result}")
      end

    end
  end
end