godfather-cli 2.0.0__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {godfather_cli-2.0.0/godfather_cli.egg-info → godfather_cli-2.1.0}/PKG-INFO +16 -6
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/README.md +14 -5
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli/__init__.py +1 -1
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli/auth.py +41 -6
- godfather_cli-2.1.0/godfather_cli/cli.py +385 -0
- godfather_cli-2.1.0/godfather_cli/picker.py +155 -0
- godfather_cli-2.1.0/godfather_cli/pod_manager.py +225 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0/godfather_cli.egg-info}/PKG-INFO +16 -6
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli.egg-info/SOURCES.txt +1 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli.egg-info/requires.txt +1 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/pyproject.toml +2 -1
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/tests/test_cli.py +10 -0
- godfather_cli-2.0.0/godfather_cli/cli.py +0 -243
- godfather_cli-2.0.0/godfather_cli/pod_manager.py +0 -149
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/LICENSE +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/MANIFEST.in +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli/ssh_connector.py +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli/ui.py +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli/update_checker.py +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli.egg-info/dependency_links.txt +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli.egg-info/entry_points.txt +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/godfather_cli.egg-info/top_level.txt +0 -0
- {godfather_cli-2.0.0 → godfather_cli-2.1.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: godfather-cli
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.1.0
|
|
4
4
|
Summary: Free compute for ASU students: connect to AI Society at ASU GPU pods
|
|
5
5
|
Author-email: AI Society ASU <theaisocietyasu@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -24,6 +24,7 @@ License-File: LICENSE
|
|
|
24
24
|
Requires-Dist: requests>=2.25.0
|
|
25
25
|
Requires-Dist: rich>=13.0.0
|
|
26
26
|
Requires-Dist: packaging>=20.0
|
|
27
|
+
Requires-Dist: questionary>=2.0.0
|
|
27
28
|
Dynamic: license-file
|
|
28
29
|
|
|
29
30
|
# Godfather CLI
|
|
@@ -79,8 +80,16 @@ Windows 10+ ship it.
|
|
|
79
80
|
|
|
80
81
|
### Interactive menu
|
|
81
82
|
|
|
82
|
-
Running `godfather` with no arguments opens a menu
|
|
83
|
-
|
|
83
|
+
Running `godfather` with no arguments opens a menu. Use the arrow keys to
|
|
84
|
+
choose Connect to a pod, List pods, Status, Log out or Exit, and press Enter.
|
|
85
|
+
Ctrl-C quits.
|
|
86
|
+
|
|
87
|
+
Connect to a pod shows a list of your pods with their status, access and
|
|
88
|
+
pod ID. A pod that is not running shows with the reason and cannot be
|
|
89
|
+
chosen. If only one pod is running, the CLI asks for a yes instead.
|
|
90
|
+
|
|
91
|
+
When input is not a terminal (for example in a script), the menu and the pod
|
|
92
|
+
list use numbered prompts.
|
|
84
93
|
|
|
85
94
|
### Commands
|
|
86
95
|
|
|
@@ -88,7 +97,7 @@ check status, or log out.
|
|
|
88
97
|
godfather list # List pods you can connect to
|
|
89
98
|
godfather connect # Connect to a pod, picking from a list
|
|
90
99
|
godfather connect <pod-id> # Connect to a specific pod
|
|
91
|
-
godfather status # Show login and
|
|
100
|
+
godfather status # Show server, org, login, token expiry and version
|
|
92
101
|
godfather auth # Log in, or refresh an expired session
|
|
93
102
|
godfather logout # Clear the stored session
|
|
94
103
|
godfather update # Update the CLI to the latest version
|
|
@@ -118,7 +127,8 @@ another server or org; the ones you last logged in with are remembered.
|
|
|
118
127
|
- **"Couldn't reach \<url\>"** — check your internet connection and that the
|
|
119
128
|
API URL is correct (`godfather status` shows what's currently configured).
|
|
120
129
|
- **"Your token is invalid or expired"** — run `godfather auth` and sign in
|
|
121
|
-
again.
|
|
130
|
+
again. In the menu, the CLI offers to do this for you.
|
|
131
|
+
- **"Pod is not running"** — the pod stopped. Ask an officer to start it.
|
|
122
132
|
- **"You are no longer a member of this organization"** — rejoin the club's
|
|
123
133
|
Discord server, then try again.
|
|
124
134
|
- **"SSH could not log in to the pod"** — the pod does not run the
|
|
@@ -130,7 +140,7 @@ another server or org; the ones you last logged in with are remembered.
|
|
|
130
140
|
|
|
131
141
|
## Development
|
|
132
142
|
|
|
133
|
-
The code is in `godfather_cli/`: `cli.py` (commands and menu), `auth.py`
|
|
143
|
+
The code is in `godfather_cli/`: `cli.py` (commands and menu), `picker.py` (arrow-key menu and pod picker), `auth.py`
|
|
134
144
|
(platform sign-in), `pod_manager.py` (API calls), `ssh_connector.py` (keys,
|
|
135
145
|
certificate, running ssh), `update_checker.py` (PyPI version check), `ui.py`
|
|
136
146
|
(shared console styling). Tests are in `tests/`:
|
|
@@ -51,8 +51,16 @@ Windows 10+ ship it.
|
|
|
51
51
|
|
|
52
52
|
### Interactive menu
|
|
53
53
|
|
|
54
|
-
Running `godfather` with no arguments opens a menu
|
|
55
|
-
|
|
54
|
+
Running `godfather` with no arguments opens a menu. Use the arrow keys to
|
|
55
|
+
choose Connect to a pod, List pods, Status, Log out or Exit, and press Enter.
|
|
56
|
+
Ctrl-C quits.
|
|
57
|
+
|
|
58
|
+
Connect to a pod shows a list of your pods with their status, access and
|
|
59
|
+
pod ID. A pod that is not running shows with the reason and cannot be
|
|
60
|
+
chosen. If only one pod is running, the CLI asks for a yes instead.
|
|
61
|
+
|
|
62
|
+
When input is not a terminal (for example in a script), the menu and the pod
|
|
63
|
+
list use numbered prompts.
|
|
56
64
|
|
|
57
65
|
### Commands
|
|
58
66
|
|
|
@@ -60,7 +68,7 @@ check status, or log out.
|
|
|
60
68
|
godfather list # List pods you can connect to
|
|
61
69
|
godfather connect # Connect to a pod, picking from a list
|
|
62
70
|
godfather connect <pod-id> # Connect to a specific pod
|
|
63
|
-
godfather status # Show login and
|
|
71
|
+
godfather status # Show server, org, login, token expiry and version
|
|
64
72
|
godfather auth # Log in, or refresh an expired session
|
|
65
73
|
godfather logout # Clear the stored session
|
|
66
74
|
godfather update # Update the CLI to the latest version
|
|
@@ -90,7 +98,8 @@ another server or org; the ones you last logged in with are remembered.
|
|
|
90
98
|
- **"Couldn't reach \<url\>"** — check your internet connection and that the
|
|
91
99
|
API URL is correct (`godfather status` shows what's currently configured).
|
|
92
100
|
- **"Your token is invalid or expired"** — run `godfather auth` and sign in
|
|
93
|
-
again.
|
|
101
|
+
again. In the menu, the CLI offers to do this for you.
|
|
102
|
+
- **"Pod is not running"** — the pod stopped. Ask an officer to start it.
|
|
94
103
|
- **"You are no longer a member of this organization"** — rejoin the club's
|
|
95
104
|
Discord server, then try again.
|
|
96
105
|
- **"SSH could not log in to the pod"** — the pod does not run the
|
|
@@ -102,7 +111,7 @@ another server or org; the ones you last logged in with are remembered.
|
|
|
102
111
|
|
|
103
112
|
## Development
|
|
104
113
|
|
|
105
|
-
The code is in `godfather_cli/`: `cli.py` (commands and menu), `auth.py`
|
|
114
|
+
The code is in `godfather_cli/`: `cli.py` (commands and menu), `picker.py` (arrow-key menu and pod picker), `auth.py`
|
|
106
115
|
(platform sign-in), `pod_manager.py` (API calls), `ssh_connector.py` (keys,
|
|
107
116
|
certificate, running ssh), `update_checker.py` (PyPI version check), `ui.py`
|
|
108
117
|
(shared console styling). Tests are in `tests/`:
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""CLI authentication: token storage, login flow, and token verification."""
|
|
2
2
|
|
|
3
|
+
import datetime
|
|
3
4
|
import json
|
|
4
5
|
import os
|
|
5
6
|
import requests
|
|
@@ -12,6 +13,15 @@ from rich.prompt import Prompt
|
|
|
12
13
|
from .ui import console, success, error, warning, spinner, BOX, BORDER
|
|
13
14
|
|
|
14
15
|
TOKEN_PREFIX = 'plat_'
|
|
16
|
+
# The platform issues CLI tokens for 90 days.
|
|
17
|
+
TOKEN_DAYS = 90
|
|
18
|
+
|
|
19
|
+
# Token states that check_token returns.
|
|
20
|
+
TOKEN_OK = 'ok'
|
|
21
|
+
TOKEN_MISSING = 'missing'
|
|
22
|
+
TOKEN_EXPIRED = 'expired'
|
|
23
|
+
TOKEN_UNREACHABLE = 'unreachable'
|
|
24
|
+
TOKEN_REFUSED = 'refused'
|
|
15
25
|
|
|
16
26
|
|
|
17
27
|
class CLIAuthenticator:
|
|
@@ -69,7 +79,10 @@ class CLIAuthenticator:
|
|
|
69
79
|
except webbrowser.Error:
|
|
70
80
|
pass
|
|
71
81
|
|
|
72
|
-
|
|
82
|
+
try:
|
|
83
|
+
token = Prompt.ask("Token", password=True).strip()
|
|
84
|
+
except EOFError:
|
|
85
|
+
token = ''
|
|
73
86
|
if not token:
|
|
74
87
|
error("No token entered")
|
|
75
88
|
return False
|
|
@@ -134,10 +147,10 @@ class CLIAuthenticator:
|
|
|
134
147
|
else:
|
|
135
148
|
warning("You weren't logged in")
|
|
136
149
|
|
|
137
|
-
def
|
|
138
|
-
"""
|
|
150
|
+
def check_token(self) -> str:
|
|
151
|
+
"""Ask the server whether the stored token works. Returns one of the TOKEN_ states."""
|
|
139
152
|
if not self.is_authenticated():
|
|
140
|
-
return
|
|
153
|
+
return TOKEN_MISSING
|
|
141
154
|
|
|
142
155
|
try:
|
|
143
156
|
response = requests.get(
|
|
@@ -145,6 +158,28 @@ class CLIAuthenticator:
|
|
|
145
158
|
headers=self.auth_headers(),
|
|
146
159
|
timeout=5
|
|
147
160
|
)
|
|
148
|
-
return response.status_code == 200
|
|
149
161
|
except requests.RequestException:
|
|
150
|
-
return
|
|
162
|
+
return TOKEN_UNREACHABLE
|
|
163
|
+
if response.status_code == 200:
|
|
164
|
+
return TOKEN_OK
|
|
165
|
+
if response.status_code == 401:
|
|
166
|
+
return TOKEN_EXPIRED
|
|
167
|
+
return TOKEN_REFUSED
|
|
168
|
+
|
|
169
|
+
def verify_token(self) -> bool:
|
|
170
|
+
"""Check the stored token is still accepted by the backend."""
|
|
171
|
+
return self.check_token() == TOKEN_OK
|
|
172
|
+
|
|
173
|
+
def token_saved_at(self) -> Optional[datetime.datetime]:
|
|
174
|
+
"""When the stored token was saved, from the config file time. Only login writes a token."""
|
|
175
|
+
if not self.is_authenticated():
|
|
176
|
+
return None
|
|
177
|
+
try:
|
|
178
|
+
return datetime.datetime.fromtimestamp(self.config_file.stat().st_mtime)
|
|
179
|
+
except OSError:
|
|
180
|
+
return None
|
|
181
|
+
|
|
182
|
+
def token_expires_at(self) -> Optional[datetime.datetime]:
|
|
183
|
+
"""About when the stored token expires: TOKEN_DAYS after it was saved."""
|
|
184
|
+
saved = self.token_saved_at()
|
|
185
|
+
return saved + datetime.timedelta(days=TOKEN_DAYS) if saved else None
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Godfather CLI entry point: argument parsing and the interactive menu."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import datetime
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Dict, List, Optional
|
|
9
|
+
|
|
10
|
+
from rich.panel import Panel
|
|
11
|
+
from rich.table import Table
|
|
12
|
+
from rich.prompt import Prompt
|
|
13
|
+
|
|
14
|
+
from . import picker
|
|
15
|
+
from .auth import (
|
|
16
|
+
CLIAuthenticator, TOKEN_DAYS, TOKEN_EXPIRED, TOKEN_MISSING, TOKEN_OK, TOKEN_REFUSED, TOKEN_UNREACHABLE,
|
|
17
|
+
)
|
|
18
|
+
from .picker import ask_menu, ask_pod, ask_yes, connectable, is_interactive
|
|
19
|
+
from .pod_manager import ApiError, EXPIRED, PodManager, report
|
|
20
|
+
from .ssh_connector import SSHConnector
|
|
21
|
+
from .update_checker import check_for_updates, show_update_warning, perform_update
|
|
22
|
+
from .ui import console, error, warning, info, spinner, BOX, BORDER, PURPLE
|
|
23
|
+
from . import __version__
|
|
24
|
+
|
|
25
|
+
# The AI Society platform API and org. Override with --api-url/--org or GODFATHER_API_URL/GODFATHER_ORG.
|
|
26
|
+
DEFAULT_API_URL = 'https://zs6k5wi0boaaax-8000.proxy.runpod.net'
|
|
27
|
+
# Servers that no longer exist. A saved login or GODFATHER_API_URL that names one falls back to the default.
|
|
28
|
+
# Installers before 2.0 wrote GODFATHER_API_URL into ~/.bashrc, ~/.zshrc and fish conf.d.
|
|
29
|
+
RETIRED_API_URLS = {
|
|
30
|
+
'https://854ap0rs1ws50n-8000.proxy.runpod.net',
|
|
31
|
+
'https://8bzhwve1ri5cw2-80.proxy.runpod.net',
|
|
32
|
+
}
|
|
33
|
+
DEFAULT_ORG = 'ais'
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class GodfatherCLI:
|
|
37
|
+
"""Wires together auth, pod listing, and SSH connection for the CLI commands."""
|
|
38
|
+
|
|
39
|
+
def __init__(self):
|
|
40
|
+
self.config_dir = Path.home() / '.godfather'
|
|
41
|
+
|
|
42
|
+
# A flag or env var wins, then what the last login saved, then the defaults.
|
|
43
|
+
saved = CLIAuthenticator.read_config(self.config_dir)
|
|
44
|
+
saved_url = saved.get('api_url')
|
|
45
|
+
if saved_url and saved_url.rstrip('/') in RETIRED_API_URLS:
|
|
46
|
+
saved_url = None
|
|
47
|
+
env_url = os.getenv('GODFATHER_API_URL')
|
|
48
|
+
if env_url and env_url.rstrip('/') in RETIRED_API_URLS:
|
|
49
|
+
warning(
|
|
50
|
+
f'GODFATHER_API_URL is set to {env_url}, a server that no longer exists. Using '
|
|
51
|
+
f'{DEFAULT_API_URL} instead. Remove the GODFATHER_API_URL line from your shell profile.'
|
|
52
|
+
)
|
|
53
|
+
env_url = None
|
|
54
|
+
self.api_base = (env_url or saved_url or DEFAULT_API_URL).rstrip('/')
|
|
55
|
+
self.org = os.getenv('GODFATHER_ORG') or saved.get('org') or DEFAULT_ORG
|
|
56
|
+
|
|
57
|
+
self.authenticator = CLIAuthenticator(self.api_base, self.org, self.config_dir)
|
|
58
|
+
self.pod_manager = PodManager(self.api_base, self.authenticator)
|
|
59
|
+
self.ssh_connector = SSHConnector(self.config_dir)
|
|
60
|
+
|
|
61
|
+
def print_banner(self):
|
|
62
|
+
banner = Panel.fit(
|
|
63
|
+
f"[bold {PURPLE}]Godfather CLI[/bold {PURPLE}]\n[dim]Free compute from AI Society at ASU v{__version__}[/dim]",
|
|
64
|
+
border_style=BORDER,
|
|
65
|
+
box=BOX,
|
|
66
|
+
)
|
|
67
|
+
console.print(banner)
|
|
68
|
+
console.print(f"[dim]Server {self.api_base} Org {self.org}[/dim]")
|
|
69
|
+
console.print()
|
|
70
|
+
|
|
71
|
+
def ensure_authenticated(self) -> bool:
|
|
72
|
+
"""Make sure the user has a valid token, prompting to log in if needed."""
|
|
73
|
+
if not self.authenticator.is_authenticated():
|
|
74
|
+
info("You're not logged in yet.")
|
|
75
|
+
return self.authenticator.authenticate()
|
|
76
|
+
|
|
77
|
+
with spinner("Checking your login..."):
|
|
78
|
+
state = self.authenticator.check_token()
|
|
79
|
+
|
|
80
|
+
if state == TOKEN_EXPIRED:
|
|
81
|
+
warning("Your token has expired, or a newer login replaced it.")
|
|
82
|
+
if is_interactive():
|
|
83
|
+
try:
|
|
84
|
+
if not ask_yes("Log in again now?"):
|
|
85
|
+
return False
|
|
86
|
+
except (KeyboardInterrupt, EOFError):
|
|
87
|
+
return False
|
|
88
|
+
return self.authenticator.authenticate()
|
|
89
|
+
if state == TOKEN_UNREACHABLE:
|
|
90
|
+
error(f"Couldn't reach {self.api_base}")
|
|
91
|
+
console.print("[dim]Check your internet connection, and the server URL with 'godfather status'.[/dim]")
|
|
92
|
+
return False
|
|
93
|
+
# TOKEN_REFUSED: the pods call that follows prints the server's reason.
|
|
94
|
+
return True
|
|
95
|
+
|
|
96
|
+
def _offer_login(self, e: ApiError) -> bool:
|
|
97
|
+
"""After an expired token error, offer to log in again. True when the user logged in."""
|
|
98
|
+
report(e)
|
|
99
|
+
if not is_interactive():
|
|
100
|
+
return False
|
|
101
|
+
try:
|
|
102
|
+
if not ask_yes("Log in again now?"):
|
|
103
|
+
return False
|
|
104
|
+
except (KeyboardInterrupt, EOFError):
|
|
105
|
+
return False
|
|
106
|
+
return self.authenticator.authenticate()
|
|
107
|
+
|
|
108
|
+
def load_pods(self) -> Optional[List[Dict]]:
|
|
109
|
+
"""Fetch the member's pods with a spinner. None after an error has been printed."""
|
|
110
|
+
for attempt in range(2):
|
|
111
|
+
try:
|
|
112
|
+
with spinner("Fetching your pods..."):
|
|
113
|
+
return self.pod_manager.fetch_pods()
|
|
114
|
+
except ApiError as e:
|
|
115
|
+
if e.kind == EXPIRED and attempt == 0:
|
|
116
|
+
if self._offer_login(e):
|
|
117
|
+
continue
|
|
118
|
+
return None
|
|
119
|
+
report(e)
|
|
120
|
+
return None
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
def list_pods(self):
|
|
124
|
+
if not self.ensure_authenticated():
|
|
125
|
+
return
|
|
126
|
+
pods = self.load_pods()
|
|
127
|
+
if pods is not None:
|
|
128
|
+
self.pod_manager.show_pods(pods)
|
|
129
|
+
|
|
130
|
+
def choose_pod(self) -> Optional[str]:
|
|
131
|
+
"""Fetch pods and let the user pick one. None when there is none or the user cancels."""
|
|
132
|
+
pods = self.load_pods()
|
|
133
|
+
if pods is None:
|
|
134
|
+
return None
|
|
135
|
+
if not pods:
|
|
136
|
+
self.pod_manager.show_no_pods()
|
|
137
|
+
return None
|
|
138
|
+
if not is_interactive():
|
|
139
|
+
return self.pod_manager.prompt_pod_number(pods)
|
|
140
|
+
if not connectable(pods):
|
|
141
|
+
error("None of your pods are running.")
|
|
142
|
+
console.print("[dim]Ask an officer to start one, then run 'godfather connect' again.[/dim]")
|
|
143
|
+
return None
|
|
144
|
+
try:
|
|
145
|
+
return ask_pod(pods)
|
|
146
|
+
except (KeyboardInterrupt, EOFError):
|
|
147
|
+
console.print("[dim]Cancelled[/dim]")
|
|
148
|
+
return None
|
|
149
|
+
|
|
150
|
+
def connect_to_pod(self, pod_id: str = None):
|
|
151
|
+
if not self.ensure_authenticated():
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
if not pod_id:
|
|
155
|
+
pod_id = self.choose_pod()
|
|
156
|
+
if not pod_id:
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
public_key = self.ssh_connector.ensure_keypair()
|
|
160
|
+
if not public_key:
|
|
161
|
+
return
|
|
162
|
+
|
|
163
|
+
ssh_info = None
|
|
164
|
+
for attempt in range(2):
|
|
165
|
+
try:
|
|
166
|
+
with spinner(f"Getting a certificate for pod {pod_id[:12]}..."):
|
|
167
|
+
ssh_info = self.pod_manager.fetch_connection_info(pod_id, public_key)
|
|
168
|
+
break
|
|
169
|
+
except ApiError as e:
|
|
170
|
+
if e.kind == EXPIRED and attempt == 0:
|
|
171
|
+
if self._offer_login(e):
|
|
172
|
+
continue
|
|
173
|
+
return
|
|
174
|
+
report(e)
|
|
175
|
+
return
|
|
176
|
+
if not ssh_info:
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
if not self.ssh_connector.save_certificate(ssh_info.get('certificate', '')):
|
|
180
|
+
return
|
|
181
|
+
|
|
182
|
+
self.ssh_connector.connect(ssh_info)
|
|
183
|
+
|
|
184
|
+
def status(self):
|
|
185
|
+
"""Print the server, org, login, token and version details."""
|
|
186
|
+
table = Table(title="Godfather CLI Status", box=BOX, border_style=BORDER)
|
|
187
|
+
table.add_column("Setting", style=f"bold {PURPLE}", no_wrap=True)
|
|
188
|
+
table.add_column("Value")
|
|
189
|
+
|
|
190
|
+
with spinner("Checking the server and PyPI..."):
|
|
191
|
+
state = self.authenticator.check_token()
|
|
192
|
+
pods = None
|
|
193
|
+
if state == TOKEN_OK:
|
|
194
|
+
try:
|
|
195
|
+
pods = self.pod_manager.fetch_pods()
|
|
196
|
+
except ApiError:
|
|
197
|
+
pods = None
|
|
198
|
+
has_update, latest = check_for_updates()
|
|
199
|
+
|
|
200
|
+
table.add_row("Server", self.api_base)
|
|
201
|
+
table.add_row("Organization", self.org)
|
|
202
|
+
table.add_row("Login", LOGIN_TEXT.get(state, state))
|
|
203
|
+
if self.authenticator.is_authenticated():
|
|
204
|
+
table.add_row("Signed in as", "Your Discord account [dim](the server does not send the name)[/dim]")
|
|
205
|
+
token = str(self.authenticator.get_token() or '')
|
|
206
|
+
table.add_row("Token", f"{token[:9]}...")
|
|
207
|
+
saved = self.authenticator.token_saved_at()
|
|
208
|
+
expires = self.authenticator.token_expires_at()
|
|
209
|
+
if saved and expires:
|
|
210
|
+
left = (expires.date() - datetime.date.today()).days
|
|
211
|
+
when = f"about {expires:%Y-%m-%d}" + (f" ({left} days left)" if left >= 0 else " (past)")
|
|
212
|
+
table.add_row(
|
|
213
|
+
"Token expires", f"{when} [dim](logged in {saved:%Y-%m-%d}, tokens last {TOKEN_DAYS} days)[/dim]"
|
|
214
|
+
)
|
|
215
|
+
if pods is not None:
|
|
216
|
+
table.add_row("Pods you can use", str(len(pods)))
|
|
217
|
+
table.add_row("Config directory", str(self.config_dir))
|
|
218
|
+
if has_update:
|
|
219
|
+
table.add_row("CLI version", f"{__version__} [yellow]({latest} available, run 'godfather update')[/yellow]")
|
|
220
|
+
else:
|
|
221
|
+
table.add_row("CLI version", f"{__version__} [dim](no newer version found on PyPI)[/dim]")
|
|
222
|
+
|
|
223
|
+
console.print(table)
|
|
224
|
+
if state == TOKEN_EXPIRED:
|
|
225
|
+
console.print("[dim]Run 'godfather auth' to log in again.[/dim]")
|
|
226
|
+
elif state == TOKEN_UNREACHABLE:
|
|
227
|
+
console.print("[dim]Check your internet connection, or pass --api-url to use another server.[/dim]")
|
|
228
|
+
elif state == TOKEN_MISSING:
|
|
229
|
+
console.print("[dim]Run 'godfather auth' to log in.[/dim]")
|
|
230
|
+
|
|
231
|
+
def logout(self):
|
|
232
|
+
self.authenticator.logout()
|
|
233
|
+
|
|
234
|
+
def authenticate(self):
|
|
235
|
+
self.authenticator.authenticate()
|
|
236
|
+
|
|
237
|
+
def update(self):
|
|
238
|
+
perform_update()
|
|
239
|
+
|
|
240
|
+
def run_action(self, action: str) -> None:
|
|
241
|
+
"""Run one main menu action."""
|
|
242
|
+
if action == picker.CONNECT:
|
|
243
|
+
self.connect_to_pod()
|
|
244
|
+
elif action == picker.LIST:
|
|
245
|
+
self.list_pods()
|
|
246
|
+
elif action == picker.STATUS:
|
|
247
|
+
self.status()
|
|
248
|
+
elif action == picker.LOGIN:
|
|
249
|
+
self.authenticate()
|
|
250
|
+
elif action == picker.LOGOUT:
|
|
251
|
+
self.logout()
|
|
252
|
+
|
|
253
|
+
def interactive_menu(self):
|
|
254
|
+
"""The main menu: arrow keys on a terminal, numbered choices otherwise."""
|
|
255
|
+
self.print_banner()
|
|
256
|
+
if not is_interactive():
|
|
257
|
+
self.plain_menu()
|
|
258
|
+
return
|
|
259
|
+
|
|
260
|
+
while True:
|
|
261
|
+
try:
|
|
262
|
+
action = ask_menu(self.authenticator.is_authenticated())
|
|
263
|
+
if action == picker.EXIT:
|
|
264
|
+
break
|
|
265
|
+
console.print()
|
|
266
|
+
self.run_action(action)
|
|
267
|
+
console.print()
|
|
268
|
+
except (KeyboardInterrupt, EOFError):
|
|
269
|
+
console.print()
|
|
270
|
+
break
|
|
271
|
+
console.print("Goodbye.")
|
|
272
|
+
|
|
273
|
+
def plain_menu(self):
|
|
274
|
+
"""The numbered menu for input that is not a terminal."""
|
|
275
|
+
actions = {'1': picker.LIST, '2': picker.CONNECT, '3': picker.STATUS, '4': picker.LOGOUT, '5': picker.EXIT}
|
|
276
|
+
while True:
|
|
277
|
+
console.print()
|
|
278
|
+
menu = Table.grid(padding=(0, 2))
|
|
279
|
+
menu.add_column(style=f"bold {PURPLE}", justify="right")
|
|
280
|
+
menu.add_column()
|
|
281
|
+
menu.add_row("1.", "List available pods")
|
|
282
|
+
menu.add_row("2.", "Connect to a pod")
|
|
283
|
+
menu.add_row("3.", "Show status")
|
|
284
|
+
menu.add_row("4.", "Log out")
|
|
285
|
+
menu.add_row("5.", "Exit")
|
|
286
|
+
console.print(Panel(menu, title="What would you like to do?", border_style=BORDER, box=BOX))
|
|
287
|
+
|
|
288
|
+
try:
|
|
289
|
+
choice = Prompt.ask("\nEnter your choice", choices=list(actions), default="1")
|
|
290
|
+
except (KeyboardInterrupt, EOFError):
|
|
291
|
+
console.print()
|
|
292
|
+
break
|
|
293
|
+
console.print()
|
|
294
|
+
if actions[choice] == picker.EXIT:
|
|
295
|
+
break
|
|
296
|
+
self.run_action(actions[choice])
|
|
297
|
+
console.print("Goodbye.")
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
LOGIN_TEXT = {
|
|
301
|
+
TOKEN_OK: f"[bold {PURPLE}]Logged in, the server accepts the token[/bold {PURPLE}]",
|
|
302
|
+
TOKEN_EXPIRED: "[yellow]Token expired or replaced by a newer login[/yellow]",
|
|
303
|
+
TOKEN_UNREACHABLE: "[red]Server not reachable[/red]",
|
|
304
|
+
TOKEN_REFUSED: "[yellow]Server refused the token (run 'godfather list' to see why)[/yellow]",
|
|
305
|
+
TOKEN_MISSING: "[red]Not logged in[/red]",
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def main():
|
|
310
|
+
"""Parse arguments and dispatch to the requested command."""
|
|
311
|
+
parser = argparse.ArgumentParser(
|
|
312
|
+
prog='godfather',
|
|
313
|
+
description='Godfather CLI - manage and connect to AI Society RunPod environments',
|
|
314
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
315
|
+
epilog="""
|
|
316
|
+
Examples:
|
|
317
|
+
godfather Open the interactive menu
|
|
318
|
+
godfather list List available pods
|
|
319
|
+
godfather connect Connect to a pod, picking from a list
|
|
320
|
+
godfather connect <pod-id> Connect to a specific pod
|
|
321
|
+
godfather status Show login and configuration status
|
|
322
|
+
godfather auth Log in or refresh your session
|
|
323
|
+
godfather logout Clear the stored session
|
|
324
|
+
godfather update Update the CLI to the latest version
|
|
325
|
+
|
|
326
|
+
Questions or issues: https://discord.gg/fXWXwz6fEG
|
|
327
|
+
"""
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
parser.add_argument(
|
|
331
|
+
'command',
|
|
332
|
+
nargs='?',
|
|
333
|
+
choices=['list', 'connect', 'status', 'logout', 'auth', 'update'],
|
|
334
|
+
help='Command to run (omit to open the interactive menu)'
|
|
335
|
+
)
|
|
336
|
+
parser.add_argument(
|
|
337
|
+
'pod_id',
|
|
338
|
+
nargs='?',
|
|
339
|
+
help='Pod ID to connect to (only used with "connect")'
|
|
340
|
+
)
|
|
341
|
+
parser.add_argument(
|
|
342
|
+
'--api-url',
|
|
343
|
+
help='Use a specific platform API URL instead of the default'
|
|
344
|
+
)
|
|
345
|
+
parser.add_argument(
|
|
346
|
+
'--org',
|
|
347
|
+
help='Organization prefix on the platform (default: ais)'
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
args = parser.parse_args()
|
|
351
|
+
|
|
352
|
+
if args.api_url:
|
|
353
|
+
os.environ['GODFATHER_API_URL'] = args.api_url
|
|
354
|
+
if args.org:
|
|
355
|
+
os.environ['GODFATHER_ORG'] = args.org
|
|
356
|
+
|
|
357
|
+
cli = GodfatherCLI()
|
|
358
|
+
|
|
359
|
+
if not args.command:
|
|
360
|
+
# Only check for updates on the interactive menu, not on every
|
|
361
|
+
# scripted invocation - a 'godfather list' shouldn't wait on a
|
|
362
|
+
# PyPI round trip.
|
|
363
|
+
with spinner("Checking for updates..."):
|
|
364
|
+
has_update, latest_version = check_for_updates()
|
|
365
|
+
if has_update:
|
|
366
|
+
show_update_warning(latest_version)
|
|
367
|
+
cli.interactive_menu()
|
|
368
|
+
return
|
|
369
|
+
|
|
370
|
+
if args.command == 'list':
|
|
371
|
+
cli.list_pods()
|
|
372
|
+
elif args.command == 'connect':
|
|
373
|
+
cli.connect_to_pod(args.pod_id)
|
|
374
|
+
elif args.command == 'status':
|
|
375
|
+
cli.status()
|
|
376
|
+
elif args.command == 'logout':
|
|
377
|
+
cli.logout()
|
|
378
|
+
elif args.command == 'auth':
|
|
379
|
+
cli.authenticate()
|
|
380
|
+
elif args.command == 'update':
|
|
381
|
+
cli.update()
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
if __name__ == '__main__':
|
|
385
|
+
main()
|