@@ -68,10 +68,11 @@ def _is_port_in_use(self, port):
68
68
with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
69
69
return s .connect_ex (('127.0.0.1' , port )) == 0
70
70
71
- def spawn_jupyter_hub (self ,jb_build_command = None ):
71
+ def spawn_jupyter_hub (self , jb_build_command = None ):
72
72
"""
73
73
Spawn a JupyterHub instance.
74
74
"""
75
+ output_logs = [] # Collect logs and cprints here
75
76
self .port = self .find_open_port ()
76
77
h = blake2b (digest_size = 20 )
77
78
h .update (os .urandom (20 ))
@@ -96,8 +97,8 @@ def spawn_jupyter_hub(self,jb_build_command=None):
96
97
97
98
if self .rees .dataset_name :
98
99
self .rees .repo2data_download (self .host_data_parent_dir )
99
- mnt_vol = {f'{ os .path .join (self .host_data_parent_dir ,self .rees .dataset_name )} ' : {'bind' : os .path .join (self .container_data_mount_dir ,self .rees .dataset_name ), 'mode' : 'ro' },
100
- self .rees .build_dir : {'bind' : f'{ self .container_build_source_mount_dir } ' , 'mode' : 'rw' }}
100
+ mnt_vol = {f'{ os .path .join (self .host_data_parent_dir , self .rees .dataset_name )} ' : {'bind' : os .path .join (self .container_data_mount_dir , self .rees .dataset_name ), 'mode' : 'ro' },
101
+ self .rees .build_dir : {'bind' : f'{ self .container_build_source_mount_dir } ' , 'mode' : 'rw' }}
101
102
else :
102
103
mnt_vol = {self .rees .build_dir : {'bind' : f'{ self .container_build_source_mount_dir } ' , 'mode' : 'rw' }}
103
104
@@ -107,33 +108,46 @@ def spawn_jupyter_hub(self,jb_build_command=None):
107
108
self .container = self .rees .docker_client .containers .run (
108
109
self .rees .docker_image ,
109
110
ports = {f'{ self .port } /tcp' : self .port },
110
- environment = {"JUPYTER_TOKEN" : f'{ self .jh_token } ' ,"port" : f'{ self .port } ' ,"JUPYTER_BASE_URL" : f'{ self .jh_url } ' },
111
- entrypoint = this_entrypoint ,
111
+ environment = {"JUPYTER_TOKEN" : f'{ self .jh_token } ' , "port" : f'{ self .port } ' , "JUPYTER_BASE_URL" : f'{ self .jh_url } ' },
112
+ entrypoint = this_entrypoint ,
112
113
volumes = mnt_vol ,
113
114
detach = True )
114
115
logging .info (f'Jupyter hub is { self .container .status } ' )
115
- self .cprint (f'[Status]' , 'light_grey' )
116
- self .cprint (f' ├─────── ⏺ running' , 'green' )
117
- self .cprint (f' └─────── Container { self .container .short_id } { self .container .name } ' , 'green' )
118
- self .cprint (f' ℹ Run the following commands in the terminal if you are debugging locally:' , 'yellow' )
119
- self .cprint (f' port=\" { self .port } \" ' , 'cyan' )
120
- self .cprint (f' export JUPYTER_BASE_URL=\" { self .jh_url } \" ' , 'cyan' )
121
- self .cprint (f' export JUPYTER_TOKEN=\" { self .jh_token } \" ' , 'cyan' )
122
- self .cprint (f'[Resources]' , 'light_grey' )
123
- self .cprint (f' ├── MyST repository' , 'magenta' )
124
- self .cprint (f' │ ├───────── ✸ { self .rees .gh_user_repo_name } ' ,'light_blue' )
125
- self .cprint (f' │ ├───────── ⎌ { self .rees .gh_repo_commit_hash } ' ,'light_blue' )
126
- self .cprint (f" │ └───────── ⏲ { self .rees .repo_commit_info ['datetime' ]} : { self .rees .repo_commit_info ['message' ]} " .replace ('\n ' , '' ),'light_blue' )
127
- self .cprint (f' └── Docker container' , 'magenta' )
128
- self .cprint (f' ├───────── ✸ { self .rees .pull_image_name } ' ,'light_blue' )
129
- self .cprint (f' ├───────── ⎌ { self .rees .binder_image_tag } ' ,'light_blue' )
130
- self .cprint (f" ├───────── ⏲ { self .rees .binder_commit_info ['datetime' ]} : { self .rees .binder_commit_info ['message' ]} " .replace ('\n ' , '' ),'light_blue' )
116
+
117
+ # Use the helper function to log and print messages
118
+ def log_and_print (message , color = None ):
119
+ output_logs .append (message )
120
+ if color :
121
+ self .cprint (message , color )
122
+ else :
123
+ print (message )
124
+
125
+ # Collecting and printing output
126
+ log_and_print ('[Status]' , 'light_grey' )
127
+ log_and_print (' ├─────── ⏺ running' , 'green' )
128
+ log_and_print (f' └─────── Container { self .container .short_id } { self .container .name } ' , 'green' )
129
+ log_and_print (' ℹ Run the following commands in the terminal if you are debugging locally:' , 'yellow' )
130
+ log_and_print (f' port="{ self .port } "' , 'cyan' )
131
+ log_and_print (f' export JUPYTER_BASE_URL="{ self .jh_url } "' , 'cyan' )
132
+ log_and_print (f' export JUPYTER_TOKEN="{ self .jh_token } "' , 'cyan' )
133
+ log_and_print ('[Resources]' , 'light_grey' )
134
+ log_and_print (' ├── MyST repository' , 'magenta' )
135
+ log_and_print (f' │ ├───────── ✸ { self .rees .gh_user_repo_name } ' , 'light_blue' )
136
+ log_and_print (f' │ ├───────── ⎌ { self .rees .gh_repo_commit_hash } ' , 'light_blue' )
137
+ log_and_print (f" │ └───────── ⏲ { self .rees .repo_commit_info ['datetime' ]} : { self .rees .repo_commit_info ['message' ]} " .replace ('\n ' , '' ), 'light_blue' )
138
+ log_and_print (' └── Docker container' , 'magenta' )
139
+ log_and_print (f' ├───────── ✸ { self .rees .pull_image_name } ' , 'light_blue' )
140
+ log_and_print (f' ├───────── ⎌ { self .rees .binder_image_tag } ' , 'light_blue' )
141
+ log_and_print (f" ├───────── ⏲ { self .rees .binder_commit_info ['datetime' ]} : { self .rees .binder_commit_info ['message' ]} " .replace ('\n ' , '' ), 'light_blue' )
131
142
if self .rees .binder_image_name :
132
- self . cprint (f' └───────── ℹ Using NeuroLibre base image { self .rees .binder_image_name } ' ,'yellow' )
143
+ log_and_print (f' └───────── ℹ Using NeuroLibre base image { self .rees .binder_image_name } ' , 'yellow' )
133
144
else :
134
- self . cprint (f' └───────── ℹ This image was built from REES-compliant { self .rees .gh_user_repo_name } repository at the commit above' ,'yellow' )
145
+ log_and_print (f' └───────── ℹ This image was built from REES-compliant { self .rees .gh_user_repo_name } repository at the commit above' , 'yellow' )
135
146
except Exception as e :
136
147
logging .error (f'Could not spawn a JH: \n { e } ' )
148
+ output_logs .append (f'Error: { e } ' ) # Collecting error output
149
+
150
+ return output_logs # Return collected logs and cprints
137
151
138
152
def delete_stopped_containers (self ):
139
153
"""
0 commit comments