1
0

command.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. enabled_profiles=get_profiles_from_options(options, environment)
  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_profiles_from_options(options, environment):
  102. profile_option = options.get('--profile')
  103. if profile_option:
  104. return profile_option
  105. profiles = environment.get('COMPOSE_PROFILE')
  106. if profiles:
  107. return profiles.split(',')
  108. return []
  109. def get_project(project_dir, config_path=None, project_name=None, verbose=False,
  110. context=None, environment=None, override_dir=None,
  111. interpolate=True, environment_file=None, enabled_profiles=None):
  112. if not environment:
  113. environment = Environment.from_env_file(project_dir)
  114. config_details = config.find(project_dir, config_path, environment, override_dir)
  115. project_name = get_project_name(
  116. config_details.working_dir, project_name, environment
  117. )
  118. config_data = config.load(config_details, interpolate)
  119. api_version = environment.get('COMPOSE_API_VERSION')
  120. client = get_client(
  121. verbose=verbose, version=api_version, context=context, environment=environment
  122. )
  123. with errors.handle_connection_errors(client):
  124. return Project.from_config(
  125. project_name,
  126. config_data,
  127. client,
  128. environment.get('DOCKER_DEFAULT_PLATFORM'),
  129. execution_context_labels(config_details, environment_file),
  130. enabled_profiles,
  131. )
  132. def execution_context_labels(config_details, environment_file):
  133. extra_labels = [
  134. '{}={}'.format(LABEL_WORKING_DIR, os.path.abspath(config_details.working_dir))
  135. ]
  136. if not use_config_from_stdin(config_details):
  137. extra_labels.append('{}={}'.format(LABEL_CONFIG_FILES, config_files_label(config_details)))
  138. if environment_file is not None:
  139. extra_labels.append('{}={}'.format(
  140. LABEL_ENVIRONMENT_FILE,
  141. os.path.normpath(environment_file))
  142. )
  143. return extra_labels
  144. def use_config_from_stdin(config_details):
  145. for c in config_details.config_files:
  146. if not c.filename:
  147. return True
  148. return False
  149. def config_files_label(config_details):
  150. return ",".join(
  151. os.path.normpath(c.filename) for c in config_details.config_files
  152. )
  153. def get_project_name(working_dir, project_name=None, environment=None):
  154. def normalize_name(name):
  155. return re.sub(r'[^-_a-z0-9]', '', name.lower())
  156. if not environment:
  157. environment = Environment.from_env_file(working_dir)
  158. project_name = project_name or environment.get('COMPOSE_PROJECT_NAME')
  159. if project_name:
  160. return normalize_name(project_name)
  161. project = os.path.basename(os.path.abspath(working_dir))
  162. if project:
  163. return normalize_name(project)
  164. return 'default'