Skip to content

Commit 34f3fe9

Browse files
authored
Run template CLI command and bugfix (#3225)
1 parent 1b16f16 commit 34f3fe9

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/zenml/cli/pipeline.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,86 @@ def run_pipeline(
315315
pipeline_instance()
316316

317317

318+
@pipeline.command(
319+
"create-run-template",
320+
help="Create a run template for a pipeline. The SOURCE argument needs to "
321+
"be an importable source path resolving to a ZenML pipeline instance, e.g. "
322+
"`my_module.my_pipeline_instance`.",
323+
)
324+
@click.argument("source")
325+
@click.option(
326+
"--name",
327+
"-n",
328+
type=str,
329+
required=True,
330+
help="Name for the template",
331+
)
332+
@click.option(
333+
"--config",
334+
"-c",
335+
"config_path",
336+
type=click.Path(exists=True, dir_okay=False),
337+
required=False,
338+
help="Path to configuration file for the build.",
339+
)
340+
@click.option(
341+
"--stack",
342+
"-s",
343+
"stack_name_or_id",
344+
type=str,
345+
required=False,
346+
help="Name or ID of the stack to use for the build.",
347+
)
348+
def create_run_template(
349+
source: str,
350+
name: str,
351+
config_path: Optional[str] = None,
352+
stack_name_or_id: Optional[str] = None,
353+
) -> None:
354+
"""Create a run template for a pipeline.
355+
356+
Args:
357+
source: Importable source resolving to a pipeline instance.
358+
name: Name of the run template.
359+
config_path: Path to pipeline configuration file.
360+
stack_name_or_id: Name or ID of the stack for which the template should
361+
be created.
362+
"""
363+
if not Client().root:
364+
cli_utils.warning(
365+
"You're running the `zenml pipeline create-run-template` command "
366+
"without a ZenML repository. Your current working directory will "
367+
"be used as the source root relative to which the registered step "
368+
"classes will be resolved. To silence this warning, run `zenml "
369+
"init` at your source code root."
370+
)
371+
372+
try:
373+
pipeline_instance = source_utils.load(source)
374+
except ModuleNotFoundError as e:
375+
source_root = source_utils.get_source_root()
376+
cli_utils.error(
377+
f"Unable to import module `{e.name}`. Make sure the source path is "
378+
f"relative to your source root `{source_root}`."
379+
)
380+
except AttributeError as e:
381+
cli_utils.error("Unable to load attribute from module: " + str(e))
382+
383+
if not isinstance(pipeline_instance, Pipeline):
384+
cli_utils.error(
385+
f"The given source path `{source}` does not resolve to a pipeline "
386+
"object."
387+
)
388+
389+
with cli_utils.temporary_active_stack(stack_name_or_id=stack_name_or_id):
390+
pipeline_instance = pipeline_instance.with_options(
391+
config_path=config_path
392+
)
393+
template = pipeline_instance.create_run_template(name=name)
394+
395+
cli_utils.declare(f"Created run template `{template.id}`.")
396+
397+
318398
@pipeline.command("list", help="List all registered pipelines.")
319399
@list_options(PipelineFilter)
320400
def list_pipelines(**kwargs: Any) -> None:

src/zenml/pipelines/pipeline_definition.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ def _create_deployment(
583583
config_path: Optional[str] = None,
584584
unlisted: bool = False,
585585
prevent_build_reuse: bool = False,
586+
skip_schedule_registration: bool = False,
586587
) -> PipelineDeploymentResponse:
587588
"""Create a pipeline deployment.
588589
@@ -609,6 +610,7 @@ def _create_deployment(
609610
to any pipeline).
610611
prevent_build_reuse: DEPRECATED: Use
611612
`DockerSettings.prevent_build_reuse` instead.
613+
skip_schedule_registration: Whether to skip schedule registration.
612614
613615
Returns:
614616
The pipeline deployment.
@@ -649,7 +651,7 @@ def _create_deployment(
649651
stack.validate()
650652

651653
schedule_id = None
652-
if schedule:
654+
if schedule and not skip_schedule_registration:
653655
if not stack.orchestrator.config.is_schedulable:
654656
raise ValueError(
655657
f"Stack {stack.name} does not support scheduling. "
@@ -1445,7 +1447,9 @@ def create_run_template(
14451447
The created run template.
14461448
"""
14471449
self._prepare_if_possible()
1448-
deployment = self._create_deployment(**self._run_args)
1450+
deployment = self._create_deployment(
1451+
**self._run_args, skip_schedule_registration=True
1452+
)
14491453

14501454
return Client().create_run_template(
14511455
name=name, deployment_id=deployment.id, **kwargs

0 commit comments

Comments
 (0)