inventory-server 0.0.4 → 0.0.5

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: 595a004bb77d3fdca856383c3d152fd56661116b
4
- data.tar.gz: 94473b188763f55c6b0fc01f273081eecaa4d985
3
+ metadata.gz: 9646b4e4c9f3f5f45ef89fa027abbbb92fa5ab74
4
+ data.tar.gz: d6e13c24240418e1ceffbfece628a8c2922f8891
5
5
  SHA512:
6
- metadata.gz: a7a68d033a11a1749a7aefc7e33507ca594547dc06de24e047495e3cef0a31cacff3ef1887e2ae64aaa07dc8de0bb49d45188c005eecdc8a4cb8cccd4e1c1301
7
- data.tar.gz: 66c8c902a5fec61afb29d5b4d0832d8ee98176095c85a1955df0ad7e5a6de07bc8f9cebe7a131bd27a48044bd07865f4a952f0a6ff1edb121dbc185523c80f15
6
+ metadata.gz: 40480cd931607662fb2627b45e6ca30f29ea0ded355b7510ae697dab9ef5f490562a638402d5089da5358c04a451507a99ec42f7633a3513d13560a57882a116
7
+ data.tar.gz: 6f7bd911501c5c8ee1dccd45b3721cc0875b8ca30ad1515aa0ab1fdadd7a3b561c150c44965c3b898e47228ca025ee1c01b8bedba210d5cfa7cf070e74350113
data/Dockerfile CHANGED
@@ -4,13 +4,12 @@ MAINTAINER Filirom1 <filirom1@gmail.com>
4
4
  RUN yum install -y epel-release
5
5
  RUN yum install -y patch tar git ruby ruby-devel rubygem-bundler make gcc gcc-c++ zlib-devel libxml2-devel nginx
6
6
 
7
- ADD . /app
8
-
9
- RUN cd /app && bundle install
10
-
11
- RUN cp /app/docker/unicorn/unicorn.conf /etc/nginx/conf.d/unicorn.conf && \
12
- cp /app/docker/unicorn/nginx.conf /etc/nginx/nginx.conf && \
13
- echo "daemon off;" >> /etc/nginx/nginx.conf
7
+ # optimize layer caching for bundle install
8
+ RUN mkdir -p /tmp/lib/inventory/server
9
+ ADD Gemfile /tmp/Gemfile
10
+ ADD inventory-server.gemspec /tmp/inventory-server.gemspec
11
+ ADD lib/inventory/server/version.rb /tmp/lib/inventory/server/version.rb
12
+ RUN cd /tmp && bundle install
14
13
 
15
14
  RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory && \
16
15
  mkdir -p /var/lib/inventory/ && chmod o+rwX /var/lib/inventory && \
@@ -19,5 +18,13 @@ RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory && \
19
18
  VOLUME ["/var/log/nginx", "/var/log/inventory"]
20
19
 
21
20
  EXPOSE 80
21
+
22
+ ADD . /app
23
+
24
+ RUN cp /app/docker/unicorn/unicorn.conf /etc/nginx/conf.d/unicorn.conf && \
25
+ cp /app/docker/unicorn/nginx.conf /etc/nginx/nginx.conf && \
26
+ echo "daemon off;" >> /etc/nginx/nginx.conf
27
+
22
28
  WORKDIR /app
29
+
23
30
  CMD ["/app/docker/unicorn/run-unicorn.sh"]
data/Dockerfile-passenger CHANGED
@@ -8,12 +8,22 @@ RUN yum install -y patch tar git ruby ruby-devel rubygem-bundler make gcc gcc-c+
8
8
  http://puias.math.ias.edu/data/puias/unsupported/7/x86_64/rubygem-passenger-native-4.0.18-9.5.sdl7.x86_64.rpm \
9
9
  http://puias.math.ias.edu/data/puias/unsupported/7/x86_64/rubygem-passenger-native-libs-4.0.18-9.5.sdl7.x86_64.rpm
10
10
 
11
+ RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory
12
+
13
+ # optimize layer caching for bundle install
14
+ RUN mkdir -p /tmp/lib/inventory/server
15
+ ADD Gemfile /tmp/Gemfile
16
+ ADD inventory-server.gemspec /tmp/inventory-server.gemspec
17
+ ADD lib/inventory/server/version.rb /tmp/lib/inventory/server/version.rb
18
+ RUN cd /tmp && bundle install
19
+
20
+ EXPOSE 80
21
+
11
22
  ADD . /app
12
23
 
13
- RUN cd /app && bundle install
14
24
  RUN cp /app/docker/passenger/passenger.conf /etc/httpd/conf.d/passenger.conf
15
- RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory
16
25
 
17
- EXPOSE 80
26
+
18
27
  WORKDIR /app
28
+
19
29
  CMD ["/app/docker/passenger/run-httpd.sh"]
data/Dockerfile-unicorn CHANGED
@@ -4,13 +4,12 @@ MAINTAINER Filirom1 <filirom1@gmail.com>
4
4
  RUN yum install -y epel-release
5
5
  RUN yum install -y patch tar git ruby ruby-devel rubygem-bundler make gcc gcc-c++ zlib-devel libxml2-devel nginx
6
6
 
7
- ADD . /app
8
-
9
- RUN cd /app && bundle install
10
-
11
- RUN cp /app/docker/unicorn/unicorn.conf /etc/nginx/conf.d/unicorn.conf && \
12
- cp /app/docker/unicorn/nginx.conf /etc/nginx/nginx.conf && \
13
- echo "daemon off;" >> /etc/nginx/nginx.conf
7
+ # optimize layer caching for bundle install
8
+ RUN mkdir -p /tmp/lib/inventory/server
9
+ ADD Gemfile /tmp/Gemfile
10
+ ADD inventory-server.gemspec /tmp/inventory-server.gemspec
11
+ ADD lib/inventory/server/version.rb /tmp/lib/inventory/server/version.rb
12
+ RUN cd /tmp && bundle install
14
13
 
15
14
  RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory && \
16
15
  mkdir -p /var/lib/inventory/ && chmod o+rwX /var/lib/inventory && \
@@ -19,5 +18,13 @@ RUN mkdir -p /var/log/inventory && chmod o+rwX /var/log/inventory && \
19
18
  VOLUME ["/var/log/nginx", "/var/log/inventory"]
20
19
 
21
20
  EXPOSE 80
21
+
22
+ ADD . /app
23
+
24
+ RUN cp /app/docker/unicorn/unicorn.conf /etc/nginx/conf.d/unicorn.conf && \
25
+ cp /app/docker/unicorn/nginx.conf /etc/nginx/nginx.conf && \
26
+ echo "daemon off;" >> /etc/nginx/nginx.conf
27
+
22
28
  WORKDIR /app
29
+
23
30
  CMD ["/app/docker/unicorn/run-unicorn.sh"]
data/circle.yml CHANGED
@@ -14,10 +14,9 @@ database:
14
14
 
15
15
  test:
16
16
  override:
17
- - docker-compose up -d
18
- - docker-compose run web cd /app && bundle install
19
- - docker-compose run web cd /app && bundle exec rspec spec
20
- - docker-compose run web cd /app && bundle exec rspec spec --tag integration
17
+ - docker-compose up -d && sleep 5
18
+ - docker-compose run web bundle exec rspec spec
19
+ - docker-compose run web bundle exec rspec spec --tag integration
21
20
 
22
21
  deployment:
23
22
  prod:
data/docker-compose.yml CHANGED
@@ -1,10 +1,12 @@
1
1
  web:
2
2
  build: .
3
- links:
4
- - elasticsearch
3
+ volumes:
4
+ - .:/app
5
5
  ports:
6
6
  - "80:80"
7
7
  environment:
8
8
  INVENTORY_ES_HOST: http://elasticsearch:9200
9
+ links:
10
+ - elasticsearch
9
11
  elasticsearch:
10
12
  image: elasticsearch:1.4.4
@@ -1,5 +1,5 @@
1
1
  module Inventory
2
2
  module Server
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -18,15 +18,19 @@ module Inventory
18
18
  type = facts[@config[:type_key]] || @config[:type_default]
19
19
  version = facts[@config[:version_key]] || @config[:version_default]
20
20
 
21
- json_schema_file = File.join @config[:json_schema_dir], type, "#{version}.json"
21
+ schema_type_file = File.join @config[:json_schema_dir], "#{type}.json"
22
+ schema_version_file = File.join @config[:json_schema_dir], type, "#{version}.json"
22
23
 
23
- if ! File.file? json_schema_file
24
- InventoryLogger.logger.info "No JSON Schema found at #{json_schema_file}, skip validation"
25
- return @app.call(env)
24
+ if File.file? schema_type_file
25
+ InventoryLogger.logger.info "Use JSON Schema #{schema_type_file}"
26
+ JSON::Validator.validate!(schema_type_file, facts)
27
+ end
28
+
29
+ if File.file? schema_version_file
30
+ InventoryLogger.logger.info "Use JSON Schema #{schema_version_file}"
31
+ JSON::Validator.validate!(schema_version_file, facts)
26
32
  end
27
33
 
28
- InventoryLogger.logger.info "Use JSON Schema #{json_schema_file}"
29
- JSON::Validator.validate!(json_schema_file, facts)
30
34
  @app.call(env)
31
35
  end
32
36
  end
@@ -0,0 +1,37 @@
1
+ require 'inventory/server/inventory_error'
2
+ require "inventory/server/logger"
3
+
4
+ require 'rest-client'
5
+
6
+ module Inventory
7
+ module Server
8
+ class LogFailuresInEs
9
+ def initialize(app, config)
10
+ @app = app
11
+ @failed_facts_dir = config[:failed_facts_dir]
12
+ @config = config
13
+ end
14
+
15
+ def call(env)
16
+ id = env[:id]
17
+ raise InventoryError.new 'id missing' if id.nil? || id.empty?
18
+
19
+ body = env[:body]
20
+ raise InventoryError.new "body missing" if body.nil? || body.empty?
21
+
22
+ url = "#{@config[:es_host]}/#{@config[:es_failure_index]}/#{@config[:es_failure_type]}/#{id}"
23
+ begin
24
+ @app.call(env)
25
+ RestClient.delete(url)
26
+ rescue => e
27
+ env['error_message'] = $!
28
+ env['stack_trace'] = "#{e.backtrace}"
29
+
30
+ RestClient.put(url, env.to_json)
31
+
32
+ raise e
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -5,19 +5,21 @@ require 'parallel'
5
5
  FIXTURES_PATH = File.join File.expand_path(File.dirname(__FILE__)), 'fixtures'
6
6
  XML_STR = File.read(File.join FIXTURES_PATH, 'facter.xml')
7
7
 
8
+ IP = ENV['WEB_PORT_80_TCP_ADDR'] || 'localhost'
9
+
8
10
  context "HTTP integration tests" do
9
11
  before(:all) do
10
12
  WebMock.allow_net_connect!
11
13
  end
12
14
 
13
15
  it "integrates one document", :integration => true do
14
- response = RestClient.post "http://localhost/api/v1/facts/MY_UUID", XML_STR
16
+ response = RestClient.post "http://#{IP}/api/v1/facts/MY_UUID", XML_STR
15
17
  expect(response.code).to eq 200
16
18
  end
17
19
 
18
20
  it "integrates 500 document", :integration => true do
19
21
  Parallel.each([*1..500], :in_threads=>20){|i|
20
- response = RestClient.post "http://localhost/api/v1/facts/MY_UUID_#{i}", XML_STR
22
+ response = RestClient.post "http://#{IP}/api/v1/facts/MY_UUID_#{i}", XML_STR
21
23
  expect(response.code).to eq 200
22
24
  }
23
25
  end
@@ -23,6 +23,7 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
23
23
  context "without version and type" do
24
24
  env = { :facts => { 'key' => 'value'} }
25
25
  it "should use the default config" do
26
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/facts.json").and_return false
26
27
  expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/facts/1-0-0.json").and_return false
27
28
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
28
29
  end
@@ -39,29 +40,51 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
39
40
 
40
41
  context "with JSON schema" do
41
42
  it "should fail if the file is not redable" do
42
- expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
43
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
43
44
  expect {
44
45
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
45
46
  }.to raise_error Errno::ENOENT
46
47
  end
47
48
 
48
49
  it "should fail if the file is not a valid JSON" do
49
- expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
50
- expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return '{"dsf": dsf}'
50
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
51
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return '{"dsf": dsf}'
51
52
  expect {
52
53
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
53
54
  }.to raise_error MultiJson::ParseError
54
55
  end
55
56
 
56
- it "should fail if the facts do not respect the type" do
57
+ it "should fail if the facts do not respect the type json schema" do
57
58
  schema = {
58
59
  "type" => "object",
59
60
  "properties" => {
60
61
  "key"=> {"type" => "integer" }
61
62
  }
62
63
  }
64
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
65
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return schema.to_json
66
+ expect {
67
+ Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
68
+ }.to raise_error JSON::Schema::ValidationError
69
+ end
70
+
71
+ it "should fail if the facts do not respect the version json schema" do
72
+ type_schema = {
73
+ "type" => "object",
74
+ "properties" => {
75
+ }
76
+ }
77
+ version_schema = {
78
+ "type" => "object",
79
+ "properties" => {
80
+ "key"=> {"type" => "integer" }
81
+ }
82
+ }
83
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
63
84
  expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
64
- expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return schema.to_json
85
+
86
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return type_schema.to_json
87
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return version_schema.to_json
65
88
  expect {
66
89
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
67
90
  }.to raise_error JSON::Schema::ValidationError
@@ -74,7 +97,10 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
74
97
  "key"=> {"type" => "string" }
75
98
  }
76
99
  }
100
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
77
101
  expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
102
+
103
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return schema.to_json
78
104
  expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return schema.to_json
79
105
 
80
106
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
@@ -86,7 +112,10 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
86
112
  "properties" => {
87
113
  }
88
114
  }
115
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
89
116
  expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
117
+
118
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return schema.to_json
90
119
  expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return schema.to_json
91
120
 
92
121
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
@@ -99,7 +128,10 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
99
128
  "optional"=> {"type" => "string" }
100
129
  }
101
130
  }
131
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
102
132
  expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
133
+
134
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return schema.to_json
103
135
  expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return schema.to_json
104
136
 
105
137
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
@@ -113,8 +145,8 @@ RSpec.describe Inventory::Server::JsonSchemaValidator, '#call' do
113
145
  "mandatory"=> {"type" => "string" }
114
146
  }
115
147
  }
116
- expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return true
117
- expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact/my_version.json").and_return schema.to_json
148
+ expect(File).to receive(:file?).with("#{config[:json_schema_dir]}/my_fact.json").and_return true
149
+ expect(File).to receive(:read).with("#{config[:json_schema_dir]}/my_fact.json").and_return schema.to_json
118
150
 
119
151
  expect {
120
152
  Inventory::Server::JsonSchemaValidator.new(noop, config).call(env)
@@ -0,0 +1,50 @@
1
+ require_relative '../../plugins/log_failures_in_es'
2
+ require 'rspec/mocks'
3
+
4
+ noop = lambda {|env|}
5
+
6
+ RSpec.describe Inventory::Server::LogFailuresInEs do
7
+ config = { :failed_facts_dir => './log' , :es_failure_index => "itdiscovery-failures", :es_failure_type => "v1", :es_host => "http://localhost:9200"}
8
+
9
+ context "without body" do
10
+ env = { :id => 'id', :body => nil }
11
+ it "should throw an error" do
12
+ expect {
13
+ Inventory::Server::LogFailuresInEs.new(noop, config).call(env)
14
+ Inventory::Server::FactsParser.new(noop, {}).call(env)
15
+ }.to raise_error 'body missing'
16
+ end
17
+ end
18
+
19
+ context "without id" do
20
+ env = { :id => nil, :body => 'body' }
21
+ it "should throw an error" do
22
+ expect {
23
+ Inventory::Server::LogFailuresInEs.new(noop, config).call(env)
24
+ Inventory::Server::FactsParser.new(noop, {}).call(env)
25
+ }.to raise_error 'id missing'
26
+ end
27
+ end
28
+
29
+ context "with all mandatory params" do
30
+ env = { :id => 'MY_UUID', :body => 'my_body' }
31
+
32
+ context "without error" do
33
+ it "should pass without writing anything" do
34
+ expect(RestClient).to receive(:delete).with('http://localhost:9200/itdiscovery-failures/v1/MY_UUID')
35
+ expect(RestClient).to_not receive(:put).with(any_args)
36
+ Inventory::Server::LogFailuresInEs.new(noop, config).call(env)
37
+ end
38
+ end
39
+
40
+ context "with error" do
41
+ it "should write facts" do
42
+ raise_error = lambda{ |e| raise "an error" }
43
+ expect(RestClient).to receive(:put).with('http://localhost:9200/itdiscovery-failures/v1/MY_UUID', /(?=.*my_body)(?=.*an error)(?=.*log_failures_in_es_spec.rb:42)/)
44
+ expect {
45
+ Inventory::Server::LogFailuresInEs.new(raise_error, config).call(env)
46
+ }.to raise_error 'an error'
47
+ end
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inventory-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filirom1
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: midi-smtp-server
@@ -333,6 +333,7 @@ files:
333
333
  - plugins/facts_parser.rb
334
334
  - plugins/index.rb
335
335
  - plugins/json_schema_validator.rb
336
+ - plugins/log_failures_in_es.rb
336
337
  - plugins/log_failures_on_disk.rb
337
338
  - plugins/sample.rb
338
339
  - public/.gitignore
@@ -348,6 +349,7 @@ files:
348
349
  - spec/unit/index_spec.rb
349
350
  - spec/unit/json_schema_validator_spec.rb
350
351
  - spec/unit/loader_spec.rb
352
+ - spec/unit/log_failures_in_es_spec.rb
351
353
  - spec/unit/log_failures_on_disk_spec.rb
352
354
  - spec/unit/server_spec.rb
353
355
  - spec/unit/smtp_server_spec.rb
@@ -389,6 +391,7 @@ test_files:
389
391
  - spec/unit/index_spec.rb
390
392
  - spec/unit/json_schema_validator_spec.rb
391
393
  - spec/unit/loader_spec.rb
394
+ - spec/unit/log_failures_in_es_spec.rb
392
395
  - spec/unit/log_failures_on_disk_spec.rb
393
396
  - spec/unit/server_spec.rb
394
397
  - spec/unit/smtp_server_spec.rb