Class: Bolt::Transport::Docker::Connection
- Inherits:
-
Object
- Object
- Bolt::Transport::Docker::Connection
- Defined in:
- lib/bolt/transport/docker/connection.rb
Instance Method Summary collapse
- #connect ⇒ Object
-
#execute(*command, options) ⇒ Object
Executes a command inside the target container.
-
#initialize(target) ⇒ Connection
constructor
A new instance of Connection.
- #make_executable(path) ⇒ Object
- #make_tempdir ⇒ Object
- #mkdirs(dirs) ⇒ Object
- #with_remote_tempdir ⇒ Object
- #write_remote_directory(source, destination) ⇒ Object
- #write_remote_executable(dir, file, filename = nil) ⇒ Object
- #write_remote_file(source, destination) ⇒ Object
Constructor Details
#initialize(target) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 |
# File 'lib/bolt/transport/docker/connection.rb', line 10 def initialize(target) raise Bolt::ValidationError, "Target #{target.name} does not have a host" unless target.host @target = target @logger = Logging.logger[target.host] @docker_host = @target.['service-url'] end |
Instance Method Details
#connect ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bolt/transport/docker/connection.rb', line 17 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_docker_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_docker_json_command('inspect', [output[index]["ID"]]) # Store the container information for later @container_info = output[0] @logger.debug { "Opened session" } true rescue StandardError => e raise Bolt::Node::ConnectError.new( "Failed to connect to #{@target.uri}: #{e.}", 'CONNECT_ERROR' ) end |
#execute(*command, options) ⇒ Object
Executes a command inside the target container
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bolt/transport/docker/connection.rb', line 43 def execute(*command, ) command.unshift([:interpreter]) if [:interpreter] # Build the `--env` parameters envs = [] if [:environment] [:environment].each { |env, val| envs.concat(['--env', "#{env}=#{val}"]) } end = [] # Need to be interactive if redirecting STDIN << '--interactive' unless [:stdin].nil? << '--tty' if [:tty] .concat(envs) unless envs.empty? << container_id .concat(command) @logger.debug { "Executing: exec #{}" } stdout_str, stderr_str, status = execute_local_docker_command('exec', , [:stdin]) # The actual result is the exitstatus not the process object status = status.nil? ? -32768 : status.exitstatus if status == 0 @logger.debug { "Command returned successfully" } else @logger.info { "Command failed with exit code #{status}" } end stdout_str.force_encoding(Encoding::UTF_8) stderr_str.force_encoding(Encoding::UTF_8) # Normalise line endings stdout_str.gsub!("\r\n", "\n") stderr_str.gsub!("\r\n", "\n") [stdout_str, stderr_str, status] rescue StandardError @logger.debug { "Command aborted" } raise end |
#make_executable(path) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/bolt/transport/docker/connection.rb', line 134 def make_executable(path) _, stderr, exitcode = execute('chmod', 'u+x', path, {}) if exitcode != 0 = "Could not make file '#{path}' executable: #{stderr}" raise Bolt::Node::FileError.new(, 'CHMOD_ERROR') end end |
#make_tempdir ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/bolt/transport/docker/connection.rb', line 103 def make_tempdir tmpdir = @target..fetch('tmpdir', container_tmpdir) tmppath = "#{tmpdir}/#{SecureRandom.uuid}" stdout, stderr, exitcode = execute('mkdir', '-m', '700', tmppath, {}) if exitcode != 0 raise Bolt::Node::FileError.new("Could not make tempdir: #{stderr}", 'TEMPDIR_ERROR') end tmppath || stdout.first end |
#mkdirs(dirs) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/bolt/transport/docker/connection.rb', line 95 def mkdirs(dirs) _, stderr, exitcode = execute('mkdir', '-p', *dirs, {}) if exitcode != 0 = "Could not create directories: #{stderr}" raise Bolt::Node::FileError.new(, 'MKDIR_ERROR') end end |
#with_remote_tempdir ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bolt/transport/docker/connection.rb', line 114 def with_remote_tempdir dir = make_tempdir yield dir ensure if dir _, stderr, exitcode = execute('rm', '-rf', dir, {}) if exitcode != 0 @logger.warn("Failed to clean up tempdir '#{dir}': #{stderr}") end end end |
#write_remote_directory(source, destination) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/bolt/transport/docker/connection.rb', line 88 def write_remote_directory(source, destination) _, stdout_str, status = execute_local_docker_command('cp', [source, "#{container_id}:#{destination}"]) raise "Error writing directory to container #{@container_id}: #{stdout_str}" unless status.exitstatus.zero? rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |
#write_remote_executable(dir, file, filename = nil) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/bolt/transport/docker/connection.rb', line 126 def write_remote_executable(dir, file, filename = nil) filename ||= File.basename(file) remote_path = File.join(dir.to_s, filename) write_remote_file(file, remote_path) make_executable(remote_path) remote_path end |
#write_remote_file(source, destination) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/bolt/transport/docker/connection.rb', line 81 def write_remote_file(source, destination) _, stdout_str, status = execute_local_docker_command('cp', [source, "#{container_id}:#{destination}"]) raise "Error writing file to container #{@container_id}: #{stdout_str}" unless status.exitstatus.zero? rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |