-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_optimized.sh
More file actions
executable file
·70 lines (60 loc) · 2.5 KB
/
Copy pathbuild_optimized.sh
File metadata and controls
executable file
·70 lines (60 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# ==============================================================================
# Optimized FydeOS Build Wrapper Script
# This script provides optimized Docker resource allocation and build management
# ==============================================================================
set -euo pipefail
# Configuration
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOCKER_IMAGE="fydeos-builder-instance"
CONTAINER_NAME="fydeos-builder-instance"
# Detect system resources
TOTAL_CPU=$(nproc)
TOTAL_MEM_GB=$(free -g | awk 'NR==2{print $2}')
AVAILABLE_MEM_GB=$(free -g | awk 'NR==2{print $7}')
# Calculate optimal resource allocation
# Use 80% of CPUs, minimum 4, maximum 16
DOCKER_CPUS=$(echo "$TOTAL_CPU * 0.8" | bc | cut -d. -f1)
DOCKER_CPUS=$((DOCKER_CPUS < 4 ? 4 : DOCKER_CPUS))
DOCKER_CPUS=$((DOCKER_CPUS > 16 ? 16 : DOCKER_CPUS))
# Use 75% of available memory, minimum 8GB
DOCKER_MEMORY_GB=$(echo "$AVAILABLE_MEM_GB * 0.75" | bc | cut -d. -f1)
DOCKER_MEMORY_GB=$((DOCKER_MEMORY_GB < 8 ? 8 : DOCKER_MEMORY_GB))
echo "=========================================="
echo "FydeOS Build Environment - Optimized"
echo "=========================================="
echo "System CPUs: $TOTAL_CPU"
echo "Total Memory: ${TOTAL_MEM_GB}GB"
echo "Available Memory: ${AVAILABLE_MEM_GB}GB"
echo "Docker CPUs: $DOCKER_CPUS"
echo "Docker Memory: ${DOCKER_MEMORY_GB}GB"
echo "=========================================="
# Check if image exists, build if needed
if ! docker image inspect "$DOCKER_IMAGE" >/dev/null 2>&1; then
echo "Building Docker image..."
docker build --tag "$DOCKER_IMAGE" "$PROJECT_DIR"
else
echo "Using existing Docker image: $DOCKER_IMAGE"
fi
# Clean up any existing container
if docker container inspect "$CONTAINER_NAME" >/dev/null 2>&1; then
echo "Removing existing container..."
docker container rm -f "$CONTAINER_NAME" >/dev/null
fi
# Create and run optimized container
echo "Starting optimized build container..."
exec docker run -it --rm \
--name "$CONTAINER_NAME" \
--cpus="$DOCKER_CPUS" \
--memory="${DOCKER_MEMORY_GB}g" \
--shm-size=4g \
--ulimit nofile=65536:65536 \
--ulimit nproc=65536:65536 \
--env HOST_USER_ID="$(id -u)" \
--env HOST_GROUP_ID="$(id -g)" \
--env DEBUG_BUILD="${DEBUG_BUILD:-0}" \
--env CCACHE_MAXSIZE="50G" \
--env MAKEOPTS="-j${DOCKER_CPUS} -l$((DOCKER_CPUS + 2))" \
--mount type=bind,source="$PROJECT_DIR",target=/home/cros-builder/fydeos-project \
--workdir /home/cros-builder/fydeos-project \
"$DOCKER_IMAGE"