-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Daily Parameter Update | ||
|
||
on: | ||
schedule: | ||
- cron: '0 0 * * *' # Schedule the workflow to run daily at midnight UTC | ||
|
||
jobs: | ||
update_and_commit: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.x | ||
|
||
- name: Run Python script | ||
run: | | ||
python update_parameters.py | ||
- name: Commit changes | ||
run: | | ||
git config user.email "[email protected]" | ||
git config user.name "Your Name" | ||
git add db_params.tf | ||
git commit -m "AI knob updates" | ||
git push | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN}} | ||
|
||
- name: Set up Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
with: | ||
terraform_version: 1.0.8 # Replace with your desired Terraform version | ||
|
||
- name: Initialize Terraform | ||
run: terraform init | ||
|
||
- name: Apply Terraform changes | ||
# CAUTION: -auto-approve may override changes to the infra | ||
# that are not saved in the terraform files | ||
run: terraform apply -auto-approve |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
resource "aws_db_parameter_group" "education" { | ||
name = "education" | ||
family = "postgres14" | ||
|
||
parameter { | ||
name = "log_connections" | ||
value = "1" | ||
} | ||
parameter { | ||
name = "autovacuum_vacuum_cost_delay" | ||
value = "2" | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
resource "aws_db_instance" "education" { | ||
identifier = "education" | ||
instance_class = "db.t3.micro" | ||
allocated_storage = 5 | ||
engine = "postgres" | ||
engine_version = "14.1" | ||
username = "edu" | ||
password = var.db_password | ||
db_subnet_group_name = aws_db_subnet_group.education.name | ||
vpc_security_group_ids = [aws_security_group.rds.id] | ||
parameter_group_name = aws_db_parameter_group.education.name | ||
publicly_accessible = true | ||
skip_final_snapshot = true | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.31.0 |
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import requests | ||
import os | ||
|
||
# Replace with your actual OtterTune API key | ||
ottertune_api_key = os.environ['OT-API-KEY'] | ||
|
||
# Replace with the desired database identifier | ||
db_identifier = 'YOUR_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' | ||
|
||
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 | ||
|
||
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 | ||
if line.strip().startswith("parameter {"): | ||
# Extract the parameter name and value | ||
param_name = None | ||
param_value = None | ||
while not line.strip().startswith("}"): | ||
if line.strip().startswith("name = "): | ||
param_name = line.strip().split(" = ")[1].strip('""') | ||
elif line.strip().startswith("value = "): | ||
param_value = line.strip().split(" = ")[1].strip('""') | ||
line = config_lines[i] | ||
i += 1 | ||
|
||
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("Terraform configuration file (main.tf) has been updated with knob recommendations.") | ||
else: | ||
print("No knob recommendations found for this database.") | ||
else: | ||
print(f'Database with dbIdentifier "{db_identifier}" not found.') | ||
|
||
except requests.exceptions.RequestException as e: | ||
print(f'Error making HTTP request: {e}') | ||
except Exception as e: | ||
print(f'An error occurred: {e}') |