Spaces:
Runtime error
Runtime error
File size: 2,095 Bytes
a1561e0 5da763a a1561e0 55f36d8 a0f09fb 7438000 a1561e0 b5c6cc2 a1561e0 f6e5539 b5c6cc2 36e8169 f6e5539 36e8169 a1561e0 36e8169 ec76e7d b5c6cc2 bbe95b6 e8c4b5e bbe95b6 ec76e7d b5c6cc2 a1561e0 36e8169 ec76e7d 5c3da8a 8efd70a 9c2d484 ab64dd3 591a7c6 bb6c41f ab64dd3 7f81767 36e8169 b5c6cc2 36e8169 32d4d1e |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import os
import streamlit as st
import googleapiclient.discovery
from google.oauth2 import service_account
def auth():
with open("/tmp/token.json", "w") as token_f:
google_key = os.getenv("GOOGLE_KEY")
token_f.write(google_key)
credentials = service_account.Credentials.from_service_account_file('/tmp/token.json')
return credentials
def get_status(credentials, instance_name):
status = False
client = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
response = client.instances().list(project='nus-cisco-corp-lab-wp1', zone='asia-southeast1-c').execute()
for item in response['items']:
if item["name"] == instance_name:
try:
ip_address = item['networkInterfaces'][0]['accessConfigs'][0]['natIP']
except Exception as e:
ip_address = None
status = True if item["status"] == "RUNNING" else False
return status, ip_address
def activate_server(credentials, instance_name):
service = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
request = service.instances().start(project='nus-cisco-corp-lab-wp1', zone='asia-southeast1-c', instance=instance_name)
response = request.execute()
return response
st.title("Lucky Reactor")
instance_name = "lucky-reactor"
credentials = auth()
status, ip_address = get_status(credentials, instance_name)
st.image("cover.jpg", caption="Lucky Reactor is currently " + ("running π" if status else "sleeping π΄") + ".")
if not status:
with st.form("my_form"):
token = st.text_input("Token (FYI: The reactor costs USD $10 per hour to operate)")
submitted = st.form_submit_button("Ignite π")
if submitted and token == os.getenv("EASY_TOKEN"):
st.write("Lucky Reactor has been ignited. Please wait a few minutes (3~5) to preceed...")
response = activate_server(credentials, instance_name)
else:
st.write(f"You can access Lucky Reactor via http://{ip_address}:7860") |