Skip to content

Commit

Permalink
Oppdater dask-infrastructure-script (#58)
Browse files Browse the repository at this point in the history
Nå som vi har refaktorert dask-infrastructure må vi også ta høyde for
dette i scriptet
  • Loading branch information
augustdahl authored Mar 21, 2024
1 parent 633ec78 commit 5a766c9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
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)

0 comments on commit 5a766c9

Please sign in to comment.