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

Mount pip_cache_path in Docker container #3556

Merged
merged 3 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 38 additions & 14 deletions readthedocs/doc_builder/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from datetime import datetime

from readthedocs.core.utils import slugify
from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from docker import Client
from docker.utils import create_host_config
Expand Down Expand Up @@ -662,6 +663,42 @@ def get_client(self):
)
)

def get_container_host_config(self):
"""
Create the ``host_config`` settings for the container.

It mainly generates the proper path bindings between the Docker
container and the Host by mounting them with the proper permissions.
Besides, it mounts the ``GLOBAL_PIP_CACHE`` if it's set and we are under
``DEBUG``.

The object returned is passed to Docker function
``client.create_container``.
"""
binds = {
SPHINX_TEMPLATE_DIR: {
'bind': SPHINX_TEMPLATE_DIR,
'mode': 'ro',
},
MKDOCS_TEMPLATE_DIR: {
'bind': MKDOCS_TEMPLATE_DIR,
'mode': 'ro',
},
self.project.doc_path: {
'bind': self.project.doc_path,
'mode': 'rw',
},
}

if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
binds.update({
self.project.pip_cache_path: {
'bind': self.project.pip_cache_path,
'mode': 'rw',
}
})
return create_host_config(binds=binds)

@property
def container_id(self):
"""Return id of container if it is valid."""
Expand Down Expand Up @@ -715,20 +752,7 @@ def create_container(self):
exit=DOCKER_TIMEOUT_EXIT_CODE)),
name=self.container_id,
hostname=self.container_id,
host_config=create_host_config(binds={
SPHINX_TEMPLATE_DIR: {
'bind': SPHINX_TEMPLATE_DIR,
'mode': 'ro'
},
MKDOCS_TEMPLATE_DIR: {
'bind': MKDOCS_TEMPLATE_DIR,
'mode': 'ro'
},
self.project.doc_path: {
'bind': self.project.doc_path,
'mode': 'rw'
},
}),
host_config=self.get_container_host_config(),
detach=True,
environment=self.environment,
mem_limit=self.container_mem_limit,
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def checkout_path(self, version=LATEST):
@property
def pip_cache_path(self):
"""Path to pip cache."""
if getattr(settings, 'GLOBAL_PIP_CACHE', False):
if getattr(settings, 'GLOBAL_PIP_CACHE', False) and settings.DEBUG:
Copy link
Member

Choose a reason for hiding this comment

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

Is there a default GLOBAL_PIP_CACHE in dev.py?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. By default this feature is deactivated. It could be something like:

GLOBAL_PIP_CACHE = os.path.join(SITE_ROOT, '.cache', 'pip')

what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say leave deactivated so our development environments are close to production use.

return settings.GLOBAL_PIP_CACHE
return os.path.join(self.doc_path, '.cache', 'pip')

Expand Down