-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-cppio-image.sh
More file actions
executable file
·204 lines (180 loc) · 5.89 KB
/
build-cppio-image.sh
File metadata and controls
executable file
·204 lines (180 loc) · 5.89 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# Bash script to automate building cppio-rel for Docker Compose on Linux
# This script builds the project in the dev container, then runs docker-compose build
# --- Configuration ---
CPPIO_PROJECT_NAME="cppio"
DEV_IMAGE_NAME="${CPPIO_PROJECT_NAME}/cppio-dev"
IMAGE_NAME="${CPPIO_PROJECT_NAME}/cppio-rel"
BUILD_CONTAINER_NAME="cppio_dev_build_temp"
ENGINE="docker"
STEP="2"
MAKE="cppio"
CPPIO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CPPIO_DEPLOY_ROOT="${CPPIO_ROOT}/deploy"
DOCKERFILE_PATH="${CPPIO_DEPLOY_ROOT}/SuseContainerImageFile"
WORKSPACE_PATH="${CPPIO_ROOT}"
# ---------------------
# Show help
show_help() {
echo "Usage: ./build-cppio-image.sh [OPTIONS]"
echo "This script automates the build process for CPPIO image ${IMAGE_NAME}:"
echo "1. Checks if the CPPIO dev image $DEV_IMAGE_NAME exists"
echo "2. Builds CPPIO project inside a tmp dev container (image $DEV_IMAGE_NAME)"
echo "3. Runs docker-compose build cppio-hsd"
echo ""
echo "OPTIONS:"
echo " -d, --dev-image NAME Specify container image name for CPPIO dev."
echo " -n, --image-name NAME Specify container image name for CPPIO."
echo " -s, --step 1|2 1 for dev image build, 2 for all."
echo " -e, --engine ENGINE Specify container engine: docker or podman (default: docker)."
echo " -m, --make all|cppio all to build all source codes, cppio for only cppio source codes."
echo " -h, --help Show this help message"
}
# Function to check if image exists
image_exists() {
local image_name=$1
if [ "$ENGINE" = "docker" ]; then
docker image inspect "${image_name}:latest" &> /dev/null
return $?
elif [ "$ENGINE" = "podman" ]; then
podman image inspect "${image_name}:latest" &> /dev/null
return $?
else
echo "Invalid engine: $ENGINE. Please specify 'docker' or 'podman'."
return 1
fi
}
# Function to check if container is running
container_running() {
local container_name=$1
if command -v docker &> /dev/null; then
local output=$(docker ps --filter "name=${container_name}" --format "{{.Names}}" 2>/dev/null)
[ "${output}" = "${container_name}" ]
return $?
elif command -v podman &> /dev/null; then
local output=$(podman ps --filter "name=${container_name}" --format "{{.Names}}" 2>/dev/null)
[ "${output}" = "${container_name}" ]
return $?
else
echo "Neither Docker nor Podman is installed."
return 1
fi
}
# Function to build CPPIO dev image
build_cppio_dev_image() {
echo "Building CPPIO dev image ${DEV_IMAGE_NAME}..."
local start_dev_script="${CPPIO_ROOT}/devcontainer/startdevcontainer.sh"
if [ ! -f "${start_dev_script}" ]; then
echo "Error: ${start_dev_script} not found."
return 1
fi
bash "${start_dev_script}" -n "${DEV_IMAGE_NAME}" -e "${ENGINE}" -b
if [ $? -eq 0 ]; then
echo "Dev image built successfully!"
return 0
else
echo "Failed to build dev image."
return 1
fi
}
# Function to run build in container
run_build_in_container() {
echo "Running build in dev container..."
local cppio_build_commands
if [ "$Make" = "all" ]; then
cppio_build_commands="cd /workspace/cppio && ./bootstrap.sh all && ./makecppio.sh"
else
cppio_build_commands="cd /workspace/cppio && ./makecppio.sh"
fi
local cmd="${ENGINE} run --rm --name ${BUILD_CONTAINER_NAME} -v ${WORKSPACE_PATH}:/workspace/cppio ${DEV_IMAGE_NAME} /bin/bash -c \"${cppio_build_commands}\""
echo "Command: ${cmd}"
eval "${cmd}"
if [ $? -eq 0 ]; then
echo "Build in container successful!"
return 0
else
echo "Build in container failed."
return 1
fi
}
# Function to run docker-compose build
run_compose_build() {
echo "Running docker-compose build..."
if ! command -v docker-compose &> /dev/null && ! command -v docker &> /dev/null; then
echo "Error: docker-compose or docker not found."
return 1
fi
local compose_cmd="docker-compose"
if ! command -v docker-compose &> /dev/null; then
compose_cmd="docker compose"
fi
local cmd="cd ${CPPIO_DEPLOY_ROOT} && ${compose_cmd} build cppio-hsd"
echo "Command: ${cmd}"
eval "${cmd}"
if [ $? -eq 0 ]; then
echo "Docker-compose build successful!"
return 0
else
echo "Docker-compose build failed."
return 1
fi
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dev-image)
DEV_IMAGE_NAME="$2"
shift 2
;;
-n|--image-name)
IMAGE_NAME="$2"
shift 2
;;
-e|--engine)
ENGINE="$2"
shift 2
;;
-s|--step)
STEP=$2
shift 2
;;
-m|--make)
MAKE=$2
shift 2
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Main script execution
echo "DOCKERFILE_PATH: ${DOCKERFILE_PATH}, WORKSPACE_PATH: ${WORKSPACE_PATH}"
echo "Starting automated build process for ${IMAGE_NAME}..."
# Check if dev image exists, build if not
if ! image_exists "${DEV_IMAGE_NAME}"; then
echo "Image ${DEV_IMAGE_NAME} does not exist."
if ! build_cppio_dev_image; then
echo "Failed to build image ${DEV_IMAGE_NAME}. Exiting."
exit 1
fi
fi
if [ $STEP -eq 1 ]; then
exit 0
fi
# Do CPPIO build in dev container
if ! run_build_in_container; then
echo "Build in container failed. Exiting."
exit 1
fi
# Run docker-compose build
if ! run_compose_build; then
echo "Docker-compose build failed. Exiting."
exit 1
fi
echo "All steps completed successfully!"