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

[Attempt 2] refactor: Clean up lms/envs/production.py cruft #36131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kdmccormick
Copy link
Member

@kdmccormick kdmccormick commented Jan 17, 2025

Description

This PR has 3 commits:

  1. We re-introduce refactor: Clean up lms/envs/production.py cruft #36115, which was reverted by Revert "refactor: Clean up lms/envs/production.py cruft" #36129 due to a typo.
  2. We fix said typo
  3. We tighten up our backwards-compatibility of ENV_TOKENS and a few other "auxillary" settings dictionaries, out of an abundance of caution and a desire not to break any existing Django plugins unintentionally. See commit description for more info.

Altogether, this PR makes exactly ONE change to lms/envs/production.py, and that is the removal of KEYS_WITH_MERGED_VALUES from the settings namespace. This "setting" was an internal implementation detail of production.py, which should have been prefixed with an underscore. I am not considering this a breaking change.

Otherwise, this PR should exactly preserve the global namespace of lms/envs/production.py given any LMS_CFG yaml file.

Testing Instructions

Here is a little script you can paste into edx-platform/diff_settings.sh (note the Tutor vs non-Tutor difference):

#!/usr/bin/env bash
# Usage: ./diff_settings.sh GIT_REF_A GIT_REF_B
# Will fail if git state is not clean.

set -xeuo pipefail  # Be verbose and strict

REF_A=$1
REF_B=$2
DIR_A=dump_settings_a
DIR_B=dump_settings_b

SETTINGS=( envs.devstack envs.production )  #  If not using Tutor
SETTINGS=( envs.tutor.production envs.devstack envs.tutor.development )  # If using Tutor

rm -rf "$DIR_A" "$DIR_B"
mkdir "$DIR_A" "$DIR_B"

for system in cms lms ; do
    for settings in "${SETTINGS[@]}" ; do
        module="${system}.${settings}"
        git checkout "$REF_A"
        DJANGO_SETTINGS_MODULE="$module" ./manage.py "$system" dump_settings > "$DIR_A/$module.json"
        git checkout "$REF_B"
        DJANGO_SETTINGS_MODULE="$module" ./manage.py "$system" dump_settings > "$DIR_B/$module.json"
    done
done

diff "$DIR_A" "$DIR_B" && echo "No difference!"

Ensure you have this PR's branch checked out and a updated master branch, and then run:

./diff_settings.sh master kdmccormick/lms-envs-production-cleanup-2

You should see that the only diff is the removal of KEYS_WITH_MERGED_VALUES from the LMS files:

+ diff dump_settings_a dump_settings_b
diff dump_settings_a/lms.envs.devstack.json dump_settings_b/lms.envs.devstack.json
2770,2780d2769
<     "KEYS_WITH_MERGED_VALUES": [
<         "FEATURES",
<         "TRACKING_BACKENDS",
<         "EVENT_TRACKING_BACKENDS",
<         "JWT_AUTH",
<         "CELERY_QUEUES",
<         "MKTG_URL_LINK_MAP",
<         "MKTG_URL_OVERRIDES",
<         "REST_FRAMEWORK",
<         "EVENT_BUS_PRODUCER_CONFIG"
<     ],
diff dump_settings_a/lms.envs.tutor.development.json dump_settings_b/lms.envs.tutor.development.json
2709,2719d2708
<     "KEYS_WITH_MERGED_VALUES": [
<         "FEATURES",
<         "TRACKING_BACKENDS",
<         "EVENT_TRACKING_BACKENDS",
<         "JWT_AUTH",
<         "CELERY_QUEUES",
<         "MKTG_URL_LINK_MAP",
<         "MKTG_URL_OVERRIDES",
<         "REST_FRAMEWORK",
<         "EVENT_BUS_PRODUCER_CONFIG"
<     ],
diff dump_settings_a/lms.envs.tutor.production.json dump_settings_b/lms.envs.tutor.production.json
2446,2456d2445
<     "KEYS_WITH_MERGED_VALUES": [
<         "FEATURES",
<         "TRACKING_BACKENDS",
<         "EVENT_TRACKING_BACKENDS",
<         "JWT_AUTH",
<         "CELERY_QUEUES",
<         "MKTG_URL_LINK_MAP",
<         "MKTG_URL_OVERRIDES",
<         "REST_FRAMEWORK",
<         "EVENT_BUS_PRODUCER_CONFIG"
<     ],

