-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_parameters.py
130 lines (106 loc) · 5.04 KB
/
update_parameters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import requests
import json
import os
import re
import sys
# Your OtterTune API key
ottertune_api_key = os.environ['OT_API_KEY']
# Your database identifier
db_identifier = os.environ['DB_IDENTIFIER']
# Base URL for the OtterTune API
base_url = 'https://service.ottertune.com/api'
# Make a call to retrieve the database ID
databases_url = f'{base_url}/databases'
headers = {'OT-API-KEY': ottertune_api_key}
# Replace with your config file name
terraform_file_path = 'db_params.tf'
# Regex patterns to match parameter 'name' and 'value' lines
name_pattern = re.compile(r'name\s*=\s*"?([^"\n]+)"?')
value_pattern = re.compile(r'value\s*=\s*"?([^"\n]+)"?')
try:
response = requests.get(databases_url, headers=headers)
response.raise_for_status()
database_data = response.json()
# Find the database id for the given dbIdentifier
database_id = None
for database in database_data['results']:
if database['dbIdentifier'] == db_identifier:
database_id = database['id']
break
if database_id is not None:
recommendations_url = f'{base_url}/databases/{database_id}/recommendations'
# Make a call to retrieve knob recommendations
params = {'status': 'created', 'type': 'knob'}
response = requests.get(recommendations_url, headers=headers, params=params)
response.raise_for_status()
recommendations_data = response.json()
# Extract knob recommendations and store in a list of tuples
knob_recommendations = {}
for recommendation in recommendations_data['results']:
knob_name = recommendation['knobName']
knob_final_value = recommendation['knobFinalValue']
knob_recommendations[knob_name] = knob_final_value
print(f"KNOB_RECOMMENDATIONS = {json.dumps(knob_recommendations, indent=4, default=str)}\n")
# We have some new knob values from OtterTune which we need to update
# in our terraform file
if len(knob_recommendations) > 0:
with open(terraform_file_path, 'r') as file:
terraform_config = file.read()
config_lines = terraform_config.splitlines()
new_config_lines = []
# Iterate over the lines and update parameter values if needed
i = 0
while i < len(config_lines):
line = config_lines[i]
# Check if the line contains the parameter definition
# parameter definitions look like:
# parameter {
# name = "param_name"
# value = "param_value"
# }
# Note: This script will only update those parameters that are present
# in the parameter configuration file, other recommendations will be ignored
if line.strip().startswith("parameter {"):
# Extract the parameter name and value
param_name = None
param_value = None
while not line.strip().startswith("}"):
name_match = name_pattern.search(line)
if name_match:
param_name = name_match.group(1)
value_match = value_pattern.search(line)
if value_match:
param_value = value_match.group(1)
line = config_lines[i]
i += 1
# the parameter in the terraform configuration has a new recommended value
if param_name in knob_recommendations:
# Replace the parameter value with the new value
param_value = knob_recommendations[param_name]
new_config_lines.append(
f' parameter {{\n name = "{param_name}"\n value = "{param_value}"\n }}')
else:
# Keep the original parameter value
new_config_lines.append(
f' parameter {{\n name = "{param_name}"\n value = "{param_value}"\n }}')
else:
# Add non-parameter lines as is
new_config_lines.append(line)
i += 1
# Join the modified configuration lines
modified_terraform_config = "\n".join(new_config_lines)
# Write the modified Terraform configuration back to the file
with open(terraform_file_path, 'w') as file:
file.write(modified_terraform_config)
print(f"Terraform configuration file ({terraform_file_path}) has been updated with knob recommendations.")
else:
print("No knob recommendations found for this database.")
else:
print(f'Error: Database with dbIdentifier "{db_identifier}" not found.')
sys.exit(1)
except requests.exceptions.RequestException as e:
print(f'Error making HTTP request: {e}')
sys.exit(1)
except Exception as e:
print(f'An error occurred: {e}')
sys.exit(1)