pagerduty_utils 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: 2734f9242e51882171cf33ec55745551f2ca150d
4
- data.tar.gz: 82bc1e579955359c49ef711a768f9920239cd911
3
+ metadata.gz: b78ef3120e4fcfc5c4579c60e349ff7ccca904ab
4
+ data.tar.gz: 5a1df5a97c35c826f4c41767ac81488195a21f5d
5
5
  SHA512:
6
- metadata.gz: fb9671f772b4516e211821437a326b46b81d1aa077b5ea5f0ee56493e04294be11d8830854e423103cfde5adfe1e58a8b7af035236b899620f603d72bf685262
7
- data.tar.gz: aaa11e2a2934c29f44b3fc07f8a20cece9d43c080057385525f5df382007305883af1f41cb5f1e608f4399d968e0e738fa4cd413dd7e8abb53c3ba4139402957
6
+ metadata.gz: 0f0e66729f30f8ac6c05a9574d9214bab47a8380068cb0435d96b819b01df5a761a197072e4863e5a8538f71cd45affb6f1e84e7787c567b14f2c3d423de7fe3
7
+ data.tar.gz: 51beffa82f6a8f690c2f172bb5bcf893da676a7d4f7e5fc523129f57505a5e336e9ab59199257e47c6db83f222f80a657a093cb0fce60ecfd9acccd514a29051
data/bin/pgutils CHANGED
@@ -1,16 +1,16 @@
1
1
  #!/usr/bin/env ruby -w
2
- require 'optparse'
2
+
3
3
  require 'pagerduty/base'
4
- require 'pagerduty/override'
5
4
 
6
- def load_trigger(trigger_name)
5
+ def load_trigger(trigger_name, message)
7
6
  config_file = "#{ENV['PAGERDUTY_CONFIG_DIR'] ? ENV['PAGERDUTY_CONFIG_DIR'] + 'triggers.yml' : ENV['HOME'] + '/.pgutils/triggers.yaml'}"
8
- if File.readable?(config_file)
7
+ if File.readable?(config_file)
9
8
  triggers = YAML.load_file(config_file)
10
9
  else
11
10
  abort("#{config_file} not found")
12
11
  end
13
- pg = TapJoy::PagerDuty::Base.new
12
+ pg = Tapjoy::PagerDuty::Base.new
13
+ triggers[trigger_name.to_sym][:description] = message if message
14
14
  puts pg.post_trigger(**triggers[trigger_name.to_sym])
15
15
  end
16
16
 
@@ -22,93 +22,42 @@ def get_level_one_users(pg)
22
22
  end
23
23
  end
24
24
 
25
- options = {}
26
-
27
- subtext = <<HELP
28
- Supported commands are:
29
- set_override :
30
- trigger :
31
- get_on_call :
32
-
33
- See '#{File.basename(__FILE__)} COMMAND --help' for more information on a specific command.
34
- HELP
35
-
36
- global = OptionParser.new do |opts|
37
- opts.banner = "Usage: #{File.basename(__FILE__)} [options] [subcommand [options]]"
38
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
39
- options[:verbose] = v
40
- end
41
- opts.separator ""
42
- opts.separator subtext
25
+ SUB_COMMANDS = %w(set_override trigger get_on_call)
26
+ Trollop::options do
27
+ usage '[SUB_COMMAND] [options]'
28
+ synopsis "\nTool to integrate with PagerDuty API.\nAvailable subcommands are: #{SUB_COMMANDS}"
29
+ version "#{File.basename($0)} #{Tapjoy::PagerDuty::VERSION} \u00A9 2014-2015 Tapjoy, Inc."
30
+ stop_on SUB_COMMANDS
43
31
  end
44
32
 
