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

Oppdater dask-infrastructure-script #58

Merged
merged 4 commits into from
Mar 21, 2024
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 .github/workflows/create-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
- name: Modify files
working-directory: current-repo
run: |
python scripts/${{ env.SCRIPT }} '../target-repo/${{ env.FILE_TO_MODIFY_PATH }}' '${{ fromJson(env.SCRIPT_PARAMS) }}'
python scripts/${{ env.SCRIPT }} '../target-repo/${{ env.FILE_TO_MODIFY_PATH }}' '${{ env.SCRIPT_PARAMS }}'

- name: Format Terraform Files
if: env.TERRAFORM_TARGET_FOLDER != null
Expand Down
99 changes: 64 additions & 35 deletions scripts/dask_infrastructure.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,76 @@
import sys
import os
import json

def edit_file(filepath, json_obj):
team_name: str = json_obj.get("team_name")
ad_group_name: str = json_obj.get("ad_group_name")
area_name: str = json_obj.get("area_name")
project_id_map: dict = json_obj.get("project_id_map")

def edit_file(filepath):
with open(filepath, 'r') as file:
# Define the new team data
new_team_data = generate_module_definition(ad_group_name, team_name, area_name, project_id_map)

append_content_to_end_of_file(filepath, new_team_data)

def generate_module_definition(ad_group_name: str, team_name: str, area_name: str, project_id_map: dict) -> str:
module = f'''
module "{team_name.lower()}" {{
source = "../dbx_team_resources"

ad_group_name = "{ad_group_name}"
team_name = "{team_name.lower()}"
area_name = "{area_name.lower()}"
deploy_sa_map = {{
sandbox = "{team_name.lower()}-deploy@{project_id_map['sandbox']}.iam.gserviceaccount.com",
dev = "{team_name.lower()}-deploy@{project_id_map['dev']}.iam.gserviceaccount.com",
test = "{team_name.lower()}-deploy@{project_id_map['test']}.iam.gserviceaccount.com",
prod = "{team_name.lower()}-deploy@{project_id_map['prod']}.iam.gserviceaccount.com"
}}
projects = {{
sandbox = "{project_id_map['sandbox']}",
dev = "{project_id_map['dev']}",
test = "{project_id_map['test']}",
prod = "{project_id_map['prod']}",
}}

providers = {{
databricks.accounts = databricks.accounts
databricks.workspace = databricks.workspace
google.global_storage = google.global_storage
}}

metastore_id = var.metastore_id
workspace_id = var.workspace_id
workspace_env = var.workspace_env
env = var.env
all_users_group_id = var.all_users_group_id
product_teams_group_id = var.product_teams_group_id
compute_sa_teams_group_id = var.compute_sa_teams_group_id
}}
'''

return module

def append_content_to_end_of_file(file_path: str, content: str) -> None:
with open(file_path) as file:
lines = file.readlines()
file.close()
lines.insert(len(lines), content)

with open(file_path, 'w') as file:
file.writelines(lines)
file.close()

# Define the new team data
new_team_data = """
"new_team" : {
"members" : [
"[email protected]",
"[email protected]"
],
"external_users" : [],
"area_name" : "new_area",
"deploy_service_account" : {
"sandbox" : "[email protected]",
"dev" : "[email protected]"
},
"project_id" : {
"sandbox" : "new-team-sandbox",
"dev" : "new-team-dev"
}
},
"""

# Find the end of the 'locals' block and insert the new team data
insert_index = next((i for i, line in enumerate(
reversed(lines)) if line.strip() == "},"), None)
if insert_index is not None:
lines.insert(len(lines) - insert_index, new_team_data)

with open(filepath, 'w') as file:
file.writelines(lines)


if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python edit_file.py <path-to-file> <script-params>")
if len(sys.argv) < 3:
print("Usage: python script.py <file_path> <json_object>")
sys.exit(1)

file_path = sys.argv[1]
absolute_path = os.path.abspath(file_path)
print(f"Attempting to access: {absolute_path}")
edit_file(file_path)
json_str = sys.argv[2]
json_obj = json.loads(json_str)

edit_file(file_path, json_obj)
Loading