Class: Gitlab::QA::Docker::Engine
- Inherits:
-
Object
- Object
- Gitlab::QA::Docker::Engine
- Defined in:
- lib/gitlab/qa/docker/engine.rb
Constant Summary collapse
- DOCKER_HOST =
ENV['DOCKER_HOST'] || 'http://localhost'
- PRIVILEGED_COMMANDS =
[/^iptables.*/].freeze
Instance Attribute Summary collapse
-
#stream_output ⇒ Object
readonly
Returns the value of attribute stream_output.
Instance Method Summary collapse
- #attach(name, &block) ⇒ Object
- #container_exists?(name) ⇒ Boolean
- #copy(name, src_path, dest_path) ⇒ Object
- #exec(name, command) ⇒ Object
- #hostname ⇒ Object
-
#initialize(stream_output: false) ⇒ Engine
constructor
A new instance of Engine.
- #inspect(name) ⇒ Object
- #login(username:, password:, registry:) ⇒ Object
- #manifest_exists?(name) ⇒ Boolean
- #network_create(name) ⇒ Object
- #network_exists?(name) ⇒ Boolean
- #port(name, port) ⇒ Object
- #privileged_command?(command) ⇒ Boolean
- #ps(name = nil) ⇒ Object
- #pull(image:, tag: nil, quiet: true) ⇒ Object
- #read_file(image, tag, path, &block) ⇒ Object
- #remove(name) ⇒ Object
- #restart(name) ⇒ Object
- #run(image:, tag: nil, args: []) ⇒ Object
- #running?(name) ⇒ Boolean
- #stop(name) ⇒ Object
-
#write_files(name) ⇒ Object
Write to file(s) in the Docker container specified by @param name.
Constructor Details
#initialize(stream_output: false) ⇒ Engine
Returns a new instance of Engine.
12 13 14 |
# File 'lib/gitlab/qa/docker/engine.rb', line 12 def initialize(stream_output: false) @stream_output = stream_output end |
Instance Attribute Details
#stream_output ⇒ Object (readonly)
Returns the value of attribute stream_output.
10 11 12 |
# File 'lib/gitlab/qa/docker/engine.rb', line 10 def stream_output @stream_output end |
Instance Method Details
#attach(name, &block) ⇒ Object
93 94 95 |
# File 'lib/gitlab/qa/docker/engine.rb', line 93 def attach(name, &block) Docker::Command.execute("attach --sig-proxy=false #{name}", &block) end |
#container_exists?(name) ⇒ Boolean
121 122 123 |
# File 'lib/gitlab/qa/docker/engine.rb', line 121 def container_exists?(name) !Docker::Command.execute("container list --all --format '{{.Names}}' --filter name=^#{name}$").empty? end |
#copy(name, src_path, dest_path) ⇒ Object
97 98 99 |
# File 'lib/gitlab/qa/docker/engine.rb', line 97 def copy(name, src_path, dest_path) Docker::Command.execute("cp #{src_path} #{name}:#{dest_path}") end |
#exec(name, command) ⇒ Object
82 83 84 85 86 |
# File 'lib/gitlab/qa/docker/engine.rb', line 82 def exec(name, command) cmd = ['exec'] cmd << '--privileged' if privileged_command?(command) Docker::Command.execute(%(#{cmd.join(' ')} #{name} bash -c "#{command.gsub('"', '\\"')}")) end |
#hostname ⇒ Object
16 17 18 |
# File 'lib/gitlab/qa/docker/engine.rb', line 16 def hostname URI(DOCKER_HOST).host end |
#inspect(name) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/gitlab/qa/docker/engine.rb', line 145 def inspect(name) Docker::Command.new('inspect').then do |command| yield command if block_given? command << name command.execute! end end |
#login(username:, password:, registry:) ⇒ Object
20 21 22 |
# File 'lib/gitlab/qa/docker/engine.rb', line 20 def login(username:, password:, registry:) Docker::Command.execute(%(login --username "#{username}" --password "#{password}" #{registry}), mask_secrets: password) end |
#manifest_exists?(name) ⇒ Boolean
113 114 115 116 117 118 119 |
# File 'lib/gitlab/qa/docker/engine.rb', line 113 def manifest_exists?(name) Docker::Command.execute("manifest inspect #{name}") rescue Docker::Shellout::StatusError false else true end |
#network_create(name) ⇒ Object
129 130 131 |
# File 'lib/gitlab/qa/docker/engine.rb', line 129 def network_create(name) Docker::Command.execute("network create #{name}") end |
#network_exists?(name) ⇒ Boolean
125 126 127 |
# File 'lib/gitlab/qa/docker/engine.rb', line 125 def network_exists?(name) !Docker::Command.execute("network list --format '{{.Name}}' --filter name=^#{name}$").empty? end |
#port(name, port) ⇒ Object
133 134 135 |
# File 'lib/gitlab/qa/docker/engine.rb', line 133 def port(name, port) Docker::Command.execute("port #{name} #{port}/tcp") end |
#privileged_command?(command) ⇒ Boolean
44 45 46 47 48 49 50 |
# File 'lib/gitlab/qa/docker/engine.rb', line 44 def privileged_command?(command) PRIVILEGED_COMMANDS.each do |privileged_regex| return true if command.match(privileged_regex) end false end |
#ps(name = nil) ⇒ Object
141 142 143 |
# File 'lib/gitlab/qa/docker/engine.rb', line 141 def ps(name = nil) Docker::Command.execute(['ps', name].compact.join(' ')) end |
#pull(image:, tag: nil, quiet: true) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/gitlab/qa/docker/engine.rb', line 24 def pull(image:, tag: nil, quiet: true) Docker::Command.new("pull").tap do |command| command << "-q" if quiet command << full_image_name(image, tag) command.execute! end end |
#read_file(image, tag, path, &block) ⇒ Object
88 89 90 91 |
# File 'lib/gitlab/qa/docker/engine.rb', line 88 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
109 110 111 |
# File 'lib/gitlab/qa/docker/engine.rb', line 109 def remove(name) Docker::Command.execute("rm -f #{name}") end |
#restart(name) ⇒ Object
101 102 103 |
# File 'lib/gitlab/qa/docker/engine.rb', line 101 def restart(name) Docker::Command.execute("restart #{name}") end |
#run(image:, tag: nil, args: []) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gitlab/qa/docker/engine.rb', line 33 def run(image:, tag: nil, args: []) Docker::Command.new('run', stream_output: stream_output).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
137 138 139 |
# File 'lib/gitlab/qa/docker/engine.rb', line 137 def running?(name) Docker::Command.execute("ps -f name=#{name}").include?(name) end |
#stop(name) ⇒ Object
105 106 107 |
# File 'lib/gitlab/qa/docker/engine.rb', line 105 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gitlab/qa/docker/engine.rb', line 61 def write_files(name) exec(name, yield( Class.new do # @param file The name of the file # @param contents The content of the file to write # @param expand_vars Set false if you need to write an environment variable '$' to a file. The variable should be escaped \\\$ def self.write(file, contents, = true) if %(echo "#{contents}" > #{file};) else %(echo '#{contents}' > #{file};) end end def self.append(file, contents) %(echo "#{contents}" >> #{file};) end end )) end |