Class: Bolt::Transport::SSH::Connection::RemoteTempdir

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

Instance Method Summary collapse

Constructor Details

#initialize(node, path) ⇒ RemoteTempdir

Returns a new instance of RemoteTempdir.



15
16
17
18
19
20
# File 'lib/bolt/transport/ssh/connection.rb', line 15

def initialize(node, path)
  @node = node
  @owner = node.user
  @path = path
  @logger = node.logger
end

Instance Method Details

#chown(owner) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bolt/transport/ssh/connection.rb', line 35

def chown(owner)
  return if owner.nil? || owner == @owner

  result = @node.execute(['id', '-g', owner])
  if result.exit_code != 0
    message = "Could not identify group of user #{owner}: #{result.stderr.string}"
    raise Bolt::Node::FileError.new(message, 'ID_ERROR')
  end
  group = result.stdout.string.chomp

  # Chown can only be run by root.
  result = @node.execute(['chown', '-R', "#{owner}:#{group}", @path], sudoable: true, run_as: 'root')
  if result.exit_code != 0
    message = "Could not change owner of '#{@path}' to #{owner}: #{result.stderr.string}"
    raise Bolt::Node::FileError.new(message, 'CHOWN_ERROR')
  end

  # File ownership successfully changed, record the new owner.
  @owner = owner
end

#deleteObject



56
57
58
59
60
61
# File 'lib/bolt/transport/ssh/connection.rb', line 56

def delete
  result = @node.execute(['rm', '-rf', @path], sudoable: true, run_as: @owner)
  if result.exit_code != 0
    @logger.warn("Failed to clean up tempdir '#{@path}': #{result.stderr.string}")
  end
end

#mkdirs(subdirs) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/bolt/transport/ssh/connection.rb', line 26

def mkdirs(subdirs)
  abs_subdirs = subdirs.map { |subdir| File.join(@path, subdir) }
  result = @node.execute(['mkdir', '-p'] + abs_subdirs)
  if result.exit_code != 0
    message = "Could not create subdirectories in '#{@path}': #{result.stderr.string}"
    raise Bolt::Node::FileError.new(message, 'MKDIR_ERROR')
  end
end

#to_sObject



22
23
24
# File 'lib/bolt/transport/ssh/connection.rb', line 22

def to_s
  @path
end