Module: PagerdutyCli::Common

Included in:
Resolve, Trigger
Defined in:
lib/pagerduty_cli/common.rb

Overview

Common functionality to pagerduty interaction

Constant Summary collapse

API_KEY_FILE =
'/etc/pagerduty_api.key'

Instance Method Summary collapse

Instance Method Details

#croak(message) ⇒ Object



12
13
14
15
# File 'lib/pagerduty_cli/common.rb', line 12

def croak(message)
  $stderr.puts "#{@me} Error: #{message} Exiting."
  exit 1
end

#incident_fileObject

return the name for our incident state file.



27
28
29
# File 'lib/pagerduty_cli/common.rb', line 27

def incident_file
  File.join(@options[:tmpdir], "pagerduty-#{incident_key}")
end

#incident_keyObject



21
22
23
24
# File 'lib/pagerduty_cli/common.rb', line 21

def incident_key
  event_key = "#{@options[:host]}:#{@options[:event]}"
  Digest::SHA1.hexdigest(event_key)
end

#load_dataObject



6
7
8
9
10
# File 'lib/pagerduty_cli/common.rb', line 6

def load_data
  @api_key = File.open(@options[:api_key_file]).read.chomp
rescue Errno::ENOENT, Errno::EPERM
  croak("Could not open PD API key file at #{@options[:api_key_file]}.")
end

#parse_common_options(opts) ⇒ Object

Disabling the MethodLength cop here because any reduction would reduce readability. rubocop:disable MethodLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pagerduty_cli/common.rb', line 34

def parse_common_options(opts)
  opts.banner = "Usage: #{@me} [options]"
  @options ||= {}
  @options.merge!(host: ENV['HOSTNAME'],
                  api_key_file: API_KEY_FILE,
                  tmpdir: '/tmp')
  opts.on('-H', '--host HOST', 'Report from the hostname given') do |h|
    @options[:host] = h
  end
  opts.on('-k', '--keyfile KEYFILE',
          'Use the key specified in file KEYFILE') do |kf|
    @options[:api_key_file] = kf
          end
  opts.on('-e', '--event EVENT', 'Report the event given') do |e|
    @options[:event] = e
  end
  opts.on('-t', '--tmpdir PATH',
          'location for incident files') do |tmp|
    @options[:tmpdir] = tmp
  end
  opts.on_tail('-h', '--help', 'Show this message') do
    $stderr.puts opts
    exit
  end
end

#require_eventObject

croak if required options are not present



62
63
64
65
# File 'lib/pagerduty_cli/common.rb', line 62

def require_event
  return if @options[:event]
  croak('Error: event option is required. Run with -h to see usage.')
end

#warn(message) ⇒ Object



17
18
19
# File 'lib/pagerduty_cli/common.rb', line 17

def warn(message)
  $stderr.puts "#{@me} Warning: #{message}"
end