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

Introduce project-name in docker-compose.yml file (fixes #745) #5369

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions compose/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import errors
from . import verbose_proxy
from .. import config
from ..config import ConfigurationError
from ..config.environment import Environment
from ..const import API_VERSIONS
from ..project import Project
Expand Down Expand Up @@ -85,10 +86,11 @@ def get_project(project_dir, config_path=None, project_name=None, verbose=False,
if not environment:
environment = Environment.from_env_file(project_dir)
config_details = config.find(project_dir, config_path, environment, override_dir)
config_project_name = get_config_project_name(config_details.config_files)
config_data = config.load(config_details)
project_name = get_project_name(
config_details.working_dir, project_name, environment
config_details.working_dir, project_name, config_project_name, environment
)
config_data = config.load(config_details)

api_version = environment.get(
'COMPOSE_API_VERSION',
Expand All @@ -103,7 +105,21 @@ def get_project(project_dir, config_path=None, project_name=None, verbose=False,
return Project.from_config(project_name, config_data, client)


def get_project_name(working_dir, project_name=None, environment=None):
def get_config_project_name(config_files):
config_project_names = set([config_file.project_name
for config_file in config_files
if config_file.project_name])
if len(config_project_names) > 1:
raise ConfigurationError("project_name has multiple values: {0}"
.format(", ".join(str(e) for e in config_project_names)))
config_project_name = None
if len(config_project_names) > 0:
config_project_name = config_project_names.pop()

return config_project_name


def get_project_name(working_dir, project_name=None, config_project_name=None, environment=None):
def normalize_name(name):
return re.sub(r'[^a-z0-9]', '', name.lower())

Expand All @@ -113,6 +129,9 @@ def normalize_name(name):
if project_name:
return normalize_name(project_name)

if config_project_name:
return normalize_name(config_project_name)

project = os.path.basename(os.path.abspath(working_dir))
if project:
return normalize_name(project)
Expand Down
2 changes: 1 addition & 1 deletion compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class TopLevelCommand(object):
version Show the Docker-Compose version information
"""

def __init__(self, project, project_dir='.'):
def __init__(self, project):
self.project = project
self.project_dir = '.'

Expand Down
11 changes: 9 additions & 2 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def version(self):

return ComposeVersion(version)

@cached_property
def project_name(self):
return self.config['project_name'] if 'project_name' in self.config else None

def get_service(self, name):
return self.get_service_dicts()[name]

Expand All @@ -226,10 +230,12 @@ def get_configs(self):
return {} if self.version < const.COMPOSEFILE_V3_3 else self.config.get('configs', {})


class Config(namedtuple('_Config', 'version services volumes networks secrets configs')):
class Config(namedtuple('_Config', 'version project_name services volumes networks secrets configs')):
"""
:param version: configuration version
:type version: int
:param project_name: Project name
:type project_name: str
:param services: List of service description dictionaries
:type services: :class:`list`
:param volumes: Dictionary mapping volume names to description dictionaries
Expand Down Expand Up @@ -395,7 +401,8 @@ def load(config_details):

check_swarm_only_config(service_dicts)

return Config(main_file.version, service_dicts, volumes, networks, secrets, configs)
return Config(main_file.version, main_file.project_name, service_dicts, volumes, networks, secrets,
configs)


def load_mapping(config_files, get_func, entity_type, working_dir=None):
Expand Down