garrison-api 1.0.8 → 2.0.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
- SHA256:
3
- metadata.gz: c2ae0db74546c3f6fff75275830323d4697a3a163a2d75afce2e271f099a1b29
4
- data.tar.gz: dc0b7aa60d8507c4082c16173526f874fc30c29c6e511dae841bf89ed370ea57
2
+ SHA1:
3
+ metadata.gz: 9d245029755f99b9f52a4e33f9e34cc4f6e68c42
4
+ data.tar.gz: f07cd2df5281a3517ff382fc80c81e4b29a703ca
5
5
  SHA512:
6
- metadata.gz: 1c73f7832214609e5b83523c8a908da55eb91c35526232f614fd57f705988c34cc48c869a38fc128172f4c44f3d3b6e9478a9ee3175ad22f7b25e2e3dc0d74aa
7
- data.tar.gz: c9a020c92477986897e5d08d2bee3047206e044a519b84cf36cb5aaaff946019bdb10cb84a4d280612ef1739cfec70cb58e6ab18cf97a9d4d434cb0502afeff7
6
+ metadata.gz: b8be2d213ca909a094e9a3b2b773063832fcea90401c09b4351f565cc147e4e966b64e4854faf9890985fd69e4537a6b6330fb9e5d341ec143173bf6e75525c4
7
+ data.tar.gz: 2876270db89b10a954f181aee69354a30afbfd2d6bda4b8f9c192e10942c9aca35e9153f0665603d127a6701faf22754ef883f90cc506a9cc958bfe185c5c927
@@ -1,24 +1,28 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- garrison-api (1.0.0)
5
- httparty (~> 0.16.2)
4
+ garrison-api (2.0.0)
5
+ httparty (>= 0.16.2, < 0.18.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- httparty (0.16.2)
10
+ httparty (0.17.0)
11
+ mime-types (~> 3.0)
11
12
  multi_xml (>= 0.5.2)
13
+ mime-types (3.2.2)
14
+ mime-types-data (~> 3.2015)
15
+ mime-types-data (3.2019.0331)
12
16
  multi_xml (0.6.0)
13
- rake (10.5.0)
17
+ rake (12.3.2)
14
18
 
15
19
  PLATFORMS
16
20
  ruby
17
21
 
18
22
  DEPENDENCIES
19
- bundler (~> 1.16)
23
+ bundler (~> 2.0)
20
24
  garrison-api!
21
- rake (~> 10.0)
25
+ rake (~> 12.3)
22
26
 
23
27
  BUNDLED WITH
24
- 1.16.1
28
+ 2.0.2
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency 'httparty', '~> 0.16.2'
24
+ spec.add_dependency 'httparty', '>= 0.16.2', '< 0.18.0'
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.16"
27
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 12.3"
28
28
  end
@@ -8,6 +8,8 @@ module Garrison
8
8
  attr_accessor :family
9
9
  attr_accessor :departments
10
10
  attr_accessor :options
11
+ attr_accessor :run_uuid
12
+ attr_accessor :state
11
13
 
12
14
  def initialize(options = {})
13
15
  @source = ENV['GARRISON_ALERT_SOURCE']
@@ -16,10 +18,12 @@ module Garrison
16
18
  @family = ENV['GARRISON_ALERT_FAMILY']
17
19
  @departments = ENV['GARRISON_ALERT_DEPARTMENTS'] ? ENV['GARRISON_ALERT_DEPARTMENTS'].split(',') : []
18
20
  @options = options
21
+ @state = :initial
19
22
 
23
+ Logging.logger.progname = Api.configuration.uuid
20
24
  Logging.info "Starting... #{self.class.name}"
21
25
  inherit_settings
22
- Logging.info "Agent Settings (source=#{self.source} severity=#{self.severity || 'dynamic'} type=#{self.type} family=#{self.family} departments=#{self.departments.join(',')})"
26
+ Logging.info "Agent Settings (uuid=#{Api.configuration.uuid} source=#{self.source} severity=#{self.severity || 'dynamic'} type=#{self.type} family=#{self.family} departments=#{self.departments.join(',')})"
23
27
 
24
28
  options_log = options.map do |key, value|
25
29
  value = value.is_a?(Array) ? value.join(',') : value
@@ -28,10 +32,31 @@ module Garrison
28
32
  Logging.info "Check Settings (#{options_log.join(' ')})" if options.any?
29
33
  end
30
34
 
35
+ def run
36
+ before_perform
37
+ begin
38
+ perform
39
+ rescue Exception => e
40
+ Logging.fatal "#{e} - #{e.message}"
41
+ self.state = :failed
42
+ end
43
+ after_perform
44
+ end
45
+
31
46
  def perform
32
47
  raise 'You must provide a perform method in your check class'
33
48
  end
34
49
 
50
+ def before_perform
51
+ self.run_uuid = Api::Run.create(self)
52
+ end
53
+
54
+ def after_perform
55
+ self.state = :complete
56
+ Api::Run.update(self)
57
+ Api::Alert.obsolete_previous_runs(self) if ENV["GARRISON_AUTO_OBSOLETE"]
58
+ end
59
+
35
60
  def key_values
36
61
  []
37
62
  end
@@ -41,6 +66,7 @@ module Garrison
41
66
  utc_time_now = Time.now.utc
42
67
 
43
68
  alert = Api::Alert.new
69
+ alert.run_uuid = self.run_uuid
44
70
  alert.type = type
45
71
  alert.family = family
46
72
  alert.source = source
@@ -1,7 +1,8 @@
1
1
  require 'httparty'
2
- require 'garrison/api/version'
3
- require 'garrison/api/alert'
4
2
  require 'garrison/agents/check'
3
+ require 'garrison/api/alert'
4
+ require 'garrison/api/run'
5
+ require 'garrison/api/version'
5
6
  require 'garrison/helpers/logger'
6
7
 
7
8
  module Garrison
@@ -18,11 +19,9 @@ module Garrison
18
19
  class Configuration
19
20
  attr_accessor :url
20
21
  attr_accessor :uuid
21
- attr_accessor :run_uuid
22
22
 
23
23
  def initialize
24
- @uuid = SecureRandom.uuid
25
- @run_uuid = SecureRandom.uuid
24
+ @uuid = SecureRandom.uuid
26
25
  end
27
26
  end
28
27
  end
@@ -3,21 +3,33 @@ module Garrison
3
3
  class Alert
4
4
 
5
5
  class << self
6
- def obsolete_previous_runs(source)
7
- raise ArgumentError, "No source defined" unless source
6
+ def obsolete_previous_runs(check)
7
+ raise ArgumentError, "No source defined" unless check.source
8
8
  url = File.join(Api.configuration.url, 'api', 'v1', 'alerts', 'obsolete')
9
- HTTParty.post(
10
- url,
9
+
10
+ party_params = {
11
11
  body: {
12
- source: source,
13
- agent_uuid: Api.configuration.uuid,
14
- agent_run_uuid: Api.configuration.run_uuid,
12
+ source: check.source,
13
+ agent_id: Api.configuration.uuid,
14
+ run_id: check.run_uuid,
15
15
  }.to_json,
16
- headers: { 'Content-Type' => 'application/json' }
17
- )
16
+ headers: { 'Content-Type' => 'application/json' },
17
+ logger: Logging.logger,
18
+ log_level: :debug,
19
+ raise_on: (400..599).to_a
20
+ }
21
+
22
+ Logging.debug "Alert::obsolete_previous_runs - #{party_params[:body]}"
23
+ HTTParty.post(url, party_params)
24
+
25
+ rescue Errno::ECONNREFUSED => e
26
+ Logging.error "#{e.class} to the Garrison API during Alert::obsolete_previous_runs - #{e.message}"
27
+ rescue HTTParty::ResponseError => e
28
+ Logging.error "#{e.class} #{e.message.split(" - ")[0]} - When calling the Garrison API during Alert::obsolete_previous_runs"
18
29
  end
19
30
  end
20
31
 
32
+ attr_accessor :run_uuid
21
33
  attr_accessor :type
22
34
  attr_accessor :family
23
35
  attr_accessor :source
@@ -39,8 +51,8 @@ module Garrison
39
51
 
40
52
  def save
41
53
  url = File.join(Api.configuration.url, 'api', 'v1', 'alerts')
42
- HTTParty.post(
43
- url,
54
+
55
+ party_params = {
44
56
  body: {
45
57
  name: name,
46
58
  target: target,
@@ -58,11 +70,22 @@ module Garrison
58
70
  departments: departments,
59
71
  no_repeat: no_repeat,
60
72
  count: count,
61
- agent_uuid: Api.configuration.uuid,
62
- agent_run_uuid: Api.configuration.run_uuid,
73
+ agent_id: Api.configuration.uuid,
74
+ run_id: run_uuid,
63
75
  }.to_json,
64
- headers: { 'Content-Type' => 'application/json' }
65
- )
76
+ headers: { 'Content-Type' => 'application/json' },
77
+ logger: Logging.logger,
78
+ log_level: :debug,
79
+ raise_on: (400..599).to_a
80
+ }
81
+
82
+ Logging.debug "Alert::save - #{party_params[:body]}"
83
+ HTTParty.post(url, party_params)
84
+
85
+ rescue Errno::ECONNREFUSED => e
86
+ Logging.error "#{e.class} to the Garrison API during Alert::save - #{e.message}"
87
+ rescue HTTParty::ResponseError => e
88
+ Logging.error "#{e.class} #{e.message.split(" - ")[0]} - When calling the Garrison API during Alert::save"
66
89
  end
67
90
  end
68
91
  end
@@ -0,0 +1,62 @@
1
+ module Garrison
2
+ module Api
3
+ class Run
4
+
5
+ class << self
6
+ def create(check)
7
+ url = File.join(Api.configuration.url, 'api', 'v1', 'runs')
8
+ party_params = {
9
+ body: {
10
+ run: {
11
+ agent_id: Api.configuration.uuid,
12
+ started_at: Time.now.utc,
13
+ state: check.state
14
+ }
15
+ }.to_json,
16
+ headers: { 'Content-Type' => 'application/json' },
17
+ logger: Logging.logger,
18
+ log_level: :debug,
19
+ raise_on: (400..599).to_a
20
+ }
21
+
22
+ Logging.debug "Run::create - #{party_params[:body]}"
23
+ response = HTTParty.post(url, party_params)
24
+
25
+ Logging.logger.progname = [Api.configuration.uuid, response["id"]].join(",")
26
+ Logging.info "Agent Run Created (uuid=#{response["id"]})"
27
+ response["id"]
28
+
29
+ rescue Errno::ECONNREFUSED => e
30
+ Logging.error "#{e.class} to the Garrison API during Run::create - #{e.message}"
31
+ rescue HTTParty::ResponseError => e
32
+ Logging.error "#{e.class} #{e.message.split(" - ")[0]} - When calling the Garrison API during Run::create"
33
+ end
34
+
35
+ def update(check)
36
+ url = File.join(Api.configuration.url, 'api', 'v1', 'runs', check.run_uuid)
37
+ party_params = {
38
+ body: {
39
+ run: {
40
+ state: check.state,
41
+ ended_at: Time.now.utc
42
+ }
43
+ }.to_json,
44
+ headers: { 'Content-Type' => 'application/json' },
45
+ logger: Logging.logger,
46
+ log_level: :debug,
47
+ raise_on: (400..599).to_a
48
+ }
49
+
50
+ Logging.debug "Run::update - #{party_params[:body]}"
51
+ HTTParty.patch(url, party_params)
52
+
53
+ rescue Errno::ECONNREFUSED => e
54
+ Logging.error "#{e.class} to the Garrison API during Run::update - #{e.message}"
55
+ rescue HTTParty::ResponseError => e
56
+ Logging.error "#{e.class} #{e.message.split(" - ")[0]} - When calling the Garrison API during Run::update"
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  module Garrison
2
2
  module Api
3
- VERSION = "1.0.8"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,57 +1,63 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garrison-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Forward3D Developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-28 00:00:00.000000000 Z
11
+ date: 2019-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.16.2
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.18.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 0.16.2
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.18.0
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: '1.16'
39
+ version: '2.0'
34
40
  type: :development
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - "~>"
39
45
  - !ruby/object:Gem::Version
40
- version: '1.16'
46
+ version: '2.0'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: rake
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
51
  - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '10.0'
53
+ version: '12.3'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '10.0'
60
+ version: '12.3'
55
61
  description: RubyGem to talk to the Garrison API
56
62
  email:
57
63
  - developers@forward3d.com
@@ -72,6 +78,7 @@ files:
72
78
  - lib/garrison/agents/check.rb
73
79
  - lib/garrison/api.rb
74
80
  - lib/garrison/api/alert.rb
81
+ - lib/garrison/api/run.rb
75
82
  - lib/garrison/api/version.rb
76
83
  - lib/garrison/helpers/logger.rb
77
84
  homepage: https://github.com/forward3d/garrison
@@ -94,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
101
  version: '0'
95
102
  requirements: []
96
103
  rubyforge_project:
97
- rubygems_version: 2.7.7
104
+ rubygems_version: 2.6.12
98
105
  signing_key:
99
106
  specification_version: 4
100
107
  summary: RubyGem to talk to the Garrison API