|
20 | 20 | import requests
|
21 | 21 | import six
|
22 | 22 |
|
| 23 | +from .. import errors |
| 24 | + |
| 25 | +DEFAULT_HTTP_HOST = "127.0.0.1" |
| 26 | +DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock" |
| 27 | + |
23 | 28 |
|
24 | 29 | def mkbuildcontext(dockerfile):
|
25 | 30 | f = tempfile.NamedTemporaryFile()
|
@@ -139,9 +144,71 @@ def parse_repository_tag(repo):
|
139 | 144 | column_index = repo.rfind(':')
|
140 | 145 | if column_index < 0:
|
141 | 146 | return repo, None
|
142 |
| - tag = repo[column_index+1:] |
| 147 | + tag = repo[column_index + 1:] |
143 | 148 | slash_index = tag.find('/')
|
144 | 149 | if slash_index < 0:
|
145 | 150 | return repo[:column_index], tag
|
146 | 151 |
|
147 | 152 | return repo, None
|
| 153 | + |
| 154 | + |
| 155 | +# Based on utils.go:ParseHost http://tinyurl.com/nkahcfh |
| 156 | +# fd:// protocol unsupported (for obvious reasons) |
| 157 | +# Added support for http and https |
| 158 | +# Protocol translation: tcp -> http, unix -> http+unix |
| 159 | +def parse_host(addr): |
| 160 | + proto = "http+unix" |
| 161 | + host = DEFAULT_HTTP_HOST |
| 162 | + port = None |
| 163 | + if not addr or addr.strip() == 'unix://': |
| 164 | + return DEFAULT_UNIX_SOCKET |
| 165 | + |
| 166 | + addr = addr.strip() |
| 167 | + if addr.startswith('http://'): |
| 168 | + addr = addr.replace('http://', 'tcp://') |
| 169 | + if addr.startswith('http+unix://'): |
| 170 | + addr = addr.replace('http+unix://', 'unix://') |
| 171 | + |
| 172 | + if addr == 'tcp://': |
| 173 | + raise errors.DockerException("Invalid bind address format: %s" % addr) |
| 174 | + elif addr.startswith('unix://'): |
| 175 | + addr = addr[7:] |
| 176 | + elif addr.startswith('tcp://'): |
| 177 | + proto = "http" |
| 178 | + addr = addr[6:] |
| 179 | + elif addr.startswith('https://'): |
| 180 | + proto = "https" |
| 181 | + addr = addr[8:] |
| 182 | + elif addr.startswith('fd://'): |
| 183 | + raise errors.DockerException("fd protocol is not implemented") |
| 184 | + else: |
| 185 | + if "://" in addr: |
| 186 | + raise errors.DockerException( |
| 187 | + "Invalid bind address protocol: %s" % addr |
| 188 | + ) |
| 189 | + proto = "http" |
| 190 | + |
| 191 | + if proto != "http+unix" and ":" in addr: |
| 192 | + host_parts = addr.split(':') |
| 193 | + if len(host_parts) != 2: |
| 194 | + raise errors.DockerException( |
| 195 | + "Invalid bind address format: %s" % addr |
| 196 | + ) |
| 197 | + if host_parts[0]: |
| 198 | + host = host_parts[0] |
| 199 | + |
| 200 | + try: |
| 201 | + port = int(host_parts[1]) |
| 202 | + except Exception: |
| 203 | + raise errors.DockerException( |
| 204 | + "Invalid port: %s", addr |
| 205 | + ) |
| 206 | + |
| 207 | + elif proto in ("http", "https") and ':' not in addr: |
| 208 | + raise errors.DockerException("Bind address needs a port: %s" % addr) |
| 209 | + else: |
| 210 | + host = addr |
| 211 | + |
| 212 | + if proto == "http+unix": |
| 213 | + return "%s://%s" % (proto, host) |
| 214 | + return "%s://%s:%d" % (proto, host, port) |
0 commit comments