Class: RuboCop::Cop::Chef::Modernize::ExecuteUpdateAlternatives

Inherits:
Base
  • Object
show all
Extended by:
TargetChefVersion
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/execute_update_alternatives.rb

Overview

Chef Infra Client 16.0 and later includes an alternatives resource that should be used to manage alternatives links instead of shelling out to update-alternatives or alternatives. The resource is idempotent and handles both the Debian and RHEL flavours of the command, where an execute re-runs on every converge and reports the resource as updated every time.

Examples:


# bad
execute 'install java alternative' do
  command 'update-alternatives --install /usr/bin/java java /usr/lib/jvm/jre-11/bin/java 1'
end

execute 'alternatives --set java /usr/lib/jvm/jre-11/bin/java'

# good
alternatives 'java' do
  link '/usr/bin/java'
  path '/usr/lib/jvm/jre-11/bin/java'
  priority 1
  action :install
end

Constant Summary collapse

MSG =
'Chef Infra Client 16.0 and later includes an alternatives resource that should be used to manage alternatives links instead of shelling out to update-alternatives or alternatives'
RESTRICT_ON_SEND =
[:execute].freeze
ALTERNATIVES_COMMAND =

Debian ships update-alternatives and RHEL ships alternatives, and the resource drives both. Matching is limited to the four subcommands the resource can express: --install, --set, --auto, and --remove. The rest are deliberately left alone -- --config and --display are interactive or read only, and --remove-all has no resource equivalent.

%r{
  \A\s*(?:/usr/sbin/|/usr/bin/|/sbin/|/bin/)?  # an optional absolute path to the binary
  (?:update-)?alternatives\s+
  --(?:install|set|auto|remove(?!-all))\b
}x.freeze
SCRIPT_RESOURCES =

the shell resources whose script lives in a code property

%i(bash sh csh ksh zsh).freeze

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods included from RuboCop::Chef::CookbookHelpers

#each_action_symbol, #match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/rubocop/cop/chef/modernize/execute_update_alternatives.rb', line 73

def on_block(node)
  match_property_in_resource?(:execute, 'command', node) do |property|
    report_property(node, property)
  end

  match_property_in_resource?(SCRIPT_RESOURCES, 'code', node) do |property|
    report_property(node, property)
  end
end

#on_send(node) ⇒ Object



66
67
68
69
70
71
# File 'lib/rubocop/cop/chef/modernize/execute_update_alternatives.rb', line 66

def on_send(node)
  command = node.arguments.first
  return unless command&.str_type? && manages_alternatives?(command.value)

  add_offense(node, severity: :refactor)
end