Class: BoltServer::TransportApp

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/bolt_server/transport_app.rb

Constant Summary collapse

PARTIAL_SCHEMAS =

These partial schemas are reused to build multiple request schemas

%w[target-any target-ssh target-winrm task].freeze
REQUEST_SCHEMAS =

These schemas combine shared schemas to describe client requests

%w[action-run_task transport-ssh transport-winrm].freeze
ACTIONS =
%w[run_task].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ TransportApp

Returns a new instance of TransportApp.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bolt_server/transport_app.rb', line 24

def initialize(config)
  @config = config
  @schemas = Hash[REQUEST_SCHEMAS.map do |basename|
    [basename, JSON.parse(File.read(File.join(__dir__, ['schemas', "#{basename}.json"])))]
  end]

  PARTIAL_SCHEMAS.each do |basename|
    schema_content = JSON.parse(File.read(File.join(__dir__, ['schemas', 'partials', "#{basename}.json"])))
    shared_schema = JSON::Schema.new(schema_content, Addressable::URI.parse("partial:#{basename}"))
    JSON::Validator.add_schema(shared_schema)
  end

  @executor = Bolt::Executor.new(0)

  @file_cache = BoltServer::FileCache.new(@config).setup

  super(nil)
end

Instance Method Details

#run_task(target, body) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bolt_server/transport_app.rb', line 59

def run_task(target, body)
  error = validate_schema(@schemas["action-run_task"], body)
  return [400, error.to_json] unless error.nil?

  task = Bolt::Task::PuppetServer.new(body['task'], @file_cache)
  parameters = body['parameters'] || {}
  results = @executor.run_task(target, task, parameters)

  # Since this will only be on one node we can just return the first result
  result = scrub_stack_trace(results.first.status_hash)
  [200, result.to_json]
end

#scrub_stack_trace(result) ⇒ Object



43
44
45
46
47
48
# File 'lib/bolt_server/transport_app.rb', line 43

def scrub_stack_trace(result)
  if result.dig(:result, '_error', 'details', 'stack_trace')
    result[:result]['_error']['details'].reject! { |k| k == 'stack_trace' }
  end
  result
end

#validate_schema(schema, body) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/bolt_server/transport_app.rb', line 50

def validate_schema(schema, body)
  schema_error = JSON::Validator.fully_validate(schema, body)
  if schema_error.any?
    Bolt::Error.new("There was an error validating the request body.",
                    'boltserver/schema-error',
                    schema_error)
  end
end