Class: Chef::Provisioning::DockerDriver::DockerTransport
- Inherits:
-
Transport
- Object
- Transport
- Chef::Provisioning::DockerDriver::DockerTransport
- Defined in:
- lib/chef/provisioning/docker_driver/docker_transport.rb
Defined Under Namespace
Classes: DockerResult
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#container ⇒ Object
Returns the value of attribute container.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #disconnect ⇒ Object
- #docker_toolkit_transport(connection_url = nil) ⇒ Object
- #download_file(path, local_path) ⇒ Object
- #execute(command, timeout: nil, keep_stdin_open: nil, tty: nil, detached: nil, **options) ⇒ Object
-
#initialize(container, config) ⇒ DockerTransport
constructor
A new instance of DockerTransport.
- #is_local_machine(host) ⇒ Object
- #make_url_available_to_remote(local_url) ⇒ Object
- #read_file(path) ⇒ Object
- #upload_file(local_path, path) ⇒ Object
- #write_file(path, content) ⇒ Object
Constructor Details
#initialize(container, config) ⇒ DockerTransport
Returns a new instance of DockerTransport.
17 18 19 20 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 17 def initialize(container, config) @container = container @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
22 23 24 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 22 def config @config end |
#container ⇒ Object
Returns the value of attribute container.
23 24 25 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 23 def container @container end |
Instance Method Details
#available? ⇒ Boolean
161 162 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 161 def available? end |
#disconnect ⇒ Object
154 155 156 157 158 159 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 154 def disconnect if @docker_toolkit_transport_thread @docker_toolkit_transport_thread.kill @docker_toolkit_transport_thread = nil end end |
#docker_toolkit_transport(connection_url = nil) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 174 def docker_toolkit_transport(connection_url=nil) if !defined?(@docker_toolkit_transport) # Figure out which docker-machine this container is in begin docker_machines = `docker-machine ls --format "{{.Name}},{{.URL}}"` rescue Errno::ENOENT Chef::Log.debug("docker-machine ls returned ENOENT: Docker Toolkit is presumably not installed.") @docker_toolkit_transport = nil return end connection_url ||= container.connection.url Chef::Log.debug("Found docker machines:") docker_machine = nil docker_machines.lines.each do |line| machine_name, machine_url = line.chomp.split(',', 2) Chef::Log.debug("- #{machine_name} at URL #{machine_url.inspect}") if machine_url == connection_url Chef::Log.debug("Docker machine #{machine_name} at URL #{machine_url} matches the container's URL #{connection_url}! Will use it for port forwarding.") docker_machine = machine_name end end if !docker_machine Chef::Log.debug("Docker Toolkit is installed, but no Docker machine's URL matches #{connection_url.inspect}. Assuming docker must be installed as well ...") @docker_toolkit_transport = nil return end # Get the SSH information for the docker-machine docker_toolkit_json = `docker-machine inspect #{docker_machine}` machine_info = JSON.parse(docker_toolkit_json, create_additions: false)["Driver"] ssh_host = machine_info["IPAddress"] ssh_username = machine_info["SSHUser"] = { # port: machine_info["SSHPort"], seems to be bad information (44930???) keys: [ machine_info["SSHKeyPath"] ], keys_only: true } Chef::Log.debug("Docker Toolkit is installed. Will use SSH transport with docker-machine #{docker_machine.inspect} to perform port forwarding.") @docker_toolkit_transport = Chef::Provisioning::Transport::SSH.new(ssh_host, ssh_username, , {}, Chef::Config) end @docker_toolkit_transport end |
#download_file(path, local_path) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 83 def download_file(path, local_path) file = File.open(local_path, 'wb') begin file.write(read_file(path)) file.close rescue File.delete(file) end end |
#execute(command, timeout: nil, keep_stdin_open: nil, tty: nil, detached: nil, **options) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 25 def execute(command, timeout: nil, keep_stdin_open: nil, tty: nil, detached: nil, **) opts = {} opts[:tty] = tty unless tty.nil? opts[:detached] = detached unless detached.nil? opts[:stdin] = keep_stdin_open unless keep_stdin_open.nil? opts[:wait] = timeout unless timeout.nil? command = Shellwords.split(command) if command.is_a?(String) Chef::Log.debug("execute #{command.inspect} on container #{container.id} with options #{opts}'") response = container.exec(command, opts) do |stream, chunk| case stream when :stdout stream_chunk(, chunk, nil) when :stderr stream_chunk(, nil, chunk) end end Chef::Log.debug("Execute complete: status #{response[2]}") DockerResult.new(command.join(' '), , response[0].join, response[1].join, response[2]) end |
#is_local_machine(host) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 164 def is_local_machine(host) local_addrs = Socket.ip_address_list host_addrs = Addrinfo.getaddrinfo(host, nil) local_addrs.any? do |local_addr| host_addrs.any? do |host_addr| local_addr.ip_address == host_addr.ip_address end end end |
#make_url_available_to_remote(local_url) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 97 def make_url_available_to_remote(local_url) uri = URI(local_url) if uri.scheme == "chefzero" || is_local_machine(uri.host) # chefzero: URLs are just http URLs with a shortcut if you are in-process. # The remote machine is definitely not in-process. uri.scheme = "http" if uri.scheme == "chefzero" if docker_toolkit_transport # Forward localhost on docker_machine -> chef-zero. The container will # be able to access this because it was started with --net=host. uri = docker_toolkit_transport.make_url_available_to_remote(uri.to_s) uri = URI(uri) @docker_toolkit_transport_thread ||= Thread.new do begin docker_toolkit_transport.send(:session).loop { true } rescue Chef::Log.error("SSH forwarding loop failed: #{$!}") raise end Chef::Log.debug("Session loop completed normally") end else # We are the host. Find the docker machine's gateway (us) and talk to that; # and set up a little proxy that will forward the container's requests to # chef-zero result = execute('ip route list', :read_only => true) Chef::Log.debug("IP route: #{result.stdout}") if result.stdout =~ /default via (\S+)/ old_uri = uri.dup uri.host = $1 if !@proxy_thread # Listen to docker instances only, and forward to localhost @proxy_thread = Thread.new do begin Chef::Log.debug("Starting proxy thread: #{old_uri.host}:#{old_uri.port} <--> #{uri.host}:#{uri.port}") ChefZeroHttpProxy.new(uri.host, uri.port, old_uri.host, old_uri.port).run rescue Chef::Log.error("Proxy thread unable to start: #{$!}") end end end else raise "Cannot forward port: ip route ls did not show default in expected format.\nSTDOUT: #{result.stdout}" end end end uri.to_s end |
#read_file(path) ⇒ Object
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 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 48 def read_file(path) begin tarfile = '' # NOTE: this would be more efficient if we made it a stream and passed that to Minitar container.archive_out(path) do |block| tarfile << block end rescue Docker::Error::NotFoundError return nil rescue Docker::Error::ServerError if $!. =~ /500/ || $!. =~ /Could not find the file/ return nil else raise end end output = '' Archive::Tar::Minitar::Input.open(StringIO.new(tarfile)) do |inp| inp.each do |entry| while next_output = entry.read output << next_output end entry.close end end output end |
#upload_file(local_path, path) ⇒ Object
93 94 95 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 93 def upload_file(local_path, path) write_file(path, IO.read(local_path, mode: "rb")) end |
#write_file(path, content) ⇒ Object
78 79 80 81 |
# File 'lib/chef/provisioning/docker_driver/docker_transport.rb', line 78 def write_file(path, content) tar = StringIO.new(Docker::Util.create_tar(path => content)) container.archive_in_stream('/') { tar.read } end |