xenfra-sdk 0.2.2__py3-none-any.whl โ†’ 0.2.4__py3-none-any.whl

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_sdk/__init__.py CHANGED
@@ -1,21 +1,61 @@
1
- # This file makes src/xenfra_sdk a Python package.
2
-
3
- from .client import XenfraClient
4
- from .exceptions import AuthenticationError, XenfraAPIError, XenfraError
5
- from .models import (
6
- CodebaseAnalysisResponse,
7
- DiagnosisResponse,
8
- PatchObject,
9
- ProjectRead,
10
- )
11
-
12
- __all__ = [
13
- "XenfraClient",
14
- "XenfraError",
15
- "AuthenticationError",
16
- "XenfraAPIError",
17
- "DiagnosisResponse",
18
- "CodebaseAnalysisResponse",
19
- "PatchObject",
20
- "ProjectRead",
21
- ]
1
+ # This file makes src/xenfra_sdk a Python package.
2
+
3
+ from .client import XenfraClient
4
+ from .exceptions import AuthenticationError, XenfraAPIError, XenfraError
5
+ from .models import (
6
+ CodebaseAnalysisResponse,
7
+ DiagnosisResponse,
8
+ PatchObject,
9
+ ProjectRead,
10
+ )
11
+
12
+ # Microservices support
13
+ from .manifest import (
14
+ ServiceDefinition,
15
+ load_services_from_xenfra_yaml,
16
+ is_microservices_project,
17
+ get_deployment_mode,
18
+ add_services_to_xenfra_yaml,
19
+ create_services_from_detected,
20
+ )
21
+ from .detection import (
22
+ auto_detect_services,
23
+ detect_docker_compose_services,
24
+ detect_pyproject_services,
25
+ )
26
+ from .orchestrator import (
27
+ ServiceOrchestrator,
28
+ get_orchestrator_for_project,
29
+ )
30
+
31
+ # Security
32
+ from .security_scanner import (
33
+ scan_directory,
34
+ scan_file_list,
35
+ ScanResult,
36
+ SecurityIssue,
37
+ Severity,
38
+ )
39
+
40
+ __all__ = [
41
+ "XenfraClient",
42
+ "XenfraError",
43
+ "AuthenticationError",
44
+ "XenfraAPIError",
45
+ "DiagnosisResponse",
46
+ "CodebaseAnalysisResponse",
47
+ "PatchObject",
48
+ "ProjectRead",
49
+ # Microservices
50
+ "ServiceDefinition",
51
+ "load_services_from_xenfra_yaml",
52
+ "is_microservices_project",
53
+ "get_deployment_mode",
54
+ "add_services_to_xenfra_yaml",
55
+ "create_services_from_detected",
56
+ "auto_detect_services",
57
+ "detect_docker_compose_services",
58
+ "detect_pyproject_services",
59
+ "ServiceOrchestrator",
60
+ "get_orchestrator_for_project",
61
+ ]
xenfra_sdk/cli/main.py CHANGED
@@ -1,226 +1,226 @@
1
- import click
2
- import yaml
3
- from rich.console import Console
4
- from rich.table import Table
5
- from xenfra_sdk import dockerizer
6
- from xenfra_sdk.db.session import create_db_and_tables
7
- from xenfra_sdk.engine import DeploymentError, InfraEngine
8
-
9
- console = Console()
10
-
11
-
12
- @click.group()
13
- @click.pass_context
14
- def main(ctx):
15
- """
16
- Xenfra CLI: A 'Zen Mode' infrastructure engine for Python developers.
17
- """
18
- try:
19
- create_db_and_tables()
20
- ctx.obj = {"engine": InfraEngine()}
21
- user_info = ctx.obj["engine"].get_user_info()
22
- console.print(
23
- f"[bold underline]Xenfra CLI[/bold underline] - Logged in as [green]{user_info.email}[/green]"
24
- )
25
- except Exception as e:
26
- console.print(f"[bold red]CRITICAL ERROR:[/bold red] Failed to initialize engine: {e}")
27
- exit(1)
28
-
29
-
30
- @main.command()
31
- @click.pass_context
32
- def init(ctx):
33
- """Initializes a project by creating a xenfra.yaml configuration file."""
34
- console.print("\n[bold blue]๐Ÿ”Ž INITIALIZING PROJECT[/bold blue]")
35
-
36
- framework, _, _ = dockerizer.detect_framework()
37
- if not framework:
38
- console.print("[yellow] Warning: No recognizable web framework detected.[/yellow]")
39
-
40
- console.print(f" - Detected [cyan]{framework or 'unknown'}[/cyan] project.")
41
-
42
- use_db = click.confirm(
43
- "\n Would you like to add a PostgreSQL database to your deployment?", default=False
44
- )
45
-
46
- config = {
47
- "name": "xenfra-app",
48
- "digitalocean": {"region": "nyc3", "size": "s-1vcpu-1gb", "image": "ubuntu-22-04-x64"},
49
- "app": {"framework": framework},
50
- }
51
-
52
- if use_db:
53
- config["database"] = {
54
- "type": "postgres",
55
- "user": "db_user",
56
- "password": "db_password", # In a real scenario, this should be handled more securely
57
- "name": "app_db",
58
- }
59
- console.print(" - Added [bold green]PostgreSQL[/bold green] to the configuration.")
60
-
61
- with open("xenfra.yaml", "w") as f:
62
- yaml.dump(config, f, default_flow_style=False, sort_keys=False)
63
-
64
- console.print("\n[bold green]โœ… SUCCESS![/bold green]")
65
- console.print(" - Created [cyan]xenfra.yaml[/cyan].")
66
- console.print("\n Next step: Review the configuration and run 'xenfra deploy'!")
67
-
68
-
69
- @main.command()
70
- @click.pass_context
71
- def deploy(ctx):
72
- """Deploys the project based on the xenfra.yaml configuration."""
73
- console.print("\n[bold green]๐Ÿš€ INITIATING DEPLOYMENT FROM CONFIGURATION[/bold green]")
74
-
75
- try:
76
- with open("xenfra.yaml", "r") as f:
77
- config = yaml.safe_load(f)
78
- except FileNotFoundError:
79
- raise click.ClickException(
80
- "No 'xenfra.yaml' found. Run 'xenfra init' to create a configuration file."
81
- )
82
-
83
- engine = ctx.obj["engine"]
84
-
85
- # Extract config values
86
- name = config.get("name", "xenfra-app")
87
- do_config = config.get("digitalocean", {})
88
- region = do_config.get("region", "nyc3")
89
- size = do_config.get("size", "s-1vcpu-1gb")
90
- image = do_config.get("image", "ubuntu-22-04-x64")
91
-
92
- # Build context for templates
93
- template_context = {
94
- "database": config.get("database", {}).get("type"),
95
- "db_user": config.get("database", {}).get("user"),
96
- "db_password": config.get("database", {}).get("password"),
97
- "db_name": config.get("database", {}).get("name"),
98
- "email": ctx.obj["engine"].get_user_info().email,
99
- }
100
-
101
- console.print(f" - App Name: [cyan]{name}[/cyan]")
102
- console.print(f" - Region: [cyan]{region}[/cyan], Size: [cyan]{size}[/cyan]")
103
- if template_context.get("database"):
104
- console.print(f" - Including Database: [cyan]{template_context['database']}[/cyan]")
105
-
106
- if not click.confirm(f"\n Ready to deploy '{name}' from 'xenfra.yaml'?"):
107
- return
108
-
109
- with console.status("[bold green]Deployment in progress...[/bold green]"):
110
- result = engine.deploy_server(
111
- name=name, region=region, size=size, image=image, logger=console.log, **template_context
112
- )
113
-
114
- console.print("\n[bold green]โœ… DEPLOYMENT COMPLETE![/bold green]")
115
- console.print(result)
116
-
117
-
118
- @main.command(name="list")
119
- @click.option("--refresh", is_flag=True, help="Sync with the cloud provider before listing.")
120
- @click.pass_context
121
- def list_projects(ctx, refresh):
122
- """Lists all active Xenfra projects from the local database."""
123
- engine = ctx.obj["engine"]
124
-
125
- if refresh:
126
- console.print("\n[bold]๐Ÿ“ก SYNCING WITH CLOUD PROVIDER...[/bold]")
127
- with console.status("Calling DigitalOcean API and reconciling state..."):
128
- projects = engine.sync_with_provider()
129
- else:
130
- console.print("\n[bold]โšก๏ธ LISTING PROJECTS FROM LOCAL DATABASE[/bold]")
131
- projects = engine.list_projects_from_db()
132
-
133
- if not projects:
134
- console.print(
135
- "[yellow] No active projects found. Run 'xenfra deploy' to create one.[/yellow]"
136
- )
137
- else:
138
- table = Table(show_header=True, header_style="bold magenta")
139
- table.add_column("Droplet ID", style="dim", width=12)
140
- table.add_column("Name", style="cyan")
141
- table.add_column("IP Address", style="green")
142
- table.add_column("Status")
143
- table.add_column("Region")
144
- table.add_column("Size")
145
- for p in projects:
146
- table.add_row(str(p.droplet_id), p.name, p.ip_address, p.status, p.region, p.size)
147
- console.print(table)
148
-
149
-
150
- @main.command(name="logs")
151
- @click.pass_context
152
- def logs(ctx):
153
- """Streams real-time logs from a deployed project."""
154
- engine = ctx.obj["engine"]
155
-
156
- console.print("\n[bold yellow]๐Ÿ“ก SELECT A PROJECT TO STREAM LOGS[/bold yellow]")
157
- projects = engine.list_projects_from_db()
158
-
159
- if not projects:
160
- console.print("[yellow] No active projects to stream logs from.[/yellow]")
161
- return
162
-
163
- project_map = {str(i + 1): p for i, p in enumerate(projects)}
164
- for k, p in project_map.items():
165
- console.print(f" [{k}] {p.name} ({p.ip_address})")
166
-
167
- choice_key = click.prompt(
168
- "\n Select Project (0 to cancel)",
169
- type=click.Choice(["0"] + list(project_map.keys())),
170
- show_choices=False,
171
- )
172
- if choice_key == "0":
173
- return
174
-
175
- target = project_map[choice_key]
176
-
177
- try:
178
- console.print(
179
- f"\n[bold green]-- Attaching to logs for {target.name} (Press Ctrl+C to stop) --[/bold green]"
180
- )
181
- engine.stream_logs(target.droplet_id)
182
- except DeploymentError as e:
183
- console.print(f"[bold red]ERROR:[/bold red] {e.message}")
184
- except KeyboardInterrupt:
185
- console.print("\n[bold yellow]-- Log streaming stopped by user. --[/bold yellow]")
186
- except Exception as e:
187
- console.print(f"[bold red]An unexpected error occurred:[/bold red] {e}")
188
-
189
-
190
- @main.command()
191
- @click.pass_context
192
- def destroy(ctx):
193
- """Destroys a deployed project."""
194
- engine = ctx.obj["engine"]
195
-
196
- console.print("\n[bold red]๐Ÿงจ SELECT A PROJECT TO DESTROY[/bold red]")
197
- projects = engine.list_projects_from_db()
198
-
199
- if not projects:
200
- console.print("[yellow] No active projects to destroy.[/yellow]")
201
- return
202
-
203
- project_map = {str(i + 1): p for i, p in enumerate(projects)}
204
- for k, p in project_map.items():
205
- console.print(f" [{k}] {p.name} ({p.ip_address})")
206
-
207
- choice_key = click.prompt(
208
- "\n Select Project to DESTROY (0 to cancel)",
209
- type=click.Choice(["0"] + list(project_map.keys())),
210
- show_choices=False,
211
- )
212
- if choice_key == "0":
213
- return
214
-
215
- target = project_map[choice_key]
216
-
217
- if click.confirm(
218
- f" Are you SURE you want to permanently delete [red]{target.name}[/red] (Droplet ID: {target.droplet_id})? This action cannot be undone."
219
- ):
220
- with console.status(f"๐Ÿ’ฅ Destroying {target.name}..."):
221
- engine.destroy_server(target.droplet_id)
222
- console.print(f"[green] Project '{target.name}' has been destroyed.[/green]")
223
-
224
-
225
- if __name__ == "__main__":
226
- main()
1
+ import click
2
+ import yaml
3
+ from rich.console import Console
4
+ from rich.table import Table
5
+ from xenfra_sdk import dockerizer
6
+ from xenfra_sdk.db.session import create_db_and_tables
7
+ from xenfra_sdk.engine import DeploymentError, InfraEngine
8
+
9
+ console = Console()
10
+
11
+
12
+ @click.group()
13
+ @click.pass_context
14
+ def main(ctx):
15
+ """
16
+ Xenfra CLI: A 'Zen Mode' infrastructure engine for Python developers.
17
+ """
18
+ try:
19
+ create_db_and_tables()
20
+ ctx.obj = {"engine": InfraEngine()}
21
+ user_info = ctx.obj["engine"].get_user_info()
22
+ console.print(
23
+ f"[bold underline]Xenfra CLI[/bold underline] - Logged in as [green]{user_info.email}[/green]"
24
+ )
25
+ except Exception as e:
26
+ console.print(f"[bold red]CRITICAL ERROR:[/bold red] Failed to initialize engine: {e}")
27
+ exit(1)
28
+
29
+
30
+ @main.command()
31
+ @click.pass_context
32
+ def init(ctx):
33
+ """Initializes a project by creating a xenfra.yaml configuration file."""
34
+ console.print("\n[bold blue]๐Ÿ”Ž INITIALIZING PROJECT[/bold blue]")
35
+
36
+ framework, _, _ = dockerizer.detect_framework()
37
+ if not framework:
38
+ console.print("[yellow] Warning: No recognizable web framework detected.[/yellow]")
39
+
40
+ console.print(f" - Detected [cyan]{framework or 'unknown'}[/cyan] project.")
41
+
42
+ use_db = click.confirm(
43
+ "\n Would you like to add a PostgreSQL database to your deployment?", default=False
44
+ )
45
+
46
+ config = {
47
+ "name": "xenfra-app",
48
+ "digitalocean": {"region": "nyc3", "size": "s-1vcpu-1gb", "image": "ubuntu-22-04-x64"},
49
+ "app": {"framework": framework},
50
+ }
51
+
52
+ if use_db:
53
+ config["database"] = {
54
+ "type": "postgres",
55
+ "user": "db_user",
56
+ "password": "db_password", # In a real scenario, this should be handled more securely
57
+ "name": "app_db",
58
+ }
59
+ console.print(" - Added [bold green]PostgreSQL[/bold green] to the configuration.")
60
+
61
+ with open("xenfra.yaml", "w") as f:
62
+ yaml.dump(config, f, default_flow_style=False, sort_keys=False)
63
+
64
+ console.print("\n[bold green]โœ… SUCCESS![/bold green]")
65
+ console.print(" - Created [cyan]xenfra.yaml[/cyan].")
66
+ console.print("\n Next step: Review the configuration and run 'xenfra deploy'!")
67
+
68
+
69
+ @main.command()
70
+ @click.pass_context
71
+ def deploy(ctx):
72
+ """Deploys the project based on the xenfra.yaml configuration."""
73
+ console.print("\n[bold green]๐Ÿš€ INITIATING DEPLOYMENT FROM CONFIGURATION[/bold green]")
74
+
75
+ try:
76
+ with open("xenfra.yaml", "r") as f:
77
+ config = yaml.safe_load(f)
78
+ except FileNotFoundError:
79
+ raise click.ClickException(
80
+ "No 'xenfra.yaml' found. Run 'xenfra init' to create a configuration file."
81
+ )
82
+
83
+ engine = ctx.obj["engine"]
84
+
85
+ # Extract config values
86
+ name = config.get("name", "xenfra-app")
87
+ do_config = config.get("digitalocean", {})
88
+ region = do_config.get("region", "nyc3")
89
+ size = do_config.get("size", "s-1vcpu-1gb")
90
+ image = do_config.get("image", "ubuntu-22-04-x64")
91
+
92
+ # Build context for templates
93
+ template_context = {
94
+ "database": config.get("database", {}).get("type"),
95
+ "db_user": config.get("database", {}).get("user"),
96
+ "db_password": config.get("database", {}).get("password"),
97
+ "db_name": config.get("database", {}).get("name"),
98
+ "email": ctx.obj["engine"].get_user_info().email,
99
+ }
100
+
101
+ console.print(f" - App Name: [cyan]{name}[/cyan]")
102
+ console.print(f" - Region: [cyan]{region}[/cyan], Size: [cyan]{size}[/cyan]")
103
+ if template_context.get("database"):
104
+ console.print(f" - Including Database: [cyan]{template_context['database']}[/cyan]")
105
+
106
+ if not click.confirm(f"\n Ready to deploy '{name}' from 'xenfra.yaml'?"):
107
+ return
108
+
109
+ with console.status("[bold green]Deployment in progress...[/bold green]"):
110
+ result = engine.deploy_server(
111
+ name=name, region=region, size=size, image=image, logger=console.log, **template_context
112
+ )
113
+
114
+ console.print("\n[bold green]โœ… DEPLOYMENT COMPLETE![/bold green]")
115
+ console.print(result)
116
+
117
+
118
+ @main.command(name="list")
119
+ @click.option("--refresh", is_flag=True, help="Sync with the cloud provider before listing.")
120
+ @click.pass_context
121
+ def list_projects(ctx, refresh):
122
+ """Lists all active Xenfra projects from the local database."""
123
+ engine = ctx.obj["engine"]
124
+
125
+ if refresh:
126
+ console.print("\n[bold]๐Ÿ“ก SYNCING WITH CLOUD PROVIDER...[/bold]")
127
+ with console.status("Calling DigitalOcean API and reconciling state..."):
128
+ projects = engine.sync_with_provider()
129
+ else:
130
+ console.print("\n[bold]โšก๏ธ LISTING PROJECTS FROM LOCAL DATABASE[/bold]")
131
+ projects = engine.list_projects_from_db()
132
+
133
+ if not projects:
134
+ console.print(
135
+ "[yellow] No active projects found. Run 'xenfra deploy' to create one.[/yellow]"
136
+ )
137
+ else:
138
+ table = Table(show_header=True, header_style="bold magenta")
139
+ table.add_column("Droplet ID", style="dim", width=12)
140
+ table.add_column("Name", style="cyan")
141
+ table.add_column("IP Address", style="green")
142
+ table.add_column("Status")
143
+ table.add_column("Region")
144
+ table.add_column("Size")
145
+ for p in projects:
146
+ table.add_row(str(p.droplet_id), p.name, p.ip_address, p.status, p.region, p.size)
147
+ console.print(table)
148
+
149
+
150
+ @main.command(name="logs")
151
+ @click.pass_context
152
+ def logs(ctx):
153
+ """Streams real-time logs from a deployed project."""
154
+ engine = ctx.obj["engine"]
155
+
156
+ console.print("\n[bold yellow]๐Ÿ“ก SELECT A PROJECT TO STREAM LOGS[/bold yellow]")
157
+ projects = engine.list_projects_from_db()
158
+
159
+ if not projects:
160
+ console.print("[yellow] No active projects to stream logs from.[/yellow]")
161
+ return
162
+
163
+ project_map = {str(i + 1): p for i, p in enumerate(projects)}
164
+ for k, p in project_map.items():
165
+ console.print(f" [{k}] {p.name} ({p.ip_address})")
166
+
167
+ choice_key = click.prompt(
168
+ "\n Select Project (0 to cancel)",
169
+ type=click.Choice(["0"] + list(project_map.keys())),
170
+ show_choices=False,
171
+ )
172
+ if choice_key == "0":
173
+ return
174
+
175
+ target = project_map[choice_key]
176
+
177
+ try:
178
+ console.print(
179
+ f"\n[bold green]-- Attaching to logs for {target.name} (Press Ctrl+C to stop) --[/bold green]"
180
+ )
181
+ engine.stream_logs(target.droplet_id)
182
+ except DeploymentError as e:
183
+ console.print(f"[bold red]ERROR:[/bold red] {e.message}")
184
+ except KeyboardInterrupt:
185
+ console.print("\n[bold yellow]-- Log streaming stopped by user. --[/bold yellow]")
186
+ except Exception as e:
187
+ console.print(f"[bold red]An unexpected error occurred:[/bold red] {e}")
188
+
189
+
190
+ @main.command()
191
+ @click.pass_context
192
+ def destroy(ctx):
193
+ """Destroys a deployed project."""
194
+ engine = ctx.obj["engine"]
195
+
196
+ console.print("\n[bold red]๐Ÿงจ SELECT A PROJECT TO DESTROY[/bold red]")
197
+ projects = engine.list_projects_from_db()
198
+
199
+ if not projects:
200
+ console.print("[yellow] No active projects to destroy.[/yellow]")
201
+ return
202
+
203
+ project_map = {str(i + 1): p for i, p in enumerate(projects)}
204
+ for k, p in project_map.items():
205
+ console.print(f" [{k}] {p.name} ({p.ip_address})")
206
+
207
+ choice_key = click.prompt(
208
+ "\n Select Project to DESTROY (0 to cancel)",
209
+ type=click.Choice(["0"] + list(project_map.keys())),
210
+ show_choices=False,
211
+ )
212
+ if choice_key == "0":
213
+ return
214
+
215
+ target = project_map[choice_key]
216
+
217
+ if click.confirm(
218
+ f" Are you SURE you want to permanently delete [red]{target.name}[/red] (Droplet ID: {target.droplet_id})? This action cannot be undone."
219
+ ):
220
+ with console.status(f"๐Ÿ’ฅ Destroying {target.name}..."):
221
+ engine.destroy_server(target.droplet_id)
222
+ console.print(f"[green] Project '{target.name}' has been destroyed.[/green]")
223
+
224
+
225
+ if __name__ == "__main__":
226
+ main()