@kdmccormick kdmccormick force-pushed the kdmccormick/lms-envs-production-cleanup-2 branch from b70d54a to c0cb126 Compare January 17, 2025 19:40
@kdmccormick kdmccormick requested review from feanil and robrap January 17, 2025 19:42
@robrap
Copy link
Contributor

robrap commented Jan 17, 2025

Note: Test plan being discussed in Slack, with request not to merge without a plan.

Copy link
Contributor

@feanil feanil left a comment

Choose a reason for hiding this comment

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

I took a look through again with a finer view of the additions to make sure there weren't any typos and I was able to bring up the LMS and CMS from this version of the settings file and the minimal.yaml locally so I think it's good to go but I'll wait till we add the unit test that we were discussing in slack before providing the final approval.

@kdmccormick kdmccormick added the create-sandbox open-craft-grove should create a sandbox environment from this PR label Jan 23, 2025
@kdmccormick
Copy link
Member Author

(rephrasing from Slack) I don't see any way to unit test this PR, since unit tests use lms/envs/test.py, which is based directly on lms/envs/common.py, not lms/envs/production.py. I think we will have to rely on code review, manual testing, and the PR sandbox for verification.

@open-craft-grove
Copy link

Sandbox deployment failed 💥
Please check the settings and requirements.
Retry deployment by pushing a new commit or updating the requirements/settings in the pull request's description.
📜 Failure Logs
ℹ️ Grove Config, Tutor Config, Tutor Requirements

@0x29a 0x29a added create-sandbox open-craft-grove should create a sandbox environment from this PR and removed create-sandbox open-craft-grove should create a sandbox environment from this PR labels Jan 24, 2025
@open-craft-grove
Copy link

Sandbox deployment successful 🚀
🎓 LMS
📝 Studio
ℹ️ Grove Config, Tutor Config, Tutor Requirements

kdmccormick added a commit to kdmccormick/edx-platform that referenced this pull request Jan 27, 2025
This command dumps the current Django settings to JSON for
debugging/diagnostics. The output of this command is for *humans*... it
is NOT suitable for consumption by production systems.

