Skip to content

Commit

Permalink
Move /deps/node_modules to /data/olympia/node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMind committed Dec 20, 2024
1 parent 03e0124 commit 137bf20
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 34 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ ENV PIP_CACHE_DIR=/deps/cache/
ENV PIP_SRC=/deps/src/
ENV PYTHONUSERBASE=/deps
ENV PATH=$PYTHONUSERBASE/bin:$PATH
ENV NPM_CONFIG_PREFIX=/deps/
ENV NPM_CACHE_DIR=/deps/cache/npm
ENV NPM_DEBUG=true
# Set python path to the project root and src to resolve olympia modules correctly
ENV PYTHONPATH=${HOME}:${HOME}/src

ENV PIP_COMMAND="python3 -m pip"
ENV NPM_ARGS="--prefix ${NPM_CONFIG_PREFIX} --cache ${NPM_CACHE_DIR} --loglevel verbose"
ENV NPM_ARGS="--cache ${NPM_CACHE_DIR} --loglevel verbose"

# All we need in "base" is pip to be installed
#this let's other layers install packages using the correct version.
Expand Down Expand Up @@ -135,8 +134,8 @@ RUN \
# Files required to install pip dependencies
--mount=type=bind,source=./requirements/prod.txt,target=${HOME}/requirements/prod.txt \
# Files required to install npm dependencies
--mount=type=bind,source=package.json,target=/deps/package.json \
--mount=type=bind,source=package-lock.json,target=/deps/package-lock.json \
--mount=type=bind,source=package.json,target=${HOME}/package.json \
--mount=type=bind,source=package-lock.json,target=${HOME}/package-lock.json \
# Mounts for caching dependencies
--mount=type=cache,target=${PIP_CACHE_DIR},uid=${OLYMPIA_UID},gid=${OLYMPIA_UID} \
--mount=type=cache,target=${NPM_CACHE_DIR},uid=${OLYMPIA_UID},gid=${OLYMPIA_UID} \
Expand Down
10 changes: 4 additions & 6 deletions Makefile-docker
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export PYTHON_COMMAND=python3
export PIP_COMMAND=$(PYTHON_COMMAND) -m pip
APP=src/olympia/

NODE_MODULES := $(NPM_CONFIG_PREFIX)node_modules/

REQUIRED_FILES := \
Makefile \
Makefile-os \
Expand Down Expand Up @@ -94,7 +92,7 @@ setup-ui-tests:
lint: ## lint the code
ruff check .
ruff format --check .
NODE_PATH=$(NODE_MODULES) npm exec $(NPM_ARGS) -- prettier --check '**'
npm exec $(NPM_ARGS) -- prettier --check '**'
curlylint src/

lint-codestyle: lint
Expand Down Expand Up @@ -199,15 +197,15 @@ test_failed: ## rerun the failed tests from the previous run

.PHONY: run_js_tests
run_js_tests: ## Run the JavaScript test suite (requires compiled/compressed assets).
NODE_PATH=$(NODE_MODULES) npm exec $(NPM_ARGS) -- jest tests/js
npm exec $(NPM_ARGS) -- jest tests/js

.PHONY: watch_js_tests
watch_js_tests: ## Run+watch the JavaScript test suite (requires compiled/compressed assets).
NODE_PATH=$(NODE_MODULES) npm exec $(NPM_ARGS) -- jest --watch
npm exec $(NPM_ARGS) -- jest --watch

.PHONY: format
format: ## Autoformat our codebase.
NODE_PATH=$(NODE_MODULES) npm exec $(NPM_ARGS) -- prettier --write '**'
npm exec $(NPM_ARGS) -- prettier --write '**'
ruff check --fix-only .
ruff format .

Expand Down
4 changes: 1 addition & 3 deletions docs/topics/development/dependency_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ Ensure to comment in the requirements file above transitive dependencies which d

## Node.js Dependencies

Node.js dependencies are managed using npm. Similar to Python dependencies, Node.js dependencies are installed into the `/deps` directory.

