Sha256: 5217ec6c74cfc77d07110e819ab3df14820e45a6d3b18b2850db23b6fe8c4468

Contents?: true

Size: 1.86 KB

Versions: 38

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Bolt
  class Shell
    class Bash < Shell
      class Tmpdir
        def initialize(shell, path)
          @shell = shell
          @owner = shell.conn.user
          @path = path
          @logger = shell.logger
        end

        def to_s
          @path
        end

        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

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

          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

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

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
bolt-2.42.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.40.2 lib/bolt/shell/bash/tmpdir.rb
bolt-2.40.1 lib/bolt/shell/bash/tmpdir.rb
bolt-2.38.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.37.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.36.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.35.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.34.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.33.2 lib/bolt/shell/bash/tmpdir.rb
bolt-2.33.1 lib/bolt/shell/bash/tmpdir.rb
bolt-2.32.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.31.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.30.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.29.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.28.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.27.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.26.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.25.0 lib/bolt/shell/bash/tmpdir.rb
bolt-2.24.1 lib/bolt/shell/bash/tmpdir.rb
bolt-2.24.0 lib/bolt/shell/bash/tmpdir.rb