In particular, we are introducing this command as part of a series of
refactorings to the Django settings files lms/envs/* and cms/envs/*.
We want to ensure that these refactorings do not introduce any
unexpected breaking changes, so the dump_settings command will both help
us manually verify our refactorings and help operators verify that our
refactorings behave expectedly when using their custom python/yaml
settings files.

Related to: openedx#36131
kdmccormick added a commit to kdmccormick/edx-platform that referenced this pull request Jan 27, 2025
This command dumps the current Django settings to JSON for
debugging/diagnostics. The output of this command is for *humans*... it
is NOT suitable for consumption by production systems.

In particular, we are introducing this command as part of a series of
refactorings to the Django settings files lms/envs/* and cms/envs/*.
We want to ensure that these refactorings do not introduce any
unexpected breaking changes, so the dump_settings command will both help
us manually verify our refactorings and help operators verify that our
refactorings behave expectedly when using their custom python/yaml
settings files.

Related to: openedx#36131
kdmccormick added a commit that referenced this pull request Jan 27, 2025
This command dumps the current Django settings to JSON for
debugging/diagnostics. The output of this command is for *humans*... it
is NOT suitable for consumption by production systems.

In particular, we are introducing this command as part of a series of
refactorings to the Django settings files lms/envs/* and cms/envs/*.
We want to ensure that these refactorings do not introduce any
unexpected breaking changes, so the dump_settings command will both help
us manually verify our refactorings and help operators verify that our
refactorings behave expectedly when using their custom python/yaml
settings files.

Related to: #36131
@kdmccormick kdmccormick force-pushed the kdmccormick/lms-envs-production-cleanup-2 branch from c0cb126 to 8cd5c90 Compare January 27, 2025 20:30
@kdmccormick kdmccormick requested a review from feanil January 27, 2025 20:31
@kdmccormick kdmccormick marked this pull request as draft January 27, 2025 20:36
@kdmccormick
Copy link
Member Author

Converting this to draft until I can manually reverify it using dump_settings from #36162

@open-craft-grove
Copy link

Sandbox deployment failed 💥
Please check the settings and requirements.
Retry deployment by pushing a new commit or updating the requirements/settings in the pull request's description.
📜 Failure Logs
ℹ️ Grove Config, Tutor Config, Tutor Requirements

@kdmccormick kdmccormick force-pushed the kdmccormick/lms-envs-production-cleanup-2 branch from 8cd5c90 to 0e4126a Compare January 28, 2025 20:20
@open-craft-grove
Copy link

Sandbox deployment successful 🚀
🎓 LMS
📝 Studio
ℹ️ Grove Config, Tutor Config, Tutor Requirements

@kdmccormick kdmccormick marked this pull request as ready for review January 29, 2025 04:09
@kdmccormick
Copy link
Member Author

@robrap @feanil This is ready for re-review.

@kdmccormick kdmccormick force-pushed the kdmccormick/lms-envs-production-cleanup-2 branch from 0e4126a to 49cbd81 Compare January 29, 2025 04:24
@open-craft-grove
Copy link

Sandbox deployment successful 🚀
🎓 LMS
📝 Studio
ℹ️ Grove Config, Tutor Config, Tutor Requirements

kdmccormick and others added 2 commits January 29, 2025 11:06
This reintroduces commit 1593923,
which was reverted due to a typo.

The typo is fixed in the commit immediately following this one.

Co-Authored-By: Feanil Patel <feanil@axim.org>
In the near term, we wish to precisely preserve the existing values of
all Django settings exposed by lms/envs/production.py in order to avoid
breaking legacy Django plugins without a proper announcement.

That includes preserving the behavior of these old, redundant dicts:
* ENV_TOKENS
* AUTH_TOKENS
* ENV_FEATURES
* ENV_CELERY_QUEUES
* ALTERNATE_QUEUE_ENVS

Particularly, it means we need to ensure that updates to Django
settings are reflected in these dicts. The most reliable way to do that
is to change the yaml-loading logic so that these values are aliased
to the corresponding values in the global namespace rather than
deep-copied.

Finally, we remove KEYS_WITH_MERGED_VALUES from the global namespace,
and inline the remaining list. We have modified the list (specifically,
we dropped the no-op MKTG_URL_OVERRIDES). Plugins should not be counting
on the value of the list, so we remove it.
@kdmccormick kdmccormick force-pushed the kdmccormick/lms-envs-production-cleanup-2 branch from 49cbd81 to b83dd8c Compare January 29, 2025 16:06
@open-craft-grove
Copy link

Sandbox deployment successful 🚀
🎓 LMS
📝 Studio
ℹ️ Grove Config, Tutor Config, Tutor Requirements

@robrap
Copy link
Contributor

robrap commented Jan 29, 2025

Thanks @kdmccormick. I'm going to have @dianakhuang take over anything required to get this over-the-line from 2U's end. Thanks!

@kdmccormick
Copy link
Member Author

Sounds good @robrap . Since non-SRE engineers cannot run management commands on stage (unless that changed since I left), I would recommend using a prod-like yaml file to compare the output of dump_settings for this PR vs for master before I merge, and ensure that KEYS_WITH_MERGED_VALUES is the only differing setting.

@robrap
Copy link
Contributor

robrap commented Jan 29, 2025

Yes. @dianakhuang is working on getting a prod-like yaml file on a sandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
create-sandbox open-craft-grove should create a sandbox environment from this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants