sensu-run 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: '0899a5379f38f596c11c1fb651c5197e9abf079a'
4
- data.tar.gz: 69854bd88196ceea54aa6a0f483c5abc3eb2e6ae
3
+ metadata.gz: f98b0011438777bc994c41d4645b91aa74f311b2
4
+ data.tar.gz: c896f234147e52ce8219f9fb8984513fbf406336
5
5
  SHA512:
6
- metadata.gz: 4918f4d4526d767a5eb575548f8d62f8e62cb9428f940a6fca6b9ec466244bb60fda6f02d4ece3caddb2880303c82c624618359330e628695663397c6d7eeb23
7
- data.tar.gz: 8896023aa4bf9411450fc3afc766259f5c260bbe779a1d4a221310fd69ad152bff6f2c0702d6d387797c75ea97c496e68bb337458539ecd1873e21e6ffa81104
6
+ metadata.gz: d62f1a5e3d6f824974b312a1bed210e1251c6a9d9af4a405b116a197db521a09c3e51ffbfba9494a0952a65893cd16b9967878d0b9836e4c0bdbd8ed0741c534
7
+ data.tar.gz: 99bacbf4faa0600679e07d429d5d2a8851622e68e5ceb2396385a584570307662c3820193c7f9547db156d7507319a868ec0e5cc621f2ee17f264e84f217da23
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ Gemfile.lock
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /_yardoc/
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic
6
6
  Versioning](http://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.2.0] - 2020-01-10
9
+
10
+ ### Added
11
+ - Create proxy Entity if missing
12
+ - Check TTL
13
+
8
14
  ## [0.1.0] - 2020-01-10
9
15
 
10
16
  ### Added
@@ -8,9 +8,11 @@ require "sensu/run"
8
8
 
9
9
  options = Sensu::Run.read_cli_opts
10
10
 
11
+ entity = Sensu::Run::Entity.new(options)
11
12
  check = Sensu::Run::Check.new(options)
12
- check.exec
13
- event = check.event
13
+ check.exec!
14
+ event = Sensu::Run::Event.new(entity, check)
14
15
 
15
16
  api = Sensu::Run::APIClient.new(options)
16
- api.post_event(event)
17
+ api.create_entity_if_missing(entity)
18
+ api.create_event(event)
@@ -1,6 +1,8 @@
1
1
  require "optparse"
2
2
  require "sensu/run/version"
3
+ require "sensu/run/entity"
3
4
  require "sensu/run/check"
5
+ require "sensu/run/event"
4
6
  require "sensu/run/api_client"
5
7
 
6
8
  module Sensu
@@ -19,12 +19,34 @@ module Sensu
19
19
  @backends.shift
20
20
  end
21
21
 
22
- def post_event(event)
23
- namespace = @options.fetch(:namespace, "default")
24
- request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{namespace}/events")
22
+ def entity_exists?(entity)
23
+ request = Net::HTTP::Get.new("/api/core/v2/namespaces/#{entity.namespace}/entities/#{entity.name}")
25
24
  request["Content-Type"] = "application/json"
26
25
  request["Authorization"] = "Key #{@options[:api_key]}"
27
- request.body = JSON.dump(event)
26
+ response = @http.request(request)
27
+ response.code == "200"
28
+ end
29
+
30
+ def create_entity(entity)
31
+ request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{entity.namespace}/entities")
32
+ request["Content-Type"] = "application/json"
33
+ request["Authorization"] = "Key #{@options[:api_key]}"
34
+ request.body = JSON.dump(entity.to_hash)
35
+ response = @http.request(request)
36
+ puts response.inspect
37
+ end
38
+
39
+ def create_entity_if_missing(entity)
40
+ unless entity_exists?(entity)
41
+ post_entity(entity)
42
+ end
43
+ end
44
+
45
+ def create_event(event)
46
+ request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{event.entity.namespace}/events")
47
+ request["Content-Type"] = "application/json"
48
+ request["Authorization"] = "Key #{@options[:api_key]}"
49
+ request.body = JSON.dump(event.to_hash)
28
50
  response = @http.request(request)
29
51
  puts response.inspect
30
52
  end
@@ -3,37 +3,31 @@ require "open3"
3
3
  module Sensu
4
4
  module Run
5
5
  class Check
6
- attr_reader :output, :status
6
+ attr_reader :name, :namespace, :output, :status
7
7
 
8
8
  def initialize(options={})
9
9
  @options = options
10
+ @name = @options[:check_name]
11
+ @namespace = @options.fetch(:namespace, "default")
10
12
  end
11
13
 
12
- def exec
14
+ def exec!
13
15
  stdin, stdout, stderr, wait = Open3.popen3(@options[:command])
14
16
  @output = "#{stdout.read}\n#{stderr.read}"
15
17
  @status = wait.value.exitstatus
16
18
  [@output, @status]
17
19
  end
18
20
 
19
- def event
21
+ def to_hash
20
22
  {
21
- :entity => {
22
- :metadata => {
23
- :name => @options[:entity_name],
24
- :namespace => @options.fetch(:namespace, "default")
25
- },
26
- :entity_class => "proxy"
23
+ :metadata => {
24
+ :name => @name,
25
+ :namespace => @namespace
27
26
  },
28
- :check => {
29
- :metadata => {
30
- :name => @options[:check_name],
31
- :namespace => @options.fetch(:namespace, "default")
32
- },
33
- :command => @options[:command],
34
- :output => @output,
35
- :status => @status
36
- }
27
+ :command => @options[:command],
28
+ :output => @output,
29
+ :status => @status,
30
+ :ttl => @options.fetch(:ttl, 0)
37
31
  }
38
32
  end
39
33
  end
@@ -0,0 +1,23 @@
1
+ module Sensu
2
+ module Run
3
+ class Entity
4
+ attr_reader :name, :namespace
5
+
6
+ def initialize(options={})
7
+ @options = options
8
+ @name = options[:entity_name]
9
+ @namespace = options.fetch(:namespace, "default")
10
+ end
11
+
12
+ def to_hash
13
+ {
14
+ :metadata => {
15
+ :name => @name,
16
+ :namespace => @namespace
17
+ },
18
+ :entity_class => "proxy"
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module Sensu
2
+ module Run
3
+ class Event
4
+ attr_reader :entity, :check
5
+
6
+ def initialize(entity, check)
7
+ @entity = entity
8
+ @check = check
9
+ end
10
+
11
+ def to_hash
12
+ {
13
+ :entity => @entity.to_hash,
14
+ :check => @check.to_hash
15
+ }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Sensu
2
2
  module Run
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-run
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
  - Sean Porter
@@ -67,7 +67,6 @@ files:
67
67
  - ".travis.yml"
68
68
  - CHANGELOG.md
69
69
  - Gemfile
70
- - Gemfile.lock
71
70
  - LICENSE.txt
72
71
  - README.md
73
72
  - Rakefile
@@ -77,6 +76,8 @@ files:
77
76
  - lib/sensu/run.rb
78
77
  - lib/sensu/run/api_client.rb
79
78
  - lib/sensu/run/check.rb
79
+ - lib/sensu/run/entity.rb
80
+ - lib/sensu/run/event.rb
80
81
  - lib/sensu/run/version.rb
81
82
  - sensu-run.gemspec
82
83
  homepage: https://github.com/sensu/sensu-run
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sensu-run (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.3)
10
- rake (10.5.0)
11
- rspec (3.8.0)
12
- rspec-core (~> 3.8.0)
13
- rspec-expectations (~> 3.8.0)
14
- rspec-mocks (~> 3.8.0)
15
- rspec-core (3.8.2)
16
- rspec-support (~> 3.8.0)
17
- rspec-expectations (3.8.4)
18
- diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.8.0)
20
- rspec-mocks (3.8.1)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.8.0)
23
- rspec-support (3.8.2)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- bundler (~> 1.16)
30
- rake (~> 10.0)
31
- rspec (~> 3.0)
32
- sensu-run!
33
-
34
- BUNDLED WITH
35
- 1.16.2