Skip to content

Commit

Permalink
Speed up charm builds dropping charmcraft lxc
Browse files Browse the repository at this point in the history
* reactive charm builds are performed by jenkins runners
* charmcraft charm builds are performed by launchpad
* other charm builds fail encouraging a move to launchpad
* reactive layers are only pulled when needed (after filtering)
  • Loading branch information
addyess committed Aug 7, 2024
1 parent daecbbb commit fe80dc9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 171 deletions.
10 changes: 0 additions & 10 deletions jobs/build-charms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@
#!/bin/bash
set -eux
. $WORKSPACE/jobs/build-charms/charmcraft-lib.sh
sudo chown -R jenkins:jenkins /var/lib/jenkins/.config/ || true
IS_FORCE=""
Expand All @@ -126,14 +124,6 @@
rm -rf "$WORKSPACE/charms" || true
# Cleanup old charmcraft containers
ci_lxc_delete "$JOB_NAME"
# Configure cleanup routine for exit
export charmcraft_lxc="$JOB_NAME-$BUILD_NUMBER"
trap 'ci_lxc_delete $charmcraft_lxc' EXIT
ci_charmcraft_launch $charmcraft_lxc
set +e
EXIT_STATUS=0
Expand Down
32 changes: 11 additions & 21 deletions jobs/build-charms/builder_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ def __init__(self, build_type):
)
if response and "Item" in response:
self.db = response["Item"]
self.db["pull_layer_manifest"] = None

def clean(self):
for each in self.clean_dirs:
Expand Down Expand Up @@ -602,7 +603,10 @@ def download(self, layer_name):
return layer_manifest

def pull_layers(self):
"""Clone all downstream layers to be processed locally when doing charm builds."""
"""Clone all downstream layers to be processed locally when doing local charm builds."""
layers = self.db.get("pull_layer_manifest")
if layers is not None:
return layers
layers_to_pull = [
layer_name
for layer in self.layers
Expand All @@ -611,8 +615,8 @@ def pull_layers(self):
]
pool = ThreadPool()
results = pool.map(self.download, layers_to_pull)

self.db["pull_layer_manifest"] = list(results)
self.db["pull_layer_manifest"] = layers = list(results)
return layers


@unique
Expand Down Expand Up @@ -831,8 +835,7 @@ def version_identification(self, source):
version_id = [{"rev": version, "url": self.entity}] if version else None
elif self.reactive and source == "local":
version_id = [
{k: curr[k] for k in comparisons}
for curr in self.build.db["pull_layer_manifest"]
{k: curr[k] for k in comparisons} for curr in self.build.pull_layers()
]

# Check the current git cloned charm repo commit and add that to
Expand Down Expand Up @@ -915,7 +918,6 @@ def within_channel_bounds(self, to_channels):

def charm_build(self):
"""Perform a build against charm/bundle."""
lxc = os.environ.get("charmcraft_lxc")
ret = SimpleNamespace(ok=False)
charm_path = Path(self.src_path, f"{self.name}.charm")
if "override-build" in self.opts:
Expand All @@ -927,6 +929,7 @@ def charm_build(self):
echo=self.echo,
)
elif self.reactive:
self.build.pull_layers()
supported_architectures = (
self.opts.get("architectures") or K8S_CHARM_SUPPORT_ARCHES
)
Expand All @@ -942,21 +945,8 @@ def charm_build(self):
ret.ok = True
except sh.ErrorReturnCode as e:
self.echo(e.stderr)
elif lxc:
msg = f"Consider moving {self.name} to launchpad builder"
border = "-".join("=" * (len(msg) // 2 + 1))
self.echo("\n".join((border, msg, border)))
self.echo(f"Building in container {lxc}.")
repository = f"https://github.com/{self.downstream}"
charmcraft_script = (
"#!/bin/bash -eux\n"
f"source {Path(__file__).parent / 'charmcraft-lib.sh'}\n"
f"ci_charmcraft_pack {lxc} {repository} {self.branch} {self.opts.get('subdir', '')}\n"
f"ci_charmcraft_copy {lxc} {charm_path}\n"
)
ret = script(charmcraft_script, echo=self.echo)
else:
self.echo("No 'charmcraft_lxc' container available")
self.echo("charmcraft pack should be performed on launchpad builder")

if not ret.ok:
self.echo("Failed to build, aborting")
Expand Down Expand Up @@ -990,7 +980,7 @@ def push(self, artifact: Artifact):
self.tag(f"{self.name}-{artifact.rev}")

def tag(self, tag: str) -> bool:
"""Tag current commit in this repo with a lightweigh tag."""
"""Tag current commit in this repo with a lightweight tag."""
tagged = False
current_sha = self.commit()
if repo := self.repository:
Expand Down
47 changes: 0 additions & 47 deletions jobs/build-charms/charmcraft-build.sh

This file was deleted.

76 changes: 0 additions & 76 deletions jobs/build-charms/charmcraft-lib.sh

This file was deleted.

1 change: 0 additions & 1 deletion jobs/build-charms/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def build(
"force": force,
}
build_env.clean()
build_env.pull_layers()

entities = []
for charm_map in build_env.job_list:
Expand Down
19 changes: 3 additions & 16 deletions tests/unit/build_charms/test_charms.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,12 @@ def test_build_entity_charm_build(

# Operator Charms, build with charmcraft container
charm_entity.reactive = False
os.environ["charmcraft_lxc"] = "unnamed-job-0"
charm_entity.artifacts = []
charm_entity.charm_build()
charm_cmd.build.assert_not_called()
mock_script.assert_called_once_with(
"#!/bin/bash -eux\n"
f"source {CHARMCRAFT_LIB_SH}\n"
f"ci_charmcraft_pack unnamed-job-0 https://github.com/{charm_entity.downstream} main \n"
f"ci_charmcraft_copy unnamed-job-0 {K8S_CI_CHARM}/k8s-ci-charm.charm\n",
echo=charm_entity.echo,
)
mock_script.reset_mock()

# Operator Charms, fail build without charmcraft container
del os.environ["charmcraft_lxc"]
with pytest.raises(builder_local.BuildException):
charm_entity.artifacts = []
charm_entity.charm_build()
charm_cmd.build.assert_not_called()
mock_script.assert_not_called()
mock_script.reset_mock()


def test_build_entity_upload(
Expand Down Expand Up @@ -706,7 +694,6 @@ def test_build_command(mock_build_env, mock_build_entity, main):
"to_channel": "edge",
"force": True,
}
mock_build_env.pull_layers.assert_called_once_with()
mock_build_env.save.assert_called_once_with()

entity.echo.assert_has_calls(
Expand Down

0 comments on commit fe80dc9

Please sign in to comment.