|
| 1 | +import os |
| 2 | + |
| 3 | +from amsc_client import Client |
1 | 4 | from trame.widgets import vuetify3 as vuetify |
2 | 5 |
|
| 6 | +from error_manager import add_error |
| 7 | +from state_manager import state |
| 8 | + |
| 9 | +IRI_ACCESS_TOKEN_ENV = "IRI_ACCESS_TOKEN" |
| 10 | + |
| 11 | + |
| 12 | +def update_iriapi_info(): |
| 13 | + print("Updating AmSC IRI API info...") |
| 14 | + try: |
| 15 | + # Create an authenticated client |
| 16 | + client = Client(token=state.iriapi_key) |
| 17 | + # Update Perlmutter info |
| 18 | + nersc = client.facility("nersc") |
| 19 | + perlmutter = nersc.resource("compute") |
| 20 | + state.perlmutter_description = f"{perlmutter.description}" |
| 21 | + state.perlmutter_status = f"{perlmutter.status}" |
| 22 | + print( |
| 23 | + f"Perlmutter status is {state.perlmutter_status} with description '{state.perlmutter_description}'" |
| 24 | + ) |
| 25 | + except Exception as e: |
| 26 | + print(f"An unexpected error occurred when connecting to AmSC IRI API:\n{e}") |
| 27 | + # Reset key expiration date |
| 28 | + state.iriapi_key_expiration = "Unavailable" |
| 29 | + # Reset Perlmutter status |
| 30 | + state.perlmutter_description = "Unavailable" |
| 31 | + state.perlmutter_status = "unavailable" |
| 32 | + title = "Unable to connect to NERSC" |
| 33 | + msg = f"Error occurred when connecting to NERSC through the AmSC IRI API: {e}" |
| 34 | + add_error(title, msg) |
| 35 | + print(msg) |
| 36 | + |
| 37 | + |
| 38 | +@state.change("iriapi_key_dict") |
| 39 | +def load_iriapi_credentials(**kwargs): |
| 40 | + # Load credentials from the uploaded token file |
| 41 | + if state.iriapi_key_dict is not None: |
| 42 | + print("Loading AmSC IRI API credentials from file...") |
| 43 | + state.iriapi_key = state.iriapi_key_dict["content"].decode("utf-8") |
| 44 | + update_iriapi_info() |
| 45 | + |
3 | 46 |
|
4 | 47 | def load_iriapi_card(): |
5 | 48 | print("Setting AmSC IRI API card...") |
6 | | - # row with component to upload input file with top padding |
| 49 | + # Prefer an environment-provided token when running in deployed contexts |
| 50 | + if IRI_ACCESS_TOKEN_ENV in os.environ: |
| 51 | + print("Loading AmSC IRI API credentials from environment...") |
| 52 | + state.iriapi_key = os.environ[IRI_ACCESS_TOKEN_ENV] |
| 53 | + update_iriapi_info() |
| 54 | + # Row with component to upload input file with top padding |
7 | 55 | with vuetify.VRow(style="padding-top: 20px;"): |
8 | 56 | with vuetify.VCol(): |
9 | 57 | vuetify.VFileInput( |
10 | | - v_model=("sfapi_key_dict",), |
| 58 | + v_model=("iriapi_key_dict",), |
11 | 59 | label="Token File", |
12 | | - accept=".pem", |
| 60 | + accept=".txt", |
13 | 61 | prepend_icon="", |
14 | 62 | prepend_inner_icon="mdi-paperclip", |
15 | 63 | __properties=["accept"], |
16 | 64 | ) |
17 | | - # row with text field to display key expiration date |
| 65 | + # Row with text field to display key expiration date |
18 | 66 | with vuetify.VRow(): |
19 | 67 | with vuetify.VCol(): |
20 | 68 | vuetify.VTextField( |
21 | | - v_model=("sfapi_key_expiration",), |
| 69 | + v_model=("iriapi_key_expiration",), |
22 | 70 | label="Token Expiration (if expired or unavailable, please upload a valid token)", |
23 | 71 | readonly=True, |
24 | 72 | ) |
25 | | - # row with text field to display Perlmutter status |
| 73 | + # Row with text field to display Perlmutter status |
26 | 74 | with vuetify.VRow(): |
27 | 75 | with vuetify.VCol(): |
28 | 76 | vuetify.VTextField( |
|
0 commit comments