forked from pewdiepie-archdaemon/odysseus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
221 lines (181 loc) · 7.52 KB
/
setup.py
File metadata and controls
221 lines (181 loc) · 7.52 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""Odysseus — first-time setup script.
Creates data directories, initializes the database, and sets up an
initial admin user. Safe to re-run (skips what already exists).
"""
import os
import shutil
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, "data")
DIRS = [
DATA_DIR,
os.path.join(DATA_DIR, "uploads"),
os.path.join(DATA_DIR, "personal_docs"),
os.path.join(DATA_DIR, "personal_uploads"),
os.path.join(DATA_DIR, "tts_cache"),
os.path.join(DATA_DIR, "generated_images"),
os.path.join(DATA_DIR, "deep_research"),
os.path.join(DATA_DIR, "chroma"),
os.path.join(DATA_DIR, "rag"),
os.path.join(DATA_DIR, "memory_vectors"),
os.path.join(BASE_DIR, "logs"),
]
def create_dirs():
for d in DIRS:
os.makedirs(d, exist_ok=True)
print(f" [ok] {os.path.relpath(d, BASE_DIR)}/")
def init_database():
"""Create all SQLAlchemy tables."""
sys.path.insert(0, BASE_DIR)
os.environ.setdefault("DATABASE_URL", f"sqlite:///{os.path.join(DATA_DIR, 'app.db')}")
from core.database import Base, engine
Base.metadata.create_all(bind=engine)
print(" [ok] Database initialized")
def _prompt_admin_credentials():
"""Interactively ask for admin username and password when running in a terminal."""
import getpass
print()
print(" Set up your admin account:")
print(" (Press Enter to accept defaults)")
print()
username = input(" Username [admin]: ").strip().lower()
if not username:
username = "admin"
while True:
password = getpass.getpass(" Password: ")
if not password:
print(" Password cannot be empty.")
continue
confirm = getpass.getpass(" Confirm password: ")
if password != confirm:
print(" Passwords don't match. Try again.")
continue
break
return username, password
def create_default_admin():
"""Create an initial admin user if none exists."""
auth_path = os.path.join(DATA_DIR, "auth.json")
if os.path.exists(auth_path):
print(" [skip] auth.json already exists")
return "exists"
try:
import bcrypt
import json
# Priority: env vars > interactive prompt > random password
username = os.getenv("ODYSSEUS_ADMIN_USER", "").strip().lower()
password = os.getenv("ODYSSEUS_ADMIN_PASSWORD", "").strip()
if username and password:
# Both provided via env — use them directly
pass
elif sys.stdin.isatty() and not os.getenv("ODYSSEUS_SKIP_ADMIN_PROMPT"):
# Interactive terminal — ask the user
username, password = _prompt_admin_credentials()
else:
# Non-interactive (Docker, CI) — fall back to generated password
username = username or "admin"
password = password or __import__("secrets").token_urlsafe(18)
username = username or "admin"
hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
auth_data = {
"users": {
username: {
"password_hash": hashed,
"is_admin": True,
}
}
}
with open(auth_path, "w", encoding="utf-8") as f:
json.dump(auth_data, f, indent=2)
if sys.stdin.isatty() and not os.getenv("ODYSSEUS_ADMIN_PASSWORD"):
print(f" [ok] Admin account created ({username})")
else:
print(f" [ok] Initial admin user created ({username})")
if not os.getenv("ODYSSEUS_ADMIN_PASSWORD"):
print(f" Temporary password: {password}")
print(f" ** Change it after first login. Set ODYSSEUS_ADMIN_PASSWORD to choose your own. **")
return "created"
except ImportError:
print(" [warn] bcrypt not installed — skipping admin user creation")
print(" Run: pip install bcrypt")
return "skipped"
def create_env():
"""Copy .env.example to .env if it doesn't exist."""
env_path = os.path.join(BASE_DIR, ".env")
example_path = os.path.join(BASE_DIR, ".env.example")
if os.path.exists(env_path):
print(" [skip] .env already exists")
return
if os.path.exists(example_path):
import shutil
shutil.copy2(example_path, env_path)
print(" [ok] .env created from .env.example")
print(" ** Edit .env with your LLM host and API keys **")
else:
print(" [warn] .env.example not found — create .env manually")
def check_deps():
"""Check for common missing dependencies."""
missing = []
for mod in ["fastapi", "uvicorn", "sqlalchemy", "bcrypt", "httpx", "dotenv"]:
try:
__import__(mod)
except ImportError:
missing.append(mod)
if missing:
print(f"\n [warn] Missing packages: {', '.join(missing)}")
print(f" Run: pip install -r requirements.txt")
else:
print(" [ok] All core dependencies installed")
if os.name != "nt" and shutil.which("tmux") is None:
print("\n [warn] tmux not found")
print(" Cookbook uses tmux for background downloads and model serves.")
print(" Install it with your OS package manager, for example:")
if sys.platform == "darwin":
print(" brew install tmux")
else:
print(" sudo apt install tmux")
print(" sudo pacman -S tmux")
print(" sudo dnf install tmux")
elif os.name != "nt":
print(" [ok] tmux installed")
def main():
print("\n=== Odysseus Setup ===\n")
print("1. Creating directories...")
create_dirs()
print("\n2. Environment file...")
create_env()
print("\n3. Checking dependencies...")
check_deps()
print("\n4. Initializing database...")
try:
init_database()
except Exception as e:
print(f" [warn] Database init failed: {e}")
print(" This is OK if dependencies aren't installed yet.")
print("\n5. Creating initial admin...")
admin_status = "failed"
try:
admin_status = create_default_admin()
except Exception as e:
print(f" [warn] Admin creation failed: {e}")
admin_status = "failed"
print("\n=== Setup complete ===")
# start-macos.sh launches the server itself (on its own port) right after
# this, so suppress the manual hint there to avoid a contradictory URL.
if not os.getenv("ODYSSEUS_SKIP_RUN_HINT"):
print(f"\nStart the server with:")
print(f" python -m uvicorn app:app --host 127.0.0.1 --port 7000")
print(f"\nThen open http://localhost:7000")
# Cleaned, action-focused final instruction strings
if admin_status == "created":
print("Login with your admin credentials.\n")
elif admin_status == "exists":
print("Login with your existing admin credentials.\n")
elif admin_status == "skipped":
print("Admin creation did not happen: dependencies are missing.\nRun 'pip install bcrypt' and rerun setup.\n")
elif admin_status == "failed":
print("Admin creation did not happen: a system or file error occurred.\nCheck write permissions for the 'data' directory and rerun setup.\n")
else: # handling "failed" or any unhandled edge case
print("Admin creation did not happen: a system or file error occurred.\nCheck write permissions for the 'data' directory and rerun setup.\n")
if __name__ == "__main__":
main()