Class: Bolt::Transport::Sudoable::Connection
- Inherits:
-
Object
- Object
- Bolt::Transport::Sudoable::Connection
- Defined in:
- lib/bolt/transport/sudoable/connection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #execute(*_args) ⇒ Object
-
#initialize(target) ⇒ Connection
constructor
A new instance of Connection.
- #make_executable(path) ⇒ Object
- #make_tempdir ⇒ Object
-
#run_as ⇒ Object
This method allows the @run_as variable to be used as a per-operation override for the user to run as.
-
#running_as(user) ⇒ Object
Run as the specified user for the duration of the block.
-
#with_tempdir ⇒ Object
A helper to create and delete a tempdir on the remote system.
- #write_executable(dir, file, filename = nil) ⇒ Object
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
#target ⇒ Object
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
69 70 71 72 |
# File 'lib/bolt/transport/sudoable/connection.rb', line 69 def execute(*_args) = "#{self.class.name} must implement #{method} to execute commands" raise NotImplementedError, 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 = "Could not make file '#{path}' executable: #{result.stderr.string}" raise Bolt::Node::FileError.new(, 'CHMOD_ERROR') end end |
#make_tempdir ⇒ Object
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..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_as ⇒ Object
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.['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_tempdir ⇒ Object
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 |