From 06bcd0a532ee3060e331465be1b3a3cc6fa42067 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Mon, 8 Apr 2019 12:42:17 +0200 Subject: [PATCH 1/6] Update rubocop from 0.38.x to 0.49.x Reason: CVE-2017-8418 Low severity --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b9e7bf42..37ebc477 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,6 @@ gem 'rake', require: false group :test do gem 'codeclimate-test-reporter' - gem 'rubocop', '~> 0.38.0' + gem 'rubocop', '~> 0.49.0' gem 'pry' end From b8a049f2543ef0d096d64d9be5c2f77fee46205d Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Mon, 8 Apr 2019 13:27:54 +0200 Subject: [PATCH 2/6] make rubocop happy again --- .rubocop.yml | 6 +++++ Gemfile | 2 +- Rakefile | 2 +- lib/moonshot/always_use_default_source.rb | 7 +++--- lib/moonshot/build_mechanism/script.rb | 4 ++-- lib/moonshot/build_mechanism/travis_deploy.rb | 6 ++--- lib/moonshot/change_set.rb | 6 +++-- lib/moonshot/command_line.rb | 2 +- lib/moonshot/command_line_dispatcher.rb | 2 +- lib/moonshot/controller.rb | 3 ++- lib/moonshot/controller_config.rb | 7 +++--- .../deployment_mechanism/code_deploy.rb | 22 +++++++++++++------ lib/moonshot/interactive_logger_proxy.rb | 6 ++--- lib/moonshot/stack.rb | 10 +++++---- lib/moonshot/stack_asg_printer.rb | 8 ++++--- lib/moonshot/stack_events_poller.rb | 2 +- lib/moonshot/tools/asg_rollout.rb | 16 +++++++------- lib/moonshot/tools/asg_rollout/asg.rb | 22 +++++++++++-------- lib/plugins/backup.rb | 21 +++++++----------- lib/plugins/encrypted_parameters.rb | 2 +- spec/moonshot/plugins/backup_spec.rb | 2 +- 21 files changed, 87 insertions(+), 71 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 78a09e30..cf5d90d8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -10,6 +10,8 @@ AllCops: - 'lib/default/**/*' Metrics/AbcSize: Max: 35 +Metrics/BlockLength: + Max: 35 Metrics/MethodLength: Max: 30 Metrics/LineLength: @@ -20,3 +22,7 @@ Metrics/ClassLength: Max: 130 Style/Documentation: Enabled: false +Style/MethodMissing: + Enabled: false +Layout/IndentHeredoc: + Enabled: false diff --git a/Gemfile b/Gemfile index 37ebc477..43bb6d2d 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,6 @@ gem 'rake', require: false group :test do gem 'codeclimate-test-reporter' - gem 'rubocop', '~> 0.49.0' gem 'pry' + gem 'rubocop', '~> 0.49.0' end diff --git a/Rakefile b/Rakefile index 79c9da1e..c6e00cd1 100644 --- a/Rakefile +++ b/Rakefile @@ -10,4 +10,4 @@ end RSpec::Core::RakeTask.new(:spec) -task default: [:spec, :rubocop] +task default: %i[spec rubocop] diff --git a/lib/moonshot/always_use_default_source.rb b/lib/moonshot/always_use_default_source.rb index 22d016f8..de4bf16a 100644 --- a/lib/moonshot/always_use_default_source.rb +++ b/lib/moonshot/always_use_default_source.rb @@ -6,12 +6,11 @@ module Moonshot # answer files or command-line arguments will always apply. class AlwaysUseDefaultSource def get(sp) - unless sp.default? - raise "Parameter #{sp.name} does not have a default, cannot use AlwaysUseDefaultSource!" - end - # Don't do anything, the default will apply on create, and the # previous value will be used on update. + return if sp.default? + + raise "Parameter #{sp.name} does not have a default, cannot use AlwaysUseDefaultSource!" end end end diff --git a/lib/moonshot/build_mechanism/script.rb b/lib/moonshot/build_mechanism/script.rb index 75eef3a0..a41f4411 100644 --- a/lib/moonshot/build_mechanism/script.rb +++ b/lib/moonshot/build_mechanism/script.rb @@ -61,10 +61,10 @@ def run_script(step, env: {}) end result = wait.value - if result.exitstatus == 0 + if result.exitstatus.zero? step.success "Build script #{@script} exited successfully!" end - unless result.exitstatus == 0 + unless result.exitstatus.zero? ilog.error "Build script failed with exit status #{result.exitstatus}!" ilog.error output.join("\n") step.failure "Build script #{@script} failed with exit status #{result.exitstatus}!" diff --git a/lib/moonshot/build_mechanism/travis_deploy.rb b/lib/moonshot/build_mechanism/travis_deploy.rb index d88ac9d7..d2e7dccc 100644 --- a/lib/moonshot/build_mechanism/travis_deploy.rb +++ b/lib/moonshot/build_mechanism/travis_deploy.rb @@ -25,8 +25,7 @@ def initialize(slug, pro: false, timeout: 900) @cli_args = "-r #{@slug} #{@endpoint}" end - def pre_build_hook(_) - end + def pre_build_hook(_); end def build_hook(version) job_number = find_build_and_job(version) @@ -34,8 +33,7 @@ def build_hook(version) check_build(version) end - def post_build_hook(_) - end + def post_build_hook(_); end private diff --git a/lib/moonshot/change_set.rb b/lib/moonshot/change_set.rb index 19c4a2f4..a0082364 100644 --- a/lib/moonshot/change_set.rb +++ b/lib/moonshot/change_set.rb @@ -60,14 +60,16 @@ def execute wait_for_change_set unless @change_set @cf_client.execute_change_set( change_set_name: @name, - stack_name: @stack_name) + stack_name: @stack_name + ) end def delete wait_for_change_set unless @change_set @cf_client.delete_change_set( change_set_name: @name, - stack_name: @stack_name) + stack_name: @stack_name + ) rescue Aws::CloudFormation::Errors::InvalidChangeSetStatus sleep 1 retry diff --git a/lib/moonshot/command_line.rb b/lib/moonshot/command_line.rb index 384161c2..5c6daad4 100644 --- a/lib/moonshot/command_line.rb +++ b/lib/moonshot/command_line.rb @@ -51,7 +51,7 @@ def run! # rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexi # Determine what command is being run, which should be the first argument. command = ARGV.shift - if %w(--help -h help).include?(command) || command.nil? + if %w[--help -h help].include?(command) || command.nil? usage return end diff --git a/lib/moonshot/command_line_dispatcher.rb b/lib/moonshot/command_line_dispatcher.rb index 5fdfa6bc..c5778eec 100644 --- a/lib/moonshot/command_line_dispatcher.rb +++ b/lib/moonshot/command_line_dispatcher.rb @@ -46,7 +46,7 @@ def build_parser(handler) # Each mechanism / plugin may manipulate the OptionParser object # associated with this command. - [:build_mechanism, :deployment_mechanism, :artifact_repository].each do |prov| + %i[build_mechanism deployment_mechanism artifact_repository].each do |prov| provider = Moonshot.config.send(prov) if provider.respond_to?(hook_func_name(@command)) diff --git a/lib/moonshot/controller.rb b/lib/moonshot/controller.rb index 22886a28..a29e7c5e 100644 --- a/lib/moonshot/controller.rb +++ b/lib/moonshot/controller.rb @@ -176,7 +176,8 @@ def doctor def ssh run_plugins(:pre_ssh) @config.ssh_instance ||= SSHTargetSelector.new( - stack, asg_name: @config.ssh_auto_scaling_group_name).choose! + stack, asg_name: @config.ssh_auto_scaling_group_name + ).choose! cb = SSHCommandBuilder.new(@config.ssh_config, @config.ssh_instance) result = cb.build(@config.ssh_command) diff --git a/lib/moonshot/controller_config.rb b/lib/moonshot/controller_config.rb index ea38c5ec..14f65a79 100644 --- a/lib/moonshot/controller_config.rb +++ b/lib/moonshot/controller_config.rb @@ -60,9 +60,10 @@ def in_account(name, &blk) def update_for_account! # Evaluated any account-specific configuration. @account_alias = Moonshot::AccountContext.get - if @account_alias && @per_account_config.key?(account_alias) - @per_account_config[@account_alias].call(self) - end + return unless @account_alias + return unless @per_account_config.key?(@account_alias) + + @per_account_config[@account_alias].call(self) end end end diff --git a/lib/moonshot/deployment_mechanism/code_deploy.rb b/lib/moonshot/deployment_mechanism/code_deploy.rb index 62ddb042..09e5cc46 100644 --- a/lib/moonshot/deployment_mechanism/code_deploy.rb +++ b/lib/moonshot/deployment_mechanism/code_deploy.rb @@ -42,7 +42,8 @@ def initialize( role: DEFAULT_ROLE_NAME, app_name: nil, group_name: nil, - config_name: 'CodeDeployDefault.OneAtATime') + config_name: 'CodeDeployDefault.OneAtATime' + ) @asg_logical_ids = Array(asg) @optional_asg_logical_ids = Array(optional_asg) @app_name = app_name @@ -183,7 +184,9 @@ def load_auto_scaling_groups end groups = as_client.describe_auto_scaling_groups( - auto_scaling_group_names: [asg_name]) + auto_scaling_group_names: [asg_name] + ) + if groups.auto_scaling_groups.empty? raise "Could not find ASG #{asg_name}." end @@ -220,13 +223,16 @@ def application_exists? def deployment_group cd_client.get_deployment_group( - application_name: app_name, deployment_group_name: group_name) - .deployment_group_info + application_name: app_name, + deployment_group_name: group_name + ).deployment_group_info end def deployment_group_exists? cd_client.get_deployment_group( - application_name: app_name, deployment_group_name: group_name) + application_name: app_name, + deployment_group_name: group_name + ) true rescue Aws::CodeDeploy::Errors::ApplicationDoesNotExistException, Aws::CodeDeploy::Errors::DeploymentGroupDoesNotExistException @@ -284,7 +290,8 @@ def delete_deployment_group ilog.start "Deleting #{pretty_deploy_group}." do |s| cd_client.delete_deployment_group( application_name: app_name, - deployment_group_name: group_name) + deployment_group_name: group_name + ) s.success end end @@ -294,7 +301,8 @@ def create_deployment_group application_name: app_name, deployment_group_name: group_name, service_role_arn: role.arn, - auto_scaling_groups: asg_names) + auto_scaling_groups: asg_names + ) end def wait_for_asg_capacity diff --git a/lib/moonshot/interactive_logger_proxy.rb b/lib/moonshot/interactive_logger_proxy.rb index 4fb8b5b8..9ca347c0 100644 --- a/lib/moonshot/interactive_logger_proxy.rb +++ b/lib/moonshot/interactive_logger_proxy.rb @@ -10,8 +10,7 @@ def initialize(logger) @logger = logger end - def blank - end + def blank; end def continue(str = nil) @logger.info(str) if str @@ -21,8 +20,7 @@ def failure(str = 'Failure') @logger.error(str) end - def repaint - end + def repaint; end def success(str = 'Success') @logger.info(str) diff --git a/lib/moonshot/stack.rb b/lib/moonshot/stack.rb index 8d7c1a1f..3c0a3add 100644 --- a/lib/moonshot/stack.rb +++ b/lib/moonshot/stack.rb @@ -152,9 +152,11 @@ def load_template_file # Support the legacy file location from Moonshot 1.0. YamlStackTemplate.new( - File.join(@config.project_root, 'cloud_formation', "#{@config.app_name}.yml")), + File.join(@config.project_root, 'cloud_formation', "#{@config.app_name}.yml") + ), JsonStackTemplate.new( - File.join(@config.project_root, 'cloud_formation', "#{@config.app_name}.json")) + File.join(@config.project_root, 'cloud_formation', "#{@config.app_name}.json") + ) ] template = templates.find(&:exist?) @@ -199,7 +201,7 @@ def upload_template_to_s3 def create_stack parameters = { stack_name: @name, - capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM), + capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM], parameters: @config.parameters.values.map(&:to_cf), tags: make_tags } @@ -224,7 +226,7 @@ def new_change_set change_set_name: change_set_name, description: "Moonshot update command for application '#{Moonshot.config.app_name}'", stack_name: @name, - capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM), + capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM], parameters: @config.parameters.values.map(&:to_cf) } if @config.template_s3_bucket diff --git a/lib/moonshot/stack_asg_printer.rb b/lib/moonshot/stack_asg_printer.rb index 779059c6..93261f1f 100644 --- a/lib/moonshot/stack_asg_printer.rb +++ b/lib/moonshot/stack_asg_printer.rb @@ -1,4 +1,5 @@ # coding: utf-8 + require 'colorize' require 'ruby-duration' @@ -16,8 +17,8 @@ def initialize(stack, table) def print asgs.each do |asg| asg_info = as_client.describe_auto_scaling_groups( - auto_scaling_group_names: [asg.physical_resource_id]) - .auto_scaling_groups.first + auto_scaling_group_names: [asg.physical_resource_id] + ).auto_scaling_groups.first t_asg_info = @table.add_leaf("ASG: #{asg.logical_resource_id}") add_asg_info(t_asg_info, asg_info) @@ -142,7 +143,8 @@ def uptime_format(launch_time) def add_recent_activity_leaf(table, asg_name) recent = table.add_leaf('Recent Activity') resp = as_client.describe_scaling_activities( - auto_scaling_group_name: asg_name).activities + auto_scaling_group_name: asg_name + ).activities rows = resp.sort_by(&:start_time).reverse.first(10).map do |activity| row_for_activity(activity) diff --git a/lib/moonshot/stack_events_poller.rb b/lib/moonshot/stack_events_poller.rb index 46a90092..19d32c7c 100644 --- a/lib/moonshot/stack_events_poller.rb +++ b/lib/moonshot/stack_events_poller.rb @@ -32,7 +32,7 @@ def latest_events def filter_events(events) if @errors_only events.select do |event| - %w(CREATE_FAILED UPDATE_FAILED DELETE_FAILED).include?(event.resource_status) + %w[CREATE_FAILED UPDATE_FAILED DELETE_FAILED].include?(event.resource_status) end else events diff --git a/lib/moonshot/tools/asg_rollout.rb b/lib/moonshot/tools/asg_rollout.rb index 0986ea90..d5d720e9 100644 --- a/lib/moonshot/tools/asg_rollout.rb +++ b/lib/moonshot/tools/asg_rollout.rb @@ -83,16 +83,16 @@ def wait_for_in_service(new_instance) end def run_pre_detach(instance) - if @config.pre_detach - log.start_threaded "Running PreDetach hook on #{instance.blue}..." do |s| - he = HookExecEnvironment.new(@controller.config, instance) - if false == @config.pre_detach.call(he) - s.failure "PreDetach hook failed for #{instance.blue}!" - raise "PreDetach hook failed for #{instance.blue}!" - end + return unless @config.pre_detach - s.success "PreDetach hook complete for #{instance.blue}!" + log.start_threaded "Running PreDetach hook on #{instance.blue}..." do |s| + he = HookExecEnvironment.new(@controller.config, instance) + if @config.pre_detach.call(he) == false + s.failure "PreDetach hook failed for #{instance.blue}!" + raise "PreDetach hook failed for #{instance.blue}!" end + + s.success "PreDetach hook complete for #{instance.blue}!" end end diff --git a/lib/moonshot/tools/asg_rollout/asg.rb b/lib/moonshot/tools/asg_rollout/asg.rb index 44332572..22f7450a 100644 --- a/lib/moonshot/tools/asg_rollout/asg.rb +++ b/lib/moonshot/tools/asg_rollout/asg.rb @@ -20,14 +20,15 @@ def set_max_and_desired(max, desired) autoscaling.update_auto_scaling_group( auto_scaling_group_name: @name, max_size: max, - desired_capacity: desired) + desired_capacity: desired + ) end def non_conforming_instances asg = load_asg asg.instances - .select { |i| i.launch_configuration_name != asg.launch_configuration_name } + .reject { |i| i.launch_configuration_name == asg.launch_configuration_name } .map(&:instance_id) end @@ -59,7 +60,8 @@ def detach_instance(id, decrement:) resp = autoscaling.detach_instances( auto_scaling_group_name: @name, instance_ids: [id], - should_decrement_desired_capacity: decrement) + should_decrement_desired_capacity: decrement + ) activity = resp.activities.first unless activity @@ -69,7 +71,8 @@ def detach_instance(id, decrement:) # Wait for the detach activity to complete: loop do resp = autoscaling.describe_scaling_activities( - auto_scaling_group_name: @name) + auto_scaling_group_name: @name + ) current_status = resp.activities .find { |a| a.activity_id == activity.activity_id } @@ -97,7 +100,8 @@ def instance_health(id) def asg_instance_state(id) resp = autoscaling.describe_auto_scaling_instances( - instance_ids: [id]) + instance_ids: [id] + ) instance_info = resp.auto_scaling_instances.first return 'Missing' unless instance_info @@ -108,9 +112,8 @@ def asg_instance_state(id) def elb_instance_state(id) resp = loadbalancing.describe_instance_health( load_balancer_name: elb_name, - instances: [ - { instance_id: id } - ]) + instances: [{ instance_id: id }] + ) instance_info = resp.instance_states.first unless instance_info @@ -133,7 +136,8 @@ def loadbalancing def load_asg resp = autoscaling.describe_auto_scaling_groups( - auto_scaling_group_names: [@name]) + auto_scaling_group_names: [@name] + ) if resp.auto_scaling_groups.empty? raise "Failed to call DescribeAutoScalingGroups for #{@name}!" diff --git a/lib/plugins/backup.rb b/lib/plugins/backup.rb index 67e2b1b5..78e4e334 100644 --- a/lib/plugins/backup.rb +++ b/lib/plugins/backup.rb @@ -20,7 +20,7 @@ class Backup # rubocop:disable Metrics/ClassLength def initialize yield self if block_given? validate_configuration - @target_name ||= '%{app_name}_%{timestamp}_%{user}.tar.gz' + @target_name ||= '%s_%s_%s.tar.gz' end # Factory method to create preconfigured Backup plugins. Uploads current @@ -33,7 +33,7 @@ def self.to_bucket(bucket) b.bucket = bucket b.backup_parameters = true b.backup_template = true - b.hooks = [:post_create, :post_update] + b.hooks = %i[post_create post_update] end end @@ -110,7 +110,7 @@ def tar(target_files) # adding template file if @backup_template - template_file_path = render('cloud_formation/%{app_name}.json') + template_file_path = render('cloud_formation/%s.json') add_file_to_tar(writer, template_file_path) end end @@ -123,7 +123,7 @@ def tar(target_files) # @param writer [TarWriter] # @param file_name [String] def add_file_to_tar(writer, file_name) - writer.add_file(File.basename(file_name), 0644) do |io| + writer.add_file(File.basename(file_name), 0o644) do |io| begin File.open(file_name, 'r') { |f| io.write(f.read) } rescue Errno::ENOENT @@ -139,7 +139,7 @@ def add_file_to_tar(writer, file_name) # @param target_filename [String] # @param content [String] def add_str_to_tar(writer, target_filename, content) - writer.add_file(File.basename(target_filename), 0644) do |io| + writer.add_file(File.basename(target_filename), 0o644) do |io| io.write(content.to_yaml) end end @@ -196,17 +196,12 @@ def iam_account end def define_bucket - case # returning already calculated bucket name - when @target_bucket - @target_bucket + return @target_bucket if @target_bucket # single bucket for all accounts - when @bucket - @bucket + return @bucket if @bucket # calculating bucket based on account name - when @buckets - bucket_by_account(iam_account) - end + return bucket_by_account(iam_account) if @buckets end def bucket_by_account(account) diff --git a/lib/plugins/encrypted_parameters.rb b/lib/plugins/encrypted_parameters.rb index 705676a7..7017df9d 100644 --- a/lib/plugins/encrypted_parameters.rb +++ b/lib/plugins/encrypted_parameters.rb @@ -96,7 +96,7 @@ def find_or_create_kms_key @ilog.start_threaded "Checking for KMS Key #{@kms_key_parameter_name}" do |s| if Moonshot.config.parameters.key?(@kms_key_parameter_name) - if 'Auto' == Moonshot.config.parameters[@kms_key_parameter_name].value + if Moonshot.config.parameters[@kms_key_parameter_name].value == 'Auto' s.continue "Auto-generating KMS Key for #{@kms_key_parameter_name.blue}... " key_arn = KmsKey.create.arn Moonshot.config.parameters[@kms_key_parameter_name].set(key_arn) diff --git a/spec/moonshot/plugins/backup_spec.rb b/spec/moonshot/plugins/backup_spec.rb index 64b6e2e1..638ddb3a 100644 --- a/spec/moonshot/plugins/backup_spec.rb +++ b/spec/moonshot/plugins/backup_spec.rb @@ -53,7 +53,7 @@ end end it 'should set a default value to target_name if not specified' do - expect(backup.target_name).to eq '%{app_name}_%{timestamp}_%{user}.tar.gz' + expect(backup.target_name).to eq '%s_%s_%s.tar.gz' end end From ee26bf400a562769138aae766f2f6f9e8309bd92 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Tue, 16 Apr 2019 08:23:50 +0200 Subject: [PATCH 3/6] try to fix travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9caa93b9..bdf23711 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,8 +15,8 @@ addons: - cmake before_install: - - gem update --system - - gem update bundler + - gem i rubygems-update -v '<3' && update_rubygems + - gem install bundler rvm: - 2.1 From 626e512007c695824e0cbd099b6111d21338f28b Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Tue, 16 Apr 2019 08:58:16 +0200 Subject: [PATCH 4/6] revert travisfile changes, originall at least 2.2 worked --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bdf23711..683572f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ addons: - cmake before_install: - - gem i rubygems-update -v '<3' && update_rubygems + - gem update --system - gem install bundler rvm: From 3fe912da00b67dfda6c82441fd62c6e9352dba58 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Tue, 16 Apr 2019 09:06:12 +0200 Subject: [PATCH 5/6] pin bundler in travisfile? --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 683572f0..0e52d508 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,8 +15,8 @@ addons: - cmake before_install: - - gem update --system - - gem install bundler + - gem i rubygems-update -v '<3' && update_rubygems + - gem install bundler -v 1.17.3 rvm: - 2.1 From 75371aa897007ea07ea2591bd7ad9a691cc3e169 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Tue, 16 Apr 2019 15:35:05 +0200 Subject: [PATCH 6/6] drop ruby 2.1 --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0e52d508..fee2d5ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,10 +15,9 @@ addons: - cmake before_install: - - gem i rubygems-update -v '<3' && update_rubygems - - gem install bundler -v 1.17.3 + - gem update --system + - gem update bundler rvm: - - 2.1 - 2.2 - 2.3.3