Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deployment defaults with project init #9146

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/prefect/cli/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def init(name: str = None, recipe: str = None):
)
file_msg = (
f"Created project in [green]{Path('.').resolve()}[/green] with the following"
f" new files:\n {files}"
f" new files:\n{files}"
)
app.console.print(file_msg if files else empty_msg)

Expand Down
2 changes: 1 addition & 1 deletion src/prefect/cli/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ async def deploy(
parameters=base_deploy["parameters"],
description=base_deploy["description"],
tags=base_deploy["tags"],
path=base_deploy["path"],
path=base_deploy.get("path"),
entrypoint=base_deploy["entrypoint"],
parameter_openapi_schema=base_deploy["parameter_openapi_schema"].dict(),
pull_steps=project["pull"],
Expand Down
28 changes: 25 additions & 3 deletions src/prefect/projects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@ def find_prefect_directory(path: Path = None) -> Optional[Path]:
parent = path.parent.resolve()


def create_default_deployment_yaml(path: str) -> bool:
def create_default_deployment_yaml(path: str, field_defaults: dict = None) -> bool:
"""
Creates default deployment.yaml file in the provided path if one does not already exist;
returns boolean specifying whether a file was created.
"""
field_defaults = field_defaults or {}

path = Path(path)
if (path / "deployment.yaml").exists():
return False

default_file = Path(__file__).parent / "templates" / "deployment.yaml"

# load default file
with open(default_file, "r") as df:
default = yaml.safe_load(df)

# apply field defaults
for field, default_value in field_defaults.items():
if isinstance(default.get(field), dict):
default[field].update(default_value)
else:
default[field] = default_value

with open(path / "deployment.yaml", "w") as f:
f.write(default_file.read_text())
yaml.dump(default, f)

return True


Expand Down Expand Up @@ -194,10 +210,16 @@ def initialize_project(name: str = None, recipe: str = None) -> List[str]:

project_name = name or dir_name

# apply deployment defaults
if "docker" in recipe:
field_defaults = {"work_pool": {"job_variables": {"image": "{{ image_name }}"}}}
else:
field_defaults = {}

files = []
if create_default_ignore_file("."):
files.append(".prefectignore")
if create_default_deployment_yaml("."):
if create_default_deployment_yaml(".", field_defaults=field_defaults):
files.append("deployment.yaml")
if create_default_project_yaml(".", name=project_name, contents=configuration):
files.append("prefect.yaml")
Expand Down
1 change: 0 additions & 1 deletion src/prefect/projects/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ schedule: null
# flow-specific fields
flow_name: null
entrypoint: null
path: null
parameters: {}
parameter_openapi_schema: null

Expand Down
29 changes: 29 additions & 0 deletions tests/projects/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ async def test_initialize_project_with_recipe(self):
build_step = contents["build"][0]
assert "prefect_docker.projects.steps.build_docker_image" in build_step

@pytest.mark.parametrize(
"recipe",
[
d.absolute().name
for d in Path(
prefect.__development_base_path__
/ "src"
/ "prefect"
/ "projects"
/ "recipes"
).iterdir()
if d.is_dir() and "docker" in d.absolute().name
],
)
async def test_initialize_project_with_docker_recipe_default_image(self, recipe):
files = initialize_project(recipe=recipe)
assert len(files) >= 3

with open("prefect.yaml", "r") as f:
contents = yaml.safe_load(f)

build_step = contents["build"][0]
assert "prefect_docker.projects.steps.build_docker_image" in build_step

with open("deployment.yaml", "r") as f:
contents = yaml.safe_load(f)

assert contents["work_pool"]["job_variables"]["image"] == "{{ image_name }}"


class TestRegisterFlow:
async def test_register_flow_works_in_root(self, project_dir):
Expand Down