Class: Bolt::Transport::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt/transport/local.rb,
lib/bolt/transport/local/shell.rb

Defined Under Namespace

Classes: Shell

Constant Summary collapse

PROVIDED_FEATURES =
['shell'].freeze

Constants inherited from Base

Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assert_batch_size_one, #batch_command, #batch_script, #batch_task, #batch_upload, #batches, #filter_options, #with_events

Constructor Details

#initializeLocal

Returns a new instance of Local.



21
22
23
24
25
26
27
28
29
# File 'lib/bolt/transport/local.rb', line 21

def initialize
  super

  if Bolt::Util.windows?
    raise NotImplementedError, "The local transport is not yet implemented on Windows"
  else
    @conn = Shell.new
  end
end

Class Method Details

.optionsObject



13
14
15
# File 'lib/bolt/transport/local.rb', line 13

def self.options
  %w[tmpdir]
end

.validate(_options) ⇒ Object



19
# File 'lib/bolt/transport/local.rb', line 19

def self.validate(_options); end

Instance Method Details

#copy_file(source, destination) ⇒ Object



41
42
43
44
45
# File 'lib/bolt/transport/local.rb', line 41

def copy_file(source, destination)
  FileUtils.copy_file(source, destination)
rescue StandardError => e
  raise Bolt::Node::FileError.new(e.message, 'WRITE_ERROR')
end

#run_command(target, command, _options = {}) ⇒ Object



62
63
64
65
66
67
# File 'lib/bolt/transport/local.rb', line 62

def run_command(target, command, _options = {})
  in_tmpdir(target.options['tmpdir']) do |_|
    output = @conn.execute(command, {})
    Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
  end
end

#run_script(target, script, arguments, _options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bolt/transport/local.rb', line 69

def run_script(target, script, arguments, _options = {})
  with_tmpscript(File.absolute_path(script), target.options['tmpdir']) do |file|
    logger.debug "Running '#{file}' with #{arguments}"

    if arguments.empty?
      # We will always provide separated arguments, so work-around Open3's handling of a single
      # argument as the entire command string for script paths containing spaces.
      arguments = ['']
    end
    output = @conn.execute(file, *arguments, {})
    Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
  end
end

#run_task(target, task, arguments, _options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bolt/transport/local.rb', line 83

def run_task(target, task, arguments, _options = {})
  executable = target.select_impl(task, PROVIDED_FEATURES)
  raise "No suitable implementation of #{task.name} for #{target.name}" unless executable

  input_method = task.input_method || "both"
  stdin = STDIN_METHODS.include?(input_method) ? JSON.dump(arguments) : nil
  env = ENVIRONMENT_METHODS.include?(input_method) ? arguments : nil

  with_tmpscript(executable, target.options['tmpdir']) do |script|
    logger.debug("Running '#{script}' with #{arguments}")

    output = @conn.execute(script, stdin: stdin, env: env)
    Bolt::Result.for_task(target, output.stdout.string, output.stderr.string, output.exit_code)
  end
end

#upload(target, source, destination, _options = {}) ⇒ Object



57
58
59
60
# File 'lib/bolt/transport/local.rb', line 57

def upload(target, source, destination, _options = {})
  copy_file(source, destination)
  Bolt::Result.for_upload(target, source, destination)
end