Class: CfnMonitor::Generate
- Inherits:
-
Object
- Object
- CfnMonitor::Generate
- Defined in:
- lib/cfn_monitor/generate.rb
Class Method Summary collapse
- .get_alarm_envs(params) ⇒ Object
- .run(options) ⇒ Object
- .write_cfdndsl_template(alarm_parameters, alarms_config_file, configs, custom_alarms_config_file, source_bucket, template_envs, output_path, upload_path, verbose_cfndsl) ⇒ Object
Class Method Details
.get_alarm_envs(params) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/cfn_monitor/generate.rb', line 196 def self.get_alarm_envs(params) envs = [] params.each do | key,value | if key.include? '.' envs << key.split('.').last end end return envs end |
.run(options) ⇒ Object
11 12 13 14 15 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cfn_monitor/generate.rb', line 11 def self.run() if ['silent'] verbose_cfndsl = false else verbose_cfndsl = STDOUT end if ['application'] application = ['application'] custom_alarms_config_file = "#{application}/alarms.yml" custom_templates_config_file = "#{application}/templates.yml" output_path = "output/#{application}" else application = File.basename(Dir.getwd) custom_alarms_config_file = "alarms.yml" custom_templates_config_file = "templates.yml" output_path = "output" end upload_path = "cloudformation/monitoring/#{application}" application = File.basename(Dir.getwd) template_path = File.join(File.dirname(__FILE__),'../config/templates.yml') config_path = File.join(File.dirname(__FILE__),'../config/config.yml') # Load global config files global_templates_config = YAML.load(File.read(template_path)) config = YAML.load(File.read(config_path)) # Load custom config files if File.file?(custom_alarms_config_file) custom_alarms_config = YAML.load(File.read(custom_alarms_config_file)) else puts "Failed to load #{custom_alarms_config_file}" exit 1 end # Merge custom template configs over global template configs if File.file?(custom_templates_config_file) custom_templates_config = YAML.load(File.read(custom_templates_config_file)) templates = CfnMonitor::Utils.deep_merge(global_templates_config, custom_templates_config) else templates = global_templates_config end # Create an array of alarms based on the templates associated with each resource alarms = [] resources = custom_alarms_config['resources'] metrics = custom_alarms_config['metrics'] hosts = custom_alarms_config['hosts'] || {} ssl = custom_alarms_config['ssl'] || {} dns = custom_alarms_config['dns'] || {} sql = custom_alarms_config['sql'] || {} services = custom_alarms_config['services'] || {} endpoints = custom_alarms_config['endpoints'] || {} ecsClusters = custom_alarms_config['ecsClusters'] || {} alarm_parameters = { resources: resources, metrics: metrics, endpoints: endpoints, hosts: hosts, ssl: ssl, dns: dns, sql: sql, services: services, ecsClusters: ecsClusters } source_bucket = custom_alarms_config['source_bucket'] alarm_parameters.each do | k,v | if !v.nil? v.each do | resource,attributeList | # set environments to 'all' by default environments = ['all'] # Support config hashs for additional parameters params = {} if !attributeList.kind_of?(Array) attributeList = [attributeList] end attributeList.each do | attributes | if attributes.kind_of?(Hash) attributes.each do | a,b | environments = b if a == 'environments' # Convert strings to arrays for consistency if !environments.kind_of?(Array) then environments = environments.split end params[a] = b if !['template','environments'].member? a end templatesEnabled = attributes['template'] else templatesEnabled = attributes end # Convert strings to arrays for looping if !templatesEnabled.kind_of?(Array) then templatesEnabled = templatesEnabled.split end templatesEnabled.each do | templateEnabled | if !templates['templates'][templateEnabled].nil? # If a template is provided, inherit that template if !templates['templates'][templateEnabled]['template'].nil? template_from = Marshal.load( Marshal.dump(templates['templates'][templates['templates'][templateEnabled]['template']]) ) template_to = templates['templates'][templateEnabled].without('template') template_merged = CfnMonitor::Utils.deep_merge(template_from, template_to) templates['templates'][templateEnabled] = template_merged end templates['templates'][templateEnabled].each do | alarm,parameters | resourceParams = parameters.clone # Override template params if overrides provided params.each do | x,y | resourceParams[x] = y end if k == :hosts resourceParams['cmds'].each do |cmd| hostParams = resourceParams.clone hostParams['cmd'] = cmd # Construct alarm object per cmd alarms << { resource: resource, type: k[0...-1], template: templateEnabled, alarm: alarm, parameters: hostParams, environments: environments } end else # Construct alarm object alarms << { resource: resource, type: k[0...-1], template: templateEnabled, alarm: alarm, parameters: resourceParams, environments: environments } end end end end end end end end # Create temp alarms file for CfnDsl temp_file = Tempfile.new(["alarms-",'.yml']) temp_file_path = temp_file.path temp_file.write({'alarms' => alarms}.to_yaml) temp_file.rewind # Split resources for mulitple templates to avoid CloudFormation template resource limits split = [] template_envs = ['production'] alarms.each_with_index do |alarm,index| split[index/config['resource_limit']] ||= {} split[index/config['resource_limit']]['alarms'] ||= [] split[index/config['resource_limit']]['alarms'] << alarm template_envs |= get_alarm_envs(alarm[:parameters]) end # Create temp files for split resources for CfnDsl input temp_files=[] temp_file_paths=[] (alarms.count/config['resource_limit'].to_f).ceil.times do | i | temp_files[i] = Tempfile.new(["alarms-#{i}-",'.yml']) temp_file_paths[i] = temp_files[i].path temp_files[i].write(split[i].to_yaml) temp_files[i].rewind end write_cfdndsl_template(alarm_parameters, temp_file_path, temp_file_paths, custom_alarms_config_file, source_bucket, template_envs, output_path, upload_path, verbose_cfndsl) end |
.write_cfdndsl_template(alarm_parameters, alarms_config_file, configs, custom_alarms_config_file, source_bucket, template_envs, output_path, upload_path, verbose_cfndsl) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/cfn_monitor/generate.rb', line 175 def self.write_cfdndsl_template(alarm_parameters,alarms_config_file,configs,custom_alarms_config_file,source_bucket,template_envs,output_path,upload_path,verbose_cfndsl) template_path = File.("../../templates", __FILE__) FileUtils::mkdir_p output_path configs.each_with_index do |config,index| File.open("#{output_path}/resources#{index}.json", 'w') { |file| file.write(JSON.pretty_generate( CfnDsl.eval_file_with_extras("#{template_path}/resources.rb",[[:yaml, config],[:raw, "template_number=#{index}"],[:raw, "source_bucket='#{source_bucket}'"],[:raw, "upload_path='#{upload_path}'"]],verbose_cfndsl)))} File.open("#{output_path}/alarms#{index}.json", 'w') { |file| file.write(JSON.pretty_generate( CfnDsl.eval_file_with_extras("#{template_path}/alarms.rb",[[:yaml, config],[:raw, "template_number=#{index}"],[:raw, "template_envs=#{template_envs}"]],verbose_cfndsl)))} end ['endpoints', 'ssl', "dns", 'sql', 'hosts', 'services','ecsClusters'].each do |template| if !alarm_parameters[template.to_sym].nil? and alarm_parameters[template.to_sym] != {} File.open("#{output_path}/#{template}.json", 'w') { |file| file.write(JSON.pretty_generate( CfnDsl.eval_file_with_extras("#{template_path}/#{template}.rb",[[:yaml, alarms_config_file],[:raw, "template_envs=#{template_envs}"]],verbose_cfndsl))) } end end File.open("#{output_path}/master.json", 'w') { |file| file.write(JSON.pretty_generate( CfnDsl.eval_file_with_extras("#{template_path}/master.rb",[[:yaml, custom_alarms_config_file],[:raw, "templateCount=#{configs.count}"],[:raw, "template_envs=#{template_envs}"],[:raw, "upload_path='#{upload_path}'"]],verbose_cfndsl)))} end |