command.py 6.1 KB

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