45
- subcommands = {
46
- 'set_override' => OptionParser.new do |opts|
47
- opts.banner = <<-EOS
48
-
49
- Usage: #{File.basename(__FILE__)} set_override [options]
50
-
51
- This code is used to temporarily override the primary person on-call for a given schedule.
52
-
53
- EOS
54
-
55
- opts.on('-e', '--email EMAIL', 'Specify email address of override user') do |e|
56
- options[:email] = e
57
- end
58
- opts.on('-s', '--schedule-name SCHEDULE', 'Name of schedule to override') do |s|
59
- options[:schedule_name] = s
60
- end
61
- opts.on('-t', '--override-length TIME', 'Number of seconds to maintain override for') do |t|
62
- options[:override_length] = t.to_i
63
- end
64
- end,
65
-
66
- 'trigger' => OptionParser.new do |opts|
67
- opts.banner = <<-EOS
68
-
69
- Usage: #{File.basename(__FILE__)} trigger [options]
70
-
71
- This script will create a pagerduty alert, based on the name of the trigger
72
- hash as specified in 'triggers.yaml'
73
-
74
- EOS
75
-
76
- opts.on("-t", "--trigger TRIGGER", 'Name of trigger to alert on') do |t|
77
- options[:trigger] = t
78
- end
79
- end,
80
-
81
- 'get_on_call' => OptionParser.new do |opts|
82
- opts.banner = <<-EOS
83
-
84
- Usage: #{File.basename(__FILE__)} get_on_call
85
-
86
- This script will return the list of each person on-call sorted by schedule.
87
- There are no options available for this command at this time.
88
-
89
- EOS
33
+ cmd = ARGV.shift
34
+ case cmd
35
+ when 'set_override'
36
+ opts = Trollop::options do
37
+ usage 'set_override [options]'
38
+ synopsis "\nTemporarily override the primary person on-call for a given schedule."
39
+ opt(:email, 'Specify email address of override user', required: true, :type => :string)
40
+ opt(:schedule_name, 'Name of schedule to override', default: 'Default', :type => :string)
41
+ opt(:override_length, 'Number of seconds to maintain override for', default: 3600, :type => :int)
90
42
  end
91
- }
92
43
 
93
- global.order!
94
- command = ARGV.shift
95
- begin
96
- subcommands[command].order!
97
- rescue NoMethodError
98
- abort("Invalid Input.\n\n#{subtext}")
99
- end
100
-
101
- case command
44
+ Tapjoy::PagerDuty::Override.new(opts[:email], opts[:schedule_name], opts[:override_length])
102
45
  when 'trigger'
103
- raise OptionParser::MissingArgument if options[:trigger].nil?
104
- load_trigger(options[:trigger])
105
- when 'set_override'
106
- raise OptionParser::MissingArgument if options[:email].nil?
107
- options[:schedule_name] = 'Default' if options[:schedule_name].nil?
108
- options[:override_length] = 3600 if options[:override_length].nil?
109
- TapJoy::PagerDuty::Override.new(options[:email], options[:schedule_name], options[:override_length])
46
+ opts = Trollop::options do
47
+ usage 'trigger [options]'
48
+ synopsis "\nThis script will create a pagerduty alert, based on the name of the trigger hash as specified in 'triggers.yaml'"
49
+ opt :trigger, 'Name of trigger to alert on', required: true, type: :string
50
+ opt :message, 'Message to include in trigger', type: :string
51
+ end
52
+
53
+ load_trigger(opts[:trigger], opts[:message])
110
54
  when 'get_on_call'
111
- pg = TapJoy::PagerDuty::Base.new
55
+ Trollop::options do
56
+ usage 'get_on_call'
57
+ synopsis "\nThis script will return the list of each person on-call sorted by schedule.\nThere are no options available for this command at this time."
58
+ end
59
+
60
+ pg = Tapjoy::PagerDuty::Base.new
112
61
  get_level_one_users(pg).each do |u|
113
62
  user = pg.get_user_details(u)['user']
114
63
  on_call = user['on_call']
@@ -122,5 +71,5 @@ when 'get_on_call'
122
71
  puts '---'
123
72
  end
124
73
  else
125
- abort("Unknown command: #{command}")
74
+ Trollop::educate
126
75
  end
@@ -1,133 +1,137 @@
1
1
  require 'httparty'
2
2
  require 'json'
3
3
  require 'yaml'
