2
2
import requests .adapters
3
3
import six
4
4
import logging
5
+ import os
5
6
6
7
from docker .transport .basehttpadapter import BaseHTTPAdapter
7
8
from .. import constants
@@ -73,17 +74,40 @@ def _get_conn(self, timeout):
73
74
class SSHHTTPAdapter (BaseHTTPAdapter ):
74
75
75
76
__attrs__ = requests .adapters .HTTPAdapter .__attrs__ + [
76
- 'pools' , 'timeout' , 'ssh_client' ,
77
+ 'pools' , 'timeout' , 'ssh_client' , 'ssh_params'
77
78
]
78
79
79
80
def __init__ (self , base_url , timeout = 60 ,
80
81
pool_connections = constants .DEFAULT_NUM_POOLS ):
81
82
logging .getLogger ("paramiko" ).setLevel (logging .WARNING )
82
83
self .ssh_client = paramiko .SSHClient ()
84
+ base_url = six .moves .urllib_parse .urlparse (base_url )
85
+ self .ssh_params = {
86
+ "hostname" : base_url .hostname ,
87
+ "port" : base_url .port ,
88
+ "username" : base_url .username
89
+ }
90
+ ssh_config_file = os .path .expanduser ("~/.ssh/config" )
91
+ if os .path .exists (ssh_config_file ):
92
+ conf = paramiko .SSHConfig ()
93
+ with open (ssh_config_file ) as f :
94
+ conf .parse (f )
95
+ host_config = conf .lookup (base_url .hostname )
96
+ self .ssh_conf = host_config
97
+ if 'proxycommand' in host_config :
98
+ self .ssh_params ["sock" ] = paramiko .ProxyCommand (
99
+ self .ssh_conf ['proxycommand' ]
100
+ )
101
+ if 'hostname' in host_config :
102
+ self .ssh_params ['hostname' ] = host_config ['hostname' ]
103
+ if base_url .port is None and 'port' in host_config :
104
+ self .ssh_params ['port' ] = self .ssh_conf ['port' ]
105
+ if base_url .username is None and 'user' in host_config :
106
+ self .ssh_params ['username' ] = self .ssh_conf ['user' ]
107
+
83
108
self .ssh_client .load_system_host_keys ()
84
109
self .ssh_client .set_missing_host_key_policy (paramiko .WarningPolicy ())
85
110
86
- self .base_url = base_url
87
111
self ._connect ()
88
112
self .timeout = timeout
89
113
self .pools = RecentlyUsedContainer (
@@ -92,10 +116,7 @@ def __init__(self, base_url, timeout=60,
92
116
super (SSHHTTPAdapter , self ).__init__ ()
93
117
94
118
def _connect (self ):
95
- parsed = six .moves .urllib_parse .urlparse (self .base_url )
96
- self .ssh_client .connect (
97
- parsed .hostname , parsed .port , parsed .username ,
98
- )
119
+ self .ssh_client .connect (** self .ssh_params )
99
120
100
121
def get_connection (self , url , proxies = None ):
101
122
with self .pools .lock :
0 commit comments