docker-remote 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cfe49856e12add82e0b4fa76a42381640b46ab0810a08db03cbd4e6619fa8d5
4
- data.tar.gz: d5d7ab9eb55c7d2189ab5127e5b9c03a5e3c2c53a16241e540907cce3158fc72
3
+ metadata.gz: fa237c85cc84f1754b6cd4e22a62d8216a82ef07777d2b47d7b786769dc6e271
4
+ data.tar.gz: 0dc9ce6503cea3974e74783cfa612e11a094419e65c5dcbc7a202f2b51b10343
5
5
  SHA512:
6
- metadata.gz: 046f4aa7357778f0ded105562fd8ec55e25dc4d012e1c9fcfd01ee4bda47670b26cb83a9fc1bb5f3b4ed625a248af8ea541effceb93e284984c1219baabe634e
7
- data.tar.gz: 38cd75b59a2a12e2339fff98a492f97851d28068184dc7b354ef080f70a7e65baf09a5ffe6fc0495d64158e74d58e4826ceec90da75a2ca28535cbbb6d2a8ffd
6
+ metadata.gz: cb25048ceb8c1e7b1b567b7741fda86a7b8eebef80450b77f18d5b6ce32a4f2558d4c9ac36fbbde6ff7b6c4591a3ffd5e988859152906b65e96e82aaf17d1e1a
7
+ data.tar.gz: 48e4aa145fb981fbdf0a28719c476084f4d1028f90e663192c9ea663cc86fe6cb92d52fd6b2a5231a2c0d306670b30cf97af0559f1445ffec76bc32e326bf7d7
@@ -1,3 +1,6 @@
1
+ ## 0.5.0
2
+ * Figure out registry port more accurately.
3
+
1
4
  ## 0.4.0
2
5
  * Support redirection when making HTTP requests.
3
6
 
@@ -13,6 +13,10 @@ module Docker
13
13
 
14
14
  attr_reader :registry_url, :repo, :username, :password
15
15
 
16
+ PORTMAP = { 'ghcr.io' => 443 }.freeze
17
+ DEFAULT_PORT = 443
18
+ STANDARD_PORTS = [DEFAULT_PORT, 80].freeze
19
+
16
20
  def initialize(registry_url, repo, username = nil, password = nil)
17
21
  @registry_url = registry_url
18
22
  @repo = repo
@@ -113,7 +117,27 @@ module Docker
113
117
  end
114
118
 
115
119
  def registry_uri
116
- @registry_uri ||= URI.parse(registry_url)
120
+ @registry_uri ||= begin
121
+ host_port, *rest = registry_url.split('/')
122
+ host, port = host_port.split(':')
123
+
124
+ ports = if port
125
+ [port.to_i]
126
+ elsif prt = PORTMAP[host]
127
+ [prt]
128
+ else
129
+ STANDARD_PORTS
130
+ end
131
+
132
+ port = ports.find { |port| can_connect?(host, port) }
133
+
134
+ unless port
135
+ raise DockerRemoteError, "couldn't determine what port to connect to"
136
+ end
137
+
138
+ scheme = port == DEFAULT_PORT ? 'https' : 'http'
139
+ URI.parse("#{scheme}://#{host}:#{port}/#{rest.join('/')}")
140
+ end
117
141
  end
118
142
 
119
143
  def make_http(uri)
@@ -125,6 +149,49 @@ module Docker
125
149
  def registry_http
126
150
  @registry_http ||= make_http(registry_uri)
127
151
  end
152
+
153
+ # Adapted from: https://spin.atomicobject.com/2013/09/30/socket-connection-timeout-ruby/
154
+ def can_connect?(host, port)
155
+ # Convert the passed host into structures the non-blocking calls
156
+ # can deal with
157
+ addr = Socket.getaddrinfo(host, nil)
158
+ sockaddr = Socket.pack_sockaddr_in(port, addr[0][3])
159
+ timeout = 3
160
+
161
+ Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
162
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
163
+
164
+ begin
165
+ # Initiate the socket connection in the background. If it doesn't fail
166
+ # immediately it will raise an IO::WaitWritable (Errno::EINPROGRESS)
167
+ # indicating the connection is in progress.
168
+ socket.connect_nonblock(sockaddr)
169
+
170
+ rescue IO::WaitWritable
171
+ # IO.select will block until the socket is writable or the timeout
172
+ # is exceeded - whichever comes first.
173
+ if IO.select(nil, [socket], nil, timeout)
174
+ begin
175
+ # Verify there is now a good connection
176
+ socket.connect_nonblock(sockaddr)
177
+ rescue Errno::EISCONN
178
+ # Good news everybody, the socket is connected!
179
+ socket.close
180
+ return true
181
+ rescue
182
+ # An unexpected exception was raised - the connection is no good.
183
+ socket.close
184
+ end
185
+ else
186
+ # IO.select returns nil when the socket is not ready before timeout
187
+ # seconds have elapsed
188
+ socket.close
189
+ end
190
+ end
191
+ end
192
+
193
+ false
194
+ end
128
195
  end
129
196
  end
130
197
  end
@@ -1,5 +1,5 @@
1
1
  module Docker
2
2
  module Remote
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-02 00:00:00.000000000 Z
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby client for communicating with the Docker registry API v2.
14
14
  email: