Skip to content

Commit

Permalink
cachi2: support clone only mode
Browse files Browse the repository at this point in the history
When users specify empty list as pkg_managers, only cloning should be
done. In this case usage of cachi2 must be skipped and required files
like env.json and bom.json must be created by OSBS.

Signed-off-by: Martin Basti <[email protected]>
  • Loading branch information
MartinBasti committed Nov 25, 2024
1 parent 2b7138a commit 6f130bd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
42 changes: 34 additions & 8 deletions atomic_reactor/plugins/cachi2_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from atomic_reactor.plugin import Plugin
from atomic_reactor.util import map_to_user_params
from atomic_reactor.utils.cachi2 import remote_source_to_cachi2
from atomic_reactor.utils.cachi2 import remote_source_to_cachi2, clone_only


class Cachi2InitPlugin(Plugin):
Expand Down Expand Up @@ -72,6 +72,27 @@ def run(self) -> Optional[List[Dict[str, Any]]]:

return processed_remote_sources

def clone_only_remote_source(self, remote_source_path: Path):
"""Step when OSBS only is doing resolution without cachi2.
Generate SBOM and env file for next plugins
"""
# generate empty SBOM
sbom_path = remote_source_path / "bom.json"
with open(sbom_path, "w") as f:
json.dump(
{
"bomFormat": "CycloneDX",
"components": [],
},
f
)

# generate empty envs
env_path = remote_source_path / "cachi2.env.json"
with open(env_path, "w") as f:
json.dump([], f)

def process_remote_sources(self) -> List[Dict[str, Any]]:
"""Process remote source requests and return information about the processed sources."""

Expand All @@ -97,13 +118,6 @@ def process_remote_sources(self) -> List[Dict[str, Any]]:

remote_source_data = remote_source["remote_source"]

self.write_cachi2_pkg_options(
remote_source_data,
source_path / CACHI2_PKG_OPTIONS_FILE)
self.write_cachi2_for_output_dir(
source_name,
source_path / CACHI2_FOR_OUTPUT_DIR_OPT_FILE)

source_path_app = source_path / CACHI2_BUILD_APP_DIR
source_path_app.mkdir()

Expand All @@ -113,6 +127,18 @@ def process_remote_sources(self) -> List[Dict[str, Any]]:
remote_source_data["ref"]
)

if clone_only(remote_source_data):
# OSBS is doing all work here
self.clone_only_remote_source(source_path)
else:
# write cachi2 files only when cachi2 should run
self.write_cachi2_pkg_options(
remote_source_data,
source_path / CACHI2_PKG_OPTIONS_FILE)
self.write_cachi2_for_output_dir(
source_name,
source_path / CACHI2_FOR_OUTPUT_DIR_OPT_FILE)

processed_remote_sources.append({
"source_path": str(source_path),
**remote_source,
Expand Down
11 changes: 11 additions & 0 deletions atomic_reactor/utils/cachi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,14 @@ def generate_request_json(
"packages": [], # this will be always empty cachi2 doesn't provide nested deps
}
return res


def clone_only(remote_source: Dict[str, Any]) -> bool:
"""Determine if only cloning is required without cachi2 run"""
if "pkg_managers" not in remote_source:
return False

if not remote_source["pkg_managers"]:
return True

return False
32 changes: 20 additions & 12 deletions tekton/tasks/binary-container-cachi2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ spec:
#!/usr/bin/bash
set -eux
CACHI2_DIR="$(workspaces.ws-build-dir.path)/_cachi2_remote_sources"
CACHI2_PKG_OPT_PATH="cachi2_pkg_options.json"
if [ ! -d "$CACHI2_DIR" ]; then
echo "Skipping step: remote sources not found"
Expand All @@ -91,27 +92,34 @@ spec:
pushd "${REMOTE_SOURCE_PATH}"
FOR_OUTPUT_DIR="$(cat cachi2_for_output_dir_opt.txt)"
if [ -f "${CACHI2_PKG_OPT_PATH}" ]; then
# missing pkg opt config file; OSBS doesn't want to run cachi2
FOR_OUTPUT_DIR="$(cat cachi2_for_output_dir_opt.txt)"
cachi2 --log-level="$LOG_LEVEL" fetch-deps \
--source="${REMOTE_SOURCE_PATH}/app/" \
--output="${REMOTE_SOURCE_PATH}" \
"$(cat cachi2_pkg_options.json)"
cachi2 --log-level="$LOG_LEVEL" fetch-deps \
--source="${REMOTE_SOURCE_PATH}/app/" \
--output="${REMOTE_SOURCE_PATH}" \
"$(cat "${CACHI2_PKG_OPT_PATH}")"
SBOMS+=("${REMOTE_SOURCE_PATH}/bom.json")
cachi2 --log-level="$LOG_LEVEL" generate-env "${REMOTE_SOURCE_PATH}" \
--format json \
--for-output-dir="${FOR_OUTPUT_DIR}" \
--output "${REMOTE_SOURCE_PATH}/cachi2.env.json"
else
mkdir deps/ # create empty deps/ dir to emulate cachi2 run
fi
cachi2 --log-level="$LOG_LEVEL" generate-env "${REMOTE_SOURCE_PATH}" \
--format json \
--for-output-dir="${FOR_OUTPUT_DIR}" \
--output "${REMOTE_SOURCE_PATH}/cachi2.env.json"
SBOMS+=("${REMOTE_SOURCE_PATH}/bom.json")
rm -fr app/.git/ # remove git directory
# create source archive before injecting files
tar -czf remote-source.tar.gz app/ deps/
cachi2 --log-level="$LOG_LEVEL" inject-files "${REMOTE_SOURCE_PATH}" \
--for-output-dir="${FOR_OUTPUT_DIR}"
if [ -f "${CACHI2_PKG_OPT_PATH}" ]; then
cachi2 --log-level="$LOG_LEVEL" inject-files "${REMOTE_SOURCE_PATH}" \
--for-output-dir="${FOR_OUTPUT_DIR}"
fi
popd
done
Expand Down

0 comments on commit 6f130bd

Please sign in to comment.