Class: Gitlab::QA::Component::Gitlab

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gitlab/qa/component/gitlab.rb

Defined Under Namespace

Classes: Availability

Constant Summary collapse

DATA_SEED_PATH =
File.expand_path('../../../../support/data', __dir__)
TRUSTED_PATH =
'/etc/gitlab/trusted-certs'
SSL_PATH =
'/etc/gitlab/ssl'
DATA_PATH =
'/tmp/data-seeds'

Constants inherited from Base

Base::CERTIFICATES_PATH

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_hosts, #airgapped_network, #docker, #environment, #logger, #network, #network_aliases, #ports, #secrets, #volumes

Instance Method Summary collapse

Methods inherited from Base

#add_network_alias, #hostname, #image, #instance, #ip_address, #prepare_airgapped_network, #prepare_docker_container, #prepare_docker_image, #prepare_network, #prepare_runner_network, #restart, #start_instance, #tag, #teardown

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeGitlab

Returns a new instance of Gitlab.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/qa/component/gitlab.rb', line 44

def initialize
  super

  @skip_availability_check = false
  @omnibus_gitlab_rails_env = {}
  @omnibus_configuration = Runtime::OmnibusConfiguration.new(Runtime::Scenario.omnibus_configuration)
  @cert_volumes = { "authority" => TRUSTED_PATH, "gitlab-ssl" => SSL_PATH }
  @seed_admin_token = Runtime::Scenario.seed_admin_token
  @seed_db = Runtime::Scenario.seed_db
  @skip_server_hooks = Runtime::Scenario.skip_server_hooks

  self.release = 'CE'
end

Instance Attribute Details

#authority_volumeObject (readonly)

Returns the value of attribute authority_volume.



20
21
22
# File 'lib/gitlab/qa/component/gitlab.rb', line 20

def authority_volume
  @authority_volume
end

#gitaly_tlsObject

Returns the value of attribute gitaly_tls.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def gitaly_tls
  @gitaly_tls
end

#nameObject



101
102
103
# File 'lib/gitlab/qa/component/gitlab.rb', line 101

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

#omnibus_configurationObject (readonly)

Returns the value of attribute omnibus_configuration.



20
21
22
# File 'lib/gitlab/qa/component/gitlab.rb', line 20

def omnibus_configuration
  @omnibus_configuration
end

#omnibus_gitlab_rails_envObject (readonly)

Returns the value of attribute omnibus_gitlab_rails_env.



20
21
22
# File 'lib/gitlab/qa/component/gitlab.rb', line 20

def omnibus_gitlab_rails_env
  @omnibus_gitlab_rails_env
end

#relative_pathObject



117
118
119
# File 'lib/gitlab/qa/component/gitlab.rb', line 117

def relative_path
  @relative_path ||= ''
end

#releaseObject

Returns the value of attribute release.



20
21
22
# File 'lib/gitlab/qa/component/gitlab.rb', line 20

def release
  @release
end

#runner_networkObject

Returns the value of attribute runner_network.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def runner_network
  @runner_network
end

#seed_admin_tokenObject

Returns the value of attribute seed_admin_token.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def seed_admin_token
  @seed_admin_token
end

#seed_dbObject

Returns the value of attribute seed_db.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def seed_db
  @seed_db
end

#skip_availability_checkObject

Returns the value of attribute skip_availability_check.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def skip_availability_check
  @skip_availability_check
end

#skip_server_hooksObject

Returns the value of attribute skip_server_hooks.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def skip_server_hooks
  @skip_server_hooks
end

#ssl_volumeObject (readonly)

Returns the value of attribute ssl_volume.



20
21
22
# File 'lib/gitlab/qa/component/gitlab.rb', line 20

def ssl_volume
  @ssl_volume
end

#tlsObject

Returns the value of attribute tls.



26
27
28
# File 'lib/gitlab/qa/component/gitlab.rb', line 26

def tls
  @tls
end

Instance Method Details

#addressObject



105
106
107
# File 'lib/gitlab/qa/component/gitlab.rb', line 105

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

#create_key_file(env_key) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/gitlab/qa/component/gitlab.rb', line 288

def create_key_file(env_key)
  directory = ENV['CI_PROJECT_DIR'] || Dir.tmpdir
  unique_filename = "#{env_key.downcase}_#{Time.now.to_i}_#{rand(100)}"
  key_file_path = File.join(directory, unique_filename)

  File.open(key_file_path, 'w') do |file|
    file.write(ENV.fetch(env_key))
    file.fsync
  end

  File.chmod(0o744, key_file_path)
  @volumes[key_file_path] = key_file_path

  key_file_path
end

#delete_key_file(path) ⇒ Object



304
305
306
# File 'lib/gitlab/qa/component/gitlab.rb', line 304

def delete_key_file(path)
  FileUtils.rm_f(path)
end

#elastic_url=(url) ⇒ Object



86
87
88
# File 'lib/gitlab/qa/component/gitlab.rb', line 86

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

#exist?(image, tag) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/gitlab/qa/component/gitlab.rb', line 138

def exist?(image, tag)
  docker.manifest_exists?("#{image}:#{tag}")
end

#get_reconfigure_log_file_from_artefactObject



216
217
218
219
# File 'lib/gitlab/qa/component/gitlab.rb', line 216

def get_reconfigure_log_file_from_artefact
  all_reconfigure_log_file = Dir["#{Runtime::Env.host_artifacts_dir}/*reconfigure.log"].sort_by { |f| File.mtime(f) }
  all_reconfigure_log_file.last
end

#gitlab_portObject



113
114
115
# File 'lib/gitlab/qa/component/gitlab.rb', line 113

def gitlab_port
  tls ? ["443:443"] : ["80"]
end

#package_versionObject



283
284
285
286
# File 'lib/gitlab/qa/component/gitlab.rb', line 283

def package_version
  manifest = JSON.parse(read_package_manifest)
  manifest['software']['package-scripts']['locked_version']
end

#prepareObject



125
126
127
128
129
130
# File 'lib/gitlab/qa/component/gitlab.rb', line 125

def prepare
  prepare_gitlab_omnibus_config
  copy_certificates

  super
end

#prepare_gitlab_omnibus_configObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gitlab/qa/component/gitlab.rb', line 142

def prepare_gitlab_omnibus_config
  @omnibus_configuration.expand_config_template(self)
  
  set_license_mode
  set_qa_user_agent
  set_cloud_connector_base_url
  env = @omnibus_gitlab_rails_env.merge(
    {
      'GITLAB_ALLOW_SEPARATE_CI_DATABASE' => Runtime::Env.allow_separate_ci_database.to_s,
      'COVERBAND_ENABLED' => Runtime::Env.coverband_enabled?.to_s
    }
  )

  @omnibus_configuration << "gitlab_rails['env'] = #{env}"
end

#process_exec_commandsObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/gitlab/qa/component/gitlab.rb', line 260

def process_exec_commands
  Support::ConfigScripts.add_git_server_hooks(docker, name) unless skip_server_hooks

  @docker.copy(name, DATA_SEED_PATH, DATA_PATH) if seed_admin_token || seed_db
  exec_commands << seed_admin_token_command if seed_admin_token
  exec_commands << seed_test_data_command if seed_db
  exec_commands << Runtime::Scenario.omnibus_exec_commands

  commands = exec_commands.flatten.uniq
  return if commands.empty?

  Runtime::Logger.info("Running exec_commands...")
  commands.each { |command| @docker.exec(name, command, mask_secrets: secrets) }
end

#pullObject



132
133
134
135
136
# File 'lib/gitlab/qa/component/gitlab.rb', line 132

def pull
  docker.(**release.) if release.

  super
end

#rails_versionObject



275
276
277
278
279
280
281
# File 'lib/gitlab/qa/component/gitlab.rb', line 275

def rails_version
  manifest = JSON.parse(read_package_manifest)
  {
    sha: manifest['software']['gitlab-rails']['locked_version'],
    source: manifest['software']['gitlab-rails']['locked_source']['git']
  }
end

#reconfigureObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gitlab/qa/component/gitlab.rb', line 223

def reconfigure
  setup_omnibus
  log_file_path = log_file
  config_file = File.open(log_file_path, "w")
  @docker.attach(name) do |line, _wait|
    config_file.write(line)

    if line.include?('There was an error running gitlab-ctl reconfigure')
      Runtime::Logger.error(
        "Failure while running gitlab-ctl reconfigure command. Please check the #{log_file_path} in the artefact for more info"
      )
    end

    # TODO, workaround which allows to detach from the container
    break if line.include?('gitlab Reconfigured!')
  end
end

#schemeObject



109
110
111
# File 'lib/gitlab/qa/component/gitlab.rb', line 109

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

#set_accept_insecure_certsObject



121
122
123
# File 'lib/gitlab/qa/component/gitlab.rb', line 121

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

#set_cloud_connector_base_urlObject



71
72
73
74
75
# File 'lib/gitlab/qa/component/gitlab.rb', line 71

def set_cloud_connector_base_url
  return if Runtime::Env.cloud_connector_base_url.blank?

  @omnibus_gitlab_rails_env['CLOUD_CONNECTOR_BASE_URL'] = Runtime::Env.cloud_connector_base_url
end

#set_ee_activation_codeObject



90
91
92
93
94
95
# File 'lib/gitlab/qa/component/gitlab.rb', line 90

def set_ee_activation_code
  raise 'QA_EE_ACTIVATION_CODE is required!' if Runtime::Env.ee_activation_code.to_s.strip.empty?

  @omnibus_gitlab_rails_env['QA_EE_ACTIVATION_CODE'] = Runtime::Env.ee_activation_code
  secrets << Runtime::Env.ee_activation_code
end

#set_formless_login_tokenObject



58
59
60
61
62
# File 'lib/gitlab/qa/component/gitlab.rb', line 58

def 
  return if Runtime::Env..to_s.strip.empty?

  @omnibus_gitlab_rails_env['GITLAB_QA_FORMLESS_LOGIN_TOKEN'] = Runtime::Env.
end

#set_license_modeObject



64
65
66
67
68
69
# File 'lib/gitlab/qa/component/gitlab.rb', line 64

def set_license_mode
  return unless Runtime::Env.gitlab_license_mode == 'test'

  @omnibus_gitlab_rails_env['GITLAB_LICENSE_MODE'] = 'test'
  @omnibus_gitlab_rails_env['CUSTOMER_PORTAL_URL'] = Runtime::Env.customer_portal_url
end

#set_qa_user_agentObject

Sets GITLAB_QA_USER_AGENT as a Rail environment variable so that it can be used by GitLab to bypass features that can’t be automated.



79
80
81
82
83
84
# File 'lib/gitlab/qa/component/gitlab.rb', line 79

def set_qa_user_agent
  return if Runtime::Env.gitlab_qa_user_agent.to_s.strip.empty?

  @omnibus_gitlab_rails_env['GITLAB_QA_USER_AGENT'] = Runtime::Env.gitlab_qa_user_agent
  secrets << Runtime::Env.gitlab_qa_user_agent
end

#startObject

rubocop:disable Metrics/AbcSize



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gitlab/qa/component/gitlab.rb', line 158

def start # rubocop:disable Metrics/AbcSize
  ensure_configured!

  docker.run(image: image, tag: tag) do |command|
    command << "-d"
    command << "--shm-size 256m"
    command << "--name #{name}"
    command << "--net #{network}"
    command << "--hostname #{hostname}"

    [*@ports, *gitlab_port].each do |mapping|
      command.port(mapping)
    end

    @volumes.to_h.merge(cert_volumes).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

    @additional_hosts.each do |host|
      command << "--add-host=#{host}"
    end
  end

  return unless runner_network

  Docker::Command.execute(
    "network connect --alias #{name}.#{network} --alias #{name}.#{runner_network} #{runner_network} #{name}"
  )
end

#teardown!Object



308
309
310
311
312
# File 'lib/gitlab/qa/component/gitlab.rb', line 308

def teardown!
  log_pg_stats

  super
end

#wait_until_readyObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/gitlab/qa/component/gitlab.rb', line 241

def wait_until_ready
  return if skip_availability_check

  availability = Availability.new(
    name,
    relative_path: relative_path,
    scheme: scheme,
    protocol_port: gitlab_port.first.to_i
  )

  Runtime::Logger.info("Waiting for GitLab to become healthy ...")

  if availability.check(Runtime::Env.gitlab_availability_timeout)
    Runtime::Logger.info("-> GitLab is available at `#{availability.uri}`!".bright)
  else
    abort '-> GitLab unavailable!'.red
  end
end