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.



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

def initialize
  @docker = Docker::Engine.new
  @logger = Runtime::Logger.logger
  @environment = {}
  @volumes = {}
  @ports = []
  @network_aliases = []
  @exec_commands = []
  @additional_hosts = []
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)


37
38
39
# File 'lib/gitlab/qa/component/base.rb', line 37

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

#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



33
34
35
# File 'lib/gitlab/qa/component/base.rb', line 33

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

#hostnameObject



41
42
43
# File 'lib/gitlab/qa/component/base.rb', line 41

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

#imageObject

Raises:

  • (NotImplementedError)


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

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



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

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

#ip_addressObject



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

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

#prepareObject



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

def prepare
  prepare_docker_image
  prepare_docker_container
  prepare_network
end

#prepare_airgapped_networkObject



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

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

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

#prepare_docker_containerObject



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

def prepare_docker_container
  return unless docker.container_exists?(name)

  docker.remove(name)
end

#prepare_docker_imageObject



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

def prepare_docker_image
  pull
end

#prepare_networkObject



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

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

  docker.network_create(network)
end

#prepare_runner_networkObject



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

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



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

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

#pullObject



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

def pull
  return if Runtime::Env.skip_pull?

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

#restartObject



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

def restart
  assert_name!

  docker.restart(name)
end

#startObject

rubocop:disable Metrics/AbcSize



111
112
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
# File 'lib/gitlab/qa/component/base.rb', line 111

def start # rubocop:disable Metrics/AbcSize
  docker.run(image: image, tag: tag) 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



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

def start_instance
  instance_no_teardown
end

#tagObject

Raises:

  • (NotImplementedError)


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

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



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

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

    return
  end

  teardown!
end

#teardown!Object



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

def teardown!
  assert_name!

  return unless docker.running?(name)

  docker.remove(name)
end