-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Oppdater dask-infrastructure-script (#58)
Nå som vi har refaktorert dask-infrastructure må vi også ta høyde for dette i scriptet
- Loading branch information
1 parent
633ec78
commit 5a766c9
Showing
2 changed files
with
65 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |