INFRA-2296 | Harinder | Updating script to get manifest with substituted environment variables

This commit is contained in:
Harinder Singh
2023-12-12 16:21:01 +05:30
parent 80dd81b04e
commit 0874f7b017

View File

@@ -64,63 +64,6 @@ def get_migration_resource_list():
migration_required_for = json.load(fp)
print("List of resources for which migration is required:" + str(migration_required_for))
def update_manifest_with_environment_variables(manifest, config, resource_key):
resource = manifest['extraResources'][resource_key]
for k, v in resource.items():
if "${" in str(v) or "$" in str(v):
if v[2:-1] in config:
manifest['extraResources'][resource_key][k] = config[v[2:-1]]
elif v[1:] in config:
manifest['extraResources'][resource_key][k] = config[v[1:]]
else:
if "READONLY" in str(v[2:-1]):
manifest['extraResources'][resource_key][k] = ""
else:
print(f"Key {v[2:-1]} or {v[1:]} not found in config")
return False
return True
def load_env_variables(manifest):
'''
Loads the environment variables from manifest and exports them to the required variables
Manifest as fetched from portal has variables in the form ${variable_name} or $variable_name
which this function replaces with the actual value from the environment variables
If this isn't done, terraform will throws error.
Uses update_manifest_with_environment_variables() to update the manifest with the actual values
'''
config = {}
if "environmentVariables" in manifest:
environment_variables = manifest["environmentVariables"]
for env_var in environment_variables:
config[env_var["name"]] = env_var["value"]
else:
print("Environment variables not found in manifest: " + str(manifest['id']))
return False
if 'extraResources' in manifest:
extra_resources = manifest['extraResources']
# NOTE: The following logic should be changed per resource type
if 'database' in extra_resources:
if not update_manifest_with_environment_variables(manifest, config, 'database'):
return False
if 'docdb' in extra_resources:
if not update_manifest_with_environment_variables(manifest, config, 'docdb'):
return False
if 'elasticSearch' in extra_resources:
if not update_manifest_with_environment_variables(manifest, config, 'elasticSearch'):
return False
else:
print("No extra resources found in manifest: " + str(manifest['id']))
return False
# Export updated manifest to file
f = open("manifest.json", "w")
json.dump(manifest, f)
f.close()
return True
def get_workspace(manifest):
# Gets the workspace which is used in selecting the appropriate terraform workspace
workspace = ""
@@ -162,13 +105,6 @@ def tf_state_update(manifest, resource_type):
setup_working_directory()
if load_env_variables(manifest):
print("Loaded environment variables")
else:
print("Issues with finding environment variables and exporting to required variables in manifest. Aborting operation for manifest: " + str(manifest['id']))
cleanup_working_directory()
return
env_vars = {}
env_vars['TEAM_NAME'] = manifest['team']['name']