Skip to content
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

ANPL-1701 post migration clearup #1206

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions controlpanel/api/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,6 @@ def auth0_client_name(self, env_name=None):
return settings.AUTH0_CLIENT_NAME_PATTERN.format(
app_name=client_name, env=env_name)

@property
def migration_info(self):
# TODO: using app.description for temporary place for storing old app info,
# The content of this field should be removed after app migration is completed.
try:
return json.loads(self.description).get("migration", {})
except ValueError:
return {}

def app_url_name(self, env_name):
format_pattern = settings.APP_URL_NAME_PATTERN.get(env_name.upper())
if not format_pattern:
Expand Down
88 changes: 88 additions & 0 deletions controlpanel/cli/management/commands/post_migration_clearup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

# Third-party
from django.core.management.base import BaseCommand

# First-party/Local
from controlpanel.api.models import App, AppS3Bucket
from controlpanel.api import auth0


class Command(BaseCommand):
help = "Clear up the redundant resources for migrated apps " \
"- remove the old auth0 related resources " \
"- remove old auth0 information from control db" \
"- remove the apps which are not required any more"

SCRIPT_LOG_FILE_NAME = "./clear_up_auth0_resources_log.txt"

EXCEPTION_APPS = ["gold-scorecard-form"]

def add_arguments(self, parser):
parser.add_argument(
"-a", "--apply", action="store_true", help="Apply the actions"
)

def _remove_old_auth0_clients(self, app, auth0_instance, apply_action: bool = False):
old_client_info = app.app_conf.get(App.KEY_WORD_FOR_AUTH_SETTINGS, {}).\
get(App.DEFAULT_AUTH_CATEGORY, {})
if not old_client_info:
self._log_info(f"No old client for {app.slug} - {app.repo_url}")
return

self._log_info(f"Removing the old client for {app.slug} - {app.repo_url}")
if apply_action:
auth0_instance.clear_up_app(old_client_info)

def _update_db(self, app, apply_action: bool = False):
self._log_info(f"Removing the migration info and old clients for {app.slug} - {app.repo_url}")
app.description = ""
if App.DEFAULT_AUTH_CATEGORY in app.app_conf.get(App.KEY_WORD_FOR_AUTH_SETTINGS, {}):
del app.app_conf[App.KEY_WORD_FOR_AUTH_SETTINGS][App.DEFAULT_AUTH_CATEGORY]
if apply_action:
app.save()

def _remove_application(self, app, apply_action: bool = False):
self._log_info(f"Removing the application {app.slug} - {app.repo_url}")

""" TODO: how to deal with related bucket? we will output
ymao2 marked this conversation as resolved.
Show resolved Hide resolved
the related datasets from this script"""
# log the related buckets information into file
related_buckets = AppS3Bucket.objects.filter(app_id=app.id)
for item in related_buckets:
self._log_info(f"The app links the bucket - {item.s3bucket.name}")
if apply_action:
app.delete()

def _log_info(self, info):
self.stdout.write(info)
ymao2 marked this conversation as resolved.
Show resolved Hide resolved
with open(self.SCRIPT_LOG_FILE_NAME, "a") as f:
f.write(info)
f.write("\n")

def _clear_up_resources(self, auth0_instance, apply_action: bool = False):
apps = App.objects.all()
counter = 1
for app in apps:
if app.slug in self.EXCEPTION_APPS:
self._log_info(f"Ignore the application {app.slug}")
continue

try:
self._log_info(f"{counter}--Processing the application {app.slug}")

self._remove_old_auth0_clients(app, auth0_instance, apply_action=apply_action)
if "moj-analytical-services" in app.repo_url:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these apps that were marked as DO NOT MIGRATE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is my assumption, if the app hasn't been updated with new github org ministryofjustice which means this app hasn't been migrated over --> means they don't need to be migrated 🤓 , this way saves me extra input for this script, and less chance to miss this kind of apps

But before I run this script , will try with apply of false first

self._remove_application(app, apply_action=apply_action)
else:
self._update_db(app, apply_action=apply_action)
self._log_info(f"{counter}--Done with the application {app.slug}")
counter += 1
except Exception as ex:
self._log_info(f"Failed to process {app.slug} due to error : {ex.__str__()}")

def handle(self, *args, **options):
self.stdout.write("start to scan the apps from database.")
auth0_instance = auth0.ExtendedAuth0()
self._clear_up_resources(auth0_instance, options.get('apply'))
self.stdout.write("Clean up action has completed.")

Empty file removed controlpanel/develop/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions controlpanel/develop/apps.py

This file was deleted.

Empty file removed controlpanel/develop/models.py
Empty file.
56 changes: 0 additions & 56 deletions controlpanel/develop/templates/develop/index.html

This file was deleted.

8 changes: 0 additions & 8 deletions controlpanel/develop/urls.py

This file was deleted.

45 changes: 0 additions & 45 deletions controlpanel/develop/views.py

This file was deleted.

127 changes: 39 additions & 88 deletions controlpanel/frontend/jinja2/webapp-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
{% set page_title = app.name %}
{% set app_domain = settings.APP_DOMAIN %}
{% set app_old_url = "https://" + app.slug + "." + settings.APP_DOMAIN_BEFORE_MIGRATION %}
{% set feature_enabled = settings.features.app_migration.enabled %}

{% set app_admins_html %}
{% include "modals/app_admins.html" %}
Expand All @@ -26,53 +25,17 @@
{% block content %}

<header>
{% if feature_enabled %}
<div class="govuk-width-container">
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Warning</span>
More information and user guidance for the webapp deployment pipeline and related settings will be provided soon. If you have questions about the app migration process, please contact us on the <a href=https://mojdt.slack.com/archives/C0504KY0Z2N>#data-platform-application-migration-support</a> Slack channel.
</strong>
</div>
<hr class="govuk-section-break govuk-section-break--visible">
</div>
<br/>
{% endif %}

<span class="govuk-caption-xl">Webapp</span>
<h1 class="govuk-heading-xl">{{ page_title }}</h1>

{% if feature_enabled %}
<h2 class="govuk-heading-m">Migration status:</h2>
{% if app_migration_status == "in_progress" %}
<p class="govuk-body" ><b>This app is currently being migrated. It has been deployed onto Cloud Platform for testing, and the original version is still available on the Alpha cluster.</b></p>
<ul class="govuk-body">
<li>Old repository: <a href="{{ app_migration_info["repo_url"] }}">{{ app_migration_info["repo_url"] }}</a></li>
<li>Old app name: <strong>{{ app_migration_info["app_name"] }}</strong></li>
<li>Old app URL: <a href="{{ app_migration_info["app_url"] }}">{{ app_migration_info["app_url"] }}</a></li>
Copy link
Contributor

@michaeljcollinsuk michaeljcollinsuk Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these changes will mean that the app manage page wont have links to the old app for gold scorecard? Wondering if we need to make a hardcoded exception in the meantime - not that I really want to! Depends how long we keep them on alpha, hopefully its not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to keep this information for gold-scorecard , the only information matter to them probably is the app_url which they know it anyway 😄 ,

</ul>
<p class="govuk-body" >
<b style="color:red">The above information about old app will be removed once the migration process is completed.</b>
</p>
{% elif app_migration_status == "done" %}
<p class="govuk-body">
<b style="color:red">The migration for this app has been completed</b>
</p>
{% else %}
<p class="govuk-body">
<b style="color:red">The migration process for this app has not been started yet!</b>
</p>
{% endif %}
{% else %}
{% if app.description %}
<p class="govuk-body">
{{ app.description }}
</p>
{% endif %}
{% if app.description %}
<p class="govuk-body">
{{ app.description }}
</p>
{% endif %}

{% if feature_enabled and repo_access_error_msg %}
{% if repo_access_error_msg %}
<div class="govuk-grid-row">
<div class="govuk-column-two-thirds">

Expand All @@ -91,14 +54,6 @@ <h2 class="govuk-heading-m govuk-error-summary-heading">
</div>
{% endif %}

{% if not app_migration_info and app_old_url %}
<h2 class="govuk-heading-m">Deployed URL</h2>
<p> If the app was successfully built on the alpha (original) cluster, it can be accessed here:</p>
<p class="govuk-body">
<a href="{{ app_old_url }}">{{ app_old_url }}</a>
</p>
{% endif %}

<h2 class="govuk-heading-m">Source Code Repository</h2>
<p class="govuk-body">
{% if app.repo_url %}
Expand All @@ -108,53 +63,49 @@ <h2 class="govuk-heading-m">Source Code Repository</h2>
{% endif %}
</p>

{% if feature_enabled %}

<h2 class="govuk-heading-m">App logs</h2>
<p class="govuk-body">
<a href="{{ settings.KIBANA_BASE_URL }}">{{ settings.KIBANA_BASE_URL }}</a>
</p>
<h2 class="govuk-heading-m">App logs</h2>
<p class="govuk-body">
<a href="{{ settings.KIBANA_BASE_URL }}">{{ settings.KIBANA_BASE_URL }}</a>
</p>

<h2 class="govuk-heading-m">App resources usage dashboard</h2>
<p class="govuk-body">
<a href="{{ settings.GRAFANA_BASE_URL }}">{{ settings.GRAFANA_BASE_URL }}</a>
</p>
<h2 class="govuk-heading-m">App resources usage dashboard</h2>
<p class="govuk-body">
<a href="{{ settings.GRAFANA_BASE_URL }}">{{ settings.GRAFANA_BASE_URL }}</a>
</p>

<h2 class="govuk-heading-m">Deployment Pipeline</h2>
<p class="govuk-body"> Github workflows on app's repo are used for deploying the app.</p>
<p class="govuk-body"><strong>You are required to be member of admin team for this app repo in order to be able to maintain the deployments settings</strong></p>
{% endif %}
<h2 class="govuk-heading-m">Deployment Pipeline</h2>
<p class="govuk-body"> Github workflows on app's repo are used for deploying the app.</p>
<p class="govuk-body"><strong>You are required to be member of admin team for this app repo in order to be able to maintain the deployments settings</strong></p>

</header>

<section class="cpanel-section track_task app-auth-settings-panel">
{% if feature_enabled %}
{% if github_settings_access_error_msg %}
<div class="govuk-error-summary" role="alert" aria-labelledby="error-summary-heading-example-1" tabindex="-1">
<p style="color:red">Couldn't load the github settings</p>
<p style="color:red">Raw error message: {{ github_settings_access_error_msg }}</p>
</div>
{% endif %}

{% for env_name, deployment_setting in deployments_settings.items() %}
<h2 class="govuk-heading-m" >Deployment settings under {{ env_name }}</h2>
{% if deployment_setting.get('is_redundant') and request.user.has_perm('api.update_app', app) %}
<section class="cpanel-section">
<p style="color:red">It appears this deployment environment is redundant and can be removed</p>
<form action="{{ url('remove-app-deployment-env', kwargs={'pk': app.id, 'env_name': env_name}) }}" method="post">
{{ csrf_input }}
<button class="govuk-button cpanel-button--destructive js-confirm"
data-confirm-message="Are you sure you want to remove this deployment environment?">
Remove redundant deployment environment
</button>
</form>
</section>
{% else %}
{{ app_deployment_settings(app, env_name, app_domain, deployment_setting, request, csrf_input) }}
{% endif %}
{% endfor %}
{% if github_settings_access_error_msg %}
<div class="govuk-error-summary" role="alert" aria-labelledby="error-summary-heading-example-1" tabindex="-1">
<p style="color:red">Couldn't load the github settings</p>
<p style="color:red">Raw error message: {{ github_settings_access_error_msg }}</p>
</div>
{% endif %}

{% for env_name, deployment_setting in deployments_settings.items() %}
<h2 class="govuk-heading-m" >Deployment settings under {{ env_name }}</h2>
{% if deployment_setting.get('is_redundant') and request.user.has_perm('api.update_app', app) %}
<section class="cpanel-section">
<p style="color:red">It appears this deployment environment is redundant and can be removed</p>
<form action="{{ url('remove-app-deployment-env', kwargs={'pk': app.id, 'env_name': env_name}) }}" method="post">
{{ csrf_input }}
<button class="govuk-button cpanel-button--destructive js-confirm"
data-confirm-message="Are you sure you want to remove this deployment environment?">
Remove redundant deployment environment
</button>
</form>
</section>
{% else %}
{{ app_deployment_settings(app, env_name, app_domain, deployment_setting, request, csrf_input) }}
{% endif %}
{% endfor %}

</section>

<section class="cpanel-section">
Expand Down
Loading
Loading