Class: Bolt::Shell::Bash::Tmpdir

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/shell/bash/tmpdir.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell, path) ⇒ Tmpdir

Returns a new instance of Tmpdir.


7
8
9
10
11
12
# File 'lib/bolt/shell/bash/tmpdir.rb', line 7

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

Instance Method Details

#chown(owner, force: false) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bolt/shell/bash/tmpdir.rb', line 27

def chown(owner, force: false)
  return if owner.nil? || (owner == @owner && !force)

  result = @shell.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 = @shell.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


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bolt/shell/bash/tmpdir.rb', line 48

def delete
  result = @shell.execute(['rm', '-rf', @path], sudoable: true, run_as: @owner)
  if result.exit_code != 0
    Bolt::Logger.warn(
      "fail_cleanup",
      "Failed to clean up tmpdir '#{@path}': #{result.stderr.string}"
    )
  end
  # For testing
  result.stderr.string
end

#mkdirs(subdirs) ⇒ Object


18
19
20
21
22
23
24
25
# File 'lib/bolt/shell/bash/tmpdir.rb', line 18

def mkdirs(subdirs)
  abs_subdirs = subdirs.map { |subdir| File.join(@path, subdir) }
  result = @shell.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


14
15
16
# File 'lib/bolt/shell/bash/tmpdir.rb', line 14

def to_s
  @path
end