- **Environment Variables**: Environment variables are set for Node.js CLIs to ensure that dependencies are installed in the `/deps` directory. This includes setting paths for `NPM_CONFIG_PREFIX` and `NPM_CACHE_DIR`.
Node.js dependencies are managed using npm.

- **Caching Mechanism**: Node.js dependencies are also cached using Docker build stages. Internal npm cache folders are cached to avoid re-downloading packages unnecessarily.

Expand Down
22 changes: 7 additions & 15 deletions scripts/install_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import sys


def copy_package_json():
"""Copy package.json files to deps directory if they exist."""
try:
shutil.copy('/data/olympia/package.json', '/deps')
shutil.copy('/data/olympia/package-lock.json', '/deps')
except (IOError, OSError):
pass # Ignore if files don't exist or can't be copied

def clean_dir(dir_path, filter):
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path) and item not in filter:
shutil.rmtree(item_path)

def main(targets):
# Constants
Expand All @@ -38,16 +35,11 @@ def main(targets):
# installed or in the host ./deps directory before running this script
if 'local' not in DOCKER_TAG or OLYMPIA_DEPS == 'production':
print('Removing existing deps')
for item in os.listdir('/deps'):
item_path = os.path.join('/deps', item)
if os.path.isdir(item_path) and item != 'cache':
shutil.rmtree(item_path)
clean_dir('/deps', ['cache'])
clean_dir('/data/olympia/node_modules', [])
else:
print('Updating existing deps')

# Copy package.json files
copy_package_json()

# Prepare the includes lists
pip_includes = []
npm_includes = []
Expand Down
12 changes: 6 additions & 6 deletions src/olympia/lib/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ def path(*folders):
# LESS CSS OPTIONS (Debug only).
LESS_PREPROCESS = True # Compile LESS with Node, rather than client-side JS?
LESS_LIVE_REFRESH = False # Refresh the CSS on save?
LESS_BIN = env('LESS_BIN', default='/deps/node_modules/less/bin/lessc')
LESS_BIN = env('LESS_BIN', default=path('node_modules/less/bin/lessc'))

# Path to cleancss (our CSS minifier).
CLEANCSS_BIN = env(
'CLEANCSS_BIN', default='/deps/node_modules/clean-css-cli/bin/cleancss'
'CLEANCSS_BIN', default=path('node_modules/clean-css-cli/bin/cleancss')
)

# Path to our JS minifier.
JS_MINIFIER_BIN = env('JS_MINIFIER_BIN', default='/deps/node_modules/terser/bin/terser')
JS_MINIFIER_BIN = env('JS_MINIFIER_BIN', default=path('node_modules/terser/bin/terser'))

# rsvg-convert is used to save our svg static theme previews to png
RSVG_CONVERT_BIN = env('RSVG_CONVERT_BIN', default='rsvg-convert')
Expand All @@ -116,7 +116,7 @@ def path(*folders):

# Path to our addons-linter binary
ADDONS_LINTER_BIN = env(
'ADDONS_LINTER_BIN', default='/deps/node_modules/addons-linter/bin/addons-linter'
'ADDONS_LINTER_BIN', default=path('node_modules/addons-linter/bin/addons-linter')
)
# --enable-background-service-worker linter flag value
ADDONS_LINTER_ENABLE_SERVICE_WORKER = False
Expand Down Expand Up @@ -1328,8 +1328,8 @@ def read_only_mode(env):
'django_node_assets.finders.NodeModulesFinder',
)

NODE_MODULES_ROOT = os.path.join('/', 'deps', 'node_modules')
NODE_PACKAGE_JSON = os.path.join('/', 'deps', 'package.json')
NODE_MODULES_ROOT = path('node_modules')
NODE_PACKAGE_JSON = path('package.json')
NODE_PACKAGE_MANAGER_INSTALL_OPTIONS = ['--dry-run']

STATIC_BUILD_PATH = os.path.join('/', 'data', 'olympia', 'static-build')
Expand Down

0 comments on commit 137bf20

Please sign in to comment.