2020console = Console ()
2121
2222
23- def check_docker () :
23+ def check_docker (client : docker . DockerClient ) -> bool :
2424 """Check if Docker daemon is running using the Docker Python API.
2525
26+ Args:
27+ client: Docker client instance
28+
2629 Returns:
2730 bool: True if Docker daemon is available and running, False otherwise
2831 """
2932 try :
30- # Initialize Docker client from environment
31- client = docker .from_env ()
3233 # Ping the Docker daemon to check if it's responsive
3334 client .ping ()
3435 return True
@@ -42,14 +43,16 @@ def check_docker():
4243 return False
4344
4445
45- def check_pgvector_container () :
46+ def check_pgvector_container (client : docker . DockerClient ) -> bool :
4647 """Check if pgvector container is running.
4748
49+ Args:
50+ client: Docker client instance
51+
4852 Returns:
4953 bool: True if pgvector container is running, False otherwise
5054 """
5155 try :
52- client = docker .from_env ()
5356 container = client .containers .get ("deadend_pg" )
5457 return container .status == "running"
5558 except NotFound :
@@ -59,15 +62,16 @@ def check_pgvector_container():
5962 return False
6063
6164
62- def setup_pgvector_database () :
65+ def setup_pgvector_database (client : docker . DockerClient ) -> bool :
6366 """Setup pgvector database using Docker API.
6467
68+ Args:
69+ client: Docker client instance
70+
6571 Returns:
6672 bool: True if setup successful, False otherwise
6773 """
6874 try :
69- client = docker .from_env ()
70-
7175 # Check if container already exists
7276 try :
7377 existing_container = client .containers .get ("deadend_pg" )
@@ -83,18 +87,18 @@ def setup_pgvector_database():
8387 return True
8488 except NotFound :
8589 pass # Container doesn't exist, create new one
86-
90+
8791 # Create postgres_data directory in cache if it doesn't exist
8892 cache_dir = Path .home () / ".cache" / "deadend"
8993 postgres_data_dir = cache_dir / "postgres_data"
9094 postgres_data_dir .mkdir (parents = True , exist_ok = True )
91-
95+
9296 console .print ("[blue]Setting up pgvector database...[/blue]" )
93-
97+
9498 # Pull the pgvector image
9599 console .print ("Pulling pgvector image..." )
96100 client .images .pull ("pgvector/pgvector:pg17" )
97-
101+
98102 # Create and run the container
99103 container = client .containers .run (
100104 "pgvector/pgvector:pg17" ,
@@ -109,11 +113,11 @@ def setup_pgvector_database():
109113 detach = True ,
110114 remove = False
111115 )
112-
116+
113117 # Wait for container to be ready
114118 console .print ("Waiting for database to be ready..." )
115119 time .sleep (10 )
116-
120+
117121 # Check if container is running
118122 container .reload ()
119123 if container .status == "running" :
@@ -132,14 +136,38 @@ def setup_pgvector_database():
132136 return False
133137
134138
135- def stop_pgvector_container ():
139+ def pull_sandboxed_kali_image (client : docker .DockerClient ) -> bool :
140+ """Pull the sandboxed Kali image.
141+
142+ Args:
143+ client: Docker client instance
144+
145+ Returns:
146+ bool: True if pull successful, False otherwise
147+ """
148+ try :
149+ console .print ("[blue]Pulling sandboxed Kali image...[/blue]" )
150+ client .images .pull ("xoxruns/sandboxed_kali" )
151+ console .print ("[green]Sandboxed Kali image pulled successfully.[/green]" )
152+ return True
153+ except DockerException as e :
154+ console .print (f"[red]Error pulling sandboxed Kali image: { e } [/red]" )
155+ return False
156+ except (OSError , ConnectionError ) as e :
157+ console .print (f"[red]Connection error pulling sandboxed Kali image: { e } [/red]" )
158+ return False
159+
160+
161+ def stop_pgvector_container (client : docker .DockerClient ) -> bool :
136162 """Stop the pgvector container.
137163
164+ Args:
165+ client: Docker client instance
166+
138167 Returns:
139168 bool: True if stopped successfully, False otherwise
140169 """
141170 try :
142- client = docker .from_env ()
143171 container = client .containers .get ("deadend_pg" )
144172 if container .status == "running" :
145173 console .print ("[blue]Stopping pgvector database...[/blue]" )
@@ -168,22 +196,37 @@ def init_cli_config():
168196 Returns:
169197 Path: The path to the created configuration file
170198 """
199+ # Create a single Docker client instance for all operations
200+ try :
201+ docker_client = docker .from_env ()
202+ except DockerException as e :
203+ console .print (f"[red]Failed to initialize Docker client: { e } [/red]" )
204+ console .print ("Please install Docker from: https://docs.docker.com/get-docker/" )
205+ console .print ("Make sure Docker daemon is running." )
206+ raise typer .Exit (1 )
207+
171208 # Check Docker availability first - exit if not available
172- if not check_docker ():
209+ if not check_docker (docker_client ):
173210 console .print ("\n [red]Docker is required for this application to function properly.[/red]" )
174211 console .print ("Please install and start Docker, then run this command again." )
175212 raise typer .Exit (1 )
176213
177214 # Check and setup pgvector database
178- if not check_pgvector_container ():
215+ if not check_pgvector_container (docker_client ):
179216 console .print ("\n [blue]pgvector database not found. Setting up...[/blue]" )
180- if not setup_pgvector_database ():
217+ if not setup_pgvector_database (docker_client ):
181218 console .print ("\n [red]Failed to setup pgvector database.[/red]" )
182219 console .print ("Please check Docker logs and try again." )
183220 raise typer .Exit (1 )
184221 else :
185222 console .print ("[green]pgvector database is already running.[/green]" )
186223
224+ # Pull sandboxed Kali image
225+ console .print ("\n [blue]Setting up sandboxed Kali image...[/blue]" )
226+ if not pull_sandboxed_kali_image (docker_client ):
227+ console .print ("\n [yellow]Warning: Failed to pull sandboxed Kali image.[/yellow]" )
228+ console .print ("Some features may not work properly. You can try again later." )
229+
187230 cache_dir = Path .home () / ".cache" / "deadend"
188231 cache_dir .mkdir (parents = True , exist_ok = True )
189232 config_file = cache_dir / "config.toml"
0 commit comments