xenfra 0.1.8__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.
- xenfra-0.1.8/PKG-INFO +95 -0
- xenfra-0.1.8/README.md +57 -0
- xenfra-0.1.8/pyproject.toml +54 -0
- xenfra-0.1.8/src/xenfra/__init__.py +1 -0
- xenfra-0.1.8/src/xenfra/api/auth.py +51 -0
- xenfra-0.1.8/src/xenfra/api/billing.py +80 -0
- xenfra-0.1.8/src/xenfra/api/connections.py +163 -0
- xenfra-0.1.8/src/xenfra/api/main.py +175 -0
- xenfra-0.1.8/src/xenfra/api/webhooks.py +146 -0
- xenfra-0.1.8/src/xenfra/cli/main.py +211 -0
- xenfra-0.1.8/src/xenfra/config.py +24 -0
- xenfra-0.1.8/src/xenfra/db/models.py +51 -0
- xenfra-0.1.8/src/xenfra/db/session.py +17 -0
- xenfra-0.1.8/src/xenfra/dependencies.py +35 -0
- xenfra-0.1.8/src/xenfra/dockerizer.py +89 -0
- xenfra-0.1.8/src/xenfra/engine.py +263 -0
- xenfra-0.1.8/src/xenfra/mcp_client.py +149 -0
- xenfra-0.1.8/src/xenfra/models.py +54 -0
- xenfra-0.1.8/src/xenfra/recipes.py +23 -0
- xenfra-0.1.8/src/xenfra/security.py +58 -0
- xenfra-0.1.8/src/xenfra/templates/Dockerfile.j2 +25 -0
- xenfra-0.1.8/src/xenfra/templates/cloud-init.sh.j2 +72 -0
- xenfra-0.1.8/src/xenfra/templates/docker-compose.yml.j2 +33 -0
- xenfra-0.1.8/src/xenfra/utils.py +69 -0
xenfra-0.1.8/PKG-INFO
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: xenfra
|
|
3
|
+
Version: 0.1.8
|
|
4
|
+
Summary: A 'Zen Mode' infrastructure engine for Python developers.
|
|
5
|
+
Author: xenfra-cloud
|
|
6
|
+
Author-email: xenfra-cloud <xenfracloud@gmail.com>
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
13
|
+
Classifier: Topic :: System :: Systems Administration
|
|
14
|
+
Requires-Dist: fabric>=3.2.2
|
|
15
|
+
Requires-Dist: python-digitalocean>=1.17.0
|
|
16
|
+
Requires-Dist: python-dotenv>=1.2.1
|
|
17
|
+
Requires-Dist: rich>=14.2.0
|
|
18
|
+
Requires-Dist: fastapi>=0.110.0
|
|
19
|
+
Requires-Dist: uvicorn[standard]>=0.27.1
|
|
20
|
+
Requires-Dist: click>=8.1.7
|
|
21
|
+
Requires-Dist: sqlmodel>=0.0.16
|
|
22
|
+
Requires-Dist: psycopg2-binary>=2.9.9
|
|
23
|
+
Requires-Dist: python-jose[cryptography]>=3.3.0
|
|
24
|
+
Requires-Dist: passlib>=1.7.4
|
|
25
|
+
Requires-Dist: httpx>=0.27.0
|
|
26
|
+
Requires-Dist: pyyaml>=6.0.1
|
|
27
|
+
Requires-Dist: python-multipart>=0.0.21
|
|
28
|
+
Requires-Dist: bcrypt==4.0.1
|
|
29
|
+
Requires-Dist: jinja2>=3.1.3
|
|
30
|
+
Requires-Dist: pytest>=9.0.2
|
|
31
|
+
Requires-Dist: pytest>=8.0.0 ; extra == 'test'
|
|
32
|
+
Requires-Dist: pytest-mock>=3.12.0 ; extra == 'test'
|
|
33
|
+
Requires-Python: >=3.13
|
|
34
|
+
Project-URL: Homepage, https://github.com/xenfra-cloud/xenfra
|
|
35
|
+
Project-URL: Issues, https://github.com/xenfra-cloud/xenfra/issues
|
|
36
|
+
Provides-Extra: test
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# 🧘 Xenfra: Infrastructure in Zen Mode
|
|
40
|
+
|
|
41
|
+
**Xenfra** is a modular infrastructure engine for Python developers that automates the deployment of applications to DigitalOcean. It is designed as a library first, with a beautiful and interactive CLI as the default frontend.
|
|
42
|
+
|
|
43
|
+
It handles the complexity of server provisioning, context-aware configuration, Dockerization, and automatic HTTPS, allowing you to focus on your code.
|
|
44
|
+
|
|
45
|
+
## ✨ Core Philosophy
|
|
46
|
+
|
|
47
|
+
* **Engine as the Brain**: `xenfra.engine` is the core library. It owns the DigitalOcean API, the SSH "Auto-Heal" retry loops, and the Dockerizer services. It is stateful, robust, and can be imported into any Python project.
|
|
48
|
+
* **Clients as the Face**: Frontends like the default CLI (`xenfra.cli`) are thin, stateless clients responsible only for user interaction.
|
|
49
|
+
* **Zen Mode**: If a server setup fails due to common issues like a locked package manager, the Engine automatically fixes it without exposing raw errors to the user.
|
|
50
|
+
|
|
51
|
+
## 🚀 Quickstart
|
|
52
|
+
|
|
53
|
+
Using the Xenfra CLI involves a simple workflow: **Configure**, **Initialize**, and then **Deploy & Manage**.
|
|
54
|
+
|
|
55
|
+
### 1. Configure
|
|
56
|
+
|
|
57
|
+
Xenfra needs your DigitalOcean API token to manage infrastructure on your behalf. Export it as an environment variable:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
export DIGITAL_OCEAN_TOKEN="dop_v1_your_secret_token_here"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. Initialize
|
|
64
|
+
|
|
65
|
+
Navigate to your project's root directory and run the `init` command. This command scans your project, asks a few questions, and creates a `xenfra.yaml` configuration file.
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
xenfra init
|
|
69
|
+
```
|
|
70
|
+
You should review the generated `xenfra.yaml` and commit it to your repository.
|
|
71
|
+
|
|
72
|
+
### 3. Deploy & Manage
|
|
73
|
+
|
|
74
|
+
Once your project is initialized, you can use the following commands to manage your application:
|
|
75
|
+
|
|
76
|
+
* **`xenfra deploy`**: Deploys your application based on the settings in `xenfra.yaml`.
|
|
77
|
+
* **`xenfra list`**: Instantly lists all your deployed projects from a local cache.
|
|
78
|
+
* Use `xenfra list --refresh` to force a sync with your cloud provider.
|
|
79
|
+
* **`xenfra logs`**: Streams real-time logs from a selected project.
|
|
80
|
+
* **`xenfra destroy`**: Decommissions and deletes a deployed project.
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
## 📦 Supported Frameworks & Features
|
|
84
|
+
|
|
85
|
+
* **Smart Context Detection**: Automatically detects your package manager (`uv` or `pip`).
|
|
86
|
+
* **Automatic Dockerization**: If a web framework is detected (`FastAPI`, `Flask`), Xenfra will:
|
|
87
|
+
* Generate a `Dockerfile`, `docker-compose.yml`, and `Caddyfile`.
|
|
88
|
+
* Deploy your application as a container.
|
|
89
|
+
* Configure **Caddy** as a reverse proxy with **automatic HTTPS**.
|
|
90
|
+
|
|
91
|
+
## 🤝 Contributing
|
|
92
|
+
|
|
93
|
+
Contributions are welcome! Please check our `CONTRIBUTING.md` for more details.
|
|
94
|
+
|
|
95
|
+
## 📄 Created by DevHusnainAi
|
xenfra-0.1.8/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# 🧘 Xenfra: Infrastructure in Zen Mode
|
|
2
|
+
|
|
3
|
+
**Xenfra** is a modular infrastructure engine for Python developers that automates the deployment of applications to DigitalOcean. It is designed as a library first, with a beautiful and interactive CLI as the default frontend.
|
|
4
|
+
|
|
5
|
+
It handles the complexity of server provisioning, context-aware configuration, Dockerization, and automatic HTTPS, allowing you to focus on your code.
|
|
6
|
+
|
|
7
|
+
## ✨ Core Philosophy
|
|
8
|
+
|
|
9
|
+
* **Engine as the Brain**: `xenfra.engine` is the core library. It owns the DigitalOcean API, the SSH "Auto-Heal" retry loops, and the Dockerizer services. It is stateful, robust, and can be imported into any Python project.
|
|
10
|
+
* **Clients as the Face**: Frontends like the default CLI (`xenfra.cli`) are thin, stateless clients responsible only for user interaction.
|
|
11
|
+
* **Zen Mode**: If a server setup fails due to common issues like a locked package manager, the Engine automatically fixes it without exposing raw errors to the user.
|
|
12
|
+
|
|
13
|
+
## 🚀 Quickstart
|
|
14
|
+
|
|
15
|
+
Using the Xenfra CLI involves a simple workflow: **Configure**, **Initialize**, and then **Deploy & Manage**.
|
|
16
|
+
|
|
17
|
+
### 1. Configure
|
|
18
|
+
|
|
19
|
+
Xenfra needs your DigitalOcean API token to manage infrastructure on your behalf. Export it as an environment variable:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
export DIGITAL_OCEAN_TOKEN="dop_v1_your_secret_token_here"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Initialize
|
|
26
|
+
|
|
27
|
+
Navigate to your project's root directory and run the `init` command. This command scans your project, asks a few questions, and creates a `xenfra.yaml` configuration file.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
xenfra init
|
|
31
|
+
```
|
|
32
|
+
You should review the generated `xenfra.yaml` and commit it to your repository.
|
|
33
|
+
|
|
34
|
+
### 3. Deploy & Manage
|
|
35
|
+
|
|
36
|
+
Once your project is initialized, you can use the following commands to manage your application:
|
|
37
|
+
|
|
38
|
+
* **`xenfra deploy`**: Deploys your application based on the settings in `xenfra.yaml`.
|
|
39
|
+
* **`xenfra list`**: Instantly lists all your deployed projects from a local cache.
|
|
40
|
+
* Use `xenfra list --refresh` to force a sync with your cloud provider.
|
|
41
|
+
* **`xenfra logs`**: Streams real-time logs from a selected project.
|
|
42
|
+
* **`xenfra destroy`**: Decommissions and deletes a deployed project.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## 📦 Supported Frameworks & Features
|
|
46
|
+
|
|
47
|
+
* **Smart Context Detection**: Automatically detects your package manager (`uv` or `pip`).
|
|
48
|
+
* **Automatic Dockerization**: If a web framework is detected (`FastAPI`, `Flask`), Xenfra will:
|
|
49
|
+
* Generate a `Dockerfile`, `docker-compose.yml`, and `Caddyfile`.
|
|
50
|
+
* Deploy your application as a container.
|
|
51
|
+
* Configure **Caddy** as a reverse proxy with **automatic HTTPS**.
|
|
52
|
+
|
|
53
|
+
## 🤝 Contributing
|
|
54
|
+
|
|
55
|
+
Contributions are welcome! Please check our `CONTRIBUTING.md` for more details.
|
|
56
|
+
|
|
57
|
+
## 📄 Created by DevHusnainAi
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "xenfra"
|
|
3
|
+
version = "0.1.8"
|
|
4
|
+
description = "A 'Zen Mode' infrastructure engine for Python developers."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "xenfra-cloud", email = "xenfracloud@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Topic :: Software Development :: Build Tools",
|
|
17
|
+
"Topic :: System :: Systems Administration",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"fabric>=3.2.2",
|
|
21
|
+
"python-digitalocean>=1.17.0",
|
|
22
|
+
"python-dotenv>=1.2.1",
|
|
23
|
+
"rich>=14.2.0",
|
|
24
|
+
"fastapi>=0.110.0",
|
|
25
|
+
"uvicorn[standard]>=0.27.1",
|
|
26
|
+
"click>=8.1.7",
|
|
27
|
+
"sqlmodel>=0.0.16",
|
|
28
|
+
"psycopg2-binary>=2.9.9",
|
|
29
|
+
"python-jose[cryptography]>=3.3.0",
|
|
30
|
+
"passlib>=1.7.4", # No longer need [bcrypt] extra
|
|
31
|
+
"httpx>=0.27.0",
|
|
32
|
+
"PyYAML>=6.0.1", # Explicitly add bcrypt backend
|
|
33
|
+
"python-multipart>=0.0.21",
|
|
34
|
+
"bcrypt==4.0.1",
|
|
35
|
+
"Jinja2>=3.1.3",
|
|
36
|
+
"pytest>=9.0.2",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/xenfra-cloud/xenfra"
|
|
41
|
+
Issues = "https://github.com/xenfra-cloud/xenfra/issues"
|
|
42
|
+
|
|
43
|
+
[project.scripts]
|
|
44
|
+
xenfra = "xenfra.cli.main:main"
|
|
45
|
+
|
|
46
|
+
[project.optional-dependencies]
|
|
47
|
+
test = [
|
|
48
|
+
"pytest>=8.0.0",
|
|
49
|
+
"pytest-mock>=3.12.0",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[build-system]
|
|
53
|
+
requires = ["uv_build>=0.9.18,<0.10.0"]
|
|
54
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# This file makes src/xenfra a Python package.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# src/xenfra/api/auth.py
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Depends, HTTPException, status
|
|
4
|
+
from fastapi.security import OAuth2PasswordRequestForm
|
|
5
|
+
from sqlmodel import Session, select
|
|
6
|
+
|
|
7
|
+
from xenfra.db.session import get_session
|
|
8
|
+
from xenfra.db.models import User, UserCreate, UserRead
|
|
9
|
+
from xenfra.security import get_password_hash, verify_password, create_access_token
|
|
10
|
+
|
|
11
|
+
router = APIRouter()
|
|
12
|
+
|
|
13
|
+
@router.post("/register", response_model=UserRead)
|
|
14
|
+
def register_user(user: UserCreate, session: Session = Depends(get_session)):
|
|
15
|
+
"""
|
|
16
|
+
Create a new user.
|
|
17
|
+
"""
|
|
18
|
+
db_user = session.exec(select(User).where(User.email == user.email)).first()
|
|
19
|
+
if db_user:
|
|
20
|
+
raise HTTPException(
|
|
21
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
22
|
+
detail="Email already registered",
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
hashed_password = get_password_hash(user.password)
|
|
26
|
+
new_user = User(email=user.email, hashed_password=hashed_password, is_active=True)
|
|
27
|
+
|
|
28
|
+
session.add(new_user)
|
|
29
|
+
session.commit()
|
|
30
|
+
session.refresh(new_user)
|
|
31
|
+
|
|
32
|
+
return new_user
|
|
33
|
+
|
|
34
|
+
@router.post("/token")
|
|
35
|
+
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), session: Session = Depends(get_session)):
|
|
36
|
+
"""
|
|
37
|
+
Login user and return a JWT access token.
|
|
38
|
+
"""
|
|
39
|
+
user = session.exec(select(User).where(User.email == form_data.username)).first()
|
|
40
|
+
if not user or not verify_password(form_data.password, user.hashed_password):
|
|
41
|
+
raise HTTPException(
|
|
42
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
43
|
+
detail="Incorrect username or password",
|
|
44
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if not user.is_active:
|
|
48
|
+
raise HTTPException(status_code=400, detail="Inactive user")
|
|
49
|
+
|
|
50
|
+
access_token = create_access_token(data={"sub": user.email})
|
|
51
|
+
return {"access_token": access_token, "token_type": "bearer"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# src/xenfra/api/billing.py
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Depends, HTTPException, status
|
|
4
|
+
from sqlmodel import Session, select
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
|
|
7
|
+
from xenfra.db.session import get_session
|
|
8
|
+
from xenfra.db.models import User, Credential
|
|
9
|
+
from xenfra.dependencies import get_current_active_user # Corrected import
|
|
10
|
+
from xenfra.engine import InfraEngine
|
|
11
|
+
from xenfra.security import decrypt_token
|
|
12
|
+
from xenfra.models import DropletCostRead # Import new model
|
|
13
|
+
|
|
14
|
+
router = APIRouter()
|
|
15
|
+
|
|
16
|
+
class BalanceRead(BaseModel):
|
|
17
|
+
month_to_date_balance: str
|
|
18
|
+
account_balance: str
|
|
19
|
+
month_to_date_usage: str
|
|
20
|
+
generated_at: str
|
|
21
|
+
error: str | None = None
|
|
22
|
+
|
|
23
|
+
@router.get("/balance", response_model=BalanceRead)
|
|
24
|
+
def get_billing_balance(
|
|
25
|
+
session: Session = Depends(get_session),
|
|
26
|
+
user: User = Depends(get_current_active_user)
|
|
27
|
+
):
|
|
28
|
+
"""
|
|
29
|
+
Get the current DigitalOcean account balance and month-to-date usage.
|
|
30
|
+
"""
|
|
31
|
+
# Find the user's DigitalOcean credential
|
|
32
|
+
do_credential = session.exec(
|
|
33
|
+
select(Credential).where(Credential.user_id == user.id, Credential.service == "digitalocean")
|
|
34
|
+
).first()
|
|
35
|
+
|
|
36
|
+
if not do_credential:
|
|
37
|
+
raise HTTPException(
|
|
38
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
39
|
+
detail="DigitalOcean credential not found for this user. Please connect your account.",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
do_token = decrypt_token(do_credential.encrypted_token)
|
|
44
|
+
engine = InfraEngine(token=do_token)
|
|
45
|
+
balance_data = engine.get_account_balance()
|
|
46
|
+
return BalanceRead(**balance_data)
|
|
47
|
+
except Exception as e:
|
|
48
|
+
raise HTTPException(
|
|
49
|
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
50
|
+
detail=f"Failed to fetch balance from DigitalOcean: {e}",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
@router.get("/droplets", response_model=list[DropletCostRead])
|
|
54
|
+
def get_billing_droplets(
|
|
55
|
+
session: Session = Depends(get_session),
|
|
56
|
+
user: User = Depends(get_current_active_user)
|
|
57
|
+
):
|
|
58
|
+
"""
|
|
59
|
+
Get a list of Xenfra-managed DigitalOcean droplets with their estimated monthly costs.
|
|
60
|
+
"""
|
|
61
|
+
do_credential = session.exec(
|
|
62
|
+
select(Credential).where(Credential.user_id == user.id, Credential.service == "digitalocean")
|
|
63
|
+
).first()
|
|
64
|
+
|
|
65
|
+
if not do_credential:
|
|
66
|
+
raise HTTPException(
|
|
67
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
68
|
+
detail="DigitalOcean credential not found for this user. Please connect your account.",
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
do_token = decrypt_token(do_credential.encrypted_token)
|
|
73
|
+
engine = InfraEngine(token=do_token)
|
|
74
|
+
droplets_data = engine.get_droplet_cost_estimates()
|
|
75
|
+
return [DropletCostRead(**d) for d in droplets_data]
|
|
76
|
+
except Exception as e:
|
|
77
|
+
raise HTTPException(
|
|
78
|
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
79
|
+
detail=f"Failed to fetch droplet cost estimates from DigitalOcean: {e}",
|
|
80
|
+
)
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# src/xenfra/api/connections.py
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
|
5
|
+
from fastapi.responses import RedirectResponse
|
|
6
|
+
from sqlmodel import Session, select
|
|
7
|
+
import secrets
|
|
8
|
+
|
|
9
|
+
from xenfra.db.session import get_session
|
|
10
|
+
from xenfra.db.models import User, Credential, CredentialCreate
|
|
11
|
+
from xenfra.dependencies import get_current_active_user # Corrected import
|
|
12
|
+
from xenfra.security import encrypt_token
|
|
13
|
+
from xenfra.config import settings
|
|
14
|
+
|
|
15
|
+
router = APIRouter()
|
|
16
|
+
|
|
17
|
+
# --- GitHub OAuth ---
|
|
18
|
+
# GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID") # Moved inside function
|
|
19
|
+
# GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET") # Moved inside function
|
|
20
|
+
# GITHUB_REDIRECT_URI = os.getenv("GITHUB_REDIRECT_URI", "http://localhost:8000/connections/github/callback") # Moved inside function
|
|
21
|
+
|
|
22
|
+
@router.get("/github/login")
|
|
23
|
+
def github_login(request: Request): # Add request: Request
|
|
24
|
+
"""Redirects the user to GitHub for authorization."""
|
|
25
|
+
GITHUB_CLIENT_ID = settings.GITHUB_CLIENT_ID
|
|
26
|
+
GITHUB_REDIRECT_URI = settings.GITHUB_REDIRECT_URI
|
|
27
|
+
|
|
28
|
+
state = secrets.token_urlsafe(32)
|
|
29
|
+
request.session["github_oauth_state"] = state
|
|
30
|
+
|
|
31
|
+
return RedirectResponse(
|
|
32
|
+
f"https://github.com/login/oauth/authorize?client_id={GITHUB_CLIENT_ID}&scope=repo%20user:email&redirect_uri={GITHUB_REDIRECT_URI}&state={state}",
|
|
33
|
+
status_code=302
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
@router.get("/github/callback")
|
|
37
|
+
async def github_callback(code: str, request: Request, session: Session = Depends(get_session), user: User = Depends(get_current_active_user)):
|
|
38
|
+
"""
|
|
39
|
+
Handles the callback from GitHub, exchanges the code for a token,
|
|
40
|
+
and stores the encrypted token.
|
|
41
|
+
"""
|
|
42
|
+
received_state = request.query_params.get("state")
|
|
43
|
+
stored_state = request.session.pop("github_oauth_state", None)
|
|
44
|
+
|
|
45
|
+
if not received_state or not stored_state or received_state != stored_state:
|
|
46
|
+
raise HTTPException(status_code=400, detail="Invalid OAuth state. Possible CSRF attack.")
|
|
47
|
+
|
|
48
|
+
GITHUB_CLIENT_ID = settings.GITHUB_CLIENT_ID
|
|
49
|
+
GITHUB_CLIENT_SECRET = settings.GITHUB_CLIENT_SECRET
|
|
50
|
+
if not GITHUB_CLIENT_SECRET: # Explicit check for client secret
|
|
51
|
+
raise HTTPException(status_code=500, detail="GitHub client secret not configured.")
|
|
52
|
+
# GITHUB_REDIRECT_URI is not needed here
|
|
53
|
+
|
|
54
|
+
async with httpx.AsyncClient() as client:
|
|
55
|
+
token_response = await client.post(
|
|
56
|
+
"https://github.com/login/oauth/access_token",
|
|
57
|
+
json={"client_id": GITHUB_CLIENT_ID, "client_secret": GITHUB_CLIENT_SECRET, "code": code},
|
|
58
|
+
headers={"Accept": "application/json"}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
token_data = await token_response.json() # Await the json() coroutine
|
|
62
|
+
access_token = token_data.get("access_token")
|
|
63
|
+
|
|
64
|
+
if not access_token:
|
|
65
|
+
# GitHub might return an error in token_data, e.g., {'error': 'bad_verification_code'}
|
|
66
|
+
error_detail = token_data.get("error_description", token_data.get("error", "Unknown error during token exchange"))
|
|
67
|
+
raise HTTPException(status_code=400, detail=f"Could not fetch GitHub access token: {error_detail}")
|
|
68
|
+
|
|
69
|
+
# Encrypt and store the token
|
|
70
|
+
encrypted_token = encrypt_token(access_token)
|
|
71
|
+
|
|
72
|
+
# Fetch GitHub App installation ID
|
|
73
|
+
async with httpx.AsyncClient() as client:
|
|
74
|
+
installations_response = await client.get(
|
|
75
|
+
"https://api.github.com/user/installations",
|
|
76
|
+
headers={
|
|
77
|
+
"Authorization": f"token {access_token}",
|
|
78
|
+
"Accept": "application/vnd.github.v3+json"
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
installations_data = await installations_response.json()
|
|
82
|
+
installation_id = None
|
|
83
|
+
if installations_data and "installations" in installations_data and len(installations_data["installations"]) > 0:
|
|
84
|
+
installation_id = installations_data["installations"][0]["id"]
|
|
85
|
+
|
|
86
|
+
new_credential = CredentialCreate(
|
|
87
|
+
service="github",
|
|
88
|
+
encrypted_token=encrypted_token,
|
|
89
|
+
user_id=user.id,
|
|
90
|
+
github_installation_id=installation_id
|
|
91
|
+
)
|
|
92
|
+
db_credential = Credential.model_validate(new_credential)
|
|
93
|
+
session.add(db_credential)
|
|
94
|
+
session.commit()
|
|
95
|
+
|
|
96
|
+
# Redirect user back to the frontend (URL should be configurable)
|
|
97
|
+
return RedirectResponse(url=f"{settings.FRONTEND_OAUTH_REDIRECT_SUCCESS}?success=github", status_code=302)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# --- DigitalOcean OAuth ---
|
|
101
|
+
# DO_CLIENT_ID = os.getenv("DO_CLIENT_ID") # Moved inside function
|
|
102
|
+
# DO_CLIENT_SECRET = os.getenv("DO_CLIENT_SECRET") # Moved inside function
|
|
103
|
+
# DO_REDIRECT_URI = os.getenv("DO_REDIRECT_URI", "http://localhost:8000/connections/digitalocean/callback") # Moved inside function
|
|
104
|
+
|
|
105
|
+
@router.get("/digitalocean/login")
|
|
106
|
+
def digitalocean_login(request: Request): # Add request: Request
|
|
107
|
+
"""Redirects the user to DigitalOcean for authorization."""
|
|
108
|
+
DO_CLIENT_ID = settings.DO_CLIENT_ID
|
|
109
|
+
DO_REDIRECT_URI = settings.DO_REDIRECT_URI
|
|
110
|
+
|
|
111
|
+
state = secrets.token_urlsafe(32)
|
|
112
|
+
request.session["digitalocean_oauth_state"] = state
|
|
113
|
+
|
|
114
|
+
return RedirectResponse(
|
|
115
|
+
f"https://cloud.digitalocean.com/v1/oauth/authorize?client_id={DO_CLIENT_ID}&response_type=code&scope=read%20write&redirect_uri={DO_REDIRECT_URI}&state={state}",
|
|
116
|
+
status_code=302
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
@router.get("/digitalocean/callback")
|
|
120
|
+
async def digitalocean_callback(code: str, request: Request, session: Session = Depends(get_session), user: User = Depends(get_current_active_user)):
|
|
121
|
+
"""
|
|
122
|
+
Handles the callback from DigitalOcean, exchanges the code for a token,
|
|
123
|
+
and stores the encrypted token.
|
|
124
|
+
"""
|
|
125
|
+
received_state = request.query_params.get("state")
|
|
126
|
+
stored_state = request.session.pop("digitalocean_oauth_state", None)
|
|
127
|
+
|
|
128
|
+
if not received_state or not stored_state or received_state != stored_state:
|
|
129
|
+
raise HTTPException(status_code=400, detail="Invalid OAuth state. Possible CSRF attack.")
|
|
130
|
+
|
|
131
|
+
DO_CLIENT_ID = settings.DO_CLIENT_ID
|
|
132
|
+
DO_CLIENT_SECRET = settings.DO_CLIENT_SECRET
|
|
133
|
+
DO_REDIRECT_URI = settings.DO_REDIRECT_URI
|
|
134
|
+
if not DO_CLIENT_SECRET: # Explicit check for client secret
|
|
135
|
+
raise HTTPException(status_code=500, detail="DigitalOcean client secret not configured.")
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async with httpx.AsyncClient() as client:
|
|
139
|
+
token_response = await client.post(
|
|
140
|
+
"https://cloud.digitalocean.com/v1/oauth/token",
|
|
141
|
+
params={
|
|
142
|
+
"grant_type": "authorization_code",
|
|
143
|
+
"client_id": DO_CLIENT_ID,
|
|
144
|
+
"client_secret": DO_CLIENT_SECRET,
|
|
145
|
+
"code": code,
|
|
146
|
+
"redirect_uri": DO_REDIRECT_URI
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
token_data = await token_response.json() # Await the json() coroutine
|
|
151
|
+
access_token = token_data.get("access_token")
|
|
152
|
+
|
|
153
|
+
if not access_token:
|
|
154
|
+
error_detail = token_data.get("error_description", token_data.get("error", "Unknown error during token exchange"))
|
|
155
|
+
raise HTTPException(status_code=400, detail=f"Could not fetch DigitalOcean access token: {error_detail}")
|
|
156
|
+
|
|
157
|
+
encrypted_token = encrypt_token(access_token)
|
|
158
|
+
new_credential = CredentialCreate(service="digitalocean", encrypted_token=encrypted_token, user_id=user.id)
|
|
159
|
+
db_credential = Credential.model_validate(new_credential)
|
|
160
|
+
session.add(db_credential)
|
|
161
|
+
session.commit()
|
|
162
|
+
|
|
163
|
+
return RedirectResponse(url=f"{settings.FRONTEND_OAUTH_REDIRECT_SUCCESS}?success=digitalocean", status_code=302)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from fastapi import FastAPI, Depends, HTTPException, status, Request, BackgroundTasks
|
|
2
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
3
|
+
from starlette.middleware.sessions import SessionMiddleware # Import SessionMiddleware
|
|
4
|
+
from fastapi.security import OAuth2PasswordBearer # This will be removed, as oauth2_scheme is in dependencies
|
|
5
|
+
from sqlmodel import Session, select
|
|
6
|
+
from jose import JWTError
|
|
7
|
+
from typing import List
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
|
|
10
|
+
import os # Keep os import for now as it is used in other places.
|
|
11
|
+
|
|
12
|
+
from xenfra.engine import InfraEngine
|
|
13
|
+
from xenfra.models import Deployment, ProjectRead # Import ProjectRead
|
|
14
|
+
from xenfra.db.session import create_db_and_tables, get_session
|
|
15
|
+
from xenfra.db.models import User, UserRead, Credential, CredentialRead
|
|
16
|
+
from xenfra.security import decode_token, decrypt_token
|
|
17
|
+
from xenfra.dependencies import get_current_user, get_current_active_user, oauth2_scheme # Import oauth2_scheme from dependencies
|
|
18
|
+
from pydantic import BaseModel
|
|
19
|
+
from xenfra.config import settings
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# --- Lifespan ---
|
|
23
|
+
from contextlib import asynccontextmanager
|
|
24
|
+
|
|
25
|
+
@asynccontextmanager
|
|
26
|
+
async def lifespan(app: FastAPI):
|
|
27
|
+
create_db_and_tables()
|
|
28
|
+
yield
|
|
29
|
+
|
|
30
|
+
# --- App Initialization ---
|
|
31
|
+
app = FastAPI(
|
|
32
|
+
title="Xenfra API",
|
|
33
|
+
description="API for the Xenfra deployment engine.",
|
|
34
|
+
version="0.1.0",
|
|
35
|
+
lifespan=lifespan
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# --- Middleware ---
|
|
39
|
+
app.add_middleware(SessionMiddleware, secret_key=settings.SECRET_KEY) # Use settings.SECRET_KEY here
|
|
40
|
+
app.add_middleware(
|
|
41
|
+
CORSMiddleware,
|
|
42
|
+
allow_origins=["*"], # For dev, allow all. In prod, strict listing.
|
|
43
|
+
allow_credentials=True,
|
|
44
|
+
allow_methods=["*"],
|
|
45
|
+
allow_headers=["*"],
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# --- Router Imports (Moved after app initialization) ---
|
|
49
|
+
from xenfra.api import auth, connections, webhooks, billing
|
|
50
|
+
|
|
51
|
+
# --- Routers ---
|
|
52
|
+
app.include_router(auth.router, prefix="/auth", tags=["Authentication"])
|
|
53
|
+
app.include_router(connections.router, prefix="/connections", tags=["Connections"])
|
|
54
|
+
app.include_router(webhooks.router, prefix="/webhooks", tags=["Webhooks"])
|
|
55
|
+
app.include_router(billing.router, prefix="/billing", tags=["Billing"])
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# --- Models for Frontend Requests ---
|
|
59
|
+
class DeploymentCreateRequest(BaseModel):
|
|
60
|
+
name: str
|
|
61
|
+
region: str
|
|
62
|
+
size: str
|
|
63
|
+
image: str
|
|
64
|
+
email: str
|
|
65
|
+
domain: str | None = None
|
|
66
|
+
repo_url: str | None = None # For future Git deployments via web UI
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# --- Endpoints ---
|
|
70
|
+
@app.get("/")
|
|
71
|
+
def read_root():
|
|
72
|
+
return {"message": "Welcome to the Xenfra API"}
|
|
73
|
+
|
|
74
|
+
@app.post("/deployments/new")
|
|
75
|
+
def create_new_deployment(
|
|
76
|
+
request: DeploymentCreateRequest,
|
|
77
|
+
background_tasks: BackgroundTasks,
|
|
78
|
+
session: Session = Depends(get_session),
|
|
79
|
+
user: User = Depends(get_current_active_user)
|
|
80
|
+
):
|
|
81
|
+
"""
|
|
82
|
+
Triggers a new deployment based on provided configuration.
|
|
83
|
+
"""
|
|
84
|
+
do_credential = session.exec(
|
|
85
|
+
select(Credential).where(Credential.user_id == user.id, Credential.service == "digitalocean")
|
|
86
|
+
).first()
|
|
87
|
+
|
|
88
|
+
if not do_credential:
|
|
89
|
+
raise HTTPException(
|
|
90
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
91
|
+
detail="DigitalOcean credential not found for this user. Please connect your account.",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
do_token = decrypt_token(do_credential.encrypted_token)
|
|
96
|
+
engine = InfraEngine(token=do_token)
|
|
97
|
+
|
|
98
|
+
# Offload the deployment to a background task
|
|
99
|
+
background_tasks.add_task(
|
|
100
|
+
engine.deploy_server,
|
|
101
|
+
name=request.name,
|
|
102
|
+
region=request.region,
|
|
103
|
+
size=request.size,
|
|
104
|
+
image=request.image,
|
|
105
|
+
email=request.email,
|
|
106
|
+
domain=request.domain,
|
|
107
|
+
repo_url=request.repo_url
|
|
108
|
+
)
|
|
109
|
+
return {"status": "success", "message": "Deployment initiated in background."}
|
|
110
|
+
except Exception as e:
|
|
111
|
+
raise HTTPException(
|
|
112
|
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
113
|
+
detail=f"Deployment failed: {e}",
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
@app.get("/users/me", response_model=UserRead)
|
|
117
|
+
def read_users_me(current_user: User = Depends(get_current_active_user)):
|
|
118
|
+
"""
|
|
119
|
+
Fetch the current logged in user.
|
|
120
|
+
"""
|
|
121
|
+
return current_user
|
|
122
|
+
|
|
123
|
+
@app.get("/users/me/connections", response_model=List[CredentialRead])
|
|
124
|
+
def read_user_connections(current_user: User = Depends(get_current_active_user), session: Session = Depends(get_session)):
|
|
125
|
+
"""
|
|
126
|
+
Fetch the current logged in user's connected credentials (GitHub, DigitalOcean).
|
|
127
|
+
"""
|
|
128
|
+
credentials = session.exec(select(Credential).where(Credential.user_id == current_user.id)).all()
|
|
129
|
+
return credentials
|
|
130
|
+
|
|
131
|
+
@app.get("/projects", response_model=List[ProjectRead])
|
|
132
|
+
def list_projects(
|
|
133
|
+
session: Session = Depends(get_session),
|
|
134
|
+
user: User = Depends(get_current_active_user)
|
|
135
|
+
):
|
|
136
|
+
"""
|
|
137
|
+
Lists Xenfra-managed Droplets as projects.
|
|
138
|
+
"""
|
|
139
|
+
do_credential = session.exec(
|
|
140
|
+
select(Credential).where(Credential.user_id == user.id, Credential.service == "digitalocean")
|
|
141
|
+
).first()
|
|
142
|
+
|
|
143
|
+
if not do_credential:
|
|
144
|
+
raise HTTPException(
|
|
145
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
146
|
+
detail="DigitalOcean credential not found for this user. Please connect your account.",
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
try:
|
|
150
|
+
do_token = decrypt_token(do_credential.encrypted_token)
|
|
151
|
+
engine = InfraEngine(token=do_token)
|
|
152
|
+
|
|
153
|
+
droplets = engine.list_servers()
|
|
154
|
+
projects = []
|
|
155
|
+
for droplet in droplets:
|
|
156
|
+
# For V0, a "project" is simply a Xenfra-managed Droplet
|
|
157
|
+
# We filter by name convention 'xenfra-'
|
|
158
|
+
if droplet.name.startswith("xenfra-"):
|
|
159
|
+
estimated_monthly_cost = droplet.size['price_monthly'] if droplet.size else None
|
|
160
|
+
projects.append(ProjectRead(
|
|
161
|
+
id=droplet.id,
|
|
162
|
+
name=droplet.name,
|
|
163
|
+
ip_address=droplet.ip_address,
|
|
164
|
+
status=droplet.status,
|
|
165
|
+
region=droplet.region['slug'],
|
|
166
|
+
size_slug=droplet.size['slug'],
|
|
167
|
+
estimated_monthly_cost=estimated_monthly_cost,
|
|
168
|
+
created_at=datetime.fromisoformat(droplet.created_at.replace('Z', '+00:00')) # Ensure datetime is timezone aware
|
|
169
|
+
))
|
|
170
|
+
return projects
|
|
171
|
+
except Exception as e:
|
|
172
|
+
raise HTTPException(
|
|
173
|
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
174
|
+
detail=f"Failed to list projects from DigitalOcean: {e}",
|
|
175
|
+
)
|