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.
-
#initialize(target) ⇒ Connection
constructor
A new instance of Connection.
- #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
61 62 63 |
# File 'lib/bolt/transport/docker/connection.rb', line 61 def add_env_vars(env_vars) @env_vars = Bolt::Util.format_env_vars_for_cli(env_vars) end |
#connect ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bolt/transport/docker/connection.rb', line 42 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') index = output.find_index { |item| item["ID"] == 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
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bolt/transport/docker/connection.rb', line 101 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 = Bolt::Util.exec_docker(['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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bolt/transport/docker/connection.rb', line 68 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 |
#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
91 92 93 94 95 96 97 98 99 |
# File 'lib/bolt/transport/docker/connection.rb', line 91 def upload_file(source, destination) @logger.trace { "Uploading #{source} to #{destination}" } _out, err, stat = Bolt::Util.exec_docker(['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 |