186 lines
5.5 KiB
Bash
186 lines
5.5 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
# Log function
|
|
log() {
|
|
echo "[MOCK-WRAPPER] $1"
|
|
}
|
|
|
|
# Function to setup build environment
|
|
setup_environment() {
|
|
local env_config="$1"
|
|
local environment=$(echo "$env_config" | jq -r '.environment // "bookworm-amd64"')
|
|
|
|
log "Setting up build environment: $environment"
|
|
|
|
# Parse environment (e.g., "bookworm-amd64" -> suite="bookworm", arch="amd64")
|
|
local suite=$(echo "$environment" | cut -d'-' -f1)
|
|
local arch=$(echo "$environment" | cut -d'-' -f2)
|
|
|
|
log "Suite: $suite, Architecture: $arch"
|
|
|
|
# Create pbuilder environment
|
|
log "Creating pbuilder environment"
|
|
pbuilder create --distribution "$suite" --architecture "$arch" --basetgz "/var/cache/pbuilder/$suite-$arch-base.tgz" || {
|
|
log "WARNING: Failed to create pbuilder environment, trying sbuild"
|
|
sbuild-createchroot --dist="$suite" --arch="$arch" "/var/cache/sbuild/$suite-$arch" http://deb.debian.org/debian/ || {
|
|
log "ERROR: Failed to create build environment"
|
|
exit 1
|
|
}
|
|
}
|
|
}
|
|
|
|
# Function to install build dependencies
|
|
install_dependencies() {
|
|
local packages_config="$1"
|
|
|
|
if [ -n "$packages_config" ]; then
|
|
local packages=$(echo "$packages_config" | jq -r '.[]? // empty')
|
|
for package in $packages; do
|
|
log "Installing build dependency: $package"
|
|
apt-get install -y "$package" || {
|
|
log "WARNING: Failed to install package $package"
|
|
}
|
|
done
|
|
fi
|
|
}
|
|
|
|
# Function to configure repositories
|
|
configure_repositories() {
|
|
local repos_config="$1"
|
|
|
|
if [ -n "$repos_config" ]; then
|
|
local repos=$(echo "$repos_config" | jq -r '.[]? // empty')
|
|
for repo in $repos; do
|
|
log "Adding repository: $repo"
|
|
echo "$repo" >> /etc/apt/sources.list.d/deb-mock.list
|
|
done
|
|
|
|
# Update package lists
|
|
apt-get update
|
|
fi
|
|
}
|
|
|
|
# Function to execute build script
|
|
execute_build() {
|
|
local build_script="$1"
|
|
|
|
if [ -n "$build_script" ]; then
|
|
log "Executing build script"
|
|
|
|
# Create temporary build script
|
|
local script_file="/tmp/build-script.sh"
|
|
echo "$build_script" > "$script_file"
|
|
chmod +x "$script_file"
|
|
|
|
# Execute the build script
|
|
"$script_file" || {
|
|
log "ERROR: Build script failed"
|
|
exit 1
|
|
}
|
|
|
|
# Clean up
|
|
rm -f "$script_file"
|
|
else
|
|
log "No build script provided, skipping build execution"
|
|
fi
|
|
}
|
|
|
|
# Function to collect artifacts
|
|
collect_artifacts() {
|
|
local artifacts_config="$1"
|
|
|
|
if [ -n "$artifacts_config" ]; then
|
|
local artifacts=$(echo "$artifacts_config" | jq -r '.[]? // empty')
|
|
local artifacts_dir="/tmp/artifacts"
|
|
|
|
mkdir -p "$artifacts_dir"
|
|
|
|
for artifact in $artifacts; do
|
|
log "Collecting artifact: $artifact"
|
|
|
|
# Handle glob patterns
|
|
if [[ $artifact == *"*"* ]]; then
|
|
# Expand glob pattern
|
|
for file in $artifact; do
|
|
if [ -e "$file" ]; then
|
|
log "Copying artifact: $file"
|
|
cp "$file" "$artifacts_dir/"
|
|
fi
|
|
done
|
|
else
|
|
# Single file
|
|
if [ -e "$artifact" ]; then
|
|
log "Copying artifact: $artifact"
|
|
cp "$artifact" "$artifacts_dir/"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
log "Artifacts collected in: $artifacts_dir"
|
|
ls -la "$artifacts_dir"
|
|
fi
|
|
}
|
|
|
|
# Function to cleanup build environment
|
|
cleanup_environment() {
|
|
log "Cleaning up build environment"
|
|
|
|
# Clean package cache
|
|
apt-get clean
|
|
apt-get autoremove -y
|
|
|
|
# Clean build artifacts
|
|
rm -rf /tmp/build-*
|
|
|
|
# Clean source packages
|
|
rm -f ../*.deb ../*.dsc ../*.tar.gz ../*.buildinfo ../*.changes
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
local module_config="$1"
|
|
|
|
log "Starting deb-mock module execution"
|
|
|
|
# Setup build environment
|
|
if echo "$module_config" | jq -e '.environment' >/dev/null 2>&1; then
|
|
log "Processing environment configuration"
|
|
setup_environment "$(echo "$module_config" | jq -c '.environment')"
|
|
else
|
|
log "Using default environment: bookworm-amd64"
|
|
setup_environment '{"environment": "bookworm-amd64"}'
|
|
fi
|
|
|
|
# Install build dependencies
|
|
if echo "$module_config" | jq -e '.packages' >/dev/null 2>&1; then
|
|
log "Processing packages configuration"
|
|
install_dependencies "$(echo "$module_config" | jq -c '.packages')"
|
|
fi
|
|
|
|
# Configure repositories
|
|
if echo "$module_config" | jq -e '.repositories' >/dev/null 2>&1; then
|
|
log "Processing repositories configuration"
|
|
configure_repositories "$(echo "$module_config" | jq -c '.repositories')"
|
|
fi
|
|
|
|
# Execute build script
|
|
if echo "$module_config" | jq -e '.build-script' >/dev/null 2>&1; then
|
|
log "Processing build script configuration"
|
|
execute_build "$(echo "$module_config" | jq -r '.build-script')"
|
|
fi
|
|
|
|
# Collect artifacts
|
|
if echo "$module_config" | jq -e '.artifacts' >/dev/null 2>&1; then
|
|
log "Processing artifacts configuration"
|
|
collect_artifacts "$(echo "$module_config" | jq -c '.artifacts')"
|
|
fi
|
|
|
|
# Cleanup
|
|
cleanup_environment
|
|
|
|
log "deb-mock module execution completed successfully"
|
|
}
|
|
|
|
# Execute main function with the module configuration
|
|
main "$1"
|