berkshelf-api 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f15f60522d71ee03d537c45600b4287520edc537
4
- data.tar.gz: 4881bdf327b30c38f7ca1be07c5a9d94ee3b5bd2
3
+ metadata.gz: 213f69bfd58c7200d8d779788f6774828087cf1b
4
+ data.tar.gz: 8ebeaff3fae7fca72a3e5d1f6a88690c80beb7fd
5
5
  SHA512:
6
- metadata.gz: b027a279599e8e893fc89fe80278955bdb2333682c6c76935dc06286d8b68261f00ed0bac6b4867f920e5dbcc60ab0fa885e08f1666b08f13c346097c65b53cd
7
- data.tar.gz: cf69c2c17c9e784a57dffb3b0b7ea0f2e92fcac349bf2e6ef7fd89e133eae347f8340bd245750c7b9950b44eba53ec3689fd7d671c11adca974c7468a464e541
6
+ metadata.gz: 7a30703cd2c06d4b43c6e0bf80c4846d74120e13fa105ee5d7573c6818aac8d1bc1d8d71ea9c39e85fe7f48a50955989d13c4131fb9b9d945d1d27f6b1474133
7
+ data.tar.gz: d3cd8e1029ddd4a73291e50feb24c3d635195a24f0531445250b73eb6a7b45a699dfc00a2f2863c6ba0a49028d95ae65c118dc3e15e0e95300b969a9e4325865
data/README.md CHANGED
@@ -21,6 +21,38 @@ Ruby 1.9 mode is required on all interpreters.
21
21
 
22
22
  Ruby 1.9.1 and 1.9.2 are not officially supported. If you encounter problems, please upgrade to Ruby 2.0 or 1.9.3.
23
23
 
24
+ ## Configuring Endpoints
25
+
26
+ Which endpoints to index can be configured by editing the JSON configuration file (by default at: `~/.berkshelf/api-server/config.json`).
27
+
28
+ ### Opscode Community Site
29
+
30
+ {
31
+ "endpoints": [
32
+ {
33
+ type: "opscode",
34
+ options: {
35
+ url: 'http://cookbooks.opscode.com/api/v1'
36
+ }
37
+ }
38
+ ]
39
+ }
40
+
41
+ ### Chef Server
42
+
43
+ {
44
+ "endpoints": [
45
+ {
46
+ "type": "chef_server",
47
+ "options": {
48
+ "url": "https://api.opscode.com/organizations/vialstudios",
49
+ "client_name": "berkshelf",
50
+ "client_key": "/etc/berkshelf/api-server/client.pem"
51
+ }
52
+ }
53
+ ]
54
+ }
55
+
24
56
  ## Contributing
25
57
 
26
58
  1. Fork it
@@ -30,6 +30,7 @@ module Berkshelf::API
30
30
 
31
31
  def_delegators :registry, :[], :[]=
32
32
 
33
+ # @return [Berkshelf::API::Config]
33
34
  def config
34
35
  @config ||= begin
35
36
  Berkshelf::API::Config.from_file(Berkshelf::API::Config.default_path)
@@ -38,6 +39,37 @@ module Berkshelf::API
38
39
  end
39
40
  end
40
41
 
42
+ # @param [Berkshelf::API::Config] config
43
+ def set_config(config)
44
+ @config = config
45
+ end
46
+
47
+ # Configure the application
48
+ #
49
+ # @option options [String] :config_file
50
+ # filepath to a configuration file to use
51
+ # @option options [String, Fixnum] :log_location (STDOUT)
52
+ # @option options [String, nil] :log_level ("INFO")
53
+ # - "DEBUG
54
+ # - "INFO"
55
+ # - "WARN"
56
+ # - "ERROR"
57
+ # - "FATAL"
58
+ #
59
+ # @raise [Berkshelf::API::ConfigNotFoundError]
60
+ #
61
+ # @return [Berkshelf::API::Config]
62
+ def configure(options = {})
63
+ unless options[:config_file].nil?
64
+ set_config Berkshelf::API::Config.from_file(options[:config_file])
65
+ end
66
+
67
+ configure_logger(options)
68
+ config
69
+ rescue Buff::Errors::ConfigNotFound => ex
70
+ raise ConfigNotFoundError, ex
71
+ end
72
+
41
73
  # @option options [String, Fixnum] :log_location (STDOUT)
42
74
  # @option options [String, nil] :log_level ("INFO")
