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

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

Constant Summary collapse

CERTIFICATES_PATH =
File.expand_path('../../../../tls_certificates', __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  @docker = Docker::Engine.new
  @logger = Runtime::Logger.logger
  @environment = {}
  @volumes = {}
  @ports = []
  @network_aliases = []
  @exec_commands = []
  @additional_hosts = []
  @secrets = []
end

Instance Attribute Details

#additional_hostsObject

Returns the value of attribute additional_hosts.



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

def additional_hosts
  @additional_hosts
end

#airgapped_networkObject

Returns the value of attribute airgapped_network.



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

def airgapped_network
  @airgapped_network
end

#dockerObject (readonly)

Returns the value of attribute docker.



11
12
13
# File 'lib/gitlab/qa/component/base.rb', line 11

def docker
  @docker
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#exec_commands=(value) ⇒ Object

Sets the attribute exec_commands

Parameters:

  • value

    the value to set the attribute exec_commands to.



12
13
14
# File 'lib/gitlab/qa/component/base.rb', line 12

def exec_commands=(value)
  @exec_commands = value
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/gitlab/qa/component/base.rb', line 11

def logger
  @logger
end

#nameObject

Raises:

  • (NotImplementedError)


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

def name
  raise NotImplementedError, "#{self.class.name} must specify a default name"
end

#networkObject

Returns the value of attribute network.



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

def network
  @network
end

#network_aliasesObject

Returns the value of attribute network_aliases.



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

def network_aliases
  @network_aliases
end

#portsObject

Returns the value of attribute ports.



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

def ports
  @ports
end

#runner_networkObject

Returns the value of attribute runner_network.



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

def runner_network
  @runner_network
end

#secretsObject

Returns the value of attribute secrets.



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

def secrets
  @secrets
end

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Instance Method Details

#add_exec_commands(*commands) ⇒ Object



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

def add_exec_commands(*commands)
  @exec_commands.concat(commands.flatten)
end

#add_network_alias(name) ⇒ Object



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

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

#get_reconfigure_log_file_from_artifactObject



183
184
185
# File 'lib/gitlab/qa/component/base.rb', line 183

def get_reconfigure_log_file_from_artifact
  nil
end

#hostnameObject



47
48
49
# File 'lib/gitlab/qa/component/base.rb', line 47

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

#imageObject

Raises:

  • (NotImplementedError)


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

def image
  return self.class.const_get(:DOCKER_IMAGE) if self.class.const_defined?(:DOCKER_IMAGE)

  raise NotImplementedError, "#{self.class.name} must specify a docker image as DOCKER_IMAGE"
end

#instance(skip_teardown: false) ⇒ Object Also known as: launch_and_teardown_instance



67
68
69
70
71
72
73
# File 'lib/gitlab/qa/component/base.rb', line 67

def instance(skip_teardown: false)
  instance_no_teardown do
    yield self if block_given?
  end
ensure
  teardown unless skip_teardown
end

#ip_addressObject



75
76
77
# File 'lib/gitlab/qa/component/base.rb', line 75

def ip_address
  docker.inspect(name) { |command| command << "-f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" }
end

#prepareObject



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

def prepare
  prepare_docker_image
  prepare_docker_container
  prepare_network
end

#prepare_airgapped_networkObject



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

def prepare_airgapped_network
  return unless airgapped_network && !docker.network_exists?(network)

  docker.network_create("--driver=bridge --internal #{network}")
end

#prepare_docker_containerObject



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

def prepare_docker_container
  return unless docker.container_exists?(name)

  docker.remove(name)
end

#prepare_docker_imageObject



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

def prepare_docker_image
  pull
end

#prepare_networkObject



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

def prepare_network
  prepare_airgapped_network
  prepare_runner_network
  return if docker.network_exists?(network)

  docker.network_create(network)
end

#prepare_runner_networkObject



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

def prepare_runner_network
  return unless runner_network && !docker.network_exists?(runner_network)

  docker.network_create("--driver=bridge --internal #{runner_network}")
end

#process_exec_commandsObject



179
180
181
# File 'lib/gitlab/qa/component/base.rb', line 179

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

#pullObject



173
174
175
176
177
# File 'lib/gitlab/qa/component/base.rb', line 173

def pull
  return if Runtime::Env.skip_pull?

  docker.pull(image: image, tag: tag)
end

#restartObject



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

def restart
  assert_name!

  docker.restart(name)
end

#startObject

rubocop:disable Metrics/AbcSize



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gitlab/qa/component/base.rb', line 117

def start # rubocop:disable Metrics/AbcSize
  docker.run(image: image, tag: tag, mask_secrets: secrets) do |command|
    command << "-d"
    command << "--name #{name}"
    command << "--net #{network}"
    command << "--hostname #{hostname}"

    @ports.each do |mapping|
      command.port(mapping)
    end

    @volumes.to_h.each do |to, from|
      command.volume(to, from, 'Z')
    end

    command.volume(*log_volume.values) unless log_volume.empty?

    @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
end

#start_instanceObject



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

def start_instance
  instance_no_teardown
end

#tagObject

Raises:

  • (NotImplementedError)


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

def tag
  return self.class.const_get(:DOCKER_IMAGE_TAG) if self.class.const_defined?(:DOCKER_IMAGE_TAG)

  raise NotImplementedError, "#{self.class.name} must specify a docker image tag as DOCKER_IMAGE_TAG"
end

#teardownObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/gitlab/qa/component/base.rb', line 154

def teardown
  unless teardown?
    Runtime::Logger.info("The orchestrated docker containers have not been removed.")
    docker.ps

    return
  end

  teardown!
end

#teardown!Object



165
166
167
168
169
170
171
# File 'lib/gitlab/qa/component/base.rb', line 165

def teardown!
  assert_name!

  return unless docker.running?(name)

  docker.remove(name)
end