rackspace_lb_cli 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae1e3adb72bfd3ffe3279224f4ebce9071d086d9
4
+ data.tar.gz: 8456a25fc10bf33554edf3c8342f43b5079c4126
5
+ SHA512:
6
+ metadata.gz: 8b66be819298d90e1708faa3a565e43ee629bb32988efd975f0e1ec824ac29eafb1e8c930e3d04e30f0072a94a8b92b16df9e2560efbf8b2317976bf0557ae2b
7
+ data.tar.gz: 10c36145c4ae451f36ed853bf02b011cb89d34da896fd53af886d3cd8efcc74de166d86aa97b410c4664f5760b2d880d947f94a7d0346175ab5b1a7d0dc4601f
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require 'rackspace_lb_cli'
5
+
6
+ rbcli = RackspaceLbCli.new
@@ -0,0 +1,236 @@
1
+ require 'fog' # http://www.rubydoc.info/github/fog/fog/Fog/Rackspace/LoadBalancers/LoadBalancer
2
+ require 'optparse' # http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html
3
+ require 'ostruct'
4
+
5
+ class RackspaceLbCli
6
+
7
+ def initialize
8
+ @options = cmdlineopts(ARGV)
9
+ @lb_api = init_rackspace_lb_api
10
+ @nova_api = init_rackspace_nova_api
11
+
12
+ debug "Action: #{@options.action.to_s}"
13
+
14
+ case @options.action
15
+ when :list
16
+ list_lbs
17
+ when :create
18
+ # Create LB, Add BEs
19
+ if get_lb(@options.lb_name,@options.lb_port,@options.lb_proto).nil?
20
+ lb = create_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
21
+ health_monitor_lb(lb)
22
+ end
23
+ when :destroy
24
+ # Destroy LB, BE are removed automatically
25
+ if get_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
26
+ destroy_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
27
+ end
28
+ when :add
29
+ # Add BE to LB
30
+ if lb = get_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
31
+ if @options.lb_backends
32
+ @options.lb_backends.each do | be |
33
+ be_name, be_port = be.split(':')
34
+ be_port = be_port || @options.lb_port
35
+ cldsrv = get_server(be_name)
36
+ if cldsrv
37
+ add_backend(lb, cldsrv, be_port)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ when :remove
43
+ # Remove BE from LB
44
+ if lb = get_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
45
+ if @options.lb_backends
46
+ @options.lb_backends.each do | be |
47
+ be_name, be_port = be.split(':')
48
+ be_port = be_port || @options.lb_port
49
+ cldsrv = get_server(be_name)
50
+ if cldsrv
51
+ remove_backend(lb, cldsrv, be_port)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ when :describe
57
+ # Describe LB with it's backends
58
+ if lb = get_lb(@options.lb_name,@options.lb_port,@options.lb_proto)
59
+ describe_lb(lb)
60
+ end
61
+ end
62
+ end
63
+
64
+ def debug(debugoutput)
65
+ if @options.debug
66
+ puts STDERR.puts(debugoutput)
67
+ end
68
+ end
69
+
70
+ def init_rackspace_lb_api()
71
+ Fog::Rackspace::LoadBalancers.new(
72
+ :rackspace_username => @options.rs_username,
73
+ :rackspace_api_key => @options.rs_apikey,
74
+ :rackspace_region => @options.rs_region
75
+ )
76
+ end
77
+
78
+ def init_rackspace_nova_api()
79
+ Fog::Compute.new(
80
+ :provider => 'rackspace',
81
+ :rackspace_username => @options.rs_username,
82
+ :rackspace_api_key => @options.rs_apikey,
83
+ :rackspace_region => @options.rs_region
84
+ )
85
+ end
86
+
87
+ def get_lb(lb_name, lb_port, lb_proto)
88
+ lb = @lb_api.load_balancers.all.find { |lb| lb.name == lb_name }
89
+ if lb
90
+ debug "LB object: name=#{lb.name.to_s} Port=#{lb.port.to_s} Proto=#{lb.protocol.to_s}"
91
+ if lb.port.to_s == lb_port and lb.protocol.to_s == lb_proto
92
+ return lb
93
+ end
94
+ end
95
+ return nil
96
+ end
97
+
98
+ def create_lb(lb_name, lb_port = '443', lb_proto = 'HTTPS', lb_viptype = 'PUBLIC')
99
+ if not get_lb(lb_name, lb_port, lb_proto)
100
+ lb = @lb_api.load_balancers.create(
101
+ :name => lb_name,
102
+ :protocol => lb_proto,
103
+ :port => lb_port,
104
+ :virtual_ips => [{ :type => lb_viptype }],
105
+ :nodes => []
106
+ )
107
+ lb.wait_for { ready? }
108
+ return lb
109
+ end
110
+ end
111
+
112
+ def destroy_lb(lb_name, lb_port, lb_proto)
113
+ if lb = get_lb(lb_name, lb_port, lb_proto)
114
+ lb.destroy
115
+ end
116
+ end
117
+
118
+ def health_monitor_lb(lb, monitor=['CONNECT',10,10,3])
119
+ lb.enable_health_monitor(*monitor)
120
+ lb.wait_for { ready? }
121
+ end
122
+
123
+ def get_server(server_name)
124
+ @nova_api.servers.all.find { |srv| srv.name == server_name }
125
+ end
126
+
127
+ def get_backend(lb, ip)
128
+ lb.nodes.all.find { |nde| nde.address == ip }
129
+ end
130
+
131
+ def remove_backend(lb, be)
132
+ be.destroy
133
+ lb.wait_for { ready? }
134
+ end
135
+
136
+ def add_backend(lb, be, be_port)
137
+ #if not be_port be_port = lb.port
138
+ if not lb.nodes.all.find { |nde| nde.address == be.public_ip_address }
139
+ lb.nodes.create(
140
+ :address => be.public_ip_address,
141
+ :port => be_port,
142
+ :condition => 'ENABLED'
143
+ )
144
+ lb.wait_for { ready? }
145
+ end
146
+ end
147
+
148
+ def list_lbs()
149
+ @lb_api.load_balancers.all.each { |lb| puts "#{lb.id} | #{lb.name.ljust(30)} | #{lb.port.to_s.ljust(5)} | #{lb.protocol.ljust(18)} | #{lb.virtual_ips.map{ |vip| vip.address }.join(',')}" }
150
+ end
151
+
152
+ def describe_lb(lb)
153
+ puts "Id: #{lb.id}"
154
+ puts "Port: #{lb.port}"
155
+ puts "Proto: #{lb.protocol}"
156
+ puts "VIPs: #{lb.virtual_ips.map{ |vip| vip.address }.join(',')}"
157
+ puts "Backends:"
158
+ puts "#{lb.nodes.map{ |nde| " #{nde.address}:#{nde.port}" }.join("\n")}"
159
+ end
160
+
161
+ def cmdlineopts(args)
162
+
163
+ # defaults
164
+ options = OpenStruct.new
165
+ options.action = :list
166
+ options.rs_region = ENV['RS_REGION'] || 'LON'
167
+ options.rs_apikey = ENV['RS_API_KEY']
168
+ options.rs_username = ENV['RS_USERNAME']
169
+ options.lb_port = '443'
170
+ options.lb_proto = 'HTTPS'
171
+ options.debug = false
172
+
173
+ args.options do |opts|
174
+
175
+ opts.banner = 'Usage: rackspace_lb_cli [options]'
176
+ opts.separator ''
177
+
178
+ opts.on_tail("-d", "--debug", "Send debug output to stderr. For developers.") do
179
+ options.debug = true
180
+ end
181
+
182
+ opts.separator 'Common options for all actions'
183
+ opts.on('-k','--rs-apikey rs_apikey','Rackspace Api Key [RS_API_KEY]') do |opt|
184
+ options.rs_apikey = opt
185
+ end
186
+
187
+ opts.on('-u','--rs-username rs_username','Rackspace username [RS_USERNAME]') do |opt|
188
+ options.rs_username = opt
189
+ end
190
+
191
+ opts.on('-r','--rs-region rs_region','Rackspace region, defaults to LON [RS_REGION]') do |opt|
192
+ options.rs_region = opt
193
+ end
194
+
195
+ opts.on("-h", "--help", "Show this message") do
196
+ puts opts
197
+ exit
198
+ end
199
+
200
+ opts.on('-v','--version', "Show version") do
201
+ spec = Gem::Specification::load("rackspace_lb_cli.gemspec")
202
+ puts spec.version
203
+ exit
204
+ end
205
+
206
+ opts.separator 'Options for the various actions'
207
+ opts.on('-a','--action action','[create|destroy|add|remove|list|describe]','create loadbalancer,destroy loadbalancer, add backends, remove backends','list list all loadbalancers, desc describe a specific loadbalancer') do |opt|
208
+ options.action = opt.to_sym
209
+ end
210
+
211
+ opts.on('-n','--name lb_name','A Name for the loadbalancer') do |opt|
212
+ options.lb_name = opt
213
+ end
214
+
215
+ opts.on('-p','--port port','The numeric port number, the loadbalancer should listen to') do |opt|
216
+ options.lb_port = opt
217
+ end
218
+
219
+ opts.on('-o','--protocol proto','The protocol. HTTPS is default') do |opt|
220
+ options.lb_proto = opt
221
+ end
222
+
223
+ opts.on('-b','--backends x,y,z','Comma-separated list of backends. Can be in the form backend:port') do |opt|
224
+ options.lb_backends = opt.split(',')
225
+ end
226
+
227
+ opts.parse!
228
+
229
+ raise OptionParser::MissingArgument if options.rs_username.nil?
230
+ raise OptionParser::MissingArgument if options.rs_apikey.nil?
231
+
232
+ end
233
+
234
+ return options
235
+ end
236
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rackspace_lb_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Andreas John
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fog
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.38'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.38'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fog-rackspace
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.1.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.1'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.6'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.3'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 10.3.2
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '10.3'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 10.3.2
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: " This is a cli tool for the management of loadbalancers in the Rackspace
96
+ public cloud. "
97
+ email:
98
+ - himself@derjohn.de
99
+ executables:
100
+ - rackspace_lb_cli
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - bin/rackspace_lb_cli
105
+ - lib/rackspace_lb_cli.rb
106
+ homepage: https://github.com/derjohn/rackspace_lb_cli
107
+ licenses:
108
+ - Apache-2.0
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: This is a cli tool for the management of loadbalancers in the Rackspace public
130
+ cloud.
131
+ test_files: []