command.py 6.8 KB

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