11#!/usr/bin/env python3
22
3- # Copyright (c) 2024 by Brockmann Consult GmbH
3+ # Copyright (c) 2024-2025 by Brockmann Consult GmbH
44# Permissions are hereby granted under the terms of the MIT License:
55# https://opensource.org/licenses/MIT.
66
1212
1313import click
1414import yaml
15+ from click .core import ParameterSource
1516
1617from .core import ScriptCreator , ImageBuilder , ContainerRunner
1718
@@ -30,13 +31,6 @@ def cli(verbose):
3031 "-b" , "--batch" , is_flag = True , help = "Run as batch script after creating"
3132)
3233
33- server_option = click .option (
34- "-s" ,
35- "--server" ,
36- is_flag = True ,
37- help = "Run as xcube server script after creating" ,
38- )
39-
4034output_option = click .option (
4135 "-o" ,
4236 "--output" ,
@@ -66,6 +60,13 @@ def cli(verbose):
6660 ),
6761)
6862
63+ server_option = click .option (
64+ "-s" ,
65+ "--server" ,
66+ is_flag = True ,
67+ help = "Run the script as an xcube server after creating it." ,
68+ )
69+
6970
7071@cli .command (help = "Create a compute engine script on the host system" )
7172@batch_option
@@ -113,25 +114,20 @@ def image_cli():
113114@image_cli .command (
114115 help = "Build, and optionally run, a compute engine as a Docker image"
115116)
116- @batch_option
117- @server_option
118- @from_saved_option
119117@click .option (
120118 "-b" ,
121119 "--build-dir" ,
122120 type = click .Path (path_type = pathlib .Path , dir_okay = True , file_okay = False ),
123121 help = "Build directory to use for preparing the Docker image. If not "
124122 "specified, an automatically created temporary directory will be used." ,
125123)
126- @keep_option
127124@click .option (
128125 "-e" ,
129126 "--environment" ,
130127 type = click .Path (path_type = pathlib .Path , dir_okay = False , file_okay = True ),
131128 help = "Conda environment file to use in Docker image. "
132129 "If not specified, try to reproduce the current environment." ,
133130)
134- @output_option
135131@click .option (
136132 "-t" ,
137133 "--tag" ,
@@ -150,55 +146,65 @@ def image_cli():
150146)
151147@notebook_argument
152148def build (
153- batch : bool ,
154- server : bool ,
155- from_saved : bool ,
156- keep : bool ,
157149 build_dir : pathlib .Path ,
158150 notebook : pathlib .Path ,
159- output : pathlib .Path ,
160151 environment : pathlib .Path ,
161152 tag : str ,
162153 eoap : pathlib .Path ,
163154) -> None :
164- init_args = dict (
165- notebook = notebook , output_dir = output , environment = environment , tag = tag
166- )
167- build_args = dict (
168- run_batch = batch , run_server = server , from_saved = from_saved , keep = keep
169- )
155+ init_args = dict (notebook = notebook , environment = environment , tag = tag )
170156 if build_dir :
171157 image_builder = ImageBuilder (build_dir = build_dir , ** init_args )
172158 os .makedirs (build_dir , exist_ok = True )
173- image_builder .build (** build_args )
159+ image = image_builder .build ()
174160 else :
175161 with tempfile .TemporaryDirectory () as temp_dir :
176162 image_builder = ImageBuilder (
177163 build_dir = pathlib .Path (temp_dir ), ** init_args
178164 )
179- image_builder .build (** build_args )
165+ image = image_builder .build ()
180166 if eoap :
181167 eoap .write_text (yaml .dump (image_builder .create_cwl ()))
168+ print (f"Built image with tags { image .tags } " )
182169
183170
184171@image_cli .command (help = "Run a compute engine image as a Docker container" )
185172@batch_option
186173@server_option
174+ @click .option (
175+ "-p" ,
176+ "--port" ,
177+ is_flag = False ,
178+ type = int ,
179+ default = 8080 ,
180+ help = "Host port for xcube server (default: 8080). Implies --server." ,
181+ )
187182@from_saved_option
188183@output_option
189184@keep_option
190185@click .argument ("image" , type = str )
186+ @click .pass_context
191187def run (
188+ ctx : click .Context ,
192189 batch : bool ,
193- server : bool ,
190+ server : False ,
191+ port : int ,
194192 from_saved : bool ,
195193 keep : bool ,
196194 image : str ,
197195 output : pathlib .Path ,
198196) -> None :
199197 runner = ContainerRunner (image = image , output_dir = output )
198+ port_specified_explicitly = (
199+ ctx .get_parameter_source ("port" )
200+ is not click .core .ParameterSource .DEFAULT
201+ )
202+ actual_port = port if server or port_specified_explicitly else None
200203 runner .run (
201- run_batch = batch , run_server = server , from_saved = from_saved , keep = keep
204+ run_batch = batch ,
205+ host_port = actual_port ,
206+ from_saved = from_saved ,
207+ keep = keep ,
202208 )
203209
204210
0 commit comments