43
75
  # - "DEBUG
@@ -49,6 +81,16 @@ module Berkshelf::API
49
81
  Logging.init(level: options[:log_level], location: options[:log_location])
50
82
  end
51
83
 
84
+ # @return [String]
85
+ def home_path
86
+ ENV['BERKSHELF_API_PATH'] || config.home_path
87
+ end
88
+
89
+ # Retrieve the running instance of the Application
90
+ #
91
+ # @raise [Berkshelf::API::NotStartedError]
92
+ #
93
+ # @return [Berkshelf::API::Application]
52
94
  def instance
53
95
  return @instance if @instance
54
96
 
@@ -92,7 +134,7 @@ module Berkshelf::API
92
134
  # @return [Berkshelf::API::Application]
93
135
  def run!(options = {})
94
136
  options = { disable_http: false, eager_build: false }.merge(options)
95
- configure_logger(options)
137
+ configure(options)
96
138
  @instance = ApplicationSupervisor.new(registry, options)
97
139
  cache_builder.async(:build_loop) if options[:eager_build]
98
140
  @instance
@@ -105,6 +147,10 @@ module Berkshelf::API
105
147
  false
106
148
  end
107
149
 
150
+ # Shutdown the running instance
151
+ #
152
+ # @raise [Berkshelf::API::NotStartedError]
153
+ # if there is no running instance
108
154
  def shutdown
109
155
  @shutdown = true
110
156
  instance.terminate
@@ -19,7 +19,8 @@ module Berkshelf::API
19
19
  @building = false
20
20
 
21
21
  Application.config.endpoints.each do |endpoint|
22
- @worker_supervisor.supervise(CacheBuilder::Worker[endpoint.type], endpoint.options)
22
+ endpoint_options = endpoint.options.to_hash.deep_symbolize_keys
23
+ @worker_supervisor.supervise(CacheBuilder::Worker[endpoint.type], endpoint_options)
23
24
  end
24
25
  end
25
26
 
@@ -27,7 +28,11 @@ module Berkshelf::API
27
28
  #
28
29
  # @return [Array]
29
30
  def build
30
- workers.collect { |actor| actor.future(:build) }.map(&:value)
31
+ workers.collect { |actor| actor.future(:build) }.map do |f|
32
+ begin
33
+ f.value
34
+ rescue; end
35
+ end
31
36
  end
32
37
 
33
38
  # Issue a build command to all workers at the scheduled interval
@@ -7,7 +7,7 @@ module Berkshelf::API
7
7
  finalizer :finalize_callback
8
8
 
9
9
  def initialize(options = {})
10
- @connection = Berkshelf::API::SiteConnector::Opscode.pool_link(size: 25)
10
+ @connection = Berkshelf::API::SiteConnector::Opscode.pool_link(size: 25, args: [ options ])
11
11
  super
12
12
  end
13
13
 
@@ -1,11 +1,9 @@
1
1
  module Berkshelf::API
2
2
  class CacheManager
3
3
  class << self
4
- attr_writer :cache_file
5
-
6
4
  # @return [String]
7
5
  def cache_file
8
- @cache_file ||= File.expand_path("~/.berkshelf/api-server/cerch")
6
+ File.join(Application.home_path, "cerch")
9
7
  end
10
8
  end
11
9
 
@@ -5,10 +5,15 @@ module Berkshelf::API
5
5
  class << self
6
6
  # @return [String]
7
7
  def default_path
8
- File.expand_path("~/.berkshelf/api-server/config.json")
8
+ home_path = ENV['BERKSHELF_API_PATH'] || "~/.berkshelf/api-server"
9
+ File.expand_path(File.join(home_path, "config.json"))
9
10
  end
10
11
  end
11
12
 
13
+ attribute 'home_path',
14
+ type: String,
15
+ default: File.expand_path("~/.berkshelf/api-server")
16
+
12
17
  attribute 'endpoints',
13
18
  type: Array,
