Sha256: 8a14acdf11c4bc00e26423e3d40106612416172c1557a696faa0abb4b75c226f

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'open3'
require 'active_support/core_ext/string/filters'

module Gitlab
  module QA
    module Docker
      class Shellout
        using Rainbow

        StatusError = Class.new(StandardError)

        def initialize(command)
          @command = command
          @output = []
          @logger = Runtime::Logger.logger
        end

        attr_reader :command, :output

        def execute! # rubocop:disable Metrics/AbcSize
          raise StatusError, 'Command already executed' if output.any?

          logger.info("Docker shell command: `#{command.mask_secrets.cyan}`")

          Open3.popen2e(@command.to_s) do |_in, out, wait|
            out.each do |line|
              output.push(line)

              if stream_progress
                print "."
              elsif command.stream_output
                puts line
              end

              yield line, wait if block_given?
            end
            puts if stream_progress && !output.empty?

            fail! if wait.value.exited? && wait.value.exitstatus.nonzero?

            logger.debug("Docker shell command output:\n#{string_output}") unless command.stream_output || output.empty?
          end

          string_output
        end

        private

        attr_reader :logger

        # Raise error and print output to error log level
        #
        # @return [void]
        def fail!
          logger.error("Docker shell command output:\n#{string_output}") unless command.stream_output
          raise StatusError, "Docker command `#{command.mask_secrets.truncate(100)}` failed! " + "✘".red
        end

        # Stream only command execution progress and log output when command finished
        #
        # @return [Boolean]
        def stream_progress
          !(Runtime::Env.ci || command.stream_output)
        end

        # Stringified command output
        #
        # @return [String]
        def string_output
          output.join.chomp
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
gitlab-qa-8.4.2 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.4.1 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.4.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.3.2 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.3.1 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.3.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.2.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.1.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-8.0.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-7.36.0 lib/gitlab/qa/docker/shellout.rb
gitlab-qa-7.35.0 lib/gitlab/qa/docker/shellout.rb