class Gitlab::QA::Component::Gitlab

def address

def address
  "#{scheme}://#{hostname}#{relative_path}"
end

def elastic_url=(url)

def elastic_url=(url)
  @environment['ELASTIC_URL'] = url
end

def ensure_configured!

def ensure_configured!
  raise 'Please configure an instance first!' unless [name, release, network].all?
end

def initialize

def initialize
  super
  @disable_animations = true
  @skip_availability_check = false
  @volumes[CERTIFICATES_PATH] = SSL_PATH
  self.release = 'CE'
end

def name

def name
  @name ||= "gitlab-#{edition}-#{SecureRandom.hex(4)}"
end

def omnibus_config=(config)

def omnibus_config=(config)
  @environment['GITLAB_OMNIBUS_CONFIG'] = config.tr("\n", ' ')
end

def port

def port
  tls ? '443' : '80'
end

def prepare

def prepare
  prepare_gitlab_omnibus_config
  super
end

def prepare_gitlab_omnibus_config

def prepare_gitlab_omnibus_config
  setup_disable_animations if disable_animations
  set_formless_login_token
end

def pull

def pull
  docker.login(**release.login_params) if release.login_params
  super
end

def reconfigure

def reconfigure
  @docker.attach(name) do |line, wait|
    puts line
    # TODO, workaround which allows to detach from the container
    #
    Process.kill('INT', wait.pid) if line =~ /gitlab Reconfigured!/
  end
end

def relative_path

def relative_path
  @relative_path ||= ''
end

def release=(release)

def release=(release)
  @release = QA::Release.new(release)
end

def scheme

def scheme
  tls ? 'https' : 'http'
end

def set_accept_insecure_certs

def set_accept_insecure_certs
  Runtime::Env.accept_insecure_certs = 'true'
end

def set_formless_login_token

def set_formless_login_token
  return if Runtime::Env.gitlab_qa_formless_login_token.to_s.strip.empty?
  @environment['GITLAB_OMNIBUS_CONFIG'] = "gitlab_rails['env'] = { 'GITLAB_QA_FORMLESS_LOGIN_TOKEN' => '#{Runtime::Env.gitlab_qa_formless_login_token}' }; #{@environment['GITLAB_OMNIBUS_CONFIG'] || ''}"
end

def setup_disable_animations

def setup_disable_animations
  @environment['GITLAB_OMNIBUS_CONFIG'] = "gitlab_rails['gitlab_disable_animations'] = true; #{@environment['GITLAB_OMNIBUS_CONFIG'] || ''}"
end

def sha_version

def sha_version
  json = @docker.read_file(
    @release.image, @release.tag,
    '/opt/gitlab/version-manifest.json'
  )
  manifest = JSON.parse(json)
  manifest['software']['gitlab-rails']['locked_version']
end

def start # rubocop:disable Metrics/AbcSize

rubocop:disable Metrics/AbcSize
def start # rubocop:disable Metrics/AbcSize
  ensure_configured!
  docker.run(image, tag) do |command|
    command << "-d -p #{port}"
    command << "--name #{name}"
    command << "--net #{network}"
    command << "--hostname #{hostname}"
    @volumes.to_h.each do |to, from|
      command.volume(to, from, 'Z')
    end
    command.volume(File.join(Runtime::Env.host_artifacts_dir, name, 'logs'), '/var/log/gitlab', 'Z')
    @environment.to_h.each do |key, value|
      command.env(key, value)
    end
    @network_aliases.to_a.each do |network_alias|
      command << "--network-alias #{network_alias}"
    end
  end
  Docker::Command.execute("network connect --alias #{name}.#{network} --alias #{name}.#{runner_network} #{runner_network} #{name}") if runner_network
end

def wait

def wait
  return if skip_availability_check
  if Availability.new(name, relative_path: relative_path, scheme: scheme, protocol_port: port.to_i).check(180)
    sleep 12 # TODO, handle that better
    puts ' -> GitLab is available.'
  else
    abort ' -> GitLab unavailable!'
  end
end