14
19
  default: [
@@ -2,7 +2,7 @@ require_relative 'rspec'
2
2
 
3
3
  World(Berkshelf::API::RSpec)
4
4
 
5
- Given(/^the Berkshelf API server cache is up to date$/) do
5
+ Given(/^the Berkshelf API server's cache is up to date$/) do
6
6
  cache_builder.build
7
7
  end
8
8
 
@@ -1,8 +1,11 @@
1
1
  module Berkshelf::API
2
2
  class APIError < StandardError; end
3
+
3
4
  # Thrown when an actor was expected to be running but wasn't
4
5
  class NotStartedError < APIError; end
6
+
5
7
  class SaveNotFoundError < APIError; end
6
8
  class InvalidSaveError < APIError; end
7
9
  class MetadataLoadError < APIError; end
10
+ class ConfigNotFoundError < APIError; end
8
11
  end
@@ -26,7 +26,7 @@ module Berkshelf::API
26
26
  finalizer :finalize_callback
27
27
 
28
28
  # @option options [String] :host ('0.0.0.0')
29
- # @option options [Integer] :port (26100)
29
+ # @option options [Integer] :port (26200)
30
30
  # @option options [Boolean] :quiet (false)
31
31
  # @option options [Integer] :workers (10)
32
32
  def initialize(options = {})
@@ -39,11 +39,11 @@ module Berkshelf::API
39
39
  # @param [Faraday::Connection] connection
40
40
  # Optional parameter for setting the connection object
41
41
  # This should only be set manually for testing
42
- def initialize(uri = V1_API, options = {})
43
- options = { retries: 5, retry_interval: 0.5 }.merge(options)
44
- @api_uri = uri
42
+ def initialize(options = {})
43
+ options = { url: V1_API, retries: 5, retry_interval: 0.5 }.merge(options)
44
+ @api_uri = options[:url]
45
45
 
46
- @connection = Faraday.new(uri) do |c|
46
+ @connection = Faraday.new(@api_uri) do |c|
47
47
  c.response :parse_json
48
48
  c.use Faraday::Adapter::NetHttp
49
49
  end
@@ -28,6 +28,10 @@ module Berkshelf
28
28
  options[:log_location] = '/dev/null'
29
29
  end
30
30
 
31
+ opts.on("-c", "--config FILE", String, "path to a configuration file to use") do |v|
32
+ options[:config_file] = v
33
+ end
34
+
31
35
  opts.on_tail("-h", "--help", "show this message") do
32
36
  puts opts
33
37
  exit
@@ -1,5 +1,5 @@
1
1
  module Berkshelf
2
2
  module API
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -25,7 +25,7 @@ Spork.prefork do
25
25
  config.before do
26
26
  Celluloid.shutdown
27
27
  Celluloid.boot
28
- Berkshelf::API::CacheManager.cache_file = tmp_path.join('cerch').to_s
28
+ ENV['BERKSHELF_API_PATH'] = tmp_path.join('api-server').to_s
29
29
  clean_tmp_path
30
30
  end
31
31
  end
@@ -6,6 +6,33 @@ describe Berkshelf::API::Application do
6
6
 
7
7
  its(:registry) { should be_a(Celluloid::Registry) }
8
8
 
9
+ describe "::configure" do
10
+ let(:options) { Hash.new }
11
+ subject(:configure) { described_class.configure(options) }
12
+ before { @original = described_class.config }
13
+ after { described_class.set_config(@original) }
14
+
15
+ context "given a value for :config_file" do
16
+ let(:filepath) { tmp_path.join('rspec-config.json') }
17
+ before { options[:config_file] = filepath }
18
+
19
+ it "sets the configuration from the contents of the file" do
20
+ generated = Berkshelf::API::Config.new(filepath)
21
+ generated.endpoints = [ { what: "this" } ]
22
+ generated.save
23
+ configure
24
+
25
+ expect(described_class.config.endpoints).to have(1).item
26
+ end
27
+
28
+ context "if the file cannot be found or loaded" do
29
+ it "raises a ConfigNotFoundError" do
30
+ expect { configure }.to raise_error(Berkshelf::API::ConfigNotFoundError)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
9
36
  describe "::run!" do
10
37
  include Berkshelf::API::Mixin::Services
11
38
 
@@ -51,6 +51,14 @@ describe Berkshelf::API::SrvCtl do
51
51
  expect(subject[:log_level]).to eql("DEBUG")
52
52
  end
53
53
  end
54
+
55
+ context "given -c" do
56
+ let(:args) { ["-c", "/path/to/config"] }
57
+
58
+ it "sets :config_file to the given value" do
59
+ expect(subject[:config_file]).to eql("/path/to/config")
60
+ end
61
+ end
54
62
  end
55
63
  end
56
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkshelf-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Winsor
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ridley