Created
June 10, 2010 13:18
-
-
Save blasterpal/432976 to your computer and use it in GitHub Desktop.
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
module Varnish | |
require 'net/telnet' | |
#requires telnet access from deploy machine | |
def global_purge(varnish_host,varnish_port) | |
#It WILL timeout, just accept it. Varnish does not have a command prompt. | |
result = run_varnish_cmd(varnish_host,varnish_port,'url.purge .*',3) | |
if result.include?("200") | |
return [true,"Varnish purged OK."] | |
else | |
return [false,"Varnish not purged."] | |
end | |
end | |
def reload_vcl(varnish_host,varnish_port,config) | |
result = [] | |
current_config = "deployed_#{Time.now.to_i}" | |
#newly loaded configs will look like this in vcl.list: | |
# 1 deployed_1257272386 | |
result << run_varnish_cmd(varnish_host,varnish_port,"vcl.load #{current_config} #{config}",3) | |
result << run_varnish_cmd(varnish_host,varnish_port,"vcl.use #{current_config}",3) | |
@vcl_ok = false | |
result.each{|ea| @vcl_ok = true if (ea && ea.include?("VCL compiled"))} | |
if @vcl_ok | |
return [true,"Varnish config reloaded."] | |
else | |
return [false,"Varnish config NOT reloaded! Result: \n#{result}"] | |
end | |
end | |
def discard_unused_configs(varnish_host,varnish_port) | |
list_s = run_varnish_cmd(varnish_host,varnish_port,"vcl.list",3) | |
@to_delete = [] | |
configs = list_s.split("\n") | |
configs.each{|ea| @to_delete << ea.gsub('available 0 ','') if (ea && ea.include?('available'))} | |
@to_delete.each{|conf| run_varnish_cmd(varnish_host,varnish_port,"vcl.discard #{conf}",3)} | |
if @to_delete.size > 0 | |
return [true,"Varnish configs discarded: #{@to_delete.join("|")}"] | |
else | |
return [false,"Varnish configs discard attempt failed: #{@to_delete}"] | |
end | |
end | |
private | |
# run_varnish_cmd('127.1.1.1','6082','vcl.list',2) | |
def run_varnish_cmd(host,port,cmd,timeout=5) | |
@res = nil | |
begin | |
telnet_host = Net::Telnet::new("Host" => host, | |
"Port" => port.to_i, | |
"Timeout" => timeout) | |
telnet_host.cmd(cmd) { |c| @res = c} | |
#It WILL timeout, just accept it. Varnish does not have a command prompt that Ruby Telnet can expect | |
rescue Exception | |
return @res | |
end | |
return nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment