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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scenario::Actable

#act, included

Constructor Details

#initializeGitlab

Returns a new instance of Gitlab.



19
20
21
22
23
24
25
26
# File 'lib/gitlab/qa/component/gitlab.rb', line 19

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

  self.release = 'CE'
end

Instance Attribute Details

#dockerObject (readonly)

Returns the value of attribute docker.



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

def docker
  @docker
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#nameObject



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

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

#networkObject

Returns the value of attribute network.



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

def network
  @network
end

#relative_pathObject



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

def relative_path
  @relative_path ||= ''
end

#releaseObject

Returns the value of attribute release.



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

def release
  @release
end

#volumesObject

Returns the value of attribute volumes.



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

def volumes
  @volumes
end

Instance Method Details

#add_network_alias(name) ⇒ Object



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

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

#addressObject



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

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

#hostnameObject



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

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

#instance {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitlab/qa/component/gitlab.rb', line 56

def instance
  raise 'Please provide a block!' unless block_given?

  prepare
  start
  reconfigure
  wait

  yield self

  teardown
end

#omnibus_config=(config) ⇒ Object



28
29
30
# File 'lib/gitlab/qa/component/gitlab.rb', line 28

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

#prepareObject



69
70
71
72
73
74
75
# File 'lib/gitlab/qa/component/gitlab.rb', line 69

def prepare
  @docker.pull(image, tag)

  return if @docker.network_exists?(network)

  @docker.network_create(network)
end

#pullObject



131
132
133
# File 'lib/gitlab/qa/component/gitlab.rb', line 131

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

#reconfigureObject



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

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



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

def restart
  @docker.restart(name)
end

#sha_versionObject



135
136
137
138
139
140
141
142
143
# File 'lib/gitlab/qa/component/gitlab.rb', line 135

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab/qa/component/gitlab.rb', line 77

def start # rubocop:disable Metrics/AbcSize
  ensure_configured!

  docker.run(image, tag) do |command|
    command << '-d -p 80'
    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



115
116
117
118
119
120
# File 'lib/gitlab/qa/component/gitlab.rb', line 115

def teardown
  raise 'Invalid instance name!' unless name

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

#waitObject



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

def wait
  if Availability.new(name, relative_path: relative_path).check(180)
    sleep 12 # TODO, handle that better
    puts ' -> GitLab is available.'
  else
    abort ' -> GitLab unavailable!'
  end
end