-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternative SSH conn that binds the remote docker socket locally
Signed-off-by: aiordache <anca.iordache@docker.com>
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import os | ||
import signal | ||
import subprocess | ||
import tempfile | ||
import time | ||
|
||
try: | ||
import requests.packages.urllib3 as urllib3 | ||
except ImportError: | ||
import urllib3 | ||
|
||
from .. import constants | ||
|
||
from docker.transport.basehttpadapter import BaseHTTPAdapter | ||
from .unixconn import UnixHTTPConnectionPool | ||
|
||
|
||
RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer | ||
|
||
|
||
class SSHClientAdapter(BaseHTTPAdapter): | ||
def __init__(self, socket_url, timeout=60, | ||
pool_connections=constants.DEFAULT_NUM_POOLS): | ||
self.ssh_host = socket_url.lstrip('ssh://') | ||
self.ssh_port = None | ||
if ':' in self.ssh_host: | ||
self.ssh_host, self.ssh_port = self.ssh_host.split(':') | ||
self.timeout = timeout | ||
self.socket_path = None | ||
|
||
self.__create_ssh_tunnel() | ||
|
||
self.pools = RecentlyUsedContainer( | ||
pool_connections, dispose_func=lambda p: p.close() | ||
) | ||
super(SSHClientAdapter, self).__init__() | ||
|
||
def __create_ssh_tunnel(self): | ||
self.temp_dir = tempfile.TemporaryDirectory() | ||
self.socket_path = os.path.join(self.temp_dir.name, "docker.sock") | ||
|
||
port = '' if not self.ssh_port else '-p {}'.format(self.ssh_port) | ||
# bind remote engine socket locally to a temporary file | ||
args = [ | ||
'ssh', | ||
'-NL', | ||
'{}:/var/run/docker.sock'.format(self.socket_path), | ||
self.ssh_host, | ||
port | ||
] | ||
self.proc = subprocess.Popen( | ||
' '.join(args), | ||
shell=True, | ||
preexec_fn=lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)) | ||
count = .0 | ||
while not os.path.exists(self.socket_path): | ||
time.sleep(.1) | ||
count = count + 0.1 | ||
if count > self.timeout: | ||
raise Exception("Failed to connect via SSH") | ||
|
||
def get_connection(self, url, proxies=None): | ||
with self.pools.lock: | ||
pool = self.pools.get(url) | ||
if pool: | ||
return pool | ||
|
||
pool = UnixHTTPConnectionPool( | ||
url, self.socket_path, self.timeout | ||
) | ||
self.pools[url] = pool | ||
|
||
return pool | ||
|
||
def request_url(self, request, proxies): | ||
return request.path_url | ||
|
||
def close(self): | ||
super(SSHClientAdapter, self).close() | ||
if self.proc: | ||
self.proc.terminate() | ||
self.proc.wait() |