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

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_connected?, #batch_script, #batch_task, #batch_upload, #batches, #envify_params, filter_options, #select_implementation, #unwrap_sensitive_args, #with_events

Constructor Details

#initializeLocal

Returns a new instance of Local.



32
33
34
35
# File 'lib/bolt/transport/local.rb', line 32

def initialize
  super
  @conn = Shell.new
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



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

def self.validate(_options); end

Instance Method Details

#connected?(_targets) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/bolt/transport/local.rb', line 173

def connected?(_targets)
  true
end

#copy_file(source, destination) ⇒ Object



51
52
53
54
55
# File 'lib/bolt/transport/local.rb', line 51

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

#default_input_method(executable) ⇒ Object



25
26
27
28
# File 'lib/bolt/transport/local.rb', line 25

def default_input_method(executable)
  input_method ||= Powershell.powershell_file?(executable) ? 'powershell' : 'both'
  input_method
end

#provided_featuresObject



17
18
19
20
21
22
23
# File 'lib/bolt/transport/local.rb', line 17

def provided_features
  if Bolt::Util.windows?
    ['powershell']
  else
    ['shell']
  end
end

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



72
73
74
75
76
77
# File 'lib/bolt/transport/local.rb', line 72

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

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bolt/transport/local.rb', line 79

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

    # unpack any Sensitive data AFTER we log
    arguments = unwrap_sensitive_args(arguments)
    if Bolt::Util.windows?
      if Powershell.powershell_file?(file)
        command = Powershell.run_script(arguments, file)
        output = @conn.execute(command, dir: dir, env: "powershell.exe")
      else
        path, args = *Powershell.process_from_extension(file)
        args += Powershell.escape_arguments(arguments)
        command = args.unshift(path).join(' ')
        output = @conn.execute(command, dir: dir)
      end
    else
      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, dir: dir)
    end
    Bolt::Result.for_command(target, output.stdout.string, output.stderr.string, output.exit_code)
  end
end

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
# File 'lib/bolt/transport/local.rb', line 107

def run_task(target, task, arguments, _options = {})
  implementation = select_implementation(target, task)
  executable = implementation['path']
  input_method = implementation['input_method']
  extra_files = implementation['files']

  in_tmpdir(target.options['tmpdir']) do |dir|
    if extra_files.empty?
      script = File.join(dir, File.basename(executable))
    else
      arguments['_installdir'] = dir
      script_dest = File.join(dir, task.tasks_dir)
      FileUtils.mkdir_p([script_dest] + extra_files.map { |file| File.join(dir, File.dirname(file['name'])) })

      script = File.join(script_dest, File.basename(executable))
      extra_files.each do |file|
        dest = File.join(dir, file['name'])
        copy_file(file['path'], dest)
        File.chmod(0o750, dest)
      end
    end

    copy_file(executable, script)
    File.chmod(0o750, script)

    # log the arguments with sensitive data redacted, do NOT log unwrapped_arguments
    logger.debug("Running '#{script}' with #{arguments}")
    unwrapped_arguments = unwrap_sensitive_args(arguments)

    stdin = STDIN_METHODS.include?(input_method) ? JSON.dump(unwrapped_arguments) : nil

    if Bolt::Util.windows?
      # WINDOWS
      if ENVIRONMENT_METHODS.include?(input_method)
        environment_params = envify_params(unwrapped_arguments).each_with_object([]) do |(arg, val), list|
          list << Powershell.set_env(arg, val)
        end
        environment_params = environment_params.join("\n") + "\n"
      else
        environment_params = ""
      end

      output =
        if Powershell.powershell_file?(script) && stdin.nil?
          command = Powershell.run_ps_task(arguments, script, input_method)
          command = environment_params + Powershell.shell_init + command
          if input_method == 'powershell'
            @conn.execute(command, dir: dir, env: "powershell.exe")
          else
            @conn.execute(command, dir: dir, stdin: stdin, env: "powershell.exe")
          end
        else
          path, args = *Powershell.process_from_extension(script)
          command = args.unshift(path).join(' ')
          command = environment_params + Powershell.shell_init + command
          @conn.execute(command, dir: dir, stdin: stdin, env: "powershell.exe")
        end
    else
      # POSIX
      env = ENVIRONMENT_METHODS.include?(input_method) ? envify_params(unwrapped_arguments) : nil
      output = @conn.execute(script, stdin: stdin, env: env, dir: dir)
    end
    Bolt::Result.for_task(target, output.stdout.string, output.stderr.string, output.exit_code)
  end
end

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



67
68
69
70
# File 'lib/bolt/transport/local.rb', line 67

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