-
-
Save mk-qi/2849308 to your computer and use it in GitHub Desktop.
Ruby Varnish Purge Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Ruby varnish purger | |
require 'net/http' | |
module Net | |
class HTTP::Purge < HTTPRequest | |
METHOD='PURGE' | |
REQUEST_HAS_BODY = false | |
RESPONSE_HAS_BODY = true | |
end | |
end | |
reqarg = ARGV[0] | |
if !(reqarg =~ /^http:\/\//) | |
reqarg = "http://#{reqarg}" | |
end | |
uri = URI(reqarg) | |
if uri.scheme != "http" | |
puts "Not a HTTP URL! Try with http://....?" | |
Process.exit(1) | |
end | |
puts "Purging: #{uri.to_s}" | |
Net::HTTP.start(uri.host,uri.port) do |http| | |
presp = http.request Net::HTTP::Purge.new uri.request_uri | |
puts "#{presp.code}: #{presp.message}" | |
unless (200...400).include?(presp.code.to_i) | |
STDERR.puts "A problem occurred. PURGE was not performed." | |
Process.exit(1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment