|
16 | 16 |
|
17 | 17 | """Charmcraft-specific code to interface with craft-providers.""" |
18 | 18 |
|
| 19 | +import enum |
19 | 20 | import os |
20 | 21 | import pathlib |
21 | 22 | import sys |
22 | 23 | from typing import Dict, List, NamedTuple, Optional, Tuple, Union |
23 | 24 |
|
24 | 25 | from craft_cli import emit, CraftError |
25 | | -from craft_providers import Executor, Provider, ProviderError, bases, lxd, multipass |
| 26 | +from craft_providers import Base, Executor, Provider, ProviderError, lxd, multipass |
| 27 | +from craft_providers.bases import get_base_alias, get_base_from_alias, BASE_NAME_TO_BASE_ALIAS |
| 28 | +from craft_providers.errors import BaseConfigurationError |
| 29 | +from craft_providers.actions.snap_installer import Snap |
26 | 30 |
|
27 | 31 | from charmcraft.bases import check_if_base_matches_host |
28 | | -from charmcraft.config import Base, BasesConfiguration |
| 32 | +from charmcraft.config import BasesConfiguration |
29 | 33 | from charmcraft.env import ( |
30 | 34 | get_managed_environment_snap_channel, |
31 | 35 | get_managed_environment_log_path, |
|
36 | 40 | from charmcraft.snap import get_snap_configuration |
37 | 41 |
|
38 | 42 |
|
39 | | -BASE_CHANNEL_TO_PROVIDER_BASE = { |
40 | | - "18.04": bases.BuilddBaseAlias.BIONIC, |
41 | | - "20.04": bases.BuilddBaseAlias.FOCAL, |
42 | | - "22.04": bases.BuilddBaseAlias.JAMMY, |
43 | | -} |
44 | | - |
45 | | - |
46 | 43 | class Plan(NamedTuple): |
47 | 44 | """A build plan for a particular base. |
48 | 45 |
|
@@ -122,9 +119,9 @@ def create_build_plan( |
122 | 119 | return build_plan |
123 | 120 |
|
124 | 121 |
|
125 | | -def get_command_environment() -> Dict[str, str]: |
| 122 | +def get_command_environment(base: Base) -> Dict[str, str]: |
126 | 123 | """Construct the required environment.""" |
127 | | - env = bases.buildd.default_command_environment() |
| 124 | + env = base.default_command_environment() |
128 | 125 | env["CHARMCRAFT_MANAGED_MODE"] = "1" |
129 | 126 |
|
130 | 127 | # Pass-through host environment that target may need. |
@@ -171,25 +168,25 @@ def get_instance_name( |
171 | 168 |
|
172 | 169 | def get_base_configuration( |
173 | 170 | *, |
174 | | - alias: bases.BuilddBaseAlias, |
| 171 | + alias: enum.Enum, |
175 | 172 | instance_name: str, |
176 | | -) -> bases.BuilddBase: |
177 | | - """Create a BuilddBase configuration.""" |
178 | | - environment = get_command_environment() |
179 | | - |
| 173 | +) -> Base: |
| 174 | + """Create a Base configuration.""" |
180 | 175 | # injecting a snap on a non-linux system is not supported, so default to |
181 | 176 | # install charmcraft from the store's stable channel |
182 | 177 | snap_channel = get_managed_environment_snap_channel() |
183 | 178 | if snap_channel is None and sys.platform != "linux": |
184 | 179 | snap_channel = "stable" |
185 | 180 |
|
186 | | - charmcraft_snap = bases.buildd.Snap(name="charmcraft", channel=snap_channel, classic=True) |
187 | | - return bases.BuilddBase( |
| 181 | + base = get_base_from_alias(alias) |
| 182 | + charmcraft_snap = Snap(name="charmcraft", channel=snap_channel, classic=True) |
| 183 | + environment = get_command_environment(base) |
| 184 | + return base( |
188 | 185 | alias=alias, |
189 | 186 | environment=environment, |
190 | 187 | hostname=instance_name, |
191 | 188 | snaps=[charmcraft_snap], |
192 | | - compatibility_tag=f"charmcraft-{bases.BuilddBase.compatibility_tag}.0", |
| 189 | + compatibility_tag=f"charmcraft-{base.compatibility_tag}.0", |
193 | 190 | ) |
194 | 191 |
|
195 | 192 |
|
@@ -244,18 +241,21 @@ def is_base_available(base: Base) -> Tuple[bool, Union[str, None]]: |
244 | 241 | f"host architecture {arch!r} not in base architectures {base.architectures!r}", |
245 | 242 | ) |
246 | 243 |
|
247 | | - if base.name != "ubuntu": |
| 244 | + if base.name not in ("ubuntu", "centos"): |
248 | 245 | return ( |
249 | 246 | False, |
250 | | - f"name {base.name!r} is not yet supported (must be 'ubuntu')", |
| 247 | + f"name {base.name!r} is not yet supported (must be 'ubuntu' or 'centos')", |
251 | 248 | ) |
252 | 249 |
|
253 | | - if base.channel not in BASE_CHANNEL_TO_PROVIDER_BASE: |
254 | | - *firsts, last = sorted(BASE_CHANNEL_TO_PROVIDER_BASE) |
| 250 | + try: |
| 251 | + get_base_alias((base.name, base.channel)) |
| 252 | + except BaseConfigurationError: |
| 253 | + *firsts, last = sorted((" ".join(s) for s in BASE_NAME_TO_BASE_ALIAS)) |
255 | 254 | allowed = f"{', '.join(map(repr, firsts))} or {last!r}" |
256 | 255 | return ( |
257 | 256 | False, |
258 | | - f"channel {base.channel!r} is not yet supported (must be {allowed})", |
| 257 | + f"base {base.name!r} channel {base.channel!r} is not yet supported " |
| 258 | + f"(must be {allowed})", |
259 | 259 | ) |
260 | 260 |
|
261 | 261 | return True, None |
|
0 commit comments