Skip to content

Commit 689acee

Browse files
authored
bases: support creating charms use CentOS 7 as base (#1065)
needs Ubuntu 18.04 for cgroup v1 needs craft-providers unreleased version. CRAFT-1697
1 parent 9816b60 commit 689acee

File tree

10 files changed

+278
-65
lines changed

10 files changed

+278
-65
lines changed

charmcraft/commands/build.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from typing import List, Optional
2626

2727
from craft_cli import emit, CraftError
28+
from craft_providers.bases import get_base_alias
2829

2930
from charmcraft import env, linters, parts, providers, instrum
3031
from charmcraft.charm_builder import DISPATCH_FILENAME, HOOKS_DIR
@@ -344,7 +345,7 @@ def pack_charm_in_instance(
344345
"(may take a while the first time but it's reusable)"
345346
)
346347

347-
build_base = providers.BASE_CHANNEL_TO_PROVIDER_BASE[build_on.channel]
348+
build_base_alias = get_base_alias((build_on.name, build_on.channel))
348349
instance_name = providers.get_instance_name(
349350
bases_index=bases_index,
350351
build_on_index=build_on_index,
@@ -353,16 +354,24 @@ def pack_charm_in_instance(
353354
target_arch=get_host_architecture(),
354355
)
355356
base_configuration = providers.get_base_configuration(
356-
alias=build_base,
357+
alias=build_base_alias,
357358
instance_name=instance_name,
358359
)
359360

361+
if build_on.name != "ubuntu":
362+
allow_unstable = True
363+
emit.message(
364+
f"Warning: Base {build_on.name} {build_on.channel} daily image may be unstable."
365+
)
366+
else:
367+
allow_unstable = False
368+
360369
with self.provider.launched_environment(
361370
project_name=self.metadata.name,
362371
project_path=self.charmdir,
363372
base_configuration=base_configuration,
364-
build_base=build_base.value,
365373
instance_name=instance_name,
374+
allow_unstable=allow_unstable,
366375
) as instance:
367376
emit.debug("Mounting directory inside the instance")
368377
with instrum.Timer("Mounting directory"):

charmcraft/parts.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pathlib
2121
import shlex
2222
import sys
23-
from typing import Any, Dict, List, Set, Optional, cast
23+
from typing import Any, Dict, List, Optional, Set, cast
2424
from contextlib import suppress
2525

2626
import pydantic
@@ -166,6 +166,13 @@ def get_build_packages(self) -> Set[str]:
166166
if (os_release.id(), os_release.version_id()) in (("centos", "7"), ("rhel", "7")):
167167
# CentOS 7 Python 3.8 from SCL repo
168168
return {
169+
"autoconf",
170+
"automake",
171+
"gcc",
172+
"gcc-c++",
173+
"git",
174+
"make",
175+
"patch",
169176
"rh-python38-python-devel",
170177
"rh-python38-python-pip",
171178
"rh-python38-python-setuptools",
@@ -175,6 +182,13 @@ def get_build_packages(self) -> Set[str]:
175182
pass
176183

177184
return {
185+
"autoconf",
186+
"automake",
187+
"gcc",
188+
"gcc-c++",
189+
"git",
190+
"make",
191+
"patch",
178192
"python3-devel",
179193
"python3-pip",
180194
"python3-setuptools",

charmcraft/providers.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616

1717
"""Charmcraft-specific code to interface with craft-providers."""
1818

19+
import enum
1920
import os
2021
import pathlib
2122
import sys
2223
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
2324

2425
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
2630

2731
from charmcraft.bases import check_if_base_matches_host
28-
from charmcraft.config import Base, BasesConfiguration
32+
from charmcraft.config import BasesConfiguration
2933
from charmcraft.env import (
3034
get_managed_environment_snap_channel,
3135
get_managed_environment_log_path,
@@ -36,13 +40,6 @@
3640
from charmcraft.snap import get_snap_configuration
3741

3842

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-
4643
class Plan(NamedTuple):
4744
"""A build plan for a particular base.
4845
@@ -122,9 +119,9 @@ def create_build_plan(
122119
return build_plan
123120

124121

125-
def get_command_environment() -> Dict[str, str]:
122+
def get_command_environment(base: Base) -> Dict[str, str]:
126123
"""Construct the required environment."""
127-
env = bases.buildd.default_command_environment()
124+
env = base.default_command_environment()
128125
env["CHARMCRAFT_MANAGED_MODE"] = "1"
129126

130127
# Pass-through host environment that target may need.
@@ -171,25 +168,25 @@ def get_instance_name(
171168

172169
def get_base_configuration(
173170
*,
174-
alias: bases.BuilddBaseAlias,
171+
alias: enum.Enum,
175172
instance_name: str,
176-
) -> bases.BuilddBase:
177-
"""Create a BuilddBase configuration."""
178-
environment = get_command_environment()
179-
173+
) -> Base:
174+
"""Create a Base configuration."""
180175
# injecting a snap on a non-linux system is not supported, so default to
181176
# install charmcraft from the store's stable channel
182177
snap_channel = get_managed_environment_snap_channel()
183178
if snap_channel is None and sys.platform != "linux":
184179
snap_channel = "stable"
185180

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(
188185
alias=alias,
189186
environment=environment,
190187
hostname=instance_name,
191188
snaps=[charmcraft_snap],
192-
compatibility_tag=f"charmcraft-{bases.BuilddBase.compatibility_tag}.0",
189+
compatibility_tag=f"charmcraft-{base.compatibility_tag}.0",
193190
)
194191

195192

@@ -244,18 +241,21 @@ def is_base_available(base: Base) -> Tuple[bool, Union[str, None]]:
244241
f"host architecture {arch!r} not in base architectures {base.architectures!r}",
245242
)
246243

247-
if base.name != "ubuntu":
244+
if base.name not in ("ubuntu", "centos"):
248245
return (
249246
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')",
251248
)
252249

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))
255254
allowed = f"{', '.join(map(repr, firsts))} or {last!r}"
256255
return (
257256
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})",
259259
)
260260

261261
return True, None

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ colorama==0.4.6
1010
coverage==7.2.3
1111
craft-cli==1.2.0
1212
craft-parts==1.19.1
13-
craft-providers==1.10.0
13+
craft-providers==1.11.0
1414
craft-store==2.4.0
1515
cryptography==40.0.2
1616
Deprecated==1.2.13

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cffi==1.15.1
44
charset-normalizer==3.1.0
55
craft-cli==1.2.0
66
craft-parts==1.19.1
7-
craft-providers==1.10.0
7+
craft-providers==1.11.0
88
craft-store==2.4.0
99
cryptography==40.0.2
1010
Deprecated==1.2.13

0 commit comments

Comments
 (0)