Class: Gitlab::QA::Docker::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/qa/docker/engine.rb

Constant Summary collapse

DOCKER_HOST =
ENV['DOCKER_HOST'] || 'http://localhost'
PRIVILEGED_COMMANDS =
[/^iptables.*/].freeze

Instance Method Summary collapse

Instance Method Details

#attach(name, &block) ⇒ Object



50
51
52
# File 'lib/gitlab/qa/docker/engine.rb', line 50

def attach(name, &block)
  Docker::Command.execute("attach --sig-proxy=false #{name}", &block)
end

#exec(name, command) ⇒ Object



39
40
41
42
43
# File 'lib/gitlab/qa/docker/engine.rb', line 39

def exec(name, command)
  cmd = ['exec']
  cmd << '--privileged' if privileged_command?(command)
  Docker::Command.execute("#{cmd.join(' ')} #{name} bash -c '#{command}'")
end

#hostnameObject



8
9
10
# File 'lib/gitlab/qa/docker/engine.rb', line 8

def hostname
  URI(DOCKER_HOST).host
end

#login(username:, password:, registry:) ⇒ Object



12
13
14
# File 'lib/gitlab/qa/docker/engine.rb', line 12

def (username:, password:, registry:)
  Docker::Command.execute(%(login --username "#{username}" --password "#{password}" #{registry}))
end

#network_create(name) ⇒ Object



74
75
76
# File 'lib/gitlab/qa/docker/engine.rb', line 74

def network_create(name)
  Docker::Command.execute("network create #{name}")
end

#network_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/gitlab/qa/docker/engine.rb', line 66

def network_exists?(name)
  Docker::Command.execute("network inspect #{name}")
rescue Docker::Shellout::StatusError
  false
else
  true
end

#port(name, port) ⇒ Object



78
79
80
# File 'lib/gitlab/qa/docker/engine.rb', line 78

def port(name, port)
  Docker::Command.execute("port #{name} #{port}/tcp")
end

#privileged_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/gitlab/qa/docker/engine.rb', line 31

def privileged_command?(command)
  PRIVILEGED_COMMANDS.each do |privileged_regex|
    return true if command.match(privileged_regex)
  end

  false
end

#pull(image, tag) ⇒ Object



16
17
18
# File 'lib/gitlab/qa/docker/engine.rb', line 16

def pull(image, tag)
  Docker::Command.execute("pull #{image}:#{tag}")
end

#read_file(image, tag, path, &block) ⇒ Object



45
46
47
48
# File 'lib/gitlab/qa/docker/engine.rb', line 45

def read_file(image, tag, path, &block)
  cat_file = "run --rm --entrypoint /bin/cat #{image}:#{tag} #{path}"
  Docker::Command.execute(cat_file, &block)
end

#remove(name) ⇒ Object



62
63
64
# File 'lib/gitlab/qa/docker/engine.rb', line 62

def remove(name)
  Docker::Command.execute("rm -f #{name}")
end

#restart(name) ⇒ Object



54
55
56
# File 'lib/gitlab/qa/docker/engine.rb', line 54

def restart(name)
  Docker::Command.execute("restart #{name}")
end

#run(image, tag, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/qa/docker/engine.rb', line 20

def run(image, tag, *args)
  Docker::Command.new('run').tap do |command|
    yield command if block_given?

    command << "#{image}:#{tag}"
    command << args if args.any?

    command.execute!
  end
end

#running?(name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/gitlab/qa/docker/engine.rb', line 82

def running?(name)
  Docker::Command.execute("ps -f name=#{name}").include?(name)
end

#stop(name) ⇒ Object



58
59
60
# File 'lib/gitlab/qa/docker/engine.rb', line 58

def stop(name)
  Docker::Command.execute("stop #{name}")
end