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



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

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

#container_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/gitlab/qa/docker/engine.rb', line 91

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

#exec(name, command) ⇒ Object



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

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

#hostnameObject



10
11
12
# File 'lib/gitlab/qa/docker/engine.rb', line 10

def hostname
  URI(DOCKER_HOST).host
end

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



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

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

#network_create(name) ⇒ Object



107
108
109
# File 'lib/gitlab/qa/docker/engine.rb', line 107

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

#network_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/gitlab/qa/docker/engine.rb', line 99

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

#port(name, port) ⇒ Object



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

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

#privileged_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

#ps(name = nil) ⇒ Object



119
120
121
# File 'lib/gitlab/qa/docker/engine.rb', line 119

def ps(name = nil)
  Docker::Command.execute(['ps', name].compact.join(' '))
end

#pull(image:, tag: nil) ⇒ Object



18
19
20
# File 'lib/gitlab/qa/docker/engine.rb', line 18

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

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



70
71
72
73
# File 'lib/gitlab/qa/docker/engine.rb', line 70

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

#remove(name) ⇒ Object



87
88
89
# File 'lib/gitlab/qa/docker/engine.rb', line 87

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

#restart(name) ⇒ Object



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

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

#run(image:, tag: nil, args: []) ⇒ Object



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

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

    command << full_image_name(image, tag)
    command << args if args.any?

    command.execute!
  end
end

#running?(name) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/gitlab/qa/docker/engine.rb', line 115

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

#stop(name) ⇒ Object



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

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

#write_files(name) ⇒ Object

Write to file(s) in the Docker container specified by @param name

Examples:

engine.write_files('gitlab-abc123') do |files|
  files.append('/etc/hosts', '127.0.0.1 localhost')
  files.write('/opt/other', <<~TEXT
    This is content
    That goes within /opt/other
  TEXT)

Parameters:

  • name

    The name of the Docker Container



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/qa/docker/engine.rb', line 50

def write_files(name)
  exec(name, yield(
    Class.new do
      def self.write(file, contents)
        %(echo "#{contents}" > #{file};)
      end

      def self.append(file, contents)
        %(echo "#{contents}" >> #{file};)
      end
    end
  ))
end