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
-
#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 16 17 18 |
# File 'lib/bolt/transport/docker/connection.rb', line 10 def initialize(target) # lazy-load expensive gem code require 'docker' raise Bolt::ValidationError, "Target #{target.name} does not have a host" unless target.host @target = target @logger = Logging.logger[target.host] end |
Instance Method Details
#connect ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bolt/transport/docker/connection.rb', line 20 def connect # Explicitly create the new Connection to avoid relying on global state in the Docker module. url = @target.['service-url'] || ::Docker.url = ::Docker..merge(@target.['service-options'] || {}) @container = ::Docker::Container.get(@target.host, {}, ::Docker::Connection.new(url, )) @logger.debug { "Opened session" } rescue StandardError => e raise Bolt::Node::ConnectError.new( "Failed to connect to #{@target.uri}: #{e.}", 'CONNECT_ERROR' ) end |
#execute(*command, options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bolt/transport/docker/connection.rb', line 33 def execute(*command, ) command.unshift([:interpreter]) if [:interpreter] if [:environment] envs = [:environment].map { |env, val| "#{env}=#{val}" } command = ['env'] + envs + command end @logger.debug { "Executing: #{command}" } result = @container.exec(command, ) { |stream, chunk| @logger.debug("#{stream}: #{chunk}") } if result[2] == 0 @logger.debug { "Command returned successfully" } else @logger.info { "Command failed with exit code #{result[2]}" } end result[0] = result[0].join.force_encoding('UTF-8') result[1] = result[1].join.force_encoding('UTF-8') result rescue StandardError @logger.debug { "Command aborted" } raise end |
#make_executable(path) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/bolt/transport/docker/connection.rb', line 108 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
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bolt/transport/docker/connection.rb', line 77 def make_tempdir tmpdir = @target..fetch('tmpdir', '/tmp') 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
69 70 71 72 73 74 75 |
# File 'lib/bolt/transport/docker/connection.rb', line 69 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
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bolt/transport/docker/connection.rb', line 88 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
61 62 63 64 65 66 67 |
# File 'lib/bolt/transport/docker/connection.rb', line 61 def write_remote_directory(source, destination) tar = ::Docker::Util.create_dir_tar(source) mkdirs([destination]) @container.archive_in_stream(destination) { tar.read(Excon.defaults[:chunk_size]).to_s } rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |
#write_remote_executable(dir, file, filename = nil) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/bolt/transport/docker/connection.rb', line 100 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
55 56 57 58 59 |
# File 'lib/bolt/transport/docker/connection.rb', line 55 def write_remote_file(source, destination) @container.store_file(destination, File.binread(source)) rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |