Class: Bolt::Transport::Sudoable::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
# File 'lib/bolt/transport/sudoable/connection.rb', line 10

def initialize(target)
  @target = target
  @run_as = nil
  @logger = Logging.logger[@target.host]
end

Instance Attribute Details

#targetObject

Returns the value of attribute target.



9
10
11
# File 'lib/bolt/transport/sudoable/connection.rb', line 9

def target
  @target
end

Instance Method Details

#execute(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
72
# File 'lib/bolt/transport/sudoable/connection.rb', line 69

def execute(*_args)
  message = "#{self.class.name} must implement #{method} to execute commands"
  raise NotImplementedError, message
end

#make_executable(path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bolt/transport/sudoable/connection.rb', line 31

def make_executable(path)
  result = execute(['chmod', 'u+x', path])
  if result.exit_code != 0
    message = "Could not make file '#{path}' executable: #{result.stderr.string}"
    raise Bolt::Node::FileError.new(message, 'CHMOD_ERROR')
  end
end

#make_tempdirObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bolt/transport/sudoable/connection.rb', line 39

def make_tempdir
  tmpdir = @target.options.fetch('tmpdir', '/tmp')
  tmppath = "#{tmpdir}/#{SecureRandom.uuid}"
  command = ['mkdir', '-m', 700, tmppath]

  result = execute(command)
  if result.exit_code != 0
    raise Bolt::Node::FileError.new("Could not make tempdir: #{result.stderr.string}", 'TEMPDIR_ERROR')
  end
  path = tmppath || result.stdout.string.chomp
  Sudoable::Tmpdir.new(self, path)
end

#run_asObject

This method allows the @run_as variable to be used as a per-operation override for the user to run as. When @run_as is unset, the user specified on the target will be used.



19
20
21
# File 'lib/bolt/transport/sudoable/connection.rb', line 19

def run_as
  @run_as || target.options['run-as']
end

#running_as(user) ⇒ Object

Run as the specified user for the duration of the block.



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

def running_as(user)
  @run_as = user
  yield
ensure
  @run_as = nil
end

#with_tempdirObject

A helper to create and delete a tempdir on the remote system. Yields the directory name.



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

def with_tempdir
  dir = make_tempdir
  yield dir
ensure
  dir&.delete
end

#write_executable(dir, file, filename = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/bolt/transport/sudoable/connection.rb', line 52

def write_executable(dir, file, filename = nil)
  filename ||= File.basename(file)
  remote_path = File.join(dir.to_s, filename)
  copy_file(file, remote_path)
  make_executable(remote_path)
  remote_path
end