Skip to content

Commit

Permalink
Decouple DEBUG from DEV_MODE to enable running dev/prod mode independ…
Browse files Browse the repository at this point in the history
…ent of debug.
  • Loading branch information
KevinMind committed Oct 9, 2024
1 parent 24dd2ee commit d3c1ba6
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 35 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ jobs:
compose_file: docker-compose.yml:docker-compose.ci.yml
run: |
make compile_locales
make update_assets
make test_needs_locales_compilation
-
name: Static Assets
services: ''
compose_file: docker-compose.yml:docker-compose.ci.yml
run: make test_static_assets
run: |
make update_assets
make test_static_assets
-
name: Internal Routes
services: ''
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ rm -rf /deps/build/*
${PIP_COMMAND} install --progress-bar=off --no-deps --exists-action=w -r requirements/pip.txt
EOF

# Expose the DOCKER_TARGET variable to all subsequent stages
# This value is used to determine if we are building for production or development
ARG DOCKER_TARGET
ENV DOCKER_TARGET=${DOCKER_TARGET}

# Define production dependencies as a single layer
# let's the rest of the stages inherit prod dependencies
# and makes copying the /deps dir to the final layer easy.
Expand Down
5 changes: 4 additions & 1 deletion Makefile-docker
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ check_debian_packages: ## check the existence of multiple debian packages

.PHONY: check_pip_packages
check_pip_packages: ## check the existence of multiple python packages
./scripts/check_pip_packages.sh prod.txt dev.txt
@ ./scripts/check_pip_packages.sh prod.txt
@if [ "$(DOCKER_TARGET)" = "development" ]; then \
./scripts/check_pip_packages.sh dev.txt; \
fi

.PHONY: check_files
check_files: ## check the existence of multiple files
Expand Down
1 change: 1 addition & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target "web" {
DOCKER_COMMIT = "${DOCKER_COMMIT}"
DOCKER_VERSION = "${DOCKER_VERSION}"
DOCKER_BUILD = "${DOCKER_BUILD}"
DOCKER_TARGET = "${DOCKER_TARGET}"
}
pull = true

Expand Down
1 change: 0 additions & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ services:
worker:
environment:
- HOST_UID=9500
- DEBUG=
volumes:
- /data/olympia

Expand Down
14 changes: 7 additions & 7 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
INTERNAL_ROUTES_ALLOWED = True

# These apps are great during development.
INSTALLED_APPS += (
'olympia.landfill',
'dbbackup',
)
INSTALLED_APPS += ('olympia.landfill',)

DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'

Expand Down Expand Up @@ -52,8 +49,11 @@ def insert_debug_toolbar_middleware(middlewares):
return tuple(ret_middleware)


if DEBUG:
INSTALLED_APPS += ('debug_toolbar',)
if DEV_MODE:
INSTALLED_APPS += (
'debug_toolbar',
'dbbackup',
)
MIDDLEWARE = insert_debug_toolbar_middleware(MIDDLEWARE)

DEBUG_TOOLBAR_CONFIG = {
Expand Down Expand Up @@ -106,7 +106,7 @@ def insert_debug_toolbar_middleware(middlewares):
FXA_OAUTH_HOST = 'https://oauth.stage.mozaws.net/v1'
FXA_PROFILE_HOST = 'https://profile.stage.mozaws.net/v1'

# When USE_FAKE_FXA_AUTH and settings.DEBUG are both True, we serve a fake
# When USE_FAKE_FXA_AUTH and settings.DEV_MODE are both True, we serve a fake
# authentication page, bypassing FxA. To disable this behavior, set
# USE_FAKE_FXA = False in your local settings.
# You will also need to specify `client_id` and `client_secret` in your
Expand Down
2 changes: 2 additions & 0 deletions settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
IN_TEST_SUITE = True

DEBUG = False
# We should default to production mode unless otherwiser specified
DEV_MODE = False

# We won't actually send an email.
SEND_REAL_EMAIL = True
Expand Down
2 changes: 1 addition & 1 deletion src/olympia/accounts/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def test_redirect_for_login_with_2fa_enforced_and_config():
assert request.session['enforce_2fa'] is True


@override_settings(DEBUG=True, USE_FAKE_FXA_AUTH=True)
@override_settings(DEV_MODE=True, USE_FAKE_FXA_AUTH=True)
def test_fxa_login_url_when_faking_fxa_auth():
path = '/en-US/addons/abp/?source=ddg'
request = RequestFactory().get(path)
Expand Down
2 changes: 1 addition & 1 deletion src/olympia/accounts/tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def test_with_id_token(self):
self.get_profile.assert_called_with('cafe')


@override_settings(USE_FAKE_FXA_AUTH=False, DEBUG=True, VERIFY_FXA_ACCESS_TOKEN=True)
@override_settings(USE_FAKE_FXA_AUTH=False, DEV_MODE=True, VERIFY_FXA_ACCESS_TOKEN=True)
class TestCheckAndUpdateFxaAccessToken(TestCase):
def setUp(self):
super().setUp()
Expand Down
6 changes: 3 additions & 3 deletions src/olympia/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def has_cors_headers(response, origin='https://addons-frontend'):


class TestLoginStartView(TestCase):
@override_settings(DEBUG=True, USE_FAKE_FXA_AUTH=True)
@override_settings(DEV_MODE=True, USE_FAKE_FXA_AUTH=True)
def test_redirect_url_fake_fxa_auth(self):
response = self.client.get(reverse_ns('accounts.login_start'))
assert response.status_code == 302
Expand Down Expand Up @@ -700,7 +700,7 @@ def test_waffle_flag_off_enforced_2fa_should_have_no_effect(self):
self.request.session['enforce_2fa'] = True
self._test_should_continue_without_redirect_for_two_factor_auth()

@override_settings(DEBUG=True, USE_FAKE_FXA_AUTH=True)
@override_settings(DEV_MODE=True, USE_FAKE_FXA_AUTH=True)
def test_fake_fxa_auth(self):
self.user = user_factory()
self.find_user.return_value = self.user
Expand All @@ -721,7 +721,7 @@ def test_fake_fxa_auth(self):
assert kwargs['next_path'] == '/a/path/?'
assert self.fxa_identify.call_count == 0

@override_settings(DEBUG=True, USE_FAKE_FXA_AUTH=True)
@override_settings(DEV_MODE=True, USE_FAKE_FXA_AUTH=True)
def test_fake_fxa_auth_with_2fa(self):
self.user = user_factory()
self.find_user.return_value = self.user
Expand Down
6 changes: 3 additions & 3 deletions src/olympia/amo/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,15 @@ def test_fake_fxa_authorization_correct_values_passed():
@pytest.mark.django_db
def test_fake_fxa_authorization_deactivated():
url = reverse('fake-fxa-authorization')
with override_settings(DEBUG=False, USE_FAKE_FXA_AUTH=False):
with override_settings(DEV_MODE=False, USE_FAKE_FXA_AUTH=False):
response = test.Client().get(url)
assert response.status_code == 404

with override_settings(DEBUG=False, USE_FAKE_FXA_AUTH=True):
with override_settings(DEV_MODE=False, USE_FAKE_FXA_AUTH=True):
response = test.Client().get(url)
assert response.status_code == 404

with override_settings(DEBUG=True, USE_FAKE_FXA_AUTH=False):
with override_settings(DEV_MODE=True, USE_FAKE_FXA_AUTH=False):
response = test.Client().get(url)
assert response.status_code == 404

Expand Down
2 changes: 1 addition & 1 deletion src/olympia/amo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ def extract_colors_from_image(path):
def use_fake_fxa():
"""Return whether or not to use a fake FxA server for authentication.
Should always return False in production"""
return settings.DEBUG and settings.USE_FAKE_FXA_AUTH
return settings.DEV_MODE and settings.USE_FAKE_FXA_AUTH


class AMOJSONEncoder(JSONEncoder):
Expand Down
2 changes: 1 addition & 1 deletion src/olympia/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_versioned_api_routes(version, url_patterns):
routes = url_patterns

# For now, this feature is only enabled in dev mode
if settings.DEBUG:
if settings.DEV_MODE:
routes.extend(
[
re_path(
Expand Down
3 changes: 3 additions & 0 deletions src/olympia/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def static_check(app_configs, **kwargs):
errors = []
output = StringIO()

if not settings.DEV_MODE:
return []

try:
call_command('compress_assets', dry_run=True, stdout=output)
file_paths = output.getvalue().strip().split('\n')
Expand Down
4 changes: 2 additions & 2 deletions src/olympia/landfill/management/commands/fetch_prod_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
if not settings.DEBUG:
if not settings.DEV_MODE:
raise CommandError(
'As a safety precaution this command only works if DEBUG=True.'
'As a safety precaution this command only works in DEV_MODE.'
)
self.fetch_addon_data(options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
if not settings.DEBUG:
if not settings.DEV_MODE:
raise CommandError(
'As a safety precaution this command only works if DEBUG=True.'
'As a safety precaution this command only works in DEV_MODE.'
)
self.options = options
self.fetch_versions_data()
Expand Down
6 changes: 2 additions & 4 deletions src/olympia/landfill/management/commands/generate_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def add_arguments(self, parser):
)

def handle(self, *args, **kwargs):
if not settings.DEBUG:
raise CommandError(
'You can only run this command with your DEBUG setting set to True.'
)
if not settings.DEV_MODE:
raise CommandError('You can only run this command in DEV_MODE.')

num = int(kwargs.get('num'))
email = kwargs.get('email')
Expand Down
6 changes: 2 additions & 4 deletions src/olympia/landfill/management/commands/generate_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ def add_arguments(self, parser):
)

def handle(self, *args, **kwargs):
if not settings.DEBUG:
raise CommandError(
'You can only run this command with your DEBUG setting set to True.'
)
if not settings.DEV_MODE:
raise CommandError('You can only run this command in DEV_MODE.')
num = int(kwargs.get('num'))
email = kwargs.get('email')

Expand Down
2 changes: 1 addition & 1 deletion src/olympia/lib/jingo_minify_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_js_urls(bundle, debug=None):
bundle.
"""
if debug is None:
debug = settings.DEBUG
debug = settings.DEV_MODE

if debug:
return [static(item) for item in settings.MINIFY_BUNDLES['js'][bundle]]
Expand Down
7 changes: 7 additions & 0 deletions src/olympia/lib/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def path(*folders):

DEBUG = env('DEBUG', default=False)

# Do NOT provide a default value, this should be explicitly
# set during the docker image build. If it is not set,
# we want to raise an error.
DOCKER_TARGET = env('DOCKER_TARGET')

DEV_MODE = DOCKER_TARGET != 'production'

DEBUG_TOOLBAR_CONFIG = {
# Deactivate django debug toolbar by default.
'SHOW_TOOLBAR_CALLBACK': lambda request: DEBUG,
Expand Down
2 changes: 1 addition & 1 deletion src/olympia/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
),
]

if settings.DEBUG:
if settings.DEV_MODE:
from django.contrib.staticfiles.views import serve as static_serve

def serve_static_files(request, path, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion src/olympia/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def sync_suppressed_emails_task(batch_size=BATCH_SIZE, **kw):
task_log.info(f'Downloaded suppression list of {size_in_mb:.2f} MB.')

with tempfile.NamedTemporaryFile(
dir=settings.TMP_PATH, delete=not settings.DEBUG, mode='w+b'
dir=settings.TMP_PATH, delete=not settings.DEV_MODE, mode='w+b'
) as csv_file:
csv_file.write(response.content)
csv_file.seek(0)
Expand Down

0 comments on commit d3c1ba6

Please sign in to comment.