KJ commited on
Commit ·
9831b1b
1
Parent(s): 432e03d
updating tunnel
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ T = {
|
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
-
# ---
|
| 39 |
SSH_KEY = st.secrets["SSH_KEY"]
|
| 40 |
SSH_USER = st.secrets["SSH_USER"]
|
| 41 |
SSH_HOST = st.secrets["DB_HOST"]
|
|
@@ -46,20 +46,30 @@ MYSQL_PASSWORD = st.secrets["MYSQL_PASSWORD"]
|
|
| 46 |
MYSQL_DB = st.secrets["DB_NAME"]
|
| 47 |
MYSQL_PORT = int(st.secrets["MYSQL_PORT"])
|
| 48 |
|
| 49 |
-
# Write SSH key to temp file
|
| 50 |
with open("/tmp/ssh_key.pem", "w") as f:
|
| 51 |
f.write(SSH_KEY)
|
| 52 |
os.chmod("/tmp/ssh_key.pem", 0o600)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
MYSQL_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{LOCAL_BIND_HOST}:{MYSQL_PORT}/{MYSQL_DB}"
|
| 64 |
engine = create_engine(MYSQL_URL)
|
| 65 |
|
|
|
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
+
# --- Secrets ---
|
| 39 |
SSH_KEY = st.secrets["SSH_KEY"]
|
| 40 |
SSH_USER = st.secrets["SSH_USER"]
|
| 41 |
SSH_HOST = st.secrets["DB_HOST"]
|
|
|
|
| 46 |
MYSQL_DB = st.secrets["DB_NAME"]
|
| 47 |
MYSQL_PORT = int(st.secrets["MYSQL_PORT"])
|
| 48 |
|
| 49 |
+
# --- Write SSH key to temp file ---
|
| 50 |
with open("/tmp/ssh_key.pem", "w") as f:
|
| 51 |
f.write(SSH_KEY)
|
| 52 |
os.chmod("/tmp/ssh_key.pem", 0o600)
|
| 53 |
|
| 54 |
+
# --- SSH Tunnel Setup (cached with check) ---
|
| 55 |
+
@st.cache_resource
|
| 56 |
+
def start_ssh_tunnel():
|
| 57 |
+
tunnel = SSHTunnelForwarder(
|
| 58 |
+
(SSH_HOST, 22),
|
| 59 |
+
ssh_username=SSH_USER,
|
| 60 |
+
ssh_pkey="/tmp/ssh_key.pem",
|
| 61 |
+
remote_bind_address=(REMOTE_BIND_HOST, MYSQL_PORT),
|
| 62 |
+
local_bind_address=(LOCAL_BIND_HOST, MYSQL_PORT)
|
| 63 |
+
)
|
| 64 |
+
if not tunnel.is_active:
|
| 65 |
+
tunnel.start()
|
| 66 |
+
return tunnel
|
| 67 |
+
|
| 68 |
+
server = start_ssh_tunnel()
|
| 69 |
+
if not server.is_active:
|
| 70 |
+
server.start()
|
| 71 |
|
| 72 |
+
# --- SQLAlchemy engine ---
|
| 73 |
MYSQL_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{LOCAL_BIND_HOST}:{MYSQL_PORT}/{MYSQL_DB}"
|
| 74 |
engine = create_engine(MYSQL_URL)
|
| 75 |
|