Skip to content

Commit

Permalink
Created render_jinja function to eliminate redundant jinja functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Feb 7, 2024
1 parent e9ab281 commit ca80b49
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 921 deletions.
57 changes: 16 additions & 41 deletions google_cloud_automlops/deployments/cloudbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

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 +51,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: 22 additions & 60 deletions google_cloud_automlops/deployments/github_actions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

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 (
GENERATED_GITHUB_ACTIONS_FILE,
COMPONENT_BASE_RELATIVE_PATH,
Expand Down Expand Up @@ -52,65 +56,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')
19 changes: 6 additions & 13 deletions google_cloud_automlops/deployments/gitops/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 +77,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 +107,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

0 comments on commit ca80b49

Please sign in to comment.