Class: SmartNotify

Inherits:
Object
  • Object
show all
Defined in:
lib/smartnotify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSmartNotify

Returns a new instance of SmartNotify.



10
11
12
13
14
# File 'lib/smartnotify.rb', line 10

def initialize
  Logging.logger.root.appenders = Logging.appenders.stdout
  Logging.logger.root.level = :debug
  @log = Logging.logger['smartnotify']
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/smartnotify.rb', line 8

def config
  @config
end

Instance Method Details

#run(template, json, env = 'production', debug = false, dryrun = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/smartnotify.rb', line 16

def run(template, json, env = 'production', debug = false, dryrun = false)

  # Ensure Options Provided
  if json.nil? || template.nil? || env.nil?
    @log.error 'Must provide template, JSON data, and environment name.'
    return false
  end

  # Load Configuration
  internal_config = YAML.load_file(File.join(__dir__, 'config/config.yml'))
  @log.info 'Loading SmartNotify config from:'
  @log.info " #{internal_config['external-config-path']}"

  external_config = nil
  begin
    external_config = YAML.load(open(internal_config['external-config-path']).read)
  rescue
    @log.error 'Unable to load and parse configuration.'
    @log.error 'Check that the file exists, and that it is valid YMAL.'
    return false
  end

  # Load Environment Configuration
  env = env.downcase

  @log.info ''
  @log.info "SmartNotify Environment: #{env}"

  config = external_config[env]
  if config.nil?
    @log.error 'Configuration does not contain configuration for specified environment.'
    return false
  end

  # Load Template Configuration
  template_config = config[template]
  if template_config.nil?
    @log.error "Template '#{template}' not found."
    return false
  end

  # Configure Development Flags
  config['global']['dryrun'] = dryrun


  # Load JSON
  begin
    template_data = JSON.parse(json)
  rescue JSON::ParserError => e
    puts 'ERROR: JSON provided is unable to be parsed.'
    return false
  end

  # Prepare Module Configuration
  module_config = Hash.new
  module_config[:config] = template_config
  module_config[:data] = template_data
  module_config[:global] = config['global']

  # Ensure Required Fields Present
  unless module_config[:config]['required_fields'].nil?
    module_config[:config]['required_fields'].each do |field|
      if module_config[:data][field].nil?
        @log.error "JSON provided is missing required field '#{field}'"
        return false
      end
    end
  end

  if module_config[:config]['module'] == 'rotation_emailer'
    require_relative 'modules/rotation_emailer/rotation_emailer'
  elsif module_config[:config]['module'] == 'slack_notifier'
    require_relative 'modules/slack_notifier/slack_notifier'
  else
    @log.error "'#{module_config[:config]['module']}' module does not exist."
    @log.error 'Ensure the correct module name is defined in the configuration.'
    return false
  end

  # Run Module
  NotificationModule.new(module_config).run
end