Class: CfnGuardian::Cli
- Inherits:
-
Thor
- Object
- Thor
- CfnGuardian::Cli
- Includes:
- Logging
- Defined in:
- lib/cfnguardian.rb
Class Method Summary collapse
Instance Method Summary collapse
- #__print_version ⇒ Object
- #bulk_deploy ⇒ Object
- #compile ⇒ Object
- #deploy ⇒ Object
- #disable_alarms ⇒ Object
- #enable_alarms ⇒ Object
- #show_alarms ⇒ Object
- #show_config_history ⇒ Object
- #show_drift ⇒ Object
- #show_history ⇒ Object
- #show_pipeline ⇒ Object
- #show_state ⇒ Object
- #tag_alarms ⇒ Object
Methods included from Logging
colors, included, logger, #logger, logger=
Class Method Details
.exit_on_failure? ⇒ Boolean
20 21 22 |
# File 'lib/cfnguardian.rb', line 20 def self.exit_on_failure? true end |
Instance Method Details
#__print_version ⇒ Object
26 27 28 |
# File 'lib/cfnguardian.rb', line 26 def __print_version puts CfnGuardian::VERSION end |
#bulk_deploy ⇒ Object
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/cfnguardian.rb', line 144 def bulk_deploy set_log_level([:debug]) set_region([:region],true) s3 = CfnGuardian::S3.new([:bucket],[:path]) s3.create_bucket_if_not_exists clean_out_directory() template_file_suffix = 'compiled.yaml' compiled = [] [:config].each do |config| config_basename = File.basename(config, ".alarms.yaml") guardian_name = config_basename == "alarms.yaml" ? "" : "-#{config_basename}" template_file = "guardian#{guardian_name}.#{template_file_suffix}" compiler = CfnGuardian::Compile.new(config, [:check_resources_exist]) compiler.get_resources compiler.compile_templates(template_file) logger.info "compiled template to out/#{template_file} from yaml config #{config}" parameters = compiler.load_parameters() compiled << {template_file: template_file, parameters: parameters} logger.debug("template file #{template_file} generated with parameters: #{parameters}") end validator = CfnGuardian::Validate.new(s3.bucket) validator.validate changesets = [] compiled.each do |stack| stack_name = stack[:template_file].gsub(".#{template_file_suffix}", "") deployer = CfnGuardian::Deploy.new(,s3.bucket,stack[:parameters],stack[:template_file],stack_name) deployer.upload_templates logger.info("creating changeset for stack #{stack_name}") change_set, change_set_type = deployer.create_change_set() changesets << {deployer: deployer, id: change_set.id, type: change_set_type} end changesets_executed = [] changesets.each do |changeset| begin changeset[:deployer].wait_for_changeset(changeset[:id]) rescue CfnGuardian::EmptyChangeSetError => e if [:fail_empty_change_set] raise e else logger.info e. next end end logger.info("executing changeset #{changeset[:id]}") changeset[:deployer].execute_change_set(changeset[:id]) logger.info("waiting for changeset #{changeset[:id]} to complete") changeset[:deployer].wait_for_execute(changeset[:type]) end end |
#compile ⇒ Object
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 |
# File 'lib/cfnguardian.rb', line 50 def compile set_log_level([:debug]) set_region([:region],[:validate]) s3 = CfnGuardian::S3.new([:bucket],[:path]) clean_out_directory() compiler = CfnGuardian::Compile.new([:config], [:check_resources_exist]) compiler.get_resources compiler.compile_templates([:template_file]) logger.info "Cloudformation templates compiled successfully in out/ directory" if [:validate] s3.create_bucket_if_not_exists() validator = CfnGuardian::Validate.new(s3.bucket) validator.validate end logger.warn "AWS cloudwatch alarms defined in the templates will cost roughly $#{'%.2f' % compiler.cost} per month" if [:template_config] logger.info "Generating a AWS CodePipeline template configuration file template-config.guardian.json" parameters = compiler.load_parameters() compiler.genrate_template_config(parameters) end end |
#deploy ⇒ Object
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 |
# File 'lib/cfnguardian.rb', line 98 def deploy set_log_level([:debug]) set_region([:region],true) s3 = CfnGuardian::S3.new([:bucket],[:path]) clean_out_directory() compiler = CfnGuardian::Compile.new([:config], [:check_resources_exist]) compiler.get_resources compiler.compile_templates([:template_file]) parameters = compiler.load_parameters() logger.info "Cloudformation templates compiled successfully in out/ directory" s3.create_bucket_if_not_exists validator = CfnGuardian::Validate.new(s3.bucket) validator.validate deployer = CfnGuardian::Deploy.new(,s3.bucket,parameters,[:template_file],[:stack_name]) deployer.upload_templates change_set, change_set_type = deployer.create_change_set() deployer.wait_for_changeset(change_set.id) deployer.execute_change_set(change_set.id) deployer.wait_for_execute(change_set_type) end |
#disable_alarms ⇒ Object
503 504 505 506 507 508 509 510 |
# File 'lib/cfnguardian.rb', line 503 def disable_alarms set_region([:region],true) alarm_names = CfnGuardian::CloudWatch.get_alarm_names([:group],[:alarm_prefix]) CfnGuardian::CloudWatch.disable_alarms(alarm_names) logger.info "Disabled #{alarm_names.length} alarms" end |
#enable_alarms ⇒ Object
522 523 524 525 526 527 528 529 |
# File 'lib/cfnguardian.rb', line 522 def enable_alarms set_region([:region],true) alarm_names = CfnGuardian::CloudWatch.get_alarm_names([:group],[:alarm_prefix]) CfnGuardian::CloudWatch.enable_alarms(alarm_names) logger.info "#{alarm_names.length} alarms enabled" end |
#show_alarms ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/cfnguardian.rb', line 295 def show_alarms set_log_level([:debug]) set_region([:region],[:compare]) if [:config] config_file = [:config] elsif [:defaults] config_file = default_config() else raise Thor::Error, 'one of `--config YAML` or `--defaults` must be supplied' end compiler = CfnGuardian::Compile.new(config_file) compiler.get_resources alarms = filter_compiled_alarms(compiler.alarms,[:filter]) if alarms.empty? raise Thor::Error, "No matches found" end headings = ['Property', 'Config'] formatter = CfnGuardian::DisplayFormatter.new(alarms) if [:compare] && ![:defaults] metric_alarms = CfnGuardian::CloudWatch.get_alarms_by_prefix(prefix: 'guardian') metric_alarms = CfnGuardian::CloudWatch.filter_alarms(filters: [:filter], alarms: metric_alarms) formatted_alarms = formatter.compare_alarms(metric_alarms) headings.push('Deployed') else formatted_alarms = formatter.alarms() end if formatted_alarms.any? formatted_alarms.each do |fa| puts Terminal::Table.new( :title => fa[:title], :headings => headings, :rows => fa[:rows]) end else if [:compare] && ![:defaults] logger.info "No difference found between you config and alarms in deployed AWS" else logger.warn "No alarms found" end end end |
#show_config_history ⇒ Object
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/cfnguardian.rb', line 442 def show_config_history set_region([:region],true) history = CfnGuardian::CodeCommit.new([:repository]).get_commit_history([:branch], [:count]) if history.any? puts Terminal::Table.new( :headings => history.first.keys.map{|h| h.to_s.to_heading}, :rows => history.map(&:values)) end end |
#show_drift ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/cfnguardian.rb', line 265 def show_drift set_region([:region],true) rows = [] drift = CfnGuardian::Drift.new([:stack_name]) nested_stacks = drift.find_nested_stacks nested_stacks.each do |stack| drift.detect_drift(stack) rows << drift.get_drift(stack) end if rows.any? puts Terminal::Table.new( :title => "Guardian Alarm Drift".green, :headings => ['Alarm Name', 'Property', 'Expected', 'Actual', 'Type'], :rows => rows.flatten(1)) end end |
#show_history ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/cfnguardian.rb', line 397 def show_history set_log_level([:debug]) set_region([:region],true) if [:alarm_names] metric_alarms = CfnGuardian::CloudWatch.get_alarms_by_name(alarm_names: [:alarm_names], state: [:state]) else metric_alarms = CfnGuardian::CloudWatch.get_alarms_by_prefix(prefix: [:alarm_prefix], state: [:state]) end metric_alarms = CfnGuardian::CloudWatch.filter_alarms(filters: [:filter], alarms: metric_alarms) case [:type] when 'state' type = 'StateUpdate' headings = ['Date', 'Summary', 'Reason'] when 'config' type = 'ConfigurationUpdate' headings = ['Date', 'Summary', 'Type'] end formatter = CfnGuardian::DisplayFormatter.new() metric_alarms.each do |alarm| history = CfnGuardian::CloudWatch.get_alarm_history(alarm.alarm_name,type) rows = formatter.alarm_history(history,type) if rows.any? puts Terminal::Table.new( :title => alarm.alarm_name.green, :headings => headings, :rows => rows) puts "\n" end end end |
#show_pipeline ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/cfnguardian.rb', line 460 def show_pipeline set_region([:region],true) pipeline = CfnGuardian::CodePipeline.new([:pipeline]) source = pipeline.get_source() build = pipeline.get_build() create = pipeline.get_create_changeset() deploy = pipeline.get_deploy_changeset() puts Terminal::Table.new( :title => "Stage: #{source[:stage]}", :rows => source[:rows]) puts "\t|" puts "\t|" puts Terminal::Table.new( :title => "Stage: #{build[:stage]}", :rows => build[:rows]) puts "\t|" puts "\t|" puts Terminal::Table.new( :title => "Stage: #{create[:stage]}", :rows => create[:rows]) puts "\t|" puts "\t|" puts Terminal::Table.new( :title => "Stage: #{deploy[:stage]}", :rows => deploy[:rows]) end |
#show_state ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/cfnguardian.rb', line 354 def show_state set_log_level([:debug]) set_region([:region],true) action_prefix = nil if [:filter].has_key?('topic') action_prefix = get_topic_arn_from_stack([:filter]['topic']) elsif [:filter].has_key?('maintenance-group') action_prefix = "arn:aws:sns:#{Aws.config[:region]}:#{CfnGuardian::CloudWatch.aws_account_id()}:#{[:filter]['maintenance-group']}MaintenanceGroup" end if [:alarm_names] metric_alarms = CfnGuardian::CloudWatch.get_alarms_by_name(alarm_names: [:alarm_names], state: [:state], action_prefix: action_prefix) else metric_alarms = CfnGuardian::CloudWatch.get_alarms_by_prefix(prefix: [:alarm_prefix], state: [:state], action_prefix: action_prefix) end metric_alarms = CfnGuardian::CloudWatch.filter_alarms(filters: [:filter], alarms: metric_alarms) formatter = CfnGuardian::DisplayFormatter.new() rows = formatter.alarm_state(metric_alarms) if rows.any? puts Terminal::Table.new( :title => "Alarm State", :headings => ['Alarm Name', 'State', 'Changed', 'Notifications'], :rows => rows) else logger.warn "No alarms found" end end |
#tag_alarms ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/cfnguardian.rb', line 216 def tag_alarms set_log_level([:debug]) set_region([:region],true) = .fetch(:tags, {}) if ENV.has_key?('CODEBUILD_RESOLVED_SOURCE_VERSION') [:'guardian:config:commit'] = ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] end [:config].each do |config| config_basename = File.basename(config, "alarms.yaml") stack_name_suffix = config_basename != "" ? "#{config_basename}-" : "" [:'guardian:stack:name'] = "guardian#{stack_name_suffix}" [:'guardian:config:yaml'] = config logger.info "tagging alarms from config file #{config}" compiler = CfnGuardian::Compile.new(config, [:check_resources_exist]) compiler.get_resources alarms = compiler.alarms = compiler..merge() tagger = CfnGuardian::Tagger.new() counter = 0 max_retries = 3 alarms.each do |alarm| begin tagger.tag_alarm(alarm, ) rescue Aws::CloudWatch::Errors::Throttling => e logger.info "cloud watch throttling alarm tagging requests, retrying ..." if (counter += 1) < max_retries sleep(1) redo else logger.warn "throttled max times (#{max_retries}) tagging alarm #{alarm.name}, skipping ..." end end end end end |