-
Notifications
You must be signed in to change notification settings - Fork 6
external-resource-rds-logs actions #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| #!/bin/bash | ||
|
|
||
| # create bucket | ||
| awslocal s3api create-bucket --bucket automated-actions |
This file contains hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
packages/automated_actions/automated_actions/celery/external_resource/_elasticache_flush.py
This file contains hidden or 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,85 @@ | ||
| from automated_actions_utils.cluster_connection import get_cluster_connection_data | ||
| from automated_actions_utils.external_resource import ( | ||
| ExternalResource, | ||
| ExternalResourceProvider, | ||
| get_external_resource, | ||
| settings, | ||
| ) | ||
| from automated_actions_utils.openshift_client import ( | ||
| OpenshiftClient, | ||
| SecretKeyRef, | ||
| job_builder, | ||
| ) | ||
|
|
||
| from automated_actions.celery.app import app | ||
| from automated_actions.celery.automated_action_task import AutomatedActionTask | ||
| from automated_actions.db.models import Action | ||
|
|
||
|
|
||
| class ExternalResourceFlushElastiCache: | ||
| def __init__( | ||
| self, action: Action, oc: OpenshiftClient, elasticache: ExternalResource | ||
| ) -> None: | ||
| self.action = action | ||
| self.oc = oc | ||
| self.elasticache = elasticache | ||
|
|
||
| def run( | ||
| self, | ||
| image: str, | ||
| command: list[str], | ||
| args: list[str], | ||
| secret_name: str, | ||
| env_secret_mappings: dict[str, str], | ||
| ) -> None: | ||
| job = job_builder( | ||
| image=image, | ||
| command=command, | ||
| args=args, | ||
| job_name_prefix="flush-elasticache-", | ||
| annotations={ | ||
| "automated-actions.action_id": str(self.action.action_id), | ||
| }, | ||
| env_secrets={ | ||
| key: SecretKeyRef( | ||
| secret=secret_name, | ||
| key=value, | ||
| ) | ||
| for key, value in env_secret_mappings.items() | ||
| }, | ||
| ) | ||
| return self.oc.run_job(namespace=self.elasticache.namespace, job=job) | ||
|
|
||
|
|
||
| @app.task(base=AutomatedActionTask) | ||
| def external_resource_flush_elasticache( | ||
| account: str, | ||
| identifier: str, | ||
| *, | ||
| action: Action, | ||
| ) -> None: | ||
| elasticache = get_external_resource( | ||
| account=account, | ||
| identifier=identifier, | ||
| provider=ExternalResourceProvider.ELASTICACHE, | ||
| ) | ||
|
|
||
| cluster_connection = get_cluster_connection_data(elasticache.cluster, settings) | ||
| oc = OpenshiftClient( | ||
| server_url=cluster_connection.url, token=cluster_connection.token | ||
| ) | ||
| if not elasticache.output_resource_name: | ||
| raise ValueError( | ||
| f"Output resource name not defined for {elasticache.identifier} in {elasticache.namespace} namespace.", | ||
| ) | ||
| ExternalResourceFlushElastiCache( | ||
| action=action, | ||
| oc=oc, | ||
| elasticache=elasticache, | ||
| ).run( | ||
| image=settings.external_resource_elasticache.image, | ||
| command=settings.external_resource_elasticache.flush_command, | ||
| args=settings.external_resource_elasticache.flush_command_args, | ||
| secret_name=elasticache.output_resource_name, | ||
| env_secret_mappings=settings.external_resource_elasticache.env_secret_mappings, | ||
| ) |
121 changes: 121 additions & 0 deletions
121
packages/automated_actions/automated_actions/celery/external_resource/_rds_logs.py
This file contains hidden or 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,121 @@ | ||
| import logging | ||
|
|
||
| from automated_actions_utils.aws_api import ( | ||
| AWSApi, | ||
| AWSStaticCredentials, | ||
| LogStream, | ||
| get_aws_credentials, | ||
| ) | ||
| from automated_actions_utils.external_resource import ( | ||
| ExternalResource, | ||
| ExternalResourceProvider, | ||
| get_external_resource, | ||
| ) | ||
|
|
||
| from automated_actions.celery.app import app | ||
| from automated_actions.celery.automated_action_task import AutomatedActionTask | ||
| from automated_actions.config import settings | ||
| from automated_actions.db.models import Action | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ExternalResourceRDSLogs: | ||
| """Class to handle RDS logs retrieval.""" | ||
|
|
||
| def __init__( | ||
| self, aws_api: AWSApi, rds: ExternalResource, s3_bucket: str, s3_prefix: str | ||
| ) -> None: | ||
| self.aws_api = aws_api | ||
| self.rds = rds | ||
| self.s3_bucket = s3_bucket | ||
| self.s3_prefix = s3_prefix | ||
|
|
||
| def run( | ||
| self, | ||
| target_aws_api: AWSApi, | ||
| expiration_days: int, | ||
| s3_file_name: str | None = None, | ||
| ) -> str | None: | ||
| """Retrieve RDS logs and upload them to S3 as a zip file.""" | ||
| s3_key = ( | ||
| s3_file_name | ||
| or f"{self.s3_prefix}/{self.rds.account.name}-{self.rds.identifier}.zip" | ||
| ) | ||
| # append .zip to the filename if not present | ||
| if not s3_key.endswith(".zip"): | ||
| s3_key += ".zip" | ||
|
|
||
| log.info( | ||
| f"Saving RDS logs for {self.rds.account.name}/{self.rds.identifier} to S3 {self.s3_bucket}/{s3_key}" | ||
| ) | ||
| log_streams = [ | ||
| LogStream( | ||
| name=log_file, | ||
| content=self.aws_api.stream_rds_log( | ||
| identifier=self.rds.identifier, log_file=log_file | ||
| ), | ||
| ) | ||
| for log_file in self.aws_api.list_rds_logs(self.rds.identifier) | ||
| ] | ||
| if not log_streams: | ||
| log.warning( | ||
| f"No logs found for RDS {self.rds.identifier} in account {self.rds.account.name}" | ||
| ) | ||
| return None | ||
| self.aws_api.stream_rds_logs_to_s3_zip( | ||
| log_streams=log_streams, | ||
| bucket=self.s3_bucket, | ||
| s3_key=s3_key, | ||
| target_aws_api=target_aws_api, | ||
| ) | ||
| return self.aws_api.generate_s3_download_url( | ||
| bucket=self.s3_bucket, | ||
| s3_key=s3_key, | ||
| expiration_secs=expiration_days * 24 * 3600, | ||
| ) | ||
|
|
||
|
|
||
| @app.task(base=AutomatedActionTask) | ||
| def external_resource_rds_logs( | ||
| account: str, | ||
| identifier: str, | ||
| expiration_days: int, | ||
| action: Action, # noqa: ARG001 | ||
| s3_file_name: str | None = None, | ||
| ) -> str: | ||
| rds = get_external_resource( | ||
| account=account, identifier=identifier, provider=ExternalResourceProvider.RDS | ||
| ) | ||
| rds_account_credentials = get_aws_credentials( | ||
| vault_secret=rds.account.automation_token, region=rds.account.region | ||
| ) | ||
|
|
||
| log_account_credentials = AWSStaticCredentials( | ||
| access_key_id=settings.external_resource_rds_logs.access_key_id, | ||
| secret_access_key=settings.external_resource_rds_logs.secret_access_key, | ||
| region=settings.external_resource_rds_logs.region, | ||
| ) | ||
|
|
||
| with ( | ||
| AWSApi(credentials=rds_account_credentials, region=rds.region) as aws_api, | ||
| AWSApi( | ||
| credentials=log_account_credentials, | ||
| s3_endpoint_url=settings.external_resource_rds_logs.s3_url, | ||
| ) as log_aws_api, | ||
| ): | ||
| url = ExternalResourceRDSLogs( | ||
| aws_api=aws_api, | ||
| rds=rds, | ||
| s3_bucket=settings.external_resource_rds_logs.bucket, | ||
| s3_prefix=settings.external_resource_rds_logs.prefix, | ||
| ).run( | ||
| target_aws_api=log_aws_api, | ||
| expiration_days=expiration_days, | ||
| s3_file_name=s3_file_name, | ||
| ) | ||
|
|
||
| if not url: | ||
| return "No logs found or no logs available for download." | ||
|
|
||
| return f"Download the RDS logs from the following URL: {url}. This link will expire in {expiration_days} days." |
42 changes: 42 additions & 0 deletions
42
packages/automated_actions/automated_actions/celery/external_resource/_rds_reboot.py
This file contains hidden or 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,42 @@ | ||
| from automated_actions_utils.aws_api import AWSApi, get_aws_credentials | ||
| from automated_actions_utils.external_resource import ( | ||
| ExternalResource, | ||
| ExternalResourceProvider, | ||
| get_external_resource, | ||
| ) | ||
|
|
||
| from automated_actions.celery.app import app | ||
| from automated_actions.celery.automated_action_task import AutomatedActionTask | ||
| from automated_actions.db.models import Action | ||
|
|
||
|
|
||
| class ExternalResourceRDSReboot: | ||
| def __init__(self, aws_api: AWSApi, rds: ExternalResource) -> None: | ||
| self.aws_api = aws_api | ||
| self.rds = rds | ||
|
|
||
| def run(self, *, force_failover: bool) -> None: | ||
| self.aws_api.reboot_rds_instance( | ||
| identifier=self.rds.identifier, force_failover=force_failover | ||
| ) | ||
|
|
||
|
|
||
| @app.task(base=AutomatedActionTask) | ||
| def external_resource_rds_reboot( | ||
| account: str, | ||
| identifier: str, | ||
| *, | ||
| force_failover: bool, | ||
| action: Action, # noqa: ARG001 | ||
| ) -> None: | ||
| rds = get_external_resource( | ||
| account=account, | ||
| identifier=identifier, | ||
| provider=ExternalResourceProvider.RDS, | ||
| ) | ||
|
|
||
| credentials = get_aws_credentials( | ||
| vault_secret=rds.account.automation_token, region=rds.account.region | ||
| ) | ||
| with AWSApi(credentials=credentials, region=rds.region) as aws_api: | ||
| ExternalResourceRDSReboot(aws_api, rds).run(force_failover=force_failover) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would clarify that this is the expiration date of the url you are going to get as result.