command.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import logging
  2. import os
  3. import re
  4. from . import errors
  5. from .. import config
  6. from .. import parallel
  7. from ..config.environment import Environment
  8. from ..const import LABEL_CONFIG_FILES
  9. from ..const import LABEL_ENVIRONMENT_FILE
  10. from ..const import LABEL_WORKING_DIR
  11. from ..project import Project
  12. from .docker_client import get_client
  13. from .docker_client import load_context
  14. from .docker_client import make_context
  15. from .errors import UserError
  16. log = logging.getLogger(__name__)
  17. SILENT_COMMANDS = {
  18. 'events',
  19. 'exec',
  20. 'kill',
  21. 'logs',
  22. 'pause',
  23. 'ps',
  24. 'restart',
  25. 'rm',
  26. 'start',
  27. 'stop',
  28. 'top',
  29. 'unpause',
  30. }
  31. def project_from_options(project_dir, options, additional_options=None):
  32. additional_options = additional_options or {}
  33. override_dir = options.get('--project-directory')
  34. environment_file = options.get('--env-file')
  35. environment = Environment.from_env_file(override_dir or project_dir, environment_file)
  36. environment.silent = options.get('COMMAND', None) in SILENT_COMMANDS
  37. set_parallel_limit(environment)
  38. # get the context for the run
  39. context = None
  40. context_name = options.get('--context', None)
  41. if context_name:
  42. context = load_context(context_name)
  43. if not context:
  44. raise UserError("Context '{}' not found".format(context_name))
  45. host = options.get('--host', None)
  46. if host is not None:
  47. if context:
  48. raise UserError(
  49. "-H, --host and -c, --context are mutually exclusive. Only one should be set.")
  50. host = host.lstrip('=')
  51. context = make_context(host, options, environment)
  52. return get_project(
  53. project_dir,
  54. get_config_path_from_options(project_dir, options, environment),
  55. project_name=options.get('--project-name'),
  56. verbose=options.get('--verbose'),
  57. context=context,
  58. environment=environment,
  59. override_dir=override_dir,
  60. interpolate=(not additional_options.get('--no-interpolate')),
  61. environment_file=environment_file
  62. )
  63. def set_parallel_limit(environment):
  64. parallel_limit = environment.get('COMPOSE_PARALLEL_LIMIT')
  65. if parallel_limit:
  66. try:
  67. parallel_limit = int(parallel_limit)
  68. except ValueError:
  69. raise errors.UserError(
  70. 'COMPOSE_PARALLEL_LIMIT must be an integer (found: "{}")'.format(
  71. environment.get('COMPOSE_PARALLEL_LIMIT')
  72. )
  73. )
  74. if parallel_limit <= 1:
  75. raise errors.UserError('COMPOSE_PARALLEL_LIMIT can not be less than 2')
  76. parallel.GlobalLimit.set_global_limit(parallel_limit)
  77. def get_config_from_options(base_dir, options, additional_options=None):
  78. additional_options = additional_options or {}
  79. override_dir = options.get('--project-directory')
  80. environment_file = options.get('--env-file')
  81. environment = Environment.from_env_file(override_dir or base_dir, environment_file)
  82. config_path = get_config_path_from_options(
  83. base_dir, options, environment
  84. )
  85. return config.load(
  86. config.find(base_dir, config_path, environment, override_dir),
  87. not additional_options.get('--no-interpolate')
  88. )
  89. def get_config_path_from_options(base_dir, options, environment):
  90. def unicode_paths(paths):
  91. return [p.decode('utf-8') if isinstance(p, bytes) else p for p in paths]
  92. file_option = options.get('--file')
  93. if file_option:
  94. return unicode_paths(file_option)
  95. config_files = environment.get('COMPOSE_FILE')
  96. if config_files:
  97. pathsep = environment.get('COMPOSE_PATH_SEPARATOR', os.pathsep)
  98. return unicode_paths(config_files.split(pathsep))
  99. return None
  100. def get_project(project_dir, config_path=None, project_name=None, verbose=False,
  101. context=None, environment=None, override_dir=None,
  102. interpolate=True, environment_file=None):
  103. if not environment:
  104. environment = Environment.from_env_file(project_dir)
  105. config_details = config.find(project_dir, config_path, environment, override_dir)
  106. project_name = get_project_name(
  107. config_details.working_dir, project_name, environment
  108. )
  109. config_data = config.load(config_details, interpolate)
  110. api_version = environment.get('COMPOSE_API_VERSION')
  111. client = get_client(
  112. verbose=verbose, version=api_version, context=context, environment=environment
  113. )
  114. with errors.handle_connection_errors(client):
  115. return Project.from_config(
  116. project_name,
  117. config_data,
  118. client,
  119. environment.get('DOCKER_DEFAULT_PLATFORM'),
  120. execution_context_labels(config_details, environment_file),
  121. )
  122. def execution_context_labels(config_details, environment_file):
  123. extra_labels = [
  124. '{}={}'.format(LABEL_WORKING_DIR, os.path.abspath(config_details.working_dir))
  125. ]
  126. if not use_config_from_stdin(config_details):
  127. extra_labels.append('{}={}'.format(LABEL_CONFIG_FILES, config_files_label(config_details)))
  128. if environment_file is not None:
  129. extra_labels.append('{}={}'.format(
  130. LABEL_ENVIRONMENT_FILE,
  131. os.path.normpath(environment_file))
  132. )
  133. return extra_labels
  134. def use_config_from_stdin(config_details):
  135. for c in config_details.config_files:
  136. if not c.filename:
  137. return True
  138. return False
  139. def config_files_label(config_details):
  140. return ",".join(
  141. os.path.normpath(c.filename) for c in config_details.config_files
  142. )
  143. def get_project_name(working_dir, project_name=None, environment=None):
  144. def normalize_name(name):
  145. return re.sub(r'[^-_a-z0-9]', '', name.lower())
  146. if not environment:
  147. environment = Environment.from_env_file(working_dir)
  148. project_name = project_name or environment.get('COMPOSE_PROJECT_NAME')
  149. if project_name:
  150. return normalize_name(project_name)
  151. project = os.path.basename(os.path.abspath(working_dir))
  152. if project:
  153. return normalize_name(project)
  154. return 'default'