mirror of
https://github.com/Monadical-SAS/cubbi.git
synced 2025-12-20 20:29:06 +00:00
feat(cli): support to join external network
This commit is contained in:
@@ -33,6 +33,7 @@ def main(ctx: typer.Context) -> None:
|
||||
project=None,
|
||||
env=[],
|
||||
volume=[],
|
||||
network=[],
|
||||
name=None,
|
||||
no_connect=False,
|
||||
no_mount=False,
|
||||
@@ -113,6 +114,9 @@ def create_session(
|
||||
volume: List[str] = typer.Option(
|
||||
[], "--volume", "-v", help="Mount volumes (LOCAL_PATH:CONTAINER_PATH)"
|
||||
),
|
||||
network: List[str] = typer.Option(
|
||||
[], "--network", "-N", help="Connect to additional Docker networks"
|
||||
),
|
||||
name: Optional[str] = typer.Option(None, "--name", "-n", help="Session name"),
|
||||
no_connect: bool = typer.Option(
|
||||
False, "--no-connect", help="Don't automatically connect to the session"
|
||||
@@ -166,6 +170,15 @@ def create_session(
|
||||
f"[yellow]Warning: Ignoring invalid volume format: {vol}. Use LOCAL_PATH:CONTAINER_PATH.[/yellow]"
|
||||
)
|
||||
|
||||
# Get default networks from user config
|
||||
default_networks = user_config.get("defaults.networks", [])
|
||||
|
||||
# Combine default networks with user-specified networks, removing duplicates
|
||||
all_networks = list(set(default_networks + network))
|
||||
|
||||
if all_networks:
|
||||
console.print(f"Networks: {', '.join(all_networks)}")
|
||||
|
||||
with console.status(f"Creating session with driver '{driver}'..."):
|
||||
session = container_manager.create_session(
|
||||
driver_name=driver,
|
||||
@@ -174,6 +187,7 @@ def create_session(
|
||||
session_name=name,
|
||||
mount_local=not no_mount and user_config.get("defaults.mount_local", True),
|
||||
volumes=volume_mounts,
|
||||
networks=all_networks,
|
||||
)
|
||||
|
||||
if session:
|
||||
@@ -315,6 +329,9 @@ def quick_create(
|
||||
volume: List[str] = typer.Option(
|
||||
[], "--volume", "-v", help="Mount volumes (LOCAL_PATH:CONTAINER_PATH)"
|
||||
),
|
||||
network: List[str] = typer.Option(
|
||||
[], "--network", "-N", help="Connect to additional Docker networks"
|
||||
),
|
||||
name: Optional[str] = typer.Option(None, "--name", "-n", help="Session name"),
|
||||
no_connect: bool = typer.Option(
|
||||
False, "--no-connect", help="Don't automatically connect to the session"
|
||||
@@ -335,6 +352,7 @@ def quick_create(
|
||||
project=project,
|
||||
env=env,
|
||||
volume=volume,
|
||||
network=network,
|
||||
name=name,
|
||||
no_connect=no_connect,
|
||||
no_mount=no_mount,
|
||||
@@ -449,6 +467,11 @@ def driver_info(
|
||||
console.print(f.read())
|
||||
|
||||
|
||||
# Create a network subcommand for config
|
||||
network_app = typer.Typer(help="Manage default networks")
|
||||
config_app.add_typer(network_app, name="network", no_args_is_help=True)
|
||||
|
||||
|
||||
# Configuration commands
|
||||
@config_app.command("list")
|
||||
def list_config() -> None:
|
||||
@@ -544,5 +567,56 @@ def reset_config(
|
||||
console.print("[green]Configuration reset to defaults[/green]")
|
||||
|
||||
|
||||
# Network configuration commands
|
||||
@network_app.command("list")
|
||||
def list_networks() -> None:
|
||||
"""List all default networks"""
|
||||
networks = user_config.get("defaults.networks", [])
|
||||
|
||||
if not networks:
|
||||
console.print("No default networks configured")
|
||||
return
|
||||
|
||||
table = Table(show_header=True, header_style="bold")
|
||||
table.add_column("Network")
|
||||
|
||||
for network in networks:
|
||||
table.add_row(network)
|
||||
|
||||
console.print(table)
|
||||
|
||||
|
||||
@network_app.command("add")
|
||||
def add_network(
|
||||
network: str = typer.Argument(..., help="Network name to add to defaults"),
|
||||
) -> None:
|
||||
"""Add a network to default networks"""
|
||||
networks = user_config.get("defaults.networks", [])
|
||||
|
||||
if network in networks:
|
||||
console.print(f"Network '{network}' is already in defaults")
|
||||
return
|
||||
|
||||
networks.append(network)
|
||||
user_config.set("defaults.networks", networks)
|
||||
console.print(f"[green]Added network '{network}' to defaults[/green]")
|
||||
|
||||
|
||||
@network_app.command("remove")
|
||||
def remove_network(
|
||||
network: str = typer.Argument(..., help="Network name to remove from defaults"),
|
||||
) -> None:
|
||||
"""Remove a network from default networks"""
|
||||
networks = user_config.get("defaults.networks", [])
|
||||
|
||||
if network not in networks:
|
||||
console.print(f"Network '{network}' is not in defaults")
|
||||
return
|
||||
|
||||
networks.remove(network)
|
||||
user_config.set("defaults.networks", networks)
|
||||
console.print(f"[green]Removed network '{network}' from defaults[/green]")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
|
||||
@@ -126,6 +126,7 @@ class ContainerManager:
|
||||
session_name: Optional[str] = None,
|
||||
mount_local: bool = True,
|
||||
volumes: Optional[Dict[str, Dict[str, str]]] = None,
|
||||
networks: Optional[List[str]] = None,
|
||||
) -> Optional[Session]:
|
||||
"""Create a new MC session
|
||||
|
||||
@@ -136,6 +137,7 @@ class ContainerManager:
|
||||
session_name: Optional session name
|
||||
mount_local: Whether to mount the current directory to /app
|
||||
volumes: Optional additional volumes to mount (dict of {host_path: {"bind": container_path, "mode": mode}})
|
||||
networks: Optional list of additional Docker networks to connect to
|
||||
"""
|
||||
try:
|
||||
# Validate driver exists
|
||||
@@ -254,7 +256,12 @@ class ContainerManager:
|
||||
f" - Created direct volume mount: {target_dir} -> {config.source}"
|
||||
)
|
||||
|
||||
# Create container
|
||||
# Default MC network
|
||||
default_network = self.config_manager.config.docker.get(
|
||||
"network", "mc-network"
|
||||
)
|
||||
|
||||
# Create container with default MC network
|
||||
container = self.client.containers.create(
|
||||
image=driver.image,
|
||||
name=session_name,
|
||||
@@ -271,13 +278,32 @@ class ContainerManager:
|
||||
"mc.driver": driver_name,
|
||||
"mc.project": project or "",
|
||||
},
|
||||
network=self.config_manager.config.docker.get("network", "mc-network"),
|
||||
network=default_network,
|
||||
ports={f"{port}/tcp": None for port in driver.ports},
|
||||
)
|
||||
|
||||
# Start container
|
||||
container.start()
|
||||
|
||||
# Connect to additional networks if specified
|
||||
if networks:
|
||||
for network_name in networks:
|
||||
try:
|
||||
# Get or create the network
|
||||
try:
|
||||
network = self.client.networks.get(network_name)
|
||||
except DockerException:
|
||||
print(f"Network '{network_name}' not found, creating it...")
|
||||
network = self.client.networks.create(
|
||||
network_name, driver="bridge"
|
||||
)
|
||||
|
||||
# Connect the container to the network
|
||||
network.connect(container)
|
||||
print(f"Connected to network: {network_name}")
|
||||
except DockerException as e:
|
||||
print(f"Error connecting to network {network_name}: {e}")
|
||||
|
||||
# Get updated port information
|
||||
container.reload()
|
||||
ports = {}
|
||||
|
||||
@@ -62,6 +62,7 @@ class UserConfigManager:
|
||||
"driver": "goose",
|
||||
"connect": True,
|
||||
"mount_local": True,
|
||||
"networks": [], # Default networks to connect to (besides mc-network)
|
||||
},
|
||||
"services": {
|
||||
"langfuse": {},
|
||||
|
||||
Reference in New Issue
Block a user