4
-
5
- module TapJoy
6
- module PagerDuty; end
7
- end
8
-
9
- class TapJoy::PagerDuty::Base
10
-
11
- # Initializer services to import values from pg_connect.yaml
12
- # to configure organization-specific values (currently, subdomain and api_token)
13
- def initialize
14
- config_file = "#{ENV['PAGERDUTY_CONFIG_DIR'] ? ENV['PAGERDUTY_CONFIG_DIR'] + 'triggers.yml' : ENV['HOME'] + '/.pgutils/triggers.yaml'}"
15
- pg_conn = YAML.load_file(config_file) if File.readable?(config_file)
16
-
17
- @AUTH_HEADER = {
18
- :subdomain => ENV['PAGERDUTY_SUBDOMAIN'] || pg_conn[:subdomain],
19
- :token_string => "Token token=#{ENV['PAGERDUTY_API_TOKEN'] || pg_conn[:api_token]}"
20
- }
21
-
22
- raise 'Missing subdomain value' if @AUTH_HEADER[:subdomain].nil?
23
- raise 'Missing API token' if @AUTH_HEADER[:token_string].nil?
24
- end
25
-
26
- # Given an email address return the user_id that pagerduty uses for lookups
27
- def get_user_id(email)
28
- endpoint = return_pagerduty_url(:users)
29
- out_array = get_object(endpoint)['users'].select { |i| i['email'].eql?(email) }
30
- return Hash[*out_array]['id']
31
- end
32
-
33
- # Given the name of a schedule return the schedule_id that pagerduty uses for lookups
34
- def get_schedule_id(schedule_name)
35
- endpoint = return_pagerduty_url(:schedules)
36
- out_array = get_object(endpoint)['schedules'].select { |i| i['name'].eql?(schedule_name)}
37
- return Hash[*out_array]['id']
38
- end
39
-
40
- # The set_override method takes in several variables and returns
41
- # the REST response upon (attempting) completion of an override action
42
- def set_override(query_start:, query_end:, override_start:, override_end:,
43
- user_id:, schedule_id:)
44
- # Ruby 2.x style kw-args is required here to make hash passing easier
45
-
46
- endpoint = "#{return_pagerduty_url(:schedules)}/#{schedule_id}/overrides?since=#{query_start}&until=#{query_end}"
47
-
48
- data = {
49
- override: {
50
- user_id: user_id,
51
- start: override_start,
52
- end: override_end,
53
- }
54
- }
55
-
56
- post_object(endpoint, data)
57
- end
58
-
59
- # Return all users on call for all schedules, which we can parse through later
60
- def get_users_on_call
61
- endpoint = return_pagerduty_url(:escalation_on_call)
62
- return get_object(endpoint)
63
- end
64
-
65
- # Given a specific user, return all details about the
66
- # user that we can parse through as needed
67
- def get_user_details(user_id)
68
- endpoint = return_pagerduty_url(:users) + "/#{user_id}/on_call"
69
- return get_object(endpoint)
70
- end
71
-
72
- # Create a page to the first person on call for a given service key
73
- def post_trigger(service_key:, incident_key:, description:, client:,
74
- client_url:, details:)
75
-
76
- # Ruby 2.x style kw-args is required here to make hash passing easier
77
- endpoint = return_pagerduty_url(:create_trigger)
78
- data = {
79
- service_key: service_key,
80
- incident_key: incident_key,
81
- event_type: 'trigger',
82
- description: description,
83
- client: client,
84
- client_url: client_url,
85
- details: details,
86
- }
87
-
88
- post_object(endpoint, data)
89
- end
90
-
91
- private
92
- # Helper method for all GETs
93
- def get_object(endpoint)
94
- response = HTTParty.get(
95
- endpoint,
96
- headers: {
97
- 'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
98
- }
99
- )
100
- return JSON.load(response.body)
101
- end
102
-
103
- # Helper method for all PUTs
104
- def post_object(endpoint, data)
105
- response = HTTParty.post(
106
- endpoint,
107
- body: data.to_json,
108
- headers: {
109
- 'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
110
- }
111
- )
112
-
113
- return response.body
114
- end
115
-
116
- # Helper method for building PagerDuty URLs
117
- def return_pagerduty_url(object_type)
118
- rest_api_url = "https://#{@AUTH_HEADER[:subdomain]}.pagerduty.com/api/v1"
119
- incident_api_url = 'https://events.pagerduty.com/generic/2010-04-15'
120
- case object_type
121
- when :users
122
- return rest_api_url + '/users'
123
- when :schedules
124
- return rest_api_url + '/schedules'
125
- when :escalation_on_call
126
- return rest_api_url + '/escalation_policies/on_call'
127
- when :create_trigger
128
- return incident_api_url + '/create_event.json'
129
- else
130
- abort("Unknown object type: #{object_type}. Can't build URL.")
4
+ require 'date'
5
+ require 'trollop'
6
+ require 'pagerduty/override'
7
+ require_relative 'version'
8
+
9
+ module Tapjoy
10
+ module PagerDuty
11
+ class Base
12
+
13
+ # Initializer services to import values from pg_connect.yaml
14
+ # to configure organization-specific values (currently, subdomain and api_token)
15
+ def initialize
16
+ config_file = "#{ENV['PAGERDUTY_CONFIG_DIR'] ? ENV['PAGERDUTY_CONFIG_DIR'] + 'pgconnect.yml' : ENV['HOME'] + '/.pgutils/pgconnect.yaml'}"
17
+ pg_conn = YAML.load_file(config_file) if File.readable?(config_file)
18
+
19
+ @AUTH_HEADER = {
20
+ :subdomain => ENV['PAGERDUTY_SUBDOMAIN'] || pg_conn[:subdomain],
21
+ :token_string => "Token token=#{ENV['PAGERDUTY_API_TOKEN'] || pg_conn[:api_token]}"
22
+ }
23
+
24
+ raise 'Missing subdomain value' if @AUTH_HEADER[:subdomain].nil?
25
+ raise 'Missing API token' if @AUTH_HEADER[:token_string].nil?
26
+ end
27
+
28
+ # Given an email address return the user_id that pagerduty uses for lookups
29
+ def get_user_id(email)
30
+ endpoint = return_pagerduty_url(:users)
31
+ out_array = get_object(endpoint)['users'].select { |i| i['email'].eql?(email) }
32
+ return Hash[*out_array]['id']
33
+ end
34
+
35
+ # Given the name of a schedule return the schedule_id that pagerduty uses for lookups
36
+ def get_schedule_id(schedule_name)
37
+ endpoint = return_pagerduty_url(:schedules)
38
+ out_array = get_object(endpoint)['schedules'].select { |i| i['name'].eql?(schedule_name)}
39
+ return Hash[*out_array]['id']
40
+ end
41
+
42
+ # The set_override method takes in several variables and returns
43
+ # the REST response upon (attempting) completion of an override action
44
+ def set_override(query_start:, query_end:, override_start:, override_end:,
45
+ user_id:, schedule_id:)
46
+ # Ruby 2.x style kw-args is required here to make hash passing easier
47
+
48
+ endpoint = "#{return_pagerduty_url(:schedules)}/#{schedule_id}/overrides?since=#{query_start}&until=#{query_end}"
49
+
50
+ data = {
51
+ override: {
52
+ user_id: user_id,
53
+ start: override_start,
54
+ end: override_end,
55
+ }
56
+ }
57
+
58
+ post_object(endpoint, data)
59
+ end
60
+
61
+ # Return all users on call for all schedules, which we can parse through later
62
+ def get_users_on_call
63
+ endpoint = return_pagerduty_url(:escalation_on_call)
64
+ return get_object(endpoint)
65
+ end
66
+
67
+ # Given a specific user, return all details about the
68
+ # user that we can parse through as needed
69
+ def get_user_details(user_id)
70
+ endpoint = return_pagerduty_url(:users) + "/#{user_id}/on_call"
71
+ return get_object(endpoint)
72
+ end
73
+
74
+ # Create a page to the first person on call for a given service key
75
+ def post_trigger(service_key:, incident_key:, description:, client:,
76
+ client_url:, details:)
77
+
78
+ # Ruby 2.x style kw-args is required here to make hash passing easier
79
+ endpoint = return_pagerduty_url(:create_trigger)
80
+ data = {
81
+ service_key: service_key,
82
+ incident_key: incident_key,
83
+ event_type: 'trigger',
84
+ description: description,
85
+ client: client,
86
+ client_url: client_url,
87
+ details: details,
88
+ }
89
+
90
+ post_object(endpoint, data)
91
+ end
92
+
93
+ private
94
+ # Helper method for all GETs
95
+ def get_object(endpoint)
96
+ response = HTTParty.get(
97
+ endpoint,
98
+ headers: {
99
+ 'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
100
+ }
101
+ )
102
+ return JSON.load(response.body)
103
+ end
104
+
105
+ # Helper method for all PUTs
106
+ def post_object(endpoint, data)
107
+ response = HTTParty.post(
108
+ endpoint,
109
+ body: data.to_json,
110
+ headers: {
111
+ 'Content-Type' => 'application/json', 'Authorization' => @AUTH_HEADER[:token_string]
112
+ }
113
+ )
114
+
115
+ return response.body
116
+ end
117
+
118
+ # Helper method for building PagerDuty URLs
119
+ def return_pagerduty_url(object_type)
120
+ rest_api_url = "https://#{@AUTH_HEADER[:subdomain]}.pagerduty.com/api/v1"
121
+ incident_api_url = 'https://events.pagerduty.com/generic/2010-04-15'
122
+ case object_type
123
+ when :users
124
+ return rest_api_url + '/users'
125
+ when :schedules
126
+ return rest_api_url + '/schedules'
127
+ when :escalation_on_call
128
+ return rest_api_url + '/escalation_policies/on_call'
129
+ when :create_trigger
130
+ return incident_api_url + '/create_event.json'
131
+ else
132
+ abort("Unknown object type: #{object_type}. Can't build URL.")
133
+ end
134
+ end
131
135
  end
