Class: Bolt::Transport::Local::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/transport/local/connection.rb

Constant Summary collapse

RUBY_ENV_VARS =
%w[GEM_PATH GEM_HOME RUBYLIB RUBYLIB_PREFIX RUBYOPT RUBYPATH RUBYSHELL].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(target)
  @target = target
  # The familiar problem: Etc.getlogin is broken on osx
  @user = ENV['USER'] || Etc.getlogin
  @logger = Bolt::Logger.logger(self)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/bolt/transport/local/connection.rb', line 15

def logger
  @logger
end

#targetObject

Returns the value of attribute target.



15
16
17
# File 'lib/bolt/transport/local/connection.rb', line 15

def target
  @target
end

#userObject

Returns the value of attribute user.



15
16
17
# File 'lib/bolt/transport/local/connection.rb', line 15

def user
  @user
end

Instance Method Details

#download_file(source, dest, _download) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bolt/transport/local/connection.rb', line 50

def download_file(source, dest, _download)
  @logger.trace { "Downloading #{source} to #{dest}" }
  # Create the destination directory for the target, or the
  # copied file will have the target's name
  FileUtils.mkdir_p(dest)
  # Mimic the behavior of `cp --remove-destination`
  # since the flag isn't supported on MacOS
  FileUtils.cp_r(source, dest, remove_destination: true)
rescue StandardError => e
  message = "Could not download file to #{dest}: #{e}"
  raise Bolt::Node::FileError.new(message, 'DOWNLOAD_ERROR')
end

#execute(command) ⇒ Object



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
# File 'lib/bolt/transport/local/connection.rb', line 63

def execute(command)
  if Bolt::Util.windows?
    # If it's already a powershell command then invoke it normally.
    # Otherwise, wrap it in powershell.exe.
    unless command.start_with?('powershell.exe')
      cmd = Bolt::Shell::Powershell::Snippets.exit_with_code(command)
      command = ['powershell.exe', *Bolt::Shell::Powershell::PS_ARGS, '-Command', cmd]
    end
  end

  # Only do this if bundled-ruby is set to false, not nil
  ruby_env_vars = if target.transport_config['bundled-ruby'] == false
                    RUBY_ENV_VARS.each_with_object({}) do |e, acc|
                      acc[e] = ENV["BOLT_ORIG_#{e}"] if ENV["BOLT_ORIG_#{e}"]
                    end
                  end

  if target.transport_config['bundled-ruby'] == false &&
     Gem.loaded_specs.keys.include?('bundler')
    Bundler.with_unbundled_env do
      Open3.popen3(ruby_env_vars || {}, *command)
    end
  else
    Open3.popen3(ruby_env_vars || {}, *command)
  end
end

#max_command_lengthObject



96
97
98
99
100
# File 'lib/bolt/transport/local/connection.rb', line 96

def max_command_length
  if Bolt::Util.windows?
    32000
  end
end

#reset_cwd?Boolean

This is used by the Bash shell to decide whether to ‘cd` before executing commands as a run-as user

Returns:

  • (Boolean)


92
93
94
# File 'lib/bolt/transport/local/connection.rb', line 92

def reset_cwd?
  false
end

#shellObject



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

def shell
  @shell ||= if Bolt::Util.windows?
               Bolt::Shell::Powershell.new(target, self)
             else
               Bolt::Shell::Bash.new(target, self)
             end
end

#upload_file(source, dest) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bolt/transport/local/connection.rb', line 32

def upload_file(source, dest)
  @logger.trace { "Uploading #{source} to #{dest}" }
  if source.is_a?(StringIO)
    Tempfile.create(File.basename(dest)) do |f|
      f.write(source.read)
      f.close
      FileUtils.mv(f, dest)
    end
  else
    # Mimic the behavior of `cp --remove-destination`
    # since the flag isn't supported on MacOS
    FileUtils.cp_r(source, dest, remove_destination: true)
  end
rescue StandardError => e
  message = "Could not copy file to #{dest}: #{e}"
  raise Bolt::Node::FileError.new(message, 'COPY_ERROR')
end