Class: VarnishBase

Inherits:
Object
  • Object
show all
Defined in:
lib/varnish_rest_api/varnish_base.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ VarnishBase

Returns a new instance of VarnishBase.



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
# File 'lib/varnish_rest_api/varnish_base.rb', line 11

def initialize(params = {})
  @mgmt_port = params.fetch(:mgmt_port, 6082)
  @port = params.fetch(:port, 4567)
  @mgmt_host = params.fetch(:mgmt_host, 'localhost')
  @instance = params.fetch(:instance, 'default')
  @use_zookeeper = params.fetch(:use_zookeeper, false)
  @zookeeper_host = params.fetch(:zookeeper_host, nil)
  @zookeeper_basenode = params.fetch(:zookeeper_basenode, '/varnish')
  @secret = params.fetch(:secret, '/etc/varnish/secret')
  @varnishadm_path = params.fetch(:varnishadm_path, '/usr/bin/varnishadm')
  @varnishadm = "#{@varnishadm_path.to_s} -T #{@mgmt_host.to_s}:#{@mgmt_port.to_s} -S #{@secret.to_s}"
  @hostname = Socket.gethostname

  puts "varnish_rest_api version #{VarnishRestApiVersion::VERSION}"
  puts "varnishadm command line: " + @varnishadm.to_s

  if @use_zookeeper && !@zookeeper_host.empty?
    puts "configured to use use zookeeper"
    begin
      @zk = ZK.new(@zookeeper_host)
    rescue RuntimeError => e
        abort "problem connecting to zookeeper host: #{@zookeeper_host}"
    end
      
    begin  
      node = @zookeeper_basenode + '/' + @hostname  
      # if the node exists, delete it since it is probably not from our zk session  
      @zk.delete(node,:ignore => [:no_node,:not_empty,:bad_version])  
      @zk.create(@zookeeper_basenode, :mode => :persistent, :ignore => [:no_node,:node_exists])
      @zk.create(node,"#{@hostname}:#{@port}", :mode => :ephemeral_sequential, :ignore => [:no_node,:not_empty,:bad_version])
    rescue ZK::Exceptions::NoNode => zke
      $stderr.puts "something went wrong creating the zookeeper node #{node}: " + zke.message
    end
  end
  
end

Instance Method Details

#ban_allObject



64
65
66
67
68
# File 'lib/varnish_rest_api/varnish_base.rb', line 64

def ban_all 
  command = varnish_major_version >= 4 ?  '\'ban req.url ~ .\'' :  '\'ban.url .\'' 
  result = output(varnishadm(command))
  JSON.pretty_generate({ command => result.empty? ? "command successful" : result })
end

Display the varnish banner



140
141
142
# File 'lib/varnish_rest_api/varnish_base.rb', line 140

def banner
  JSON.pretty_generate({ 'banner' => output(varnishadm("banner"))})
end

#list_backends(options = {}) ⇒ Object

list backends



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/varnish_rest_api/varnish_base.rb', line 100

def list_backends(options={})
 default_options = {
  :expression => nil,
  :json => false
  }
  options = default_options.merge!(options)
  backends = Array.new
  command = "backend.list"
  
  unless options[:expression].nil? || options[:expression].empty?
    command += " #{options[:expression]}"
  end
  
  varnishadm_result = varnishadm(command)
  #puts "command => " + command
  
  unless varnishadm_result[:error].empty?
    return options[:json] ? JSON.pretty_generate(varnishadm_result[:error]) : varnishadm_result[:error]
  end
  
  varnishadm_result[:output].to_a.each_with_index do |line,i|
  #varnishadm(command)[:output].to_a.each_with_index do |line,i| 
    next if i < 1
    backend = OpenStruct.new
    #server1(127.0.0.1,80) 1 probe Sick 0/5 
    #line = "server1(127.0.0.1,80) 1 probe Sick 0/5"
    components = line.squeeze.split
    host_re = /(.*?)\((.*?)\)\s+(\d+)\s+(.*?)\s+(.*)/
    match = host_re.match(line)
    backend.backend_name = match[1].to_s
    backend.host = match[2].to_s
    backend.refs = match[3].to_s
    backend.admin = match[4].to_s
    backend.health = match[5].to_s
    backends << backend
  end
  options[:json] ? JSON.pretty_generate(backends.map { |o| Hash[o.each_pair.to_a] }) : backends
end

#pingObject

ping



71
72
73
# File 'lib/varnish_rest_api/varnish_base.rb', line 71

def ping
  JSON.pretty_generate({ 'ping' => output(varnishadm("ping")) })
end

#set_health(backend, health, options = {}) ⇒ Object

backend enable/disable



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/varnish_rest_api/varnish_base.rb', line 76

def set_health(backend,health,options={})
  default_options = {
   :safe => true,
   :json => false
   }    
  options = default_options.merge!(options)
          
  unless ["sick","auto"].include?(health)
    error = { 'error' => "invalid health '#{health}'. health must be 'sick' or 'auto'"}
    return options[:json] ? JSON.pretty_generate(error) : error 
  end
  
  backends_found = list_backends(:expression => backend)

  if options[:safe] && backends_found.size > 1
    error = { 'error' => "multiple backends found for pattern '#{backend}': " +  backends_found.collect { |b| b.backend_name }.join(',')}
    return  options[:json] ? JSON.pretty_generate(error) : error
  end
  
  varnishadm("backend.set_health #{backend} #{health}")    
  list_backends(:expression => backend, :json => options[:json])
end

#statusObject

Display the current status of the varnish process



145
146
147
# File 'lib/varnish_rest_api/varnish_base.rb', line 145

def status
  JSON.pretty_generate({ 'status' => output(varnishadm("status"))}) 
end

#to_sObject



185
186
187
# File 'lib/varnish_rest_api/varnish_base.rb', line 185

def to_s
  "instance #{@instance}"
end