boxcutter 0.0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in ..gemspec
3
+ # Specify your gem's dependencies in boxcutter.gemspec
4
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,15 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ boxcutter (0.0.1)
5
+ faraday
6
+ trollop
7
+ yajl-ruby
8
+
1
9
  GEM
2
- remote: http://rubygems.org/
10
+ remote: https://rubygems.org/
3
11
  specs:
4
12
  faraday (0.8.1)
5
13
  multipart-post (~> 1.1)
6
- faraday_middleware (0.8.7)
7
- faraday (>= 0.7.4, < 0.9)
8
14
  multipart-post (1.1.5)
15
+ trollop (1.16.2)
16
+ yajl-ruby (1.1.0)
9
17
 
10
18
  PLATFORMS
11
19
  ruby
12
20
 
13
21
  DEPENDENCIES
14
- faraday
15
- faraday_middleware
22
+ boxcutter!
data/README.markdown CHANGED
@@ -23,7 +23,7 @@ section for more information.
23
23
  Then load up the API in irb:
24
24
 
25
25
  ```irb
26
- $ irb -r './lib/blue_box_group'
26
+ $ irb -r './lib/boxcutter'
27
27
  1.9.3p194 :001 > apps = Boxcutter::LoadBalancer::Application.all
28
28
  => [#<Application id:'88888888-cccc-4444-8888-dddddddddddd' name:'example.com' ip_v4:'192.168.1.1' ip_v6:'::1'>]
29
29
  ```
data/boxcutter.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = %q{Wrapper for BlueBoxGroup's API}
9
9
  gem.homepage = "https://github.com/dplummer/boxcutter"
10
10
 
11
- gem.files = %w[
11
+ gem.files = %w[
12
12
  .gitignore
13
13
  Gemfile
14
14
  Gemfile.lock
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  boxcutter.gemspec
19
19
  lib/boxcutter.rb
20
20
  lib/boxcutter/api.rb
21
+ lib/boxcutter/command.rb
21
22
  lib/boxcutter/load_balancer.rb
22
23
  lib/boxcutter/load_balancer/application.rb
23
24
  lib/boxcutter/load_balancer/backend.rb
@@ -33,5 +34,6 @@ Gem::Specification.new do |gem|
33
34
  gem.version = Boxcutter::VERSION
34
35
 
35
36
  gem.add_dependency('faraday')
36
- gem.add_dependency('faraday_middleware')
37
+ gem.add_dependency('trollop')
38
+ gem.add_dependency('yajl-ruby')
37
39
  end
data/lib/boxcutter/api.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'faraday'
2
+ require 'yajl'
3
+
1
4
  module Boxcutter
2
5
  class Api
3
6
  def initialize(customer_id, api_key_secret)
@@ -10,12 +13,16 @@ module Boxcutter
10
13
  end
11
14
 
12
15
  def get(path)
13
- conn.get(path).body
16
+ response = conn.get(path)
17
+ begin
18
+ Yajl::Parser.new.parse(response.body)
19
+ rescue Yajl::ParseError
20
+ {:message => response.body}
21
+ end
14
22
  end
15
23
 
16
24
  def conn
17
25
  @conn ||= Faraday.new(:url => "https://boxpanel.bluebox.net/api") do |conn|
18
- conn.response :json
19
26
  conn.request :url_encoded
20
27
  conn.adapter Faraday.default_adapter
21
28
  conn.basic_auth(@customer_id, @api_key_secret)
@@ -68,7 +75,7 @@ module Boxcutter
68
75
  # backup - Only direct traffic to this node if all the other nodes are down.
69
76
  def create_machine(backend_id, machine_id, options = {})
70
77
  data = { 'lb_machine' => machine_id }
71
- data['lb_options'] = options unless options.blank?
78
+ data['lb_options'] = options unless options.empty?
72
79
  conn.post("lb_backends/#{backend_id}/lb_machines", data)
73
80
  end
74
81
 
@@ -0,0 +1,74 @@
1
+ module Boxcutter
2
+ class Command
3
+ def initialize(logger = $stdout)
4
+ @logger = logger
5
+ end
6
+
7
+ # options:
8
+ # - :backend
9
+ # - :hostname
10
+ # - :dryrun
11
+ def remove_machine(opts = {})
12
+ backend_name = opts.fetch(:backend, 'default')
13
+ dryrun = opts.fetch(:dryrun, false)
14
+ hostname = opts.fetch(:hostname)
15
+
16
+ app = Boxcutter::LoadBalancer::Application.all.first
17
+
18
+ app.services.each do |service|
19
+ if backend = service.backends.detect {|backend| backend.name == backend_name}
20
+ if machine = backend.machines.detect {|machine| machine.hostname == hostname}
21
+ log "Removing #{machine} from #{backend}"
22
+
23
+ unless dryrun
24
+ response = machine.remove!
25
+ log "Response was: #{response.body}"
26
+ else
27
+ log "#{hostname} was not removed from the backend because --dryrun was specified"
28
+ end
29
+ else
30
+ log "Could not find '#{hostname}' on #{backend}"
31
+ end
32
+ else
33
+ log "Could not find '#{backend_name}' backend on #{service}"
34
+ end
35
+ end
36
+ end
37
+
38
+ # options:
39
+ # - :backend
40
+ # - :hostname
41
+ # - :dryrun
42
+ def add_machine(opts = {})
43
+ backend_name = opts.fetch(:backend, 'default')
44
+ dryrun = opts.fetch(:dryrun, false)
45
+ hostname = opts.fetch(:hostname)
46
+
47
+ if machine = Boxcutter::Server.find_by_hostname("#{hostname}.blueboxgrid.com")
48
+
49
+ app = Boxcutter::LoadBalancer::Application.all.first
50
+
51
+ app.services.each do |service|
52
+ if backend = service.backends.detect {|backend| backend.name == backend_name}
53
+ log "Adding machine #{machine.hostname} to backend #{backend.name}"
54
+ unless dryrun
55
+ response = backend.add_machine(machine.id, :port => 80)
56
+ log "Added #{machine.hostname} to #{backend.name} with response: #{response.body}"
57
+ else
58
+ log "#{machine.hostname} was not added to the backend because --dryrun was specified"
59
+ end
60
+ else
61
+ log "Could not find default backend on #{service}"
62
+ end
63
+ end
64
+ else
65
+ log "Could not find server '#{hostname}' on BlueBoxGroup"
66
+ end
67
+ end
68
+
69
+ private
70
+ def log(msg)
71
+ @logger.puts msg
72
+ end
73
+ end
74
+ end
@@ -27,6 +27,14 @@ module Boxcutter::LoadBalancer
27
27
  @attrs["service_type"]
28
28
  end
29
29
 
30
+ def http?
31
+ service_type == 'http'
32
+ end
33
+
34
+ def https?
35
+ service_type == 'https'
36
+ end
37
+
30
38
  def backends
31
39
  api.backends(id).map {|attrs| Backend.new(api, attrs)}
32
40
  end
@@ -3,7 +3,7 @@ module Boxcutter
3
3
  end
4
4
  end
5
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'
6
+ require 'boxcutter/load_balancer/application'
7
+ require 'boxcutter/load_balancer/service'
8
+ require 'boxcutter/load_balancer/backend'
9
+ require 'boxcutter/load_balancer/machine'
@@ -5,6 +5,10 @@ module Boxcutter
5
5
  api.servers.map {|attrs| new(api, attrs)}
6
6
  end
7
7
 
8
+ def self.find_by_hostname(hostname)
9
+ all.detect {|server| server.hostname == hostname}
10
+ end
11
+
8
12
  attr_reader :api
9
13
 
10
14
  def initialize(api, attrs)
data/lib/boxcutter.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require "rubygems"
2
2
  require "bundler"
3
- Bundler.require(:default)
3
+ Bundler.setup(:default)
4
4
 
5
5
  module Boxcutter
6
6
  end
7
7
 
8
- require_relative 'boxcutter/api'
9
- require_relative 'boxcutter/server'
10
- require_relative 'boxcutter/load_balancer'
8
+ require 'boxcutter/api'
9
+ require 'boxcutter/server'
10
+ require 'boxcutter/load_balancer'
11
+ require 'boxcutter/command'
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Boxcutter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -1,55 +1,73 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: boxcutter
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Donald Plummer
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-06-15 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2012-06-15 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
15
20
  name: faraday
16
- requirement: !ruby/object:Gem::Requirement
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
17
23
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
22
31
  type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: trollop
23
35
  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
36
+ requirement: &id002 !ruby/object:Gem::Requirement
33
37
  none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
38
45
  type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: yajl-ruby
39
49
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
50
+ requirement: &id003 !ruby/object:Gem::Requirement
41
51
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :runtime
60
+ version_requirements: *id003
46
61
  description: Wrapper for BlueBoxGroup's API
47
- email:
62
+ email:
48
63
  - donald@cideasphere.com
49
64
  executables: []
65
+
50
66
  extensions: []
67
+
51
68
  extra_rdoc_files: []
52
- files:
69
+
70
+ files:
53
71
  - .gitignore
54
72
  - Gemfile
55
73
  - Gemfile.lock
@@ -59,6 +77,7 @@ files:
59
77
  - boxcutter.gemspec
60
78
  - lib/boxcutter.rb
61
79
  - lib/boxcutter/api.rb
80
+ - lib/boxcutter/command.rb
62
81
  - lib/boxcutter/load_balancer.rb
63
82
  - lib/boxcutter/load_balancer/application.rb
64
83
  - lib/boxcutter/load_balancer/backend.rb
@@ -68,27 +87,37 @@ files:
68
87
  - lib/version.rb
69
88
  homepage: https://github.com/dplummer/boxcutter
70
89
  licenses: []
90
+
71
91
  post_install_message:
72
92
  rdoc_options: []
73
- require_paths:
93
+
94
+ require_paths:
74
95
  - lib
75
- required_ruby_version: !ruby/object:Gem::Requirement
96
+ required_ruby_version: !ruby/object:Gem::Requirement
76
97
  none: false
77
- requirements:
78
- - - ! '>='
79
- - !ruby/object:Gem::Version
80
- version: '0'
81
- required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
106
  none: false
83
- requirements:
84
- - - ! '>='
85
- - !ruby/object:Gem::Version
86
- version: '0'
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
87
114
  requirements: []
115
+
88
116
  rubyforge_project:
89
117
  rubygems_version: 1.8.24
90
118
  signing_key:
91
119
  specification_version: 3
92
120
  summary: Wrapper for BlueBoxGroup's API
93
121
  test_files: []
122
+
94
123
  has_rdoc: