Class: Component::LoadBalancer

Inherits:
Object
  • Object
show all
Defined in:
lib/component/load_balancer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoadBalancer

Returns a new instance of LoadBalancer.



8
9
10
11
# File 'lib/component/load_balancer.rb', line 8

def initialize
  @pools = []
  @configurations = []
end

Instance Attribute Details

#configurationsObject (readonly)

Returns the value of attribute configurations.



6
7
8
# File 'lib/component/load_balancer.rb', line 6

def configurations
  @configurations
end

#poolsObject (readonly)

Returns the value of attribute pools.



6
7
8
# File 'lib/component/load_balancer.rb', line 6

def pools
  @pools
end

Class Method Details

.generate_xml(interfaces) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/component/load_balancer.rb', line 61

def self.generate_xml interfaces
  return if LoadBalancer.instance.pools.nil? or LoadBalancer.instance.pools.empty?
  Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.EdgeGatewayServiceConfiguration('xmlns' => "http://www.vmware.com/vcloud/v1.5", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:schemaLocation' => "http://www.vmware.com/vcloud/v1.5 http://vendor-api-url.net/v1.5/schema/master.xsd") {
      xml.LoadBalancerService {
        xml.IsEnabled 'false'
        LoadBalancer.instance.pools.each do |pool|
          xml.Pool {
            xml.Name pool[:name]

            pool[:ports].each do |port|
              xml.ServicePort {
                xml.IsEnabled port[:enabled]
                xml.Protocol port[:port] == 443 ? "HTTPS" : "HTTP"
                xml.Algorithm 'ROUND_ROBIN'
                xml.Port port[:port]

                if port[:health_check_port]
                  xml.HealthCheckPort port[:health_check_port]
                else
                  xml.HealthCheckPort
                end

                xml.HealthCheck {
                  xml.Mode port[:health_check_mode]
                  xml.Uri port[:health_check_path]
                  xml.HealthThreshold "2"
                  xml.UnhealthThreshold "3"
                  xml.Interval "5"
                  xml.Timeout	"15"
                }
              }
            end

            xml.ServicePort {
              xml.IsEnabled 'false'
              xml.Protocol "TCP"
              xml.Algorithm 'ROUND_ROBIN'
              xml.Port
              xml.HealthCheckPort
              xml.HealthCheck {
                xml.Mode "TCP"
                xml.HealthThreshold "2"
                xml.UnhealthThreshold "3"
                xml.Interval "5"
                xml.Timeout	"15"
              }
            }

            pool[:nodes].each do |node|
              xml.Member {
                xml.IpAddress node
                xml.Weight "1"

                ["HTTP", "HTTPS"].each do |protocol|
                  xml.ServicePort {
                    xml.Protocol protocol
                    xml.Port pool[:ports].find { |port| port[:type] == protocol.downcase.to_sym }[:port]
                    xml.HealthCheckPort
                  }
                end

                xml.ServicePort {
                  xml.Protocol "TCP"
                  xml.Port
                  xml.HealthCheckPort
                }

              }
            end
            xml.Operational "false"
          }
        end

        LoadBalancer.instance.configurations.each do |configuration|
          configuration[:virtual_servers].each do |virtual_server|
            xml.VirtualServer {
              xml.IsEnabled "true"
              xml.Name virtual_server[:name]
              xml.Interface('type' => "application/vnd.vmware.vcloud.orgVdcNetwork+xml", :name => virtual_server[:interface], :href => interfaces[virtual_server[:interface]])
              xml.IpAddress virtual_server[:ip]

              configuration[:pool][:ports].each do |port|
                xml.ServiceProfile {
                  xml.IsEnabled port[:enabled]
                  xml.Protocol port[:type].to_s.upcase
                  xml.Port port[:port]
                  xml.Persistence {
                    if port[:type] == :https
                      xml.Method
                    else
                      xml.Method
                    end
                  }
                }
              end

              xml.ServiceProfile {
                xml.IsEnabled "false"
                xml.Protocol "TCP"
                xml.Port
                xml.Persistence {
                  xml.Method
                }
              }
              xml.Logging "false"
              xml.Pool configuration[:pool][:name]

            }
          end
        end
      }
    }
  end
end

.instanceObject



57
58
59
# File 'lib/component/load_balancer.rb', line 57

def self.instance
  @lb ||= LoadBalancer.new
end

Instance Method Details

#configure(name, &block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/component/load_balancer.rb', line 13

def configure(name, &block)
  @current_configuration = { :name => name, :virtual_servers => [], :pool => nil }
  @configurations << @current_configuration
  yield
ensure
  @current_pool = nil
end

#http(options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/component/load_balancer.rb', line 34

def http(options = {})
  defaults = { :enabled => true, :health_check_path => "/", :port => 80, :health_check_mode => "HTTP" }
  options = defaults.merge(options)
  @current_pool[:ports] << { :port => options[:port], :health_check_port => options[:health_check_port],
                             :health_check_path => options[:health_check_path], :enabled => options[:enabled],
                             :type => :http, :health_check_mode=>options[:health_check_mode] }
end

#https(options = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/component/load_balancer.rb', line 42

def https(options = {})
  defaults = { :enabled => true, :health_check_path => "/", :port => 443, :health_check_mode => "SSL" }
  options = defaults.merge(options)
  @current_pool[:ports] << { :port => options[:port], :health_check_port => options[:health_check_port],
                             :health_check_path => options[:health_check_path], :enabled => options[:enabled],
                             :type => :https, :health_check_mode => options[:health_check_mode] }
end

#load_balances(port, options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/component/load_balancer.rb', line 50

def load_balances(port, options = {})
  defaults = { :enabled => true, :health_check_path => "/" }
  options = defaults.merge(options)
  @current_pool[:ports] << { :port => port, :health_check_port => options[:health_check_port], :health_check_path => options[:health_check_path],
                             :enabled => options[:enabled], :type => options[:type] }
end

#pool(nodes, &block) ⇒ Object



27
28
29
30
31
32
# File 'lib/component/load_balancer.rb', line 27

def pool(nodes, &block)
  @current_pool = { :name => "#{@current_configuration[:name]} pool", :ports => [], :nodes => nodes }
  @current_configuration[:pool] = @current_pool
  @pools << @current_pool
  yield
end

#virtual_server(options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/component/load_balancer.rb', line 21

def virtual_server(options = {})
  raise "Can't add a virtual server without first having defined a pool" if @current_pool.nil?
  virtual_server = { :name => options[:name], :pool => @current_pool[:name], :ip => options[:ip], :interface => options[:interface] }
  @current_configuration[:virtual_servers] << virtual_server
end