Class: BoltServer::TransportApp
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- BoltServer::TransportApp
- 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-check_node_connections action-run_command action-run_task action-run_script action-upload_file transport-ssh transport-winrm connect-data action-apply_prep action-apply ].freeze
- PE_BOLTLIB_PATH =
PE_BOLTLIB_PATH is intended to function exactly like the BOLTLIB_PATH used in Bolt::PAL. Paths and variable names are similar to what exists in Bolt::PAL, but with a ‘PE’ prefix.
'/opt/puppetlabs/server/apps/bolt-server/pe-bolt-modules'
- DEFAULT_BOLT_CODEDIR =
For now at least, we maintain an entirely separate codedir from puppetserver by default, so that filesync can work properly. If filesync is not used, this can instead match the usual puppetserver codedir. See the ‘orchestrator.bolt.codedir` tk config setting.
'/opt/puppetlabs/server/data/orchestration-services/code'
- ACTIONS =
%w[ check_node_connections run_command run_task run_script upload_file apply apply_prep ].freeze
Instance Method Summary collapse
- #allowed_helper(pal, metadata, allowlist) ⇒ Object
- #apply(target, body) ⇒ Object
- #apply_prep(target, body) ⇒ Object
-
#build_project_plugins_tarball(versioned_project, &block) ⇒ Object
The provided block takes a module object and returns the list of directories to search through.
- #build_puppetserver_uri(file_identifier, module_name, parameters) ⇒ Object
- #check_node_connections(targets, body) ⇒ Object
- #config_from_project(versioned_project) ⇒ Object
- #extract_install_task(target) ⇒ Object
- #file_metadatas(versioned_project, module_name, file) ⇒ Object
- #in_bolt_project(versioned_project) ⇒ Object
- #in_pe_pal_env(environment) ⇒ Object
-
#initialize(config) ⇒ TransportApp
constructor
A new instance of TransportApp.
- #make_ssh_target(target_hash) ⇒ Object
- #make_winrm_target(target_hash) ⇒ Object
-
#modulepath_from_environment(environment_name) ⇒ Object
Use puppet to identify the modulepath from an environment.
- #pal_from_project_bolt_config(bolt_config) ⇒ Object
- #pe_plan_info(pal, module_name, plan_name) ⇒ Object
- #pe_task_info(pal, module_name, task_name, parameters) ⇒ Object
- #plan_list(pal) ⇒ Object
-
#plugin_tarball(versioned_project, tarball_type) ⇒ Object
This helper is responsible for computing or retrieving from the cache a plugin tarball.
-
#project_task_metadata(versioned_project, task_name_segments, job_id) ⇒ Object
This helper is responsible for computing or retrieving task metadata for a project.
-
#result_set_to_data(result_set, aggregate: false) ⇒ Object
Turns a Bolt::ResultSet object into a status hash that is fit to return to the client in a response.
- #run_command(target, body) ⇒ Object
- #run_script(target, body) ⇒ Object
- #run_task(target, body) ⇒ Object
- #scrub_stack_trace(result) ⇒ Object
- #task_helper(target, task, parameters) ⇒ Object
- #task_list(pal) ⇒ Object
- #upload_file(target, body) ⇒ Object
- #validate_schema(schema, body) ⇒ Object
-
#with_pe_pal_init_settings(codedir, environmentpath, basemodulepath) ⇒ Object
This function is nearly identical to Bolt::Pal’s ‘with_puppet_settings` with the one difference that we set the codedir to point to actual code, rather than the tmpdir.
Constructor Details
#initialize(config) ⇒ TransportApp
Returns a new instance of TransportApp.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bolt_server/transport_app.rb', line 61 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 # This is needed until the PAL is threadsafe. @pal_mutex = Mutex.new # Avoid redundant plugin tarbal construction @plugin_mutex = Mutex.new # Avoid redundant project_task metadata construction @task_metadata_mutex = Mutex.new @logger = Bolt::Logger.logger(self) super(nil) end |
Instance Method Details
#allowed_helper(pal, metadata, allowlist) ⇒ Object
453 454 455 456 |
# File 'lib/bolt_server/transport_app.rb', line 453 def allowed_helper(pal, , allowlist) allowed = !pal.filter_content([['name']], allowlist).empty? .merge({ 'allowed' => allowed }) end |
#apply(target, body) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/bolt_server/transport_app.rb', line 236 def apply(target, body) validate_schema(@schemas["action-apply"], body) plugins_tarball = plugin_tarball(body['versioned_project'], 'all_plugins') task_data = (body['versioned_project'], %w[apply_helpers apply_catalog], body["job_id"]) task = Bolt::Task::PuppetServer.new(task_data['name'], task_data['metadata'], task_data['files'], @file_cache) task_helper(target, task, body['parameters'].merge({ 'plugins' => plugins_tarball })) end |
#apply_prep(target, body) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/bolt_server/transport_app.rb', line 223 def apply_prep(target, body) validate_schema(@schemas["action-apply_prep"], body) plugins_tarball = plugin_tarball(body['versioned_project'], 'fact_plugins') install_task_segments = extract_install_task(target.first) task_data = (body['versioned_project'], install_task_segments, body["job_id"]) task = Bolt::Task::PuppetServer.new(task_data['name'], task_data['metadata'], task_data['files'], @file_cache) install_task_result = task_helper(target, task, target.first.plugin_hooks['puppet_library']['parameters'] || {}) return install_task_result unless install_task_result.ok task_data = (body['versioned_project'], %w[apply_helpers custom_facts], body["job_id"]) task = Bolt::Task::PuppetServer.new(task_data['name'], task_data['metadata'], task_data['files'], @file_cache) task_helper(target, task, { 'plugins' => plugins_tarball }) end |
#build_project_plugins_tarball(versioned_project, &block) ⇒ Object
The provided block takes a module object and returns the list of directories to search through. This is similar to Bolt::Applicator.build_plugin_tarball.
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/bolt_server/transport_app.rb', line 515 def build_project_plugins_tarball(versioned_project, &block) start_time = Time.now # Fetch the plugin files plugin_files = in_bolt_project(versioned_project) do |context| files = {} # Bolt also sets plugin_modulepath to user modulepath so do it here too for # consistency plugin_modulepath = context[:pal].user_modulepath Puppet.lookup(:current_environment).override_with(modulepath: plugin_modulepath).modules.each do |mod| search_dirs = block.call(mod) files[mod] ||= [] Find.find(*search_dirs).each do |file| files[mod] << file if File.file?(file) end end files end # Pack the plugin files sio = StringIO.new begin output = Minitar::Output.new(Zlib::GzipWriter.new(sio)) plugin_files.each do |mod, files| tar_dir = Pathname.new(mod.name) mod_dir = Pathname.new(mod.path) files.each do |file| tar_path = tar_dir + Pathname.new(file).relative_path_from(mod_dir) stat = File.stat(file) content = File.binread(file) output.tar.add_file_simple( tar_path.to_s, data: content, size: content.size, mode: stat.mode & 0o777, mtime: stat.mtime ) end end duration = Time.now - start_time @logger.trace("Packed plugins in #{duration * 1000} ms") ensure output.close end Base64.encode64(sio.string) end |
#build_puppetserver_uri(file_identifier, module_name, parameters) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/bolt_server/transport_app.rb', line 405 def build_puppetserver_uri(file_identifier, module_name, parameters) segments = file_identifier.split('/', 3) if segments.size == 1 { 'path' => "/puppet/v3/file_content/tasks/#{module_name}/#{file_identifier}", 'params' => parameters } else module_segment, mount_segment, name_segment = *segments { 'path' => case mount_segment when 'files' "/puppet/v3/file_content/modules/#{module_segment}/#{name_segment}" when 'scripts' "/puppet/v3/file_content/scripts/#{module_segment}/#{name_segment}" when 'tasks' "/puppet/v3/file_content/tasks/#{module_segment}/#{name_segment}" when 'lib' "/puppet/v3/file_content/plugins/#{name_segment}" end, 'params' => parameters } end end |
#check_node_connections(targets, body) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/bolt_server/transport_app.rb', line 250 def check_node_connections(targets, body) validate_schema(@schemas["action-check_node_connections"], body) # Puppet Enterprise's orchestrator service uses the # check_node_connections endpoint to check whether nodes that should be # contacted over SSH or WinRM are responsive. The wait time here is 0 # because the endpoint is meant to be used for a single check of all # nodes; External implementations of wait_until_available (like # orchestrator's) should contact the endpoint in their own loop. @executor.wait_until_available(targets, wait_time: 0) end |
#config_from_project(versioned_project) ⇒ Object
361 362 363 364 365 366 367 368 369 |
# File 'lib/bolt_server/transport_app.rb', line 361 def config_from_project(versioned_project) project_dir = File.join(@config['projects-dir'], versioned_project) unless Dir.exist?(project_dir) raise BoltServer::RequestError, "versioned_project: '#{project_dir}' does not exist" end project = Bolt::Project.create_project(project_dir) Bolt::Config.from_project(project, { log: { 'bolt-debug.log' => 'disable' } }) end |
#extract_install_task(target) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/bolt_server/transport_app.rb', line 156 def extract_install_task(target) unless target.plugin_hooks['puppet_library']['task'] raise BoltServer::RequestError, "Target must have 'task' plugin hook" end install_task = target.plugin_hooks['puppet_library']['task'].split('::', 2) install_task << 'init' if install_task.count == 1 install_task end |
#file_metadatas(versioned_project, module_name, file) ⇒ Object
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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bolt_server/transport_app.rb', line 468 def (versioned_project, module_name, file) result = @pal_mutex.synchronize do bolt_config = config_from_project(versioned_project) pal = pal_from_project_bolt_config(bolt_config) pal.in_bolt_compiler do mod = Puppet.lookup(:current_environment).module(module_name) raise BoltServer::RequestError, "module_name: '#{module_name}' does not exist" unless mod # First, look in the 'old' location <module>/files/<path>. # If not found, and the path starts with `files` or `scripts`, munge # the path and look inside that directory. if (abs_path = mod.file(file)) { abs_file_path: abs_path, puppetserver_root: "modules/#{module_name}/#{file}" } else subdir, relative_path = file.split(File::SEPARATOR, 2) abs_path, mount = case subdir when 'files' [mod.file(relative_path), 'modules'] when 'scripts' [mod.script(relative_path), 'scripts'] end next nil unless abs_path { abs_file_path: abs_path, puppetserver_root: "#{mount}/#{module_name}/#{relative_path}" } end end end unless result raise BoltServer::RequestError, "file: '#{file}' does not exist inside #{module_name} 'files' or 'scripts' directories" end abs_file_path = result[:abs_file_path] puppetserver_root = result[:puppetserver_root] fileset = Puppet::FileServing::Fileset.new(abs_file_path, 'recurse' => 'yes') Puppet::FileServing::Fileset.merge(fileset).collect do |relative_file_path, base_path| = Puppet::FileServing::Metadata.new(base_path, relative_path: relative_file_path) .checksum_type = 'sha256' .links = 'follow' .collect .to_data_hash.merge(puppetserver_root: puppetserver_root) end end |
#in_bolt_project(versioned_project) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/bolt_server/transport_app.rb', line 380 def in_bolt_project(versioned_project) @pal_mutex.synchronize do bolt_config = config_from_project(versioned_project) pal = pal_from_project_bolt_config(bolt_config) context = { pal: pal, config: bolt_config } yield context end end |
#in_pe_pal_env(environment) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/bolt_server/transport_app.rb', line 347 def in_pe_pal_env(environment) raise BoltServer::RequestError, "'environment' is a required argument" if environment.nil? @pal_mutex.synchronize do modulepath_obj = Bolt::Config::Modulepath.new( modulepath_from_environment(environment), boltlib_path: [PE_BOLTLIB_PATH, Bolt::Config::Modulepath::BOLTLIB_PATH] ) pal = Bolt::PAL.new(modulepath_obj, nil, nil) yield pal rescue Puppet::Environments::EnvironmentNotFound raise BoltServer::RequestError, "environment: '#{environment}' does not exist" end end |
#make_ssh_target(target_hash) ⇒ Object
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/bolt_server/transport_app.rb', line 603 def make_ssh_target(target_hash) defaults = { 'host-key-check' => false } overrides = { 'load-config' => false } opts = defaults.merge(target_hash).merge(overrides) if opts['private-key-content'] private_key_content = opts.delete('private-key-content') opts['private-key'] = { 'key-data' => private_key_content } end data = { 'uri' => target_hash['hostname'], 'config' => { 'transport' => 'ssh', 'ssh' => opts.slice(*Bolt::Config::Transport::SSH.) }, 'plugin_hooks' => target_hash['plugin_hooks'] } inventory = Bolt::Inventory.empty Bolt::Target.from_hash(data, inventory) end |
#make_winrm_target(target_hash) ⇒ Object
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
# File 'lib/bolt_server/transport_app.rb', line 650 def make_winrm_target(target_hash) defaults = { 'ssl' => false, 'ssl-verify' => false } opts = defaults.merge(target_hash) data = { 'uri' => target_hash['hostname'], 'config' => { 'transport' => 'winrm', 'winrm' => opts.slice(*Bolt::Config::Transport::WinRM.) }, 'plugin_hooks' => target_hash['plugin_hooks'] } inventory = Bolt::Inventory.empty Bolt::Target.from_hash(data, inventory) end |
#modulepath_from_environment(environment_name) ⇒ Object
Use puppet to identify the modulepath from an environment.
WARNING: THIS FUNCTION SHOULD ONLY BE CALLED INSIDE A SYNCHRONIZED PAL MUTEX
337 338 339 340 341 342 343 344 345 |
# File 'lib/bolt_server/transport_app.rb', line 337 def modulepath_from_environment(environment_name) codedir = @config['environments-codedir'] || DEFAULT_BOLT_CODEDIR environmentpath = @config['environmentpath'] || "#{codedir}/environments" basemodulepath = @config['basemodulepath'] || "#{codedir}/modules:/opt/puppetlabs/puppet/modules" with_pe_pal_init_settings(codedir, environmentpath, basemodulepath) do environment = Puppet.lookup(:environments).get!(environment_name) environment.modulepath end end |
#pal_from_project_bolt_config(bolt_config) ⇒ Object
371 372 373 374 375 376 377 378 |
# File 'lib/bolt_server/transport_app.rb', line 371 def pal_from_project_bolt_config(bolt_config) modulepath_object = Bolt::Config::Modulepath.new( bolt_config.modulepath, boltlib_path: [PE_BOLTLIB_PATH, Bolt::Config::Modulepath::BOLTLIB_PATH], builtin_content_path: @config['builtin-content-dir'] ) Bolt::PAL.new(modulepath_object, nil, nil, nil, nil, nil, bolt_config.project) end |
#pe_plan_info(pal, module_name, plan_name) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/bolt_server/transport_app.rb', line 392 def pe_plan_info(pal, module_name, plan_name) # Handle case where plan name is simply module name with special `init.pp` plan plan_name = if plan_name == 'init' || plan_name.nil? module_name else "#{module_name}::#{plan_name}" end plan_info = pal.get_plan_info(plan_name) # Path to module is meaningless in PE plan_info.delete('module') plan_info end |
#pe_task_info(pal, module_name, task_name, parameters) ⇒ Object
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/bolt_server/transport_app.rb', line 430 def pe_task_info(pal, module_name, task_name, parameters) # Handle case where task name is simply module name with special `init` task task_name = if task_name == 'init' || task_name.nil? module_name else "#{module_name}::#{task_name}" end task = pal.get_task(task_name) files = task.files.map do |file_hash| { 'filename' => file_hash['name'], 'sha256' => Digest::SHA256.hexdigest(File.read(file_hash['path'])), 'size_bytes' => File.size(file_hash['path']), 'uri' => build_puppetserver_uri(file_hash['name'], module_name, parameters) } end { 'metadata' => task., 'name' => task.name, 'files' => files } end |
#plan_list(pal) ⇒ Object
463 464 465 466 |
# File 'lib/bolt_server/transport_app.rb', line 463 def plan_list(pal) plans = pal.list_plans.flatten plans.map { |plan_name| { 'name' => plan_name } } end |
#plugin_tarball(versioned_project, tarball_type) ⇒ Object
This helper is responsible for computing or retrieving from the cache a plugin tarball. There are two supported plugin types ‘fact_plugins’, and ‘all_plugins’. Note that this is cached based on versioned_project as there are no plugins in the “builtin content” directory
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 |
# File 'lib/bolt_server/transport_app.rb', line 169 def plugin_tarball(versioned_project, tarball_type) tarball_types = %w[fact_plugins all_plugins] unless tarball_types.include?(tarball_type) raise ArgumentError, "tarball_type must be one of: #{tarball_types.join(', ')}" end # lock this so that in the case an apply/apply_prep with multiple targets hits this endpoint # the tarball computation only happens once (all the other targets will just need to read the cached data) @plugin_mutex.synchronize do if (tarball = @file_cache.get_cached_project_file(versioned_project, tarball_type)) tarball else new_tarball = build_project_plugins_tarball(versioned_project) do |mod| search_dirs = [] search_dirs << mod.plugins if mod.plugins? search_dirs << mod.pluginfacts if mod.pluginfacts? if tarball_type == 'all_plugins' search_dirs << mod.files if mod.files? search_dirs << mod.scripts if mod.scripts? type_files = "#{mod.path}/types" search_dirs << type_files if File.exist?(type_files) end search_dirs end @file_cache.cache_project_file(versioned_project, tarball_type, new_tarball) new_tarball end end end |
#project_task_metadata(versioned_project, task_name_segments, job_id) ⇒ Object
This helper is responsible for computing or retrieving task metadata for a project. It expects task name in segments and uses the combination of task name and versioned_project as a unique identifier for caching in addition to the job_id. The job id is added to protect against a case where the buildtin content is update (where the apply_helpers would be managed)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/bolt_server/transport_app.rb', line 203 def (versioned_project, task_name_segments, job_id) cached_file_name = "#{task_name_segments.join('_')}_#{job_id}" # lock this so that in the case an apply/apply_prep with multiple targets hits this endpoint the # metadata computation will only be computed once, then the cache will be read. @task_metadata_mutex.synchronize do if ( = @file_cache.get_cached_project_file(versioned_project, cached_file_name)) JSON.parse() else = in_bolt_project(versioned_project) do |context| ps_parameters = { 'versioned_project' => versioned_project } pe_task_info(context[:pal], *task_name_segments, ps_parameters) end @file_cache.cache_project_file(versioned_project, cached_file_name, .to_json) end end end |
#result_set_to_data(result_set, aggregate: false) ⇒ Object
Turns a Bolt::ResultSet object into a status hash that is fit to return to the client in a response. In the case of every action except check_node_connections the response will be a single serialized Result. In the check_node_connections case the response will be a hash with the top level “status” of the result and the serialized individual target results.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bolt_server/transport_app.rb', line 111 def result_set_to_data(result_set, aggregate: false) # use ResultSet#ok method to determine status of a (potentially) aggregate result before serializing result_set_status = result_set.ok ? 'success' : 'failure' scrubbed_results = result_set.map do |result| scrub_stack_trace(result.to_data) end if aggregate { status: result_set_status, result: scrubbed_results } else # If there was only one target, return the first result on its own scrubbed_results.first end end |
#run_command(target, body) ⇒ Object
244 245 246 247 248 |
# File 'lib/bolt_server/transport_app.rb', line 244 def run_command(target, body) validate_schema(@schemas["action-run_command"], body) command = body['command'] @executor.run_command(target, command) end |
#run_script(target, body) ⇒ Object
303 304 305 306 307 308 |
# File 'lib/bolt_server/transport_app.rb', line 303 def run_script(target, body) validate_schema(@schemas["action-run_script"], body) # Download the file onto the machine. file_location = @file_cache.update_file(body['script']) @executor.run_script(target, file_location, body['arguments']) end |
#run_task(target, body) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/bolt_server/transport_app.rb', line 148 def run_task(target, body) validate_schema(@schemas["action-run_task"], body) task_data = body['task'] task = Bolt::Task::PuppetServer.new(task_data['name'], task_data['metadata'], task_data['files'], @file_cache) task_helper(target, task, body['parameters'] || {}) end |
#scrub_stack_trace(result) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/bolt_server/transport_app.rb', line 91 def scrub_stack_trace(result) if result.dig('value', '_error', 'details', 'stack_trace') result['value']['_error']['details'].reject! { |k| k == 'stack_trace' } end result end |
#task_helper(target, task, parameters) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/bolt_server/transport_app.rb', line 129 def task_helper(target, task, parameters) # Wrap parameters marked with '"sensitive": true' in the task metadata with a # Sensitive wrapper type. This way it's not shown in logs. if (param_spec = task.parameters) parameters.each do |k, v| if param_spec[k] && param_spec[k]['sensitive'] parameters[k] = Puppet::Pops::Types::PSensitiveType::Sensitive.new(v) end end end @executor.run_task(target, task, parameters).each do |result| value = result.value next unless value.is_a?(Hash) next unless value.key?('_sensitive') value['_sensitive'] = value['_sensitive'].unwrap end end |
#task_list(pal) ⇒ Object
458 459 460 461 |
# File 'lib/bolt_server/transport_app.rb', line 458 def task_list(pal) tasks = pal.list_tasks tasks.map { |task_name, _description| { 'name' => task_name } } end |
#upload_file(target, body) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/bolt_server/transport_app.rb', line 262 def upload_file(target, body) validate_schema(@schemas["action-upload_file"], body) files = body['files'] destination = body['destination'] job_id = body['job_id'] cache_dir = @file_cache.create_cache_dir(job_id.to_s) FileUtils.mkdir_p(cache_dir) files.each do |file| relative_path = file['relative_path'] uri = file['uri'] sha256 = file['sha256'] kind = file['kind'] path = File.join(cache_dir, relative_path) case kind when 'file' # The parent should already be created by `directory` entries, # but this is to be on the safe side. parent = File.dirname(path) FileUtils.mkdir_p(parent) @file_cache.serial_execute { @file_cache.download_file(path, sha256, uri) } when 'directory' # Create directory in cache so we can move files in. FileUtils.mkdir_p(path) else raise BoltServer::RequestError, "Invalid kind: '#{kind}' supplied. Must be 'file' or 'directory'." end end # We need to special case the scenario where only one file was # included in the request to download. Otherwise, the call to upload_file # will attempt to upload with a directory as a source and potentially a # filename as a destination on the host. In that case the end result will # be the file downloaded to a directory with the same name as the source # filename, rather than directly to the filename set in the destination. upload_source = if files.size == 1 && files[0]['kind'] == 'file' File.join(cache_dir, files[0]['relative_path']) else cache_dir end @executor.upload_file(target, upload_source, destination) end |
#validate_schema(schema, body) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/bolt_server/transport_app.rb', line 98 def validate_schema(schema, body) schema_error = JSON::Validator.fully_validate(schema, body) if schema_error.any? raise BoltServer::RequestError.new("There was an error validating the request body.", schema_error) end end |
#with_pe_pal_init_settings(codedir, environmentpath, basemodulepath) ⇒ Object
This function is nearly identical to Bolt::Pal’s ‘with_puppet_settings` with the one difference that we set the codedir to point to actual code, rather than the tmpdir. We only use this funtion inside the Modulepath initializer so that Puppet is correctly configured to pull environment configuration correctly. If we don’t set codedir in this way: when we try to load and interpolate the modulepath it won’t correctly load.
WARNING: THIS FUNCTION SHOULD ONLY BE CALLED INSIDE A SYNCHRONIZED PAL MUTEX
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/bolt_server/transport_app.rb', line 318 def with_pe_pal_init_settings(codedir, environmentpath, basemodulepath) Dir.mktmpdir('pe-bolt') do |dir| cli = [] Puppet::Settings::REQUIRED_APP_SETTINGS.each do |setting| dir = setting == :codedir ? codedir : dir cli << "--#{setting}" << dir end cli << "--environmentpath" << environmentpath cli << "--basemodulepath" << basemodulepath Puppet.settings.send(:clear_everything_for_tests) Puppet.initialize_settings(cli) Puppet[:versioned_environment_dirs] = true yield end end |