Skip to content

Commit

Permalink
Merge branch 'master' into jruby_support
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-graham committed Jun 23, 2015
2 parents bf77868 + 3cda4e8 commit 30e3c7c
Show file tree
Hide file tree
Showing 50 changed files with 3,405 additions and 2,900 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,7 @@ rvm:
- '2.0'
- '2.1'
- '2.2'
- 'jruby-9.0.0.0.pre1'
- 'jruby-9.0.0.0.rc1'
jdk: oraclejdk8
script: "bundle exec rspec spec && bundle exec rake pact:verify && bundle exec cucumber features && cd src/flapjack && go test -v"
env: GOPATH=$TRAVIS_BUILD_DIR
Expand Down
46 changes: 30 additions & 16 deletions Gemfile.lock
Expand Up @@ -10,6 +10,7 @@ PATH
ice_cube
json-stream
mail
puma
redis (>= 3.0.7)
sinatra
swagger-blocks
Expand All @@ -23,10 +24,10 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.1)
activesupport (= 4.2.1)
activemodel (4.2.2)
activesupport (= 4.2.2)
builder (~> 3.1)
activesupport (4.2.1)
activesupport (4.2.2)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
Expand Down Expand Up @@ -55,21 +56,25 @@ GEM
diff-lcs (1.2.5)
docile (1.1.5)
find_a_port (1.0.1)
gherkin (2.12.2)
multi_json (~> 1.3)
gherkin (2.12.2-java)
multi_json (~> 1.3)
gli (2.13.0)
hiredis (0.6.0)
hiredis (0.6.0-java)
i18n (0.7.0)
ice_cube (0.12.1)
ice_cube (0.13.0)
json (1.8.2)
json (1.8.2-java)
json-stream (0.2.1)
json_spec (1.1.4)
multi_json (~> 1.0)
rspec (>= 2.0, < 4.0)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.4.3)
minitest (5.6.0)
mime-types (2.6.1)
minitest (5.7.0)
multi_json (1.11.0)
multi_test (0.1.2)
numerizer (0.1.1)
Expand All @@ -85,7 +90,7 @@ GEM
term-ansicolor (~> 1.0)
thor
webrick
pact-mock_service (0.5.1)
pact-mock_service (0.5.2)
find_a_port (~> 1.0.1)
json
pact-support (~> 0.4.0)
Expand All @@ -95,7 +100,7 @@ GEM
term-ansicolor (~> 1.0)
thor
webrick
pact-support (0.4.0)
pact-support (0.4.1)
awesome_print (~> 1.1)
find_a_port (~> 1.0.1)
json
Expand All @@ -104,6 +109,10 @@ GEM
rspec (>= 2.14)
term-ansicolor (~> 1.0)
thor
puma (2.11.3)
rack (>= 1.1, < 2.0)
puma (2.11.3-java)
rack (>= 1.1, < 2.0)
rack (1.6.0)
rack-protection (1.5.3)
rack
Expand All @@ -126,28 +135,29 @@ GEM
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
safe_yaml (1.0.4)
simplecov (0.9.2)
simplecov (0.10.0)
docile (~> 1.1.0)
multi_json (~> 1.0)
simplecov-html (~> 0.9.0)
simplecov-html (0.9.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
swagger-blocks (1.1.1)
swagger-blocks (1.1.3)
term-ansicolor (1.3.0)
tins (~> 1.0)
terminal-table (1.4.5)
thor (0.19.1)
thread_safe (0.3.5)
thread_safe (0.3.5-java)
tilt (2.0.1)
tins (1.3.5)
toml-rb (0.3.0)
tins (1.4.2)
toml-rb (0.3.8)
citrus (~> 3.0, > 3.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
tzinfo-data (1.2015.3)
tzinfo-data (1.2015.5)
tzinfo (>= 1.0.0)
webmock (1.21.0)
addressable (>= 2.3.6)
Expand All @@ -159,6 +169,7 @@ GEM

PLATFORMS
java
ruby

DEPENDENCIES
cucumber (>= 2.0.0.rc.4)
Expand All @@ -171,3 +182,6 @@ DEPENDENCIES
rspec
simplecov
webmock

BUNDLED WITH
1.10.4
1 change: 1 addition & 0 deletions LICENCE
@@ -1,4 +1,5 @@
Copyright (c) 2009 Lindsay Holmwood
Copyright (c) 2012-2015 Ali Graham, Lindsay Holmwood, Sarah Kowalik, Jesse Reynolds and contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
4 changes: 3 additions & 1 deletion build.sh
Expand Up @@ -10,7 +10,9 @@ go get gopkg.in/alecthomas/kingpin.v1
go get github.com/oguzbilgic/pandik
mv bin/pandik libexec/httpchecker

go test flapjack
if [ ! -z "$SKIPTESTS" ]; then
go test flapjack
fi

go build -x -o libexec/httpbroker libexec/httpbroker.go
go build -x -o libexec/oneoff libexec/oneoff.go
Expand Down
2 changes: 1 addition & 1 deletion doc
Submodule doc updated from e4020a to 0bebe0
6 changes: 3 additions & 3 deletions etc/flapjack_config.toml.example
Expand Up @@ -154,9 +154,9 @@ daemonize = true
queue = "pagerduty_notifications"
[gateways.pagerduty.credentials]
service_key = "1234567890"
subdomain = thecompany
username = user
password = pass
subdomain = "thecompany"
username = "user"
password = "pass"
[gateways.pagerduty.logger]
level = "INFO"
syslog_errors = true
Expand Down
22 changes: 11 additions & 11 deletions features/notification_rules.feature
Expand Up @@ -31,17 +31,17 @@ Feature: Notification rules on a per contact basis
| 982fc9fb-fbf8-44cd-b6de-6ccbab8e7230 | buf:ping | buf,ping |

And the following rules exist:
| id | contact_id | tags | condition | time_restrictions | media_ids |
| b0c8deb9-b8c8-4fdd-acc4-72493852ca15 | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | foo,ping | critical | 8-18 weekdays | 28032dbf-388d-4f52-91b2-dc5e5be2becc |
| 2df6bbc4-d6a4-4f23-b6e5-5c4a07c6e686 | 65d32027-1942-43b3-93c5-52f4b12d36b0 | bar,ping | critical,unknown | | 1d473cef-5369-4396-9f59-533f3db6c1cb |
| fc2d1b1f-1480-45dd-814b-4655bc5b1474 | 9f77502c-1daf-47a2-b806-f3ae7d04cefb | foo,ping | critical | | 65d32027-1942-43b3-93c5-52f4b12d36b0 |
| 7c123a29-1a67-4a32-b38e-2658e63834d8 | 9f77502c-1daf-47a2-b806-f3ae7d04cefb | foo,ping | | | 55d3778e-e4b2-4dcc-8337-03fcbd2e5f80 |
| e8a67e7c-4f3d-4d9b-afe4-ef276bbeb0df | 158ec8fd-36ca-4d10-a2f4-dc04d374e321 | baz,ping | critical | 8-18 weekdays | 19ef48b1-9a42-488b-9734-00314c79e5eb,ad25c952-c300-4285-9301-ef4408c9d645 |
| 0a3c66f2-6245-49cf-a02c-28d586b2f55a | 158ec8fd-36ca-4d10-a2f4-dc04d374e321 | baz,ping | warning | 8-18 weekdays | 19ef48b1-9a42-488b-9734-00314c79e5eb |
| 9b437f3e-4b48-4516-8067-a57935684777 | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | buf,ping | critical | | 28032dbf-388d-4f52-91b2-dc5e5be2becc |
| dd7005b9-d30b-4875-9e83-dec7fb70895c | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | buf,ping | | | |
| 724bf183-215c-4ba9-b835-56db781c4844 | 5da490ec-72a0-42b0-834f-4049867dfce7 | | | | f15078cf-3643-4cf1-b701-ac9fe2836365 |
| 1c501800-6b20-458d-bb99-a78d17397c00 | 5da490ec-72a0-42b0-834f-4049867dfce7 | | | | 862228f8-fc80-4887-bc4c-e133fcda4107 |
| id | contact_id | tags | condition | is_blackhole | time_restrictions | media_ids |
| b0c8deb9-b8c8-4fdd-acc4-72493852ca15 | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | foo,ping | critical | false | 8-18 weekdays | 28032dbf-388d-4f52-91b2-dc5e5be2becc |
| 2df6bbc4-d6a4-4f23-b6e5-5c4a07c6e686 | 65d32027-1942-43b3-93c5-52f4b12d36b0 | bar,ping | critical,unknown | false | | 1d473cef-5369-4396-9f59-533f3db6c1cb |
| fc2d1b1f-1480-45dd-814b-4655bc5b1474 | 9f77502c-1daf-47a2-b806-f3ae7d04cefb | foo,ping | critical | false | | 65d32027-1942-43b3-93c5-52f4b12d36b0 |
| 7c123a29-1a67-4a32-b38e-2658e63834d8 | 9f77502c-1daf-47a2-b806-f3ae7d04cefb | foo,ping | | false | | 55d3778e-e4b2-4dcc-8337-03fcbd2e5f80 |
| e8a67e7c-4f3d-4d9b-afe4-ef276bbeb0df | 158ec8fd-36ca-4d10-a2f4-dc04d374e321 | baz,ping | critical | false | 8-18 weekdays | 19ef48b1-9a42-488b-9734-00314c79e5eb,ad25c952-c300-4285-9301-ef4408c9d645 |
| 0a3c66f2-6245-49cf-a02c-28d586b2f55a | 158ec8fd-36ca-4d10-a2f4-dc04d374e321 | baz,ping | warning | false | 8-18 weekdays | 19ef48b1-9a42-488b-9734-00314c79e5eb |
| 9b437f3e-4b48-4516-8067-a57935684777 | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | buf,ping | critical | false | | 28032dbf-388d-4f52-91b2-dc5e5be2becc |
| dd7005b9-d30b-4875-9e83-dec7fb70895c | 7f96a216-76aa-45fc-a88e-7431cd6d7aac | buf,ping | | true | | 28032dbf-388d-4f52-91b2-dc5e5be2becc |
| 724bf183-215c-4ba9-b835-56db781c4844 | 5da490ec-72a0-42b0-834f-4049867dfce7 | | | false | | f15078cf-3643-4cf1-b701-ac9fe2836365 |
| 1c501800-6b20-458d-bb99-a78d17397c00 | 5da490ec-72a0-42b0-834f-4049867dfce7 | | | false | | 862228f8-fc80-4887-bc4c-e133fcda4107 |

@time_restrictions @time
Scenario: Alerts only during specified time restrictions
Expand Down
1 change: 1 addition & 0 deletions features/steps/notifications_steps.rb
Expand Up @@ -118,6 +118,7 @@ def find_or_create_check(check_data)

rule = Flapjack::Data::Rule.new(
:id => rule_data['id'],
:is_blackhole => ['1', 't', 'true', 'y', 'yes'].include?((rule_data['is_blackhole'] || '').strip.downcase),
:conditions_list => conditions.empty? ? nil : conditions
)

Expand Down
1 change: 1 addition & 0 deletions flapjack.gemspec
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'gli', '= 2.13.0'
gem.add_dependency 'terminal-table'
gem.add_dependency 'toml-rb'
gem.add_dependency 'puma'

gem.add_development_dependency 'rake'
end
94 changes: 36 additions & 58 deletions lib/flapjack/data/check.rb
Expand Up @@ -37,9 +37,10 @@ class Check
:ack_hash => :string,
:initial_failure_delay => :integer,
:repeat_failure_delay => :integer,
:notification_count => :integer
:notification_count => :integer,
:failing => :boolean

index_by :enabled
index_by :enabled, :failing
unique_index_by :name, :ack_hash

# TODO validate uniqueness of :name, :ack_hash
Expand Down Expand Up @@ -174,6 +175,10 @@ def self.jsonapi_type
key :type, :boolean
key :enum, [true, false]
end
property :failing do
key :type, :boolean
key :enum, [true, false]
end
property :links do
key :"$ref", :CheckLinks
end
Expand Down Expand Up @@ -208,6 +213,9 @@ def self.jsonapi_type
key :type, :boolean
key :enum, [true, false]
end
property :links do
key :"$ref", :CheckChangeLinks
end
end

swagger_schema :CheckUpdate do
Expand All @@ -228,29 +236,32 @@ def self.jsonapi_type
key :enum, [true, false]
end
property :links do
key :"$ref", :CheckUpdateLinks
key :"$ref", :CheckChangeLinks
end
end

swagger_schema :CheckUpdateLinks do
swagger_schema :CheckChangeLinks do
property :scheduled_maintenances do
key :"$ref", :jsonapi_UnscheduledMaintenancesLinkage
end
property :tags do
key :type, :array
items do
key :"$ref", :TagReference
end
key :"$ref", :jsonapi_TagsLinkage
end
property :unscheduled_maintenances do
key :"$ref", :jsonapi_ScheduledMaintenancesLinkage
end
end

def self.jsonapi_attributes
[:name, :enabled]
[:name, :enabled, :ack_hash, :failing]
end

def self.jsonapi_search_string_attributes
[:name, :ack_hash]
end

def self.jsonapi_search_boolean_attributes
[:enabled]
[:enabled, :failing]
end

def self.jsonapi_singular_associations
Expand All @@ -261,15 +272,6 @@ def self.jsonapi_multiple_associations
[:scheduled_maintenances, :tags, :unscheduled_maintenances]
end

def self.failing(scope = nil)
scope ||= self
scope.all.each_with_object([]) do |check, memo|
e = check.last_change
next if e.nil? || Flapjack::Data::Condition.healthy?(e.condition)
memo << check
end
end

# takes an array of ages (in seconds) to split all checks up by
# - age means how long since the last update
# - 0 age is implied if not explicitly passed
Expand Down Expand Up @@ -341,17 +343,17 @@ def self.split_by_freshness(ages, options = {})
end
end

def in_scheduled_maintenance?
return false if scheduled_maintenance_ids_at(Time.now).empty?
def in_scheduled_maintenance?(time = Time.now)
return false if scheduled_maintenance_ids_at(time).empty?
self.routes.intersect(:is_alerting => true).each do |route|
route.is_alerting = false
route.save
end
true
end

def in_unscheduled_maintenance?
!unscheduled_maintenance_ids_at(Time.now).empty?
def in_unscheduled_maintenance?(time = Time.now)
!unscheduled_maintenance_ids_at(time).empty?
end

def add_scheduled_maintenance(sched_maint)
Expand Down Expand Up @@ -453,7 +455,7 @@ def clear_unscheduled_maintenance(end_time)
# this includes generic rules, i.e. ones with no tags

# A generic rule in Flapjack v2 means that it applies to all checks, not
# just all checks the contact is separately regeistered for, as in v1.
# just all checks the contact is separately registered for, as in v1.
# These are not automatically created for users any more, but can be
# deliberately configured.

Expand Down Expand Up @@ -494,43 +496,19 @@ def rule_ids_and_route_ids(opts = {})
"Matching rules for routes (#{rule_ids.size}): #{rule_ids.inspect}"
}

# TODO could maybe also eliminate rules with no media here?
rule_ids_by_contact_id = Flapjack::Data::Rule.intersect(:id => rule_ids).
# if a rule has no media, it's irrelevant in any routing calculations
rule_ids_by_contact_id = Flapjack::Data::Rule.
intersect(:id => rule_ids, :has_media => true).
associated_ids_for(:contact, :inversed => true)

[rule_ids_by_contact_id, route_ids_by_rule_id]
end

def self.pagerduty_credentials_for(check_ids)
rule_ids_by_check_id = Flapjack::Data::Check.rules_for(check_ids)

rule_ids_by_media_id = Flapjack::Data::Medium.
intersect(:transport => 'pagerduty').associated_ids_for(:rules)

return nil if rule_ids_by_media_id.empty? ||
rule_ids_by_media_id.values.all? {|r| r.empty? }

rule_ids = Set.new(rule_ids_by_media_id.values).flatten

media_ids_by_rule_id = Flapjack::Data::Rule.
intersect(:id => rule_ids).associated_ids_for(:media)

pagerduty_objs_by_id = Flapjack::Data::Medium.find_by_ids!(rule_ids_by_media_id.keys)

Flapjack::Data::Check.intersect(:id => check_ids).all.each_with_object({}) do |check, memo|
memo[check] = rule_ids_by_check_id[check.id].each_with_object([]) do |rule_id, m|
m += media_ids_by_rule_id[rule_id].collect do |media_id|
medium = pagerduty_objs_by_id[media_id]
ud = medium.userdata || {}
{
'service_key' => medium.address,
'subdomain' => ud['subdomain'],
'username' => ud['username'],
'password' => ud['password'],
}
end
end
# clear any route_ids for rules without media
active_rule_ids = rule_ids_by_contact_id.values.reduce(:|)
active_route_ids = route_ids_by_rule_id.inject([]) do |memo, (ru_id, ro_ids)|
memo |= ro_ids if active_rule_ids.include?(ru_id)
memo
end

[rule_ids_by_contact_id, active_route_ids]
end

private
Expand Down

0 comments on commit 30e3c7c

Please sign in to comment.