lib/gitlab/qa/component/jira.rb



module Gitlab
  module QA
    module Component
      class Jira
        include Scenario::Actable

        JIRA_IMAGE = 'registry.gitlab.com/gitlab-org/gitlab-qa/jira-gitlab'.freeze
        JIRA_IMAGE_TAG = '8.8-project-and-issue'.freeze

        attr_reader :docker
        attr_accessor :environment, :network
        attr_writer :name

        def initialize
          @docker = Docker::Engine.new
          @environment = {}
        end

        def name
          @name ||= "jira"
        end

        def hostname
          "#{name}.#{network}"
        end

        def instance
          raise 'Please provide a block!' unless block_given?

          prepare
          start

          yield self
        ensure
          teardown
        end

        def prepare
          @docker.pull(JIRA_IMAGE, JIRA_IMAGE_TAG)

          return if @docker.network_exists?(network)

          @docker.network_create(network)
        end

        def start
          docker.run(JIRA_IMAGE, JIRA_IMAGE_TAG) do |command|
            command << '-d '
            command << "--name #{name}"
            command << "--net #{network}"
            command << "--hostname #{hostname}"
            command << "--publish 8080:8080"
          end
        end

        def restart
          @docker.restart(name)
        end

        def teardown
          raise 'Invalid instance name!' unless name

          @docker.stop(name)
          @docker.remove(name)
        end

        def set_jira_hostname
          ::Gitlab::QA::Runtime::Env.jira_hostname = hostname
        end
      end
    end
  end
end