132
136
  end
133
137
  end
@@ -1,41 +1,38 @@
1
- require 'pagerduty/base'
2
- require 'date'
3
-
4
- module TapJoy
5
- module PagerDuty; end
6
- end
7
-
8
- class TapJoy::PagerDuty::Override
9
-
10
- # Initializer services to import values from pg_connect.yaml
11
- # to configure organization-specific values (currently, subdomain and api_token)
12
- def initialize(email, schedule_name, override_length)
13
- pg = TapJoy::PagerDuty::Base.new
14
- override_window_hash = override_window(override_length)
15
- puts pg.set_override(**query_dates, **override_window_hash,
16
- user_id: pg.get_user_id(email),
17
- schedule_id: pg.get_schedule_id(schedule_name) # case-sensitive
18
- )
19
- end
20
-
21
- private
22
- def time_string(time_object)
23
- return time_object.iso8601.to_s
24
- end
25
-
26
- def query_dates
27
- # This shrinks the query to a one-day window
28
- since_date = time_string(Time.now)
29
- until_date = time_string((Time.now + (1*86400)))
30
-
31
- return {query_start: since_date, query_end: until_date}
32
- end
33
-
34
- def override_window(override_time)
35
- from_time = Time.now.iso8601.to_s
36
- # 3600 is number of seconds, change this to alter the override window
37
- until_time = (Time.now + override_time).iso8601.to_s
38
-
39
- return {override_start: from_time, override_end: until_time}
1
+ module Tapjoy
2
+ module PagerDuty
3
+ class Override
4
+
5
+ # Initializer services to import values from pg_connect.yaml
6
+ # to configure organization-specific values (currently, subdomain and api_token)
7
+ def initialize(email, schedule_name, override_length)
8
+ pg = Tapjoy::PagerDuty::Base.new
9
+ override_window_hash = override_window(override_length)
10
+ puts pg.set_override(**query_dates, **override_window_hash,
11
+ user_id: pg.get_user_id(email),
12
+ schedule_id: pg.get_schedule_id(schedule_name) # case-sensitive
13
+ )
14
+ end
15
+
16
+ private
17
+ def time_string(time_object)
18
+ return time_object.iso8601.to_s
19
+ end
20
+
21
+ def query_dates
22
+ # This shrinks the query to a one-day window
23
+ since_date = time_string(Time.now)
24
+ until_date = time_string((Time.now + (1*86400)))
25
+
26
+ return {query_start: since_date, query_end: until_date}
27
+ end
28
+
29
+ def override_window(override_time)
30
+ from_time = Time.now.iso8601.to_s
31
+ # 3600 is number of seconds, change this to alter the override window
32
+ until_time = (Time.now + override_time).iso8601.to_s
33
+
34
+ return {override_start: from_time, override_end: until_time}
35
+ end
36
+ end
40
37
  end
41
38
  end
@@ -0,0 +1,11 @@
1
+ module Tapjoy
2
+ module PagerDuty
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 2
6
+ PATCH = 0
7
+ end
8
+
9
+ VERSION = [Version::MAJOR, Version::MINOR, Version::PATCH].join('.')
10
+ end
11
+ end
metadata CHANGED
@@ -1,15 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagerduty_utils
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
  - Ali Tayarani
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: trollop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
13
69
  description: A set of tools to make leveraging the PagerDuty APIs easier
14
70
  email: ali.tayarani@tapjoy.com
15
71
  executables:
@@ -20,6 +76,7 @@ files:
20
76
  - bin/pgutils
21
77
  - lib/pagerduty/base.rb
22
78
  - lib/pagerduty/override.rb
79
+ - lib/pagerduty/version.rb
23
80
  homepage: https://github.com/tapjoy/pagerduty_utils
24
81
  licenses:
25
82
  - MIT
@@ -43,5 +100,5 @@ rubyforge_project:
43
100
  rubygems_version: 2.2.2
44
101
  signing_key:
45
102
  specification_version: 4
46
- summary: TapJoy PagerDuty Tools
103
+ summary: Tapjoy PagerDuty Tools
47
104
  test_files: []