|
29 | 29 |
|
30 | 30 | POLL_INTERVAL = 5
|
31 | 31 |
|
32 |
| -_iam_token = None |
33 |
| -_sdk = None |
34 |
| -_tenant_id = None |
35 |
| -_project_id = None |
36 |
| - |
37 | 32 | _IMPORT_ERROR_MESSAGE = ('Failed to import dependencies for Nebius AI Cloud.'
|
38 | 33 | 'Try pip install "skypilot[nebius]"')
|
39 | 34 |
|
@@ -81,56 +76,49 @@ def vpc():
|
81 | 76 | return vpc_v1
|
82 | 77 |
|
83 | 78 |
|
| 79 | +@annotations.lru_cache(scope='request') |
84 | 80 | def get_iam_token():
|
85 |
| - global _iam_token |
86 |
| - if _iam_token is None: |
87 |
| - try: |
88 |
| - with open(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH), |
89 |
| - encoding='utf-8') as file: |
90 |
| - _iam_token = file.read().strip() |
91 |
| - except FileNotFoundError: |
92 |
| - return None |
93 |
| - return _iam_token |
| 81 | + try: |
| 82 | + with open(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH), |
| 83 | + encoding='utf-8') as file: |
| 84 | + return file.read().strip() |
| 85 | + except FileNotFoundError: |
| 86 | + return None |
94 | 87 |
|
95 | 88 |
|
| 89 | +@annotations.lru_cache(scope='request') |
96 | 90 | def is_token_or_cred_file_exist():
|
97 | 91 | return (os.path.exists(os.path.expanduser(NEBIUS_IAM_TOKEN_PATH)) or
|
98 | 92 | os.path.exists(os.path.expanduser(NEBIUS_CREDENTIALS_PATH)))
|
99 | 93 |
|
100 | 94 |
|
| 95 | +@annotations.lru_cache(scope='request') |
101 | 96 | def get_project_id():
|
102 |
| - global _project_id |
103 |
| - if _project_id is None: |
104 |
| - try: |
105 |
| - with open(os.path.expanduser(NEBIUS_PROJECT_ID_PATH), |
106 |
| - encoding='utf-8') as file: |
107 |
| - _project_id = file.read().strip() |
108 |
| - except FileNotFoundError: |
109 |
| - return None |
110 |
| - return _project_id |
| 97 | + try: |
| 98 | + with open(os.path.expanduser(NEBIUS_PROJECT_ID_PATH), |
| 99 | + encoding='utf-8') as file: |
| 100 | + return file.read().strip() |
| 101 | + except FileNotFoundError: |
| 102 | + return None |
111 | 103 |
|
112 | 104 |
|
| 105 | +@annotations.lru_cache(scope='request') |
113 | 106 | def get_tenant_id():
|
114 |
| - global _tenant_id |
115 |
| - if _tenant_id is None: |
116 |
| - try: |
117 |
| - with open(os.path.expanduser(NEBIUS_TENANT_ID_PATH), |
118 |
| - encoding='utf-8') as file: |
119 |
| - _tenant_id = file.read().strip() |
120 |
| - except FileNotFoundError: |
121 |
| - return None |
122 |
| - return _tenant_id |
| 107 | + try: |
| 108 | + with open(os.path.expanduser(NEBIUS_TENANT_ID_PATH), |
| 109 | + encoding='utf-8') as file: |
| 110 | + return file.read().strip() |
| 111 | + except FileNotFoundError: |
| 112 | + return None |
123 | 113 |
|
124 | 114 |
|
| 115 | +@annotations.lru_cache(scope='request') |
125 | 116 | def sdk():
|
126 |
| - global _sdk |
127 |
| - if _sdk is None: |
128 |
| - if get_iam_token() is not None: |
129 |
| - _sdk = nebius.sdk.SDK(credentials=get_iam_token()) |
130 |
| - return _sdk |
131 |
| - _sdk = nebius.sdk.SDK( |
132 |
| - credentials_file_name=os.path.expanduser(NEBIUS_CREDENTIALS_PATH)) |
133 |
| - return _sdk |
| 117 | + token = get_iam_token() |
| 118 | + if token is not None: |
| 119 | + return nebius.sdk.SDK(credentials=token) |
| 120 | + return nebius.sdk.SDK( |
| 121 | + credentials_file_name=os.path.expanduser(NEBIUS_CREDENTIALS_PATH)) |
134 | 122 |
|
135 | 123 |
|
136 | 124 | def get_nebius_credentials(boto3_session):
|
|
0 commit comments