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

Created render_jinja function to eliminate redundant jinja functions #54

Merged
merged 10 commits into from
Apr 1, 2024
59 changes: 16 additions & 43 deletions google_cloud_automlops/deployments/cloudbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
# Try backported to PY<37 `importlib_resources`
from importlib_resources import files as import_files

from jinja2 import Template

from google_cloud_automlops.utils.utils import write_file
from google_cloud_automlops.utils.utils import (
render_jinja,
write_file
)
from google_cloud_automlops.utils.constants import (
BASE_DIR,
CLOUDBUILD_TEMPLATES_PATH,
Expand All @@ -48,46 +49,18 @@ def build(config: CloudBuildConfig):
config.use_ci: Flag that determines whether to use Cloud CI/CD.
"""
# Write cloud build config
write_file(GENERATED_CLOUDBUILD_FILE, create_cloudbuild_jinja(
config.artifact_repo_location,
config.artifact_repo_name,
config.naming_prefix,
config.project_id,
config.pubsub_topic_name,
config.use_ci), 'w')

def create_cloudbuild_jinja(
artifact_repo_location: str,
artifact_repo_name: str,
naming_prefix: str,
project_id: str,
pubsub_topic_name: str,
use_ci: bool) -> str:
"""Generates content for the cloudbuild.yaml, to be written to the base_dir.
This file contains the ci/cd manifest for AutoMLOps.

Args:
artifact_repo_location: Region of the artifact repo (default use with Artifact Registry).
artifact_repo_name: Artifact repo name where components are stored (default use with Artifact Registry).
naming_prefix: Unique value used to differentiate pipelines and services across AutoMLOps runs.
project_id: The project ID.
pubsub_topic_name: The name of the pubsub topic to publish to.
use_ci: Flag that determines whether to use Cloud CI/CD.

Returns:
str: Contents of cloudbuild.yaml.
"""
component_base_relative_path = COMPONENT_BASE_RELATIVE_PATH if use_ci else f'{BASE_DIR}{COMPONENT_BASE_RELATIVE_PATH}'
template_file = import_files(CLOUDBUILD_TEMPLATES_PATH) / 'cloudbuild.yaml.j2'
with template_file.open('r', encoding='utf-8') as f:
template = Template(f.read())
return template.render(
artifact_repo_location=artifact_repo_location,
artifact_repo_name=artifact_repo_name,
component_base_relative_path = COMPONENT_BASE_RELATIVE_PATH if config.use_ci else f'{BASE_DIR}{COMPONENT_BASE_RELATIVE_PATH}'
write_file(
filepath=GENERATED_CLOUDBUILD_FILE,
text=render_jinja(
template_path=import_files(CLOUDBUILD_TEMPLATES_PATH) / 'cloudbuild.yaml.j2',
artifact_repo_location=config.artifact_repo_location,
artifact_repo_name=config.artifact_repo_name,
component_base_relative_path=component_base_relative_path,
generated_license=GENERATED_LICENSE,
generated_parameter_values_path=GENERATED_PARAMETER_VALUES_PATH,
naming_prefix=naming_prefix,
project_id=project_id,
pubsub_topic_name=pubsub_topic_name,
use_ci=use_ci)
naming_prefix=config.naming_prefix,
project_id=config.project_id,
pubsub_topic_name=config.pubsub_topic_name,
use_ci=config.use_ci),
mode='w')
82 changes: 21 additions & 61 deletions google_cloud_automlops/deployments/github_actions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
# Try backported to PY<37 `importlib_resources`
from importlib_resources import files as import_files

from jinja2 import Template
from google_cloud_automlops.utils.utils import (
render_jinja,
write_file
)

from google_cloud_automlops.utils.utils import write_file
from google_cloud_automlops.utils.constants import (
GENERATED_GITHUB_ACTIONS_FILE,
COMPONENT_BASE_RELATIVE_PATH,
Expand Down Expand Up @@ -52,65 +54,23 @@ def build(config: GitHubActionsConfig):
config.workload_identity_service_account: Service account for workload identity federation.
"""
# Write github actions config
write_file(GENERATED_GITHUB_ACTIONS_FILE, create_github_actions_jinja(
config.artifact_repo_location,
config.artifact_repo_name,
config.naming_prefix,
config.project_id,
config.project_number,
config.pubsub_topic_name,
config.source_repo_branch,
config.use_ci,
config.workload_identity_pool,
config.workload_identity_provider,
config.workload_identity_service_account), 'w')

def create_github_actions_jinja(
artifact_repo_location: str,
artifact_repo_name: str,
naming_prefix: str,
project_id: str,
project_number: str,
pubsub_topic_name: str,
source_repo_branch: str,
use_ci: bool,
workload_identity_pool: str,
workload_identity_provider: str,
workload_identity_service_account: str) -> str:
"""Generates content for the github_actions.yaml, to be written to the .github/workflows directory.
This file contains the ci/cd manifest for AutoMLOps.

Args:
artifact_repo_location: Region of the artifact repo (default use with Artifact Registry).
artifact_repo_name: Artifact repo name where components are stored (default use with Artifact Registry).
naming_prefix: Unique value used to differentiate pipelines and services across AutoMLOps runs.
project_id: The project ID.
project_number: The project number.
pubsub_topic_name: The name of the pubsub topic to publish to.
source_repo_branch: The branch to use in the source repository.
use_ci: Flag that determines whether to use Cloud CI/CD.
workload_identity_pool: Pool for workload identity federation.
workload_identity_provider: Provider for workload identity federation.
workload_identity_service_account: Service account for workload identity federation.

Returns:
str: Contents of github_actions.yaml.
"""
template_file = import_files(GITHUB_ACTIONS_TEMPLATES_PATH) / 'github_actions.yaml.j2'
with template_file.open('r', encoding='utf-8') as f:
template = Template(f.read())
return template.render(
artifact_repo_location=artifact_repo_location,
artifact_repo_name=artifact_repo_name,
write_file(
filepath=GENERATED_GITHUB_ACTIONS_FILE,
text=render_jinja(
template_path=import_files(GITHUB_ACTIONS_TEMPLATES_PATH) / 'github_actions.yaml.j2',
artifact_repo_location=config.artifact_repo_location,
artifact_repo_name=config.artifact_repo_name,
component_base_relative_path=COMPONENT_BASE_RELATIVE_PATH,
generated_license=GENERATED_LICENSE,
generated_parameter_values_path=GENERATED_PARAMETER_VALUES_PATH,
naming_prefix=naming_prefix,
project_id=project_id,
project_number=project_number,
pubsub_topic_name=pubsub_topic_name,
source_repo_branch=source_repo_branch,
use_ci=use_ci,
workload_identity_pool=workload_identity_pool,
workload_identity_provider=workload_identity_provider,
workload_identity_service_account=workload_identity_service_account)
naming_prefix=config.naming_prefix,
project_id=config.project_id,
project_number=config.project_number,
pubsub_topic_name=config.pubsub_topic_name,
source_repo_branch=config.source_repo_branch,
use_ci=config.use_ci,
workload_identity_pool=config.workload_identity_pool,
workload_identity_provider=config.workload_identity_provider,
workload_identity_service_account=config.workload_identity_service_account
),
mode='w')
21 changes: 6 additions & 15 deletions google_cloud_automlops/deployments/gitops/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import os
import subprocess

from jinja2 import Template

from google_cloud_automlops.utils.constants import (
BASE_DIR,
GENERATED_DEFAULTS_FILE,
Expand All @@ -39,6 +37,7 @@
from google_cloud_automlops.utils.utils import (
execute_process,
read_yaml_file,
render_jinja,
write_file
)
from google_cloud_automlops.deployments.enums import (
Expand Down Expand Up @@ -76,7 +75,11 @@ def git_workflow():
has_remote_branch = subprocess.check_output(
[f'''git -C {BASE_DIR} ls-remote origin {defaults['gcp']['source_repository_branch']}'''], shell=True, stderr=subprocess.STDOUT)

write_file(f'{BASE_DIR}.gitignore', _create_gitignore_jinja(), 'w')
write_file(
f'{BASE_DIR}.gitignore',
render_jinja(template_path=import_files(GITOPS_TEMPLATES_PATH) / 'gitignore.j2'),
'w')

# This will initialize the branch, a second push will be required to trigger the cloudbuild job after initializing
if not has_remote_branch:
execute_process(f'git -C {BASE_DIR} add .gitignore', to_null=False)
Expand All @@ -102,15 +105,3 @@ def git_workflow():
if deployment_framework == Deployer.CLOUDBUILD.value:
logging.info(
f'''Cloud Build job running at: https://console.cloud.google.com/cloud-build/builds;region={defaults['gcp']['build_trigger_location']}''')


def _create_gitignore_jinja() -> str:
"""Generates code for .gitignore file.

Returns:
str: .gitignore file.
"""
template_file = import_files(GITOPS_TEMPLATES_PATH) / 'gitignore.j2'
with template_file.open('r', encoding='utf-8') as f:
template = Template(f.read())
return template.render()
Loading
Loading