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

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

Defined Under Namespace

Classes: Availability

Constant Summary collapse

CERTIFICATES_PATH =
File.expand_path('../../../../tls_certificates/gitlab'.freeze, __dir__)
SSL_PATH =
'/etc/gitlab/ssl'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeGitlab

Returns a new instance of Gitlab.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/qa/component/gitlab.rb', line 23

def initialize
  @docker = Docker::Engine.new
  @environment = {}
  @volumes = {}
  @network_aliases = []

  @volumes[CERTIFICATES_PATH] = SSL_PATH

  self.release = 'CE'
  self.exec_commands = []
end

Instance Attribute Details

#dockerObject (readonly)

Returns the value of attribute docker.



14
15
16
# File 'lib/gitlab/qa/component/gitlab.rb', line 14

def docker
  @docker
end

#environmentObject

Returns the value of attribute environment.



15
16
17
# File 'lib/gitlab/qa/component/gitlab.rb', line 15

def environment
  @environment
end

#exec_commands=(value) ⇒ Object

Sets the attribute exec_commands

Parameters:

  • value

    the value to set the attribute exec_commands to.



16
17
18
# File 'lib/gitlab/qa/component/gitlab.rb', line 16

def exec_commands=(value)
  @exec_commands = value
end

#nameObject



51
52
53
# File 'lib/gitlab/qa/component/gitlab.rb', line 51

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

#networkObject

Returns the value of attribute network.



15
16
17
# File 'lib/gitlab/qa/component/gitlab.rb', line 15

def network
  @network
end

#relative_pathObject



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

def relative_path
  @relative_path ||= ''
end

#releaseObject

Returns the value of attribute release.



14
15
16
# File 'lib/gitlab/qa/component/gitlab.rb', line 14

def release
  @release
end

#tlsObject

Returns the value of attribute tls.



15
16
17
# File 'lib/gitlab/qa/component/gitlab.rb', line 15

def tls
  @tls
end

#volumesObject

Returns the value of attribute volumes.



15
16
17
# File 'lib/gitlab/qa/component/gitlab.rb', line 15

def volumes
  @volumes
end

Instance Method Details

#add_network_alias(name) ⇒ Object



43
44
45
# File 'lib/gitlab/qa/component/gitlab.rb', line 43

def add_network_alias(name)
  @network_aliases.push(name)
end

#addressObject



55
56
57
# File 'lib/gitlab/qa/component/gitlab.rb', line 55

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

#elastic_url=(url) ⇒ Object



39
40
41
# File 'lib/gitlab/qa/component/gitlab.rb', line 39

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

#hostnameObject



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

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

#instanceObject Also known as: launch_and_teardown_instance



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gitlab/qa/component/gitlab.rb', line 75

def instance
  prepare
  start
  reconfigure
  wait
  process_exec_commands

  yield self if block_given?
ensure
  teardown
end

#omnibus_config=(config) ⇒ Object



35
36
37
# File 'lib/gitlab/qa/component/gitlab.rb', line 35

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

#portObject



63
64
65
# File 'lib/gitlab/qa/component/gitlab.rb', line 63

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

#prepareObject



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

def prepare
  @docker.pull(image, tag) unless Runtime::Env.skip_pull?

  return if @docker.network_exists?(network)

  @docker.network_create(network)
end

#process_exec_commandsObject



176
177
178
# File 'lib/gitlab/qa/component/gitlab.rb', line 176

def process_exec_commands
  exec_commands.each { |command| @docker.exec(name, command) }
end

#pullObject



162
163
164
# File 'lib/gitlab/qa/component/gitlab.rb', line 162

def pull
  @docker.pull(@release.image, @release.tag)
end

#reconfigureObject



133
134
135
136
137
138
139
140
# File 'lib/gitlab/qa/component/gitlab.rb', line 133

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

#restartObject



142
143
144
# File 'lib/gitlab/qa/component/gitlab.rb', line 142

def restart
  @docker.restart(name)
end

#schemeObject



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

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

#sha_versionObject



166
167
168
169
170
171
172
173
174
# File 'lib/gitlab/qa/component/gitlab.rb', line 166

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

#startObject

rubocop:disable Metrics/AbcSize



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gitlab/qa/component/gitlab.rb', line 97

def start # rubocop:disable Metrics/AbcSize
  ensure_configured!

  if release.dev_gitlab_org?
    Docker::Command.execute(
      [
        'login',
        '--username gitlab-qa-bot',
        %(--password "#{Runtime::Env.dev_access_token_variable}"),
        Release::DEV_REGISTRY
      ]
    )
  end

  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
end

#teardownObject



146
147
148
149
150
151
# File 'lib/gitlab/qa/component/gitlab.rb', line 146

def teardown
  raise 'Invalid instance name!' unless name

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

#waitObject



153
154
155
156
157
158
159
160
# File 'lib/gitlab/qa/component/gitlab.rb', line 153

def wait
  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