-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored ssh connection handling to only open network connections w…
…hen needed and to allow multiple servers in the future.
- Loading branch information
Showing
3 changed files
with
67 additions
and
27 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,38 @@ | ||
import paramiko | ||
|
||
|
||
class HostConnectionCache(object): | ||
def __init__(self): | ||
self._cache = dict() | ||
|
||
def set_ec2(self, ec2): | ||
self._ec2 = ec2 | ||
|
||
def set_log(self, log): | ||
self._log = log | ||
|
||
def keys(self): | ||
return self._cache.keys() | ||
|
||
def opened(self, key): | ||
if key in self._cache: | ||
return True | ||
|
||
def __getitem__(self, key): | ||
if key not in self._cache and key in self._ec2.servers: | ||
server = self._ec2.servers[key] | ||
try: | ||
user, host, port, client, known_hosts = server.init_ssh_key() | ||
except paramiko.SSHException, e: | ||
self._log.error("Couldn't validate fingerprint for ssh connection.") | ||
self._log.error(e) | ||
self._log.error("Is the server finished starting up?") | ||
return | ||
self._cache[key] = client | ||
return client | ||
return self._cache[key] | ||
|
||
|
||
def patch(): | ||
import fabric.network | ||
fabric.network.HostConnectionCache = HostConnectionCache |