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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeBase

Returns a new instance of Base.



13
14
15
16
17
18
19
20
# File 'lib/gitlab/qa/component/base.rb', line 13

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

Instance Attribute Details

#airgapped_networkObject

Returns the value of attribute airgapped_network.



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

def airgapped_network
  @airgapped_network
end

#dockerObject (readonly)

Returns the value of attribute docker.



9
10
11
# File 'lib/gitlab/qa/component/base.rb', line 9

def docker
  @docker
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#exec_commands=(value) ⇒ Object

Sets the attribute exec_commands

Parameters:

  • value

    the value to set the attribute exec_commands to.



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

def exec_commands=(value)
  @exec_commands = value
end

#nameObject

Raises:

  • (NotImplementedError)


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

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

#networkObject

Returns the value of attribute network.



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

def network
  @network
end

#portsObject

Returns the value of attribute ports.



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

def ports
  @ports
end

#runner_networkObject

Returns the value of attribute runner_network.



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

def runner_network
  @runner_network
end

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Instance Method Details

#add_network_alias(name) ⇒ Object



22
23
24
# File 'lib/gitlab/qa/component/base.rb', line 22

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

#hostnameObject



30
31
32
# File 'lib/gitlab/qa/component/base.rb', line 30

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

#imageObject

Raises:

  • (NotImplementedError)


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

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



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

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

#ip_addressObject



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

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

#prepareObject



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

def prepare
  prepare_docker_image
  prepare_docker_container
  prepare_network
end

#prepare_airgapped_networkObject



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

def prepare_airgapped_network
  docker.network_create("--driver=bridge --internal #{network}") if airgapped_network && !docker.network_exists?(network)
end

#prepare_docker_containerObject



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

def prepare_docker_container
  return unless docker.container_exists?(name)

  docker.remove(name)
end

#prepare_docker_imageObject



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

def prepare_docker_image
  pull
end

#prepare_networkObject



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

def prepare_network
  prepare_airgapped_network
  prepare_runner_network

  return if docker.network_exists?(network)

  docker.network_create(network)
end

#prepare_runner_networkObject



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

def prepare_runner_network
  docker.network_create("--driver=bridge --internal #{runner_network}") if runner_network && !docker.network_exists?(runner_network)
end

#process_exec_commandsObject



155
156
157
# File 'lib/gitlab/qa/component/base.rb', line 155

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

#pullObject



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

def pull
  return if Runtime::Env.skip_pull?

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

#restartObject



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

def restart
  assert_name!

  docker.restart(name)
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
# File 'lib/gitlab/qa/component/base.rb', line 97

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

#start_instanceObject



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

def start_instance
  instance_no_teardown
end

#tagObject

Raises:

  • (NotImplementedError)


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

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/gitlab/qa/component/base.rb', line 130

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

    return
  end

  teardown!
end

#teardown!Object



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

def teardown!
  assert_name!

  return unless docker.running?(name)

  docker.remove(name)
end