diff --git a/controlpanel/api/models/app.py b/controlpanel/api/models/app.py index 21dc8b1a3..e94041b28 100644 --- a/controlpanel/api/models/app.py +++ b/controlpanel/api/models/app.py @@ -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: diff --git a/controlpanel/cli/management/commands/post_migration_clearup.py b/controlpanel/cli/management/commands/post_migration_clearup.py new file mode 100644 index 000000000..8315a72c6 --- /dev/null +++ b/controlpanel/cli/management/commands/post_migration_clearup.py @@ -0,0 +1,91 @@ + +# 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): + old_client_info = (app.app_conf or {}).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 self.apply_action: + auth0_instance.clear_up_app(old_client_info) + + def _update_db(self, app): + 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 or {}).get(App.KEY_WORD_FOR_AUTH_SETTINGS, {}): + del app.app_conf[App.KEY_WORD_FOR_AUTH_SETTINGS][App.DEFAULT_AUTH_CATEGORY] + if self.apply_action: + app.save() + + def _remove_application(self, app): + self._log_info(f"Removing the application {app.slug} - {app.repo_url}") + + """ TODO: how to deal with related bucket? we will output + 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}") + # Remove the relationship to avoid removal of the bucket when removing the app + if self.apply_action: + item.delete() + if self.apply_action: + app.delete() + + def _log_info(self, info): + self.stdout.write(info) + with open(self.SCRIPT_LOG_FILE_NAME, "a") as f: + f.write(info) + f.write("\n") + + def _clear_up_resources(self, auth0_instance): + 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) + self._update_db(app) + if "moj-analytical-services" in app.repo_url: + self._remove_application(app) + 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.apply_action = options.get('apply') or False + self._clear_up_resources(auth0_instance) + self.stdout.write("Clean up action has completed.") + diff --git a/controlpanel/develop/__init__.py b/controlpanel/develop/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/controlpanel/develop/apps.py b/controlpanel/develop/apps.py deleted file mode 100644 index 0aa76d51a..000000000 --- a/controlpanel/develop/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -# Third-party -from django.apps import AppConfig - - -class DevelopConfig(AppConfig): - name = "controlpanel.develop" diff --git a/controlpanel/develop/models.py b/controlpanel/develop/models.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/controlpanel/develop/templates/develop/index.html b/controlpanel/develop/templates/develop/index.html deleted file mode 100644 index f4ff53dec..000000000 --- a/controlpanel/develop/templates/develop/index.html +++ /dev/null @@ -1,56 +0,0 @@ - -
- - - - -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.
-- The above information about old app will be removed once the migration process is completed. -
- {% elif app_migration_status == "done" %} -- The migration for this app has been completed -
- {% else %} -- The migration process for this app has not been started yet! -
- {% endif %} - {% else %} - {% if app.description %} -- {{ app.description }} -
- {% endif %} + {% if app.description %} ++ {{ app.description }} +
{% endif %} - {% if feature_enabled and repo_access_error_msg %} + {% if repo_access_error_msg %}If the app was successfully built on the alpha (original) cluster, it can be accessed here:
- - {% endif %} -{% if app.repo_url %} @@ -108,53 +63,49 @@
- {{ settings.KIBANA_BASE_URL }} -
++ {{ settings.KIBANA_BASE_URL }} +
-- {{ settings.GRAFANA_BASE_URL }} -
++ {{ settings.GRAFANA_BASE_URL }} +
-Github workflows on app's repo are used for deploying the app.
-You are required to be member of admin team for this app repo in order to be able to maintain the deployments settings
- {% endif %} +Github workflows on app's repo are used for deploying the app.
+You are required to be member of admin team for this app repo in order to be able to maintain the deployments settings
Couldn't load the github settings
-Raw error message: {{ github_settings_access_error_msg }}
-It appears this deployment environment is redundant and can be removed
- -Couldn't load the github settings
+Raw error message: {{ github_settings_access_error_msg }}
+It appears this deployment environment is redundant and can be removed
+ +