forked from runpod/runpod-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
111 lines (85 loc) · 3.09 KB
/
functions.py
File metadata and controls
111 lines (85 loc) · 3.09 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
runpod | cli | config.py
A collection of functions to set and validate configurations.
Configurations are TOML files located under ~/.runpod/
"""
import os
import tempfile
from pathlib import Path
import tomli as toml
import tomlkit
CREDENTIAL_FILE = os.path.expanduser("~/.runpod/config.toml")
def set_credentials(api_key: str, profile: str = "default", overwrite=False) -> None:
"""
Sets the user's credentials in ~/.runpod/config.toml
If profile already exists user must pass overwrite=True.
Args:
api_key (str): The user's API key.
profile (str): The profile to set the credentials for.
--- File Structure ---
[default]
api_key = "RUNPOD_API_KEY"
"""
cred_dir = os.path.dirname(CREDENTIAL_FILE)
os.makedirs(cred_dir, exist_ok=True)
Path(CREDENTIAL_FILE).touch(exist_ok=True)
with open(CREDENTIAL_FILE, "r", encoding="UTF-8") as cred_file:
try:
content = cred_file.read()
config = (
tomlkit.parse(content)
if content.strip()
else tomlkit.document()
)
except tomlkit.exceptions.ParseError as exc:
raise ValueError("~/.runpod/config.toml is not a valid TOML file.") from exc
if not overwrite:
if profile in config:
raise ValueError(
"Profile already exists. Use set_credentials(overwrite=True) to update."
)
config[profile] = {"api_key": api_key}
fd, tmp_path = tempfile.mkstemp(dir=cred_dir, suffix=".toml")
try:
with os.fdopen(fd, "w", encoding="UTF-8") as tmp_file:
tomlkit.dump(config, tmp_file)
os.replace(tmp_path, CREDENTIAL_FILE)
except BaseException:
os.unlink(tmp_path)
raise
def check_credentials(profile: str = "default"):
"""
Checks if the credentials file exists and is valid.
"""
if not os.path.exists(CREDENTIAL_FILE):
return False, "~/.runpod/config.toml does not exist."
# Check for default api_key
try:
with open(CREDENTIAL_FILE, "rb") as cred_file:
config = toml.load(cred_file)
if profile not in config:
return False, f"~/.runpod/config.toml is missing {profile} profile."
if "api_key" not in config[profile]:
return (
False,
f"~/.runpod/config.toml is missing api_key for {profile} profile.",
)
except (TypeError, ValueError):
return False, "~/.runpod/config.toml is not a valid TOML file."
return True, None
def get_credentials(profile="default"):
"""
Returns the credentials for the specified profile from ~/.runpod/config.toml
Returns None if the file does not exist, is not valid TOML, or does not
contain the requested profile.
"""
if not os.path.exists(CREDENTIAL_FILE):
return None
try:
with open(CREDENTIAL_FILE, "rb") as cred_file:
credentials = toml.load(cred_file)
except (TypeError, ValueError):
return None
if profile not in credentials:
return None
return credentials[profile]