boxcutter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ..gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ faraday (0.8.1)
5
+ multipart-post (~> 1.1)
6
+ faraday_middleware (0.8.7)
7
+ faraday (>= 0.7.4, < 0.9)
8
+ multipart-post (1.1.5)
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ faraday
15
+ faraday_middleware
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Donald Plummer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,58 @@
1
+ # Ruby Wrapper for BlueBoxGroup API
2
+
3
+ This ruby library consumes the BlueBoxGroup APIs.
4
+
5
+ Currently supported:
6
+
7
+ - [Load Balancing](https://boxpanel.bluebox.net/public/the_vault/index.php/Load_Balancing_API)
8
+ - [Servers](https://boxpanel.bluebox.net/public/the_vault/index.php/Servers_API)
9
+
10
+ ## Example Usage
11
+
12
+ You need to specify your `BBG_API_KEY` in your environment first.
13
+
14
+ ```bash
15
+ $ export BBG_API_KEY=123456:abcdefg
16
+ ```
17
+
18
+ Where `123456` is your customer id and `abcdefg` is your api key. See
19
+ the [Optain your api
20
+ key](https://boxpanel.bluebox.net/public/the_vault/index.php/Load_Balancing_API#Step_1:_Obtain_your_API_Key)
21
+ section for more information.
22
+
23
+ Then load up the API in irb:
24
+
25
+ ```irb
26
+ $ irb -r './lib/blue_box_group'
27
+ 1.9.3p194 :001 > apps = Boxcutter::LoadBalancer::Application.all
28
+ => [#<Application id:'88888888-cccc-4444-8888-dddddddddddd' name:'example.com' ip_v4:'192.168.1.1' ip_v6:'::1'>]
29
+ ```
30
+
31
+ This is definately a work in progress. The goal is to use this wrapper
32
+ to add and remove servers from the load balancer during a deploy for
33
+ zero downtime.
34
+
35
+ ## License
36
+
37
+ (The MIT License)
38
+
39
+ Copyright © 2012 Donald Plummer
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining a
42
+ copy of this software and associated documentation files (the
43
+ ‘Software’), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be included
50
+ in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS
53
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/boxcutter.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/./version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Donald Plummer"]
6
+ gem.email = ["donald@cideasphere.com"]
7
+ gem.description = %q{Wrapper for BlueBoxGroup's API}
8
+ gem.summary = %q{Wrapper for BlueBoxGroup's API}
9
+ gem.homepage = "https://github.com/dplummer/boxcutter"
10
+
11
+ gem.files = %w[
12
+ .gitignore
13
+ Gemfile
14
+ Gemfile.lock
15
+ LICENSE
16
+ README.markdown
17
+ Rakefile
18
+ boxcutter.gemspec
19
+ lib/boxcutter.rb
20
+ lib/boxcutter/api.rb
21
+ lib/boxcutter/load_balancer.rb
22
+ lib/boxcutter/load_balancer/application.rb
23
+ lib/boxcutter/load_balancer/backend.rb
24
+ lib/boxcutter/load_balancer/machine.rb
25
+ lib/boxcutter/load_balancer/service.rb
26
+ lib/boxcutter/server.rb
27
+ lib/version.rb
28
+ ]
29
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
30
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
31
+ gem.name = "boxcutter"
32
+ gem.require_paths = ["lib"]
33
+ gem.version = Boxcutter::VERSION
34
+
35
+ gem.add_dependency('faraday')
36
+ gem.add_dependency('faraday_middleware')
37
+ end
@@ -0,0 +1,79 @@
1
+ module Boxcutter
2
+ class Api
3
+ def initialize(customer_id, api_key_secret)
4
+ @customer_id = customer_id
5
+ @api_key_secret = api_key_secret
6
+ end
7
+
8
+ def to_s
9
+ "#<Api customer_id:'#{@customer_id}' api_key_secret:'***'>"
10
+ end
11
+
12
+ def get(path)
13
+ conn.get(path).body
14
+ end
15
+
16
+ def conn
17
+ @conn ||= Faraday.new(:url => "https://boxpanel.bluebox.net/api") do |conn|
18
+ conn.response :json
19
+ conn.request :url_encoded
20
+ conn.adapter Faraday.default_adapter
21
+ conn.basic_auth(@customer_id, @api_key_secret)
22
+ end
23
+ end
24
+
25
+ def applications
26
+ get('lb_applications')
27
+ end
28
+
29
+ def application(app_id)
30
+ get("lb_applications/#{app_id}")
31
+ end
32
+
33
+ def services(app_id)
34
+ get("lb_applications/#{app_id}/lb_services")
35
+ end
36
+
37
+ def service(app_id, service_id)
38
+ get("lb_applications/#{app_id}/lb_services/#{service_id}")
39
+ end
40
+
41
+ def backends(service_id)
42
+ get("lb_services/#{service_id}/lb_backends")
43
+ end
44
+
45
+ def backend(service_id, backend_id)
46
+ get("lb_services/#{service_id}/lb_backends/#{backend_id}")
47
+ end
48
+
49
+ def machines(backend_id)
50
+ get("lb_backends/#{backend_id}/lb_machines")
51
+ end
52
+
53
+ def machine(backend_id, machine_id)
54
+ get("lb_backends/#{backend_id}/lb_machines/#{machine_id}")
55
+ end
56
+
57
+ def delete_machine(backend_id, machine_id)
58
+ conn.delete("lb_backends/#{backend_id}/lb_machines/#{machine_id}")
59
+ end
60
+
61
+ # Options are:
62
+ # port - The port the machine is serving http or https on. Defaults to the
63
+ # port the service is listening on.
64
+ # weight - The multiplier for what portion of the incoming traffic should be
65
+ # routed to this machine.
66
+ # maxconn - The maximum number of simultaneous connections allowed for this
67
+ # machine.
68
+ # backup - Only direct traffic to this node if all the other nodes are down.
69
+ def create_machine(backend_id, machine_id, options = {})
70
+ data = { 'lb_machine' => machine_id }
71
+ data['lb_options'] = options unless options.blank?
72
+ conn.post("lb_backends/#{backend_id}/lb_machines", data)
73
+ end
74
+
75
+ def servers
76
+ get("servers")
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,44 @@
1
+ module Boxcutter::LoadBalancer
2
+ class Application
3
+ def self.all
4
+ api = ::Boxcutter::Api.new(*ENV['BBG_API_KEY'].split(':'))
5
+ api.applications.map {|attrs| new(api, attrs)}
6
+ end
7
+
8
+ def self.find(id, api = nil)
9
+ api = ::Boxcutter::Api.new(*ENV['BBG_API_KEY'].split(':')) if api.nil?
10
+ new(api, api.application(id))
11
+ end
12
+
13
+ attr_reader :api
14
+
15
+ def initialize(api, attrs)
16
+ @api = api
17
+ @attrs = attrs
18
+ end
19
+
20
+ def to_s
21
+ "#<Application id:'#{id}' name:'#{name}' ip_v4:'#{ip_v4}' ip_v6:'#{ip_v6}'>"
22
+ end
23
+
24
+ def id
25
+ @attrs["id"]
26
+ end
27
+
28
+ def ip_v4
29
+ @attrs["ip_v4"]
30
+ end
31
+
32
+ def ip_v6
33
+ @attrs["ip_v6"]
34
+ end
35
+
36
+ def name
37
+ @attrs["name"]
38
+ end
39
+
40
+ def services
41
+ api.services(id).map {|attrs| Service.new(api, attrs)}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ module Boxcutter::LoadBalancer
2
+ class Backend
3
+ attr_reader :api
4
+
5
+ def initialize(api, attrs)
6
+ @api = api
7
+ @attrs = attrs
8
+ end
9
+
10
+ def to_s
11
+ "#<Backend id:'#{id}' name:'#{name}'>"
12
+ end
13
+
14
+ def name
15
+ @attrs["backend_name"]
16
+ end
17
+
18
+ def id
19
+ @attrs["id"]
20
+ end
21
+
22
+ def machines
23
+ api.machines(id).map {|attrs| Machine.new(self, api, attrs)}
24
+ end
25
+
26
+ def remove_machine(machine_id)
27
+ api.delete_machine(id, machine_id)
28
+ end
29
+
30
+ def add_machine(machine_id, options = {})
31
+ api.create_machine(id, machine_id, options)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ module Boxcutter::LoadBalancer
2
+ class Machine
3
+ attr_reader :api, :backend
4
+
5
+ def initialize(backend, api, attrs)
6
+ @backend = backend
7
+ @api = api
8
+ @attrs = attrs
9
+ end
10
+
11
+ def to_s
12
+ "#<Machine id:'#{id}' backend_id:'#{backend.id}' hostname:'#{hostname}' ip:'#{ip}' port:'#{port}'>"
13
+ end
14
+
15
+ def port
16
+ @attrs["port"]
17
+ end
18
+
19
+ def ip
20
+ @attrs["ip"]
21
+ end
22
+
23
+ def id
24
+ @attrs["id"]
25
+ end
26
+
27
+ def hostname
28
+ @attrs["hostname"]
29
+ end
30
+
31
+ def remove!
32
+ backend.remove_machine(id)
33
+ end
34
+
35
+ def add!
36
+ backend.add_machine(id)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+ module Boxcutter::LoadBalancer
2
+ class Service
3
+ attr_reader :api
4
+
5
+ def initialize(api, attrs)
6
+ @api = api
7
+ @attrs = attrs
8
+ end
9
+
10
+ def to_s
11
+ "#<Service id:'#{id}' name:'#{name}' port:'#{port}' service_type:'#{service_type}'>"
12
+ end
13
+
14
+ def name
15
+ @attrs["name"]
16
+ end
17
+
18
+ def port
19
+ @attrs["port"]
20
+ end
21
+
22
+ def id
23
+ @attrs["id"]
24
+ end
25
+
26
+ def service_type
27
+ @attrs["service_type"]
28
+ end
29
+
30
+ def backends
31
+ api.backends(id).map {|attrs| Backend.new(api, attrs)}
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module Boxcutter
2
+ module LoadBalancer
3
+ end
4
+ end
5
+
6
+ require_relative 'load_balancer/application'
7
+ require_relative 'load_balancer/service'
8
+ require_relative 'load_balancer/backend'
9
+ require_relative 'load_balancer/machine'
@@ -0,0 +1,61 @@
1
+ module Boxcutter
2
+ class Server
3
+ def self.all
4
+ api = Api.new(*ENV['BBG_API_KEY'].split(':'))
5
+ api.servers.map {|attrs| new(api, attrs)}
6
+ end
7
+
8
+ attr_reader :api
9
+
10
+ def initialize(api, attrs)
11
+ @api = api
12
+ @attrs = attrs
13
+ end
14
+
15
+ def to_s
16
+ "#<Server id:'#{id}' hostname:'#{hostname}' description:'#{description}' status:'#{status}'>"
17
+ end
18
+
19
+ def ips
20
+ @attrs["ips"].map {|ip| ip["address"]}
21
+ end
22
+
23
+ def memory
24
+ @attrs["memory"]
25
+ end
26
+
27
+ def id
28
+ @attrs["id"]
29
+ end
30
+
31
+ def storage
32
+ @attrs["storage"]
33
+ end
34
+
35
+ def location_id
36
+ @attrs["location_id"]
37
+ end
38
+
39
+ def hostname
40
+ @attrs["hostname"]
41
+ end
42
+
43
+ def description
44
+ @attrs["description"]
45
+ end
46
+
47
+ def cpu
48
+ @attrs["cpu"]
49
+ end
50
+
51
+ def status
52
+ @attrs["status"]
53
+ end
54
+
55
+ def lb_applications
56
+ @attrs["lb_applications"].map do |lb_app_attrs|
57
+ LoadBalancer::Application.find(lb_app_attrs["lb_application_id"], api)
58
+ end
59
+ end
60
+ end
61
+ end
data/lib/boxcutter.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.require(:default)
4
+
5
+ module Boxcutter
6
+ end
7
+
8
+ require_relative 'boxcutter/api'
9
+ require_relative 'boxcutter/server'
10
+ require_relative 'boxcutter/load_balancer'
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Boxcutter
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boxcutter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Donald Plummer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Wrapper for BlueBoxGroup's API
47
+ email:
48
+ - donald@cideasphere.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE
57
+ - README.markdown
58
+ - Rakefile
59
+ - boxcutter.gemspec
60
+ - lib/boxcutter.rb
61
+ - lib/boxcutter/api.rb
62
+ - lib/boxcutter/load_balancer.rb
63
+ - lib/boxcutter/load_balancer/application.rb
64
+ - lib/boxcutter/load_balancer/backend.rb
65
+ - lib/boxcutter/load_balancer/machine.rb
66
+ - lib/boxcutter/load_balancer/service.rb
67
+ - lib/boxcutter/server.rb
68
+ - lib/version.rb
69
+ homepage: https://github.com/dplummer/boxcutter
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Wrapper for BlueBoxGroup's API
93
+ test_files: []
94
+ has_rdoc: