-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshtunnel.py
28 lines (24 loc) · 1.05 KB
/
sshtunnel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import sshtunnel
import creds as creds
class SSHTunnel:
def __enter__(self):
if os.environ.get('ENV') == 'development':
self.my_tunnel = sshtunnel.SSHTunnelForwarder((creds.ip, creds.port),
ssh_username=creds.username,
ssh_private_key=creds.private_key,
ssh_private_key_password=creds.passphrase,
remote_bind_address=('localhost', 3306))
self.my_tunnel.start()
self.local_bind_port = self.my_tunnel.local_bind_port
return self
elif os.environ.get('ENV') == 'production':
self.my_tunnel = None
self.local_bind_port = '3306'
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if os.environ.get('ENV') == 'development':
self.my_tunnel.stop()
# In your code
with SSHTunnel() as tunnel:
...
...