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)


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

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_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

#hostnameObject



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

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

#imageObject

Raises:

  • (NotImplementedError)


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

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



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

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

#ip_addressObject



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

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

#prepareObject



77
78
79
80
81
# File 'lib/gitlab/qa/component/base.rb', line 77

def prepare
  prepare_docker_image
  prepare_docker_container
  prepare_network
end

#prepare_airgapped_networkObject



95
96
97
98
99
# File 'lib/gitlab/qa/component/base.rb', line 95

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

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

#prepare_docker_containerObject



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

def prepare_docker_container
  return unless docker.container_exists?(name)

  docker.remove(name)
end

#prepare_docker_imageObject



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

def prepare_docker_image
  pull
end

#prepare_networkObject



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

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

  docker.network_create(network)
end

#prepare_runner_networkObject



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

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



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

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

#pullObject



169
170
171
172
173
# File 'lib/gitlab/qa/component/base.rb', line 169

def pull
  return if Runtime::Env.skip_pull?

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

#restartObject



144
145
146
147
148
# File 'lib/gitlab/qa/component/base.rb', line 144

def restart
  assert_name!

  docker.restart(name)
end

#startObject

rubocop:disable Metrics/AbcSize



113
114
115
116
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
# File 'lib/gitlab/qa/component/base.rb', line 113

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



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

def start_instance
  instance_no_teardown
end

#tagObject

Raises:

  • (NotImplementedError)


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

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



150
151
152
153
154
155
156
157
158
159
# File 'lib/gitlab/qa/component/base.rb', line 150

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

    return
  end

  teardown!
end

#teardown!Object



161
162
163
164
165
166
167
# File 'lib/gitlab/qa/component/base.rb', line 161

def teardown!
  assert_name!

  return unless docker.running?(name)

  docker.remove(name)
end