| 58 | |
| 59 | |
| 60 | class ComposeConfig: |
| 61 | |
| 62 | def __init__(self, config_path, dotenv_path, compose_bin, |
| 63 | using_docker=False, using_buildx=False, |
| 64 | params=None, debug=False): |
| 65 | self.using_docker = using_docker |
| 66 | self.using_buildx = using_buildx |
| 67 | self.debug = debug |
| 68 | config_path = _ensure_path(config_path) |
| 69 | if dotenv_path: |
| 70 | dotenv_path = _ensure_path(dotenv_path) |
| 71 | else: |
| 72 | dotenv_path = config_path.parent / '.env' |
| 73 | if self.debug: |
| 74 | # Log docker version |
| 75 | Docker().run('version') |
| 76 | |
| 77 | self._read_env(dotenv_path, params) |
| 78 | self._read_config(config_path, compose_bin) |
| 79 | |
| 80 | def _read_env(self, dotenv_path, params): |
| 81 | """ |
| 82 | Read .env and merge it with explicitly passed parameters. |
| 83 | """ |
| 84 | self.dotenv = dotenv_values(str(dotenv_path)) |
| 85 | if params is None: |
| 86 | self.params = {} |
| 87 | else: |
| 88 | self.params = {k: v for k, v in params.items() if k in self.dotenv} |
| 89 | |
| 90 | # forward the process' environment variables |
| 91 | self.env = os.environ.copy() |
| 92 | # set the defaults from the dotenv files |
| 93 | self.env.update(self.dotenv) |
| 94 | # override the defaults passed as parameters |
| 95 | self.env.update(self.params) |
| 96 | |
| 97 | # translate docker's architecture notation to a more widely used one |
| 98 | arch = self.env.get('ARCH', 'amd64') |
| 99 | self.env['ARCH_ALIAS'] = _arch_alias_mapping.get(arch, arch) |
| 100 | self.env['ARCH_SHORT'] = _arch_short_mapping.get(arch, arch) |
| 101 | |
| 102 | def _read_config(self, config_path, compose_bin): |
| 103 | """ |
| 104 | Validate and read the compose.yaml |
| 105 | """ |
| 106 | yaml = YAML() |
| 107 | with config_path.open() as fp: |
| 108 | self.raw_config = yaml.load(fp) |
| 109 | |
| 110 | services = self.raw_config['services'].keys() |
| 111 | self.hierarchy = dict(flatten(self.raw_config.get('x-hierarchy', {}))) |
| 112 | self.limit_presets = self.raw_config.get('x-limit-presets', {}) |
| 113 | self.with_gpus = self.raw_config.get('x-with-gpus', []) |
| 114 | nodes = self.hierarchy.keys() |
| 115 | errors = [] |
| 116 | |
| 117 | for name in self.with_gpus: |