Class: Bolt::Transport::Docker::Connection
- Inherits:
-
Object
- Object
- Bolt::Transport::Docker::Connection
- Defined in:
- lib/bolt/transport/docker/connection.rb
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #add_env_vars(env_vars) ⇒ Object
- #connect ⇒ Object
-
#container_id ⇒ String
The full ID of the target container.
- #download_file(source, destination, _download) ⇒ Object
-
#execute(command) ⇒ Object
Executes a command inside the target container.
-
#execute_local_json_command(subcommand, arguments = []) ⇒ Object
Executes a Docker CLI command and parses the output in JSON format.
-
#initialize(target) ⇒ Connection
constructor
A new instance of Connection.
- #run_cmd(cmd, env_vars) ⇒ Object
- #shell ⇒ Object
- #upload_file(source, destination) ⇒ Object
Constructor Details
#initialize(target) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 18 19 20 |
# File 'lib/bolt/transport/docker/connection.rb', line 12 def initialize(target) raise Bolt::ValidationError, "Target #{target.safe_name} does not have a host" unless target.host @target = target @user = ENV['USER'] || Etc.getlogin @logger = Bolt::Logger.logger(target.safe_name) @container_info = {} @docker_host = target.['service-url'] @logger.trace("Initializing docker connection to #{target.safe_name}") end |
Instance Attribute Details
#target ⇒ Object (readonly)
Returns the value of attribute target.
10 11 12 |
# File 'lib/bolt/transport/docker/connection.rb', line 10 def target @target end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
10 11 12 |
# File 'lib/bolt/transport/docker/connection.rb', line 10 def user @user end |
Instance Method Details
#add_env_vars(env_vars) ⇒ Object
65 66 67 |
# File 'lib/bolt/transport/docker/connection.rb', line 65 def add_env_vars(env_vars) @env_vars = Bolt::Util.format_env_vars_for_cli(env_vars) end |
#connect ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bolt/transport/docker/connection.rb', line 46 def connect # We don't actually have a connection, but we do need to # check that the container exists and is running. output = execute_local_json_command('ps', ['--no-trunc']) index = output.find_index { |item| item["ID"].start_with?(target.host) || item["Names"] == target.host } raise "Could not find a container with name or ID matching '#{target.host}'" if index.nil? # Now find the indepth container information output = execute_local_json_command('inspect', [output[index]["ID"]]) # Store the container information for later @container_info = output[0] @logger.trace { "Opened session" } true rescue StandardError => e raise Bolt::Node::ConnectError.new( "Failed to connect to #{target.safe_name}: #{e.}", 'CONNECT_ERROR' ) end |
#container_id ⇒ String
The full ID of the target container
33 34 35 |
# File 'lib/bolt/transport/docker/connection.rb', line 33 def container_id @container_info["Id"] end |
#download_file(source, destination, _download) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/bolt/transport/docker/connection.rb', line 105 def download_file(source, destination, _download) @logger.trace { "Downloading #{source} to #{destination}" } # Create the destination directory, otherwise copying a source directory with Docker will # copy the *contents* of the directory. # https://docs.docker.com/engine/reference/commandline/cp/ FileUtils.mkdir_p(destination) _out, err, stat = run_cmd(['cp', "#{container_id}:#{source}", destination], env_hash) unless stat.exitstatus.zero? raise "Error downloading content from container #{container_id}: #{err}" end rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |
#execute(command) ⇒ Object
Executes a command inside the target container. This is called from the shell class.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bolt/transport/docker/connection.rb', line 72 def execute(command) args = [] # CODEREVIEW: Is it always safe to pass --interactive? args += %w[--interactive] args += %w[--tty] if target.['tty'] args += %W[--env DOCKER_HOST=#{@docker_host}] if @docker_host args += @env_vars if @env_vars if target.['shell-command'] && !target.['shell-command'].empty? # escape any double quotes in command command = command.gsub('"', '\"') command = "#{target.['shell-command']} \"#{command}\"" end docker_command = %w[docker exec] + args + [container_id] + Shellwords.split(command) @logger.trace { "Executing: #{docker_command.join(' ')}" } Open3.popen3(*docker_command) rescue StandardError @logger.trace { "Command aborted" } raise end |
#execute_local_json_command(subcommand, arguments = []) ⇒ Object
Executes a Docker CLI command and parses the output in JSON format
126 127 128 129 130 |
# File 'lib/bolt/transport/docker/connection.rb', line 126 def execute_local_json_command(subcommand, arguments = []) cmd = [subcommand, '--format', '{{json .}}'].concat(arguments) out, _err, _stat = run_cmd(cmd, env_hash) extract_json(out) end |
#run_cmd(cmd, env_vars) ⇒ Object
37 38 39 |
# File 'lib/bolt/transport/docker/connection.rb', line 37 def run_cmd(cmd, env_vars) Bolt::Util.exec_docker(cmd, env_vars) end |
#shell ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/bolt/transport/docker/connection.rb', line 22 def shell @shell ||= if Bolt::Util.windows? Bolt::Shell::Powershell.new(target, self) else Bolt::Shell::Bash.new(target, self) end end |
#upload_file(source, destination) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/bolt/transport/docker/connection.rb', line 95 def upload_file(source, destination) @logger.trace { "Uploading #{source} to #{destination}" } _out, err, stat = run_cmd(['cp', source, "#{container_id}:#{destination}"], env_hash) unless stat.exitstatus.zero? raise "Error writing to container #{container_id}: #{err}" end rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |