Skip to content

Commit 7536df9

Browse files
authored
feat: support --async-builds flag for generate_dag.py (#424)
1 parent beb48e3 commit 7536df9

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

scripts/generate_dag.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ def main(
5151
env: str,
5252
all_pipelines: bool = False,
5353
skip_builds: bool = False,
54+
async_builds: bool = False,
5455
format_code: bool = True,
5556
):
5657
if not skip_builds:
57-
build_images(dataset_id, env)
58+
build_images(dataset_id, env, async_builds)
5859

5960
if all_pipelines:
6061
for pipeline_dir in list_subdirs(DATASETS_PATH / dataset_id / "pipelines"):
@@ -229,7 +230,7 @@ def copy_files_to_dot_dir(dataset_id: str, pipeline_id: str, env_dir: pathlib.Pa
229230
)
230231

231232

232-
def build_images(dataset_id: str, env: str):
233+
def build_images(dataset_id: str, env: str, async_builds: bool):
233234
parent_dir = DATASETS_PATH / dataset_id / "pipelines" / "_images"
234235
if not parent_dir.exists():
235236
return
@@ -238,7 +239,7 @@ def build_images(dataset_id: str, env: str):
238239
dataset_id, parent_dir, PROJECT_ROOT / f".{env}"
239240
)
240241
for image_dir in image_dirs:
241-
build_and_push_image(dataset_id, image_dir)
242+
build_and_push_image(dataset_id, image_dir, async_builds)
242243

243244

244245
def copy_image_files_to_dot_dir(
@@ -253,24 +254,27 @@ def copy_image_files_to_dot_dir(
253254
return list_subdirs(target_dir / "_images")
254255

255256

256-
def build_and_push_image(dataset_id: str, image_dir: pathlib.Path):
257+
def build_and_push_image(
258+
dataset_id: str, image_dir: pathlib.Path, async_builds: bool = False
259+
):
257260
image_name = f"{dataset_id}__{image_dir.name}"
258-
tag = f"gcr.io/{gcp_project_id()}/{image_name}"
261+
command = [
262+
"gcloud",
263+
"builds",
264+
"submit",
265+
"--async",
266+
"--tag",
267+
f"gcr.io/{gcp_project_id()}/{image_name}",
268+
]
269+
270+
if not async_builds:
271+
command.remove("--async")
259272

260273
# gcloud builds submit --tag gcr.io/PROJECT_ID/IMAGE_NAME
261-
subprocess.check_call(
262-
[
263-
"gcloud",
264-
"builds",
265-
"submit",
266-
"--tag",
267-
str(tag),
268-
],
269-
cwd=image_dir,
270-
)
274+
subprocess.check_call(command, cwd=image_dir)
271275

272276

273-
def gcp_project_id(project_id: str = None) -> str:
277+
def gcp_project_id() -> str:
274278
_, project_id = google.auth.default()
275279
return project_id
276280

@@ -308,6 +312,16 @@ def gcp_project_id(project_id: str = None) -> str:
308312
parser.add_argument(
309313
"--skip-builds", required=False, dest="skip_builds", action="store_true"
310314
)
315+
parser.add_argument(
316+
"--async-builds", required=False, dest="async_builds", action="store_false"
317+
)
311318

312319
args = parser.parse_args()
313-
main(args.dataset, args.pipeline, args.env, args.all_pipelines, args.skip_builds)
320+
main(
321+
args.dataset,
322+
args.pipeline,
323+
args.env,
324+
args.all_pipelines,
325+
args.skip_builds,
326+
args.async_builds,
327+
)

tests/scripts/test_generate_dag.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,58 @@ def test_build_images_copies_image_files_to_env_dir(
372372

373373
def test_build_images_called_when_dataset_has_images_dir(
374374
dataset_path: pathlib.Path, pipeline_path: pathlib.Path, env: str, mocker
375+
):
376+
copy_config_files_and_set_tmp_folder_names_as_ids(dataset_path, pipeline_path)
377+
num_containers = random.randint(1, 3)
378+
generate_image_files(dataset_path, num_containers=num_containers)
379+
assert (
380+
sum([f.is_dir() for f in (dataset_path / "pipelines" / "_images").iterdir()])
381+
== num_containers
382+
)
383+
384+
mocker.patch("scripts.generate_dag.build_images")
385+
generate_dag.main(dataset_path.name, pipeline_path.name, env, format_code=False)
386+
387+
async_builds_default = False
388+
generate_dag.build_images.assert_called_once_with(
389+
dataset_path.name, env, async_builds_default
390+
)
391+
392+
393+
def test_build_images_called_with_async_false_by_default(
394+
dataset_path: pathlib.Path, pipeline_path: pathlib.Path, env: str, mocker
375395
):
376396
copy_config_files_and_set_tmp_folder_names_as_ids(dataset_path, pipeline_path)
377397
generate_image_files(dataset_path, num_containers=random.randint(1, 3))
378398

379399
mocker.patch("scripts.generate_dag.build_images")
380400
generate_dag.main(dataset_path.name, pipeline_path.name, env, format_code=False)
381-
generate_dag.build_images.assert_called_once_with(dataset_path.name, env)
401+
402+
async_builds_default = False
403+
generate_dag.build_images.assert_called_once_with(
404+
dataset_path.name, env, async_builds_default
405+
)
406+
407+
408+
def test_build_images_called_with_async_builds(
409+
dataset_path: pathlib.Path, pipeline_path: pathlib.Path, env: str, mocker
410+
):
411+
copy_config_files_and_set_tmp_folder_names_as_ids(dataset_path, pipeline_path)
412+
generate_image_files(dataset_path, num_containers=random.randint(1, 3))
413+
414+
mocker.patch("scripts.generate_dag.build_images")
415+
async_builds = True
416+
generate_dag.main(
417+
dataset_path.name,
418+
pipeline_path.name,
419+
env,
420+
async_builds=async_builds,
421+
format_code=False,
422+
)
423+
424+
generate_dag.build_images.assert_called_once_with(
425+
dataset_path.name, env, async_builds
426+
)
382427

383428

384429
def test_build_images_not_called_given_skip_builds_argument(

0 commit comments

Comments
 (0)