class Gitlab::QA::Docker::Engine
def self.append(file, contents)
def self.append(file, contents) %(echo "#{contents}" >> #{file};) end
def self.write(file, contents, expand_vars = true)
-
expand_vars
() -- Set false if you need to write an environment variable '$' to a file. The variable should be escaped \\\$
-
contents
() -- The content of the file to write
-
file
() -- The name of the file
def self.write(file, contents, expand_vars = true) if expand_vars %(echo "#{contents}" > #{file};) else %(echo '#{contents}' > #{file};) end end
def attach(name, &block)
def attach(name, &block) Docker::Command.execute("attach --sig-proxy=false #{name}", &block) end
def container_exists?(name)
def container_exists?(name) !Docker::Command.execute("container list --all --format '{{.Names}}' --filter name=^#{name}$").empty? end
def copy(name, src_path, dest_path)
def copy(name, src_path, dest_path) Docker::Command.execute("cp #{src_path} #{name}:#{dest_path}") end
def exec(name, command, mask_secrets: nil)
def exec(name, command, mask_secrets: nil) cmd = ['exec'] cmd << '--privileged' if privileged_command?(command) Docker::Command.execute(%(#{cmd.join(' ')} #{name} bash -c "#{command.gsub('"', '\\"')}"), mask_secrets: mask_secrets) end
def full_image_name(image, tag)
def full_image_name(image, tag) [image, tag].compact.join(':') end
def hostname
def hostname URI(DOCKER_HOST).host end
def initialize(stream_output: false)
def initialize(stream_output: false) @stream_output = stream_output end
def inspect(name)
def inspect(name) Docker::Command.new('inspect').then do |command| yield command if block_given? command << name command.execute! end end
def login(username:, password:, registry:)
def login(username:, password:, registry:) Docker::Command.execute(%(login --username "#{username}" --password "#{password}" #{registry}), mask_secrets: password) end
def manifest_exists?(name)
def manifest_exists?(name) Docker::Command.execute("manifest inspect #{name}") rescue Support::ShellCommand::StatusError false else true end
def network_create(name)
def network_create(name) Docker::Command.execute("network create #{name}") end
def network_exists?(name)
def network_exists?(name) !Docker::Command.execute("network list --format '{{.Name}}' --filter name=^#{name}$").empty? end
def port(name, port)
def port(name, port) Docker::Command.execute("port #{name} #{port}/tcp") end
def privileged_command?(command)
def privileged_command?(command) PRIVILEGED_COMMANDS.each do |privileged_regex| return true if command.match(privileged_regex) end false end
def ps(name = nil)
def ps(name = nil) Docker::Command.execute(['ps', name].compact.join(' ')) end
def pull(image:, tag: nil, quiet: true)
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
def read_file(image, tag, path, &block)
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
def remove(name)
def remove(name) Docker::Command.execute("rm -f #{name}") end
def restart(name)
def restart(name) Docker::Command.execute("restart #{name}") end
def run(image:, tag: nil, args: [])
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
def running?(name)
def running?(name) Docker::Command.execute("ps -f name=#{name}").include?(name) end
def stop(name)
def stop(name) Docker::Command.execute("stop #{name}") end
def write_files(name, mask_secrets: nil)
-
name
() -- The name of the Docker Container
def write_files(name, mask_secrets: nil) 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, expand_vars = true) if expand_vars %(echo "#{contents}" > #{file};) else %(echo '#{contents}' > #{file};) end end def self.append(file, contents) %(echo "#{contents}" >> #{file};) end end ), mask_secrets: mask_secrets) end