Class: Bolt::Applicator

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

Instance Method Summary collapse

Constructor Details

#initialize(inventory, executor, modulepath, pdb_client, hiera_config, max_compiles) ⇒ Applicator

Returns a new instance of Applicator.



13
14
15
16
17
18
19
20
21
22
# File 'lib/bolt/applicator.rb', line 13

def initialize(inventory, executor, modulepath, pdb_client, hiera_config, max_compiles)
  @inventory = inventory
  @executor = executor
  @modulepath = modulepath
  @pdb_client = pdb_client
  @hiera_config = hiera_config ? validate_hiera_config(hiera_config) : nil

  @pool = Concurrent::ThreadPoolExecutor.new(max_threads: max_compiles)
  @logger = Logging.logger[self]
end

Instance Method Details

#apply(args, apply_body, scope) ⇒ Object

Raises:

  • (ArgumentError)


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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bolt/applicator.rb', line 142

def apply(args, apply_body, scope)
  raise(ArgumentError, 'apply requires a TargetSpec') if args.empty?
  type0 = Puppet.lookup(:pal_script_compiler).type('TargetSpec')
  Puppet::Pal.assert_type(type0, args[0], 'apply targets')

  options = {}
  if args.count > 1
    type1 = Puppet.lookup(:pal_script_compiler).type('Hash[String, Data]')
    Puppet::Pal.assert_type(type1, args[1], 'apply options')
    options = args[1]
  end

  # collect plan vars and merge them over target vars
  plan_vars = scope.to_hash
  %w[trusted server_facts facts].each { |k| plan_vars.delete(k) }

  targets = @inventory.get_targets(args[0])
  ast = Puppet::Pops::Serialization::ToDataConverter.convert(apply_body, rich_data: true, symbol_to_string: true)
  notify = proc { |_| nil }

  r = @executor.log_action('apply catalog', targets) do
    futures = targets.map do |target|
      Concurrent::Future.execute(executor: @pool) do
        @executor.with_node_logging("Compiling manifest block", [target]) do
          compile(target, ast, plan_vars)
        end
      end
    end

    result_promises = targets.zip(futures).flat_map do |target, future|
      @executor.queue_execute([target]) do |transport, batch|
        @executor.with_node_logging("Applying manifest block", batch) do
          arguments = { 'catalog' => future.value, '_noop' => options['_noop'] }
          raise future.reason if future.rejected?
          result = transport.batch_task(batch, catalog_apply_task, arguments, options, &notify)
          result = provide_puppet_missing_errors(result)
          identify_resource_failures(result)
        end
      end
    end

    @executor.await_results(result_promises)
  end

  if !r.ok && !options['_catch_errors']
    raise Bolt::ApplyFailure, r
  end
  r
end

#catalog_apply_taskObject



28
29
30
31
32
33
34
# File 'lib/bolt/applicator.rb', line 28

def catalog_apply_task
  @catalog_apply_task ||= begin
    path = File.join(libexec, 'apply_catalog.rb')
    impl = { 'name' => 'apply_catalog.rb', 'path' => path, 'requirements' => [], 'supports_noop' => true }
    Task.new('apply_catalog', [impl], 'stdin')
  end
end

#compile(target, ast, plan_vars) ⇒ Object

Raises:



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
# File 'lib/bolt/applicator.rb', line 36

def compile(target, ast, plan_vars)
  trusted = Puppet::Context::TrustedInformation.new('local', target.host, {})

  catalog_input = {
    code_ast: ast,
    modulepath: @modulepath,
    pdb_config: @pdb_client.config.to_hash,
    hiera_config: @hiera_config,
    target: {
      name: target.host,
      facts: @inventory.facts(target),
      variables: @inventory.vars(target).merge(plan_vars),
      trusted: trusted.to_h
    }
  }

  bolt_catalog_exe = File.join(libexec, 'bolt_catalog')

  old_path = ENV['PATH']
  ENV['PATH'] = "#{RbConfig::CONFIG['bindir']}#{File::PATH_SEPARATOR}#{old_path}"
  out, err, stat = Open3.capture3('ruby', bolt_catalog_exe, 'compile', stdin_data: catalog_input.to_json)
  ENV['PATH'] = old_path

  # stderr may contain formatted logs from Puppet's logger or other errors.
  # Print them in order, but handle them separately. Anything not a formatted log is assumed
  # to be an error message.
  logs = err.lines.map do |l|
    begin
      JSON.parse(l)
    rescue StandardError
      l
    end
  end
  logs.each do |log|
    if log.is_a?(String)
      @logger.error(log.chomp)
    else
      log.map { |k, v| [k.to_sym, v] }.each do |level, msg|
        bolt_level = Bolt::Util::PuppetLogLevel::MAPPING[level]
        @logger.send(bolt_level, "#{target.name}: #{msg.chomp}")
      end
    end
  end

  raise(ApplyError, target.name) unless stat.success?
  JSON.parse(out)
end

#identify_resource_failures(result) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bolt/applicator.rb', line 125

def identify_resource_failures(result)
  if result.ok? && result.value['status'] == 'failed'
    resources = result.value['resource_statuses']
    failed = resources.select { |_, r| r['failed'] }.flat_map do |key, resource|
      resource['events'].select { |e| e['status'] == 'failure' }.map do |event|
        "\n  #{key}: #{event['message']}"
      end
    end

    result.value['_error'] = {
      'msg' => "Resources failed to apply for #{result.target.name}#{failed.join}",
      'kind' => 'bolt/resource-failure'
    }
  end
  result
end

#provide_puppet_missing_errors(result) ⇒ Object



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
# File 'lib/bolt/applicator.rb', line 94

def provide_puppet_missing_errors(result)
  error_hash = result.error_hash
  exit_code = error_hash['details']['exit_code'] if error_hash && error_hash['details']
  # If we get exit code 126 or 127 back, it means the shebang command wasn't found; Puppet isn't present
  if [126, 127].include?(exit_code)
    Result.new(result.target, error:
      {
        'msg' => "Puppet is not installed on the target, please install it to enable 'apply'",
        'kind' => 'bolt/apply-error'
      })
  elsif exit_code == 1 && error_hash['msg'] =~ /Could not find executable 'ruby.exe'/
    # Windows does not have Ruby present
    Result.new(result.target, error:
      {
        'msg' => "Puppet is not installed on the target in $env:ProgramFiles, please install it to enable 'apply'",
        'kind' => 'bolt/apply-error'
      })
  elsif exit_code == 1 && error_hash['msg'] =~ /cannot load such file -- puppet \(LoadError\)/
    # Windows uses a Ruby that doesn't have Puppet installed
    # TODO: fix so we don't find other Rubies, or point to a known issues URL for more info
    Result.new(result.target, error:
      {
        'msg' => 'Found a Ruby without Puppet present, please install Puppet ' \
                 "or remove Ruby from $env:Path to enable 'apply'",
        'kind' => 'bolt/apply-error'
      })
  else
    result
  end
end

#validate_hiera_config(hiera_config) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/bolt/applicator.rb', line 84

def validate_hiera_config(hiera_config)
  if File.exist?(File.path(hiera_config))
    data = File.open(File.path(hiera_config), "r:UTF-8") { |f| YAML.safe_load(f.read) }
    unless data['version'] == 5
      raise Bolt::ParseError, "Hiera v5 is required, found v#{data['version'] || 3} in #{hiera_config}"
    end
    hiera_config
  end
end