Skip to content

Commit 8e4cb28

Browse files
committed
refactor(ci): improve error handling and logging in SGX installation script
1 parent 288abe3 commit 8e4cb28

1 file changed

Lines changed: 36 additions & 33 deletions

File tree

ci/install_sgx_for_applicaiton_developer.sh

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# Reference:
44
# https://cc-enabling.trustedservices.intel.com/intel-sgx-sw-installation-guide-linux/02/installation_instructions/#intel-sgx-application-developer
55

6+
#TODO:
7+
# report error when curl fails to download files, e.g. due to network issues or incorrect URLs
8+
69
set -euo pipefail
710
if [ "${DEBUG:-0}" -eq 1 ]; then
811
set -o xtrace
@@ -15,7 +18,7 @@ error_handler() {
1518
local bash_lineno=${2:-$BASH_LINENO}
1619
local last_command=${3:-$BASH_COMMAND}
1720
local function_stack=${4:-${FUNCNAME[*]}}
18-
21+
1922
# Log error context to file
2023
{
2124
echo "=== ERROR OCCURRED ==="
@@ -27,25 +30,25 @@ error_handler() {
2730
echo "Timestamp: $(date '+%Y-%m-%d %H:%M:%S')"
2831
echo "======================"
2932
} >> "${LOG_FILE:-/tmp/install_sgx.log}" 2>/dev/null || true
30-
33+
3134
# Print concise error to stderr
3235
echo "ERROR: Script failed at line $line_number with exit code $exit_code" >&2
3336
echo "Failed command: $last_command" >&2
3437
echo "Check log file: ${LOG_FILE:-/tmp/install_sgx.log}" >&2
35-
38+
3639
# Call cleanup function if it exists
3740
if type cleanup >/dev/null 2>&1; then
3841
cleanup || true
3942
fi
40-
43+
4144
exit $exit_code
4245
}
4346

4447
# Set up error trap
4548
trap 'error_handler $LINENO $BASH_LINENO "$BASH_COMMAND" "${FUNCNAME[*]}"' ERR
4649

4750
# Platform will be detected dynamically by platform_detect() function
48-
# Supported platforms: Debian12, Debian11, Ubuntu22.04-server, Ubuntu20.04-server
51+
# Supported platforms: Debian12, Debian11, Ubuntu22.04-server, Ubuntu20.04-server
4952
PLATFORM=""
5053

5154
# Logging infrastructure
@@ -74,22 +77,22 @@ log_exec() {
7477
# Print environment sourcing instructions
7578
print_env_instructions() {
7679
log_info "Printing environment setup instructions"
77-
80+
7881
echo "========================================================================"
7982
echo " IMPORTANT: Before building or running SGX applications, you must run:"
8083
echo " source /opt/intel/sgxsdk/environment"
8184
echo " in your current shell to activate SGX SDK environment variables."
8285
echo "========================================================================"
83-
86+
8487
log_info "Environment setup instructions displayed to user"
8588
}
8689

8790
check_sgx_packages() {
8891
log_info "Checking for existing SGX packages..."
89-
92+
9093
local packages=("libsgx-quote-ex" "libsgx-dcap-ql" "libsgx-enclave-common-dev" "libsgx-dcap-ql-dev" "libsgx-dcap-default-qpl-dev" "tee-appraisal-tool")
9194
local missing_packages=()
92-
95+
9396
for package in "${packages[@]}"; do
9497
if ! dpkg -l "$package" >> "${LOG_FILE}" 2>&1; then
9598
missing_packages+=("$package")
@@ -98,7 +101,7 @@ check_sgx_packages() {
98101
log_info "Package $package already installed"
99102
fi
100103
done
101-
104+
102105
if [ ${#missing_packages[@]} -eq 0 ]; then
103106
log_info "All SGX packages are already installed"
104107
return 0
@@ -110,10 +113,10 @@ check_sgx_packages() {
110113

111114
check_sgx_sdk() {
112115
log_info "Checking for existing SGX SDK..."
113-
116+
114117
if [ -d "/opt/intel/sgxsdk" ] && [ -f "/opt/intel/sgxsdk/environment" ]; then
115118
log_info "SGX SDK already installed at /opt/intel/sgxsdk"
116-
119+
117120
# Validate SDK installation by checking key components
118121
if [ -f "/opt/intel/sgxsdk/bin/sgx-gdb" ] && [ -d "/opt/intel/sgxsdk/include" ]; then
119122
log_info "SGX SDK installation appears complete"
@@ -130,7 +133,7 @@ check_sgx_sdk() {
130133

131134
check_sgx_repo() {
132135
log_info "Checking for existing SGX local repository..."
133-
136+
134137
if [ -d "/opt/intel/sgx_debian_local_repo" ] && [ -f "/etc/apt/sources.list.d/sgx_debian_local_repo.list" ]; then
135138
log_info "SGX local repository already configured"
136139
return 0
@@ -145,20 +148,20 @@ check_sgx_repo() {
145148
# Platform detection and configuration
146149
platform_detect() {
147150
log_info "Entering platform_detect() function"
148-
151+
149152
if [ ! -f "/etc/os-release" ]; then
150153
log_info "ERROR: /etc/os-release not found - cannot detect OS"
151154
echo "ERROR: Cannot detect operating system. /etc/os-release not found." >&2
152155
log_info "Exiting platform_detect() function"
153156
return 1
154157
fi
155-
158+
156159
# Parse OS information from /etc/os-release
157160
local os_id=$(grep '^ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
158161
local version_id=$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
159-
162+
160163
log_info "Raw OS detection: ID=${os_id}, VERSION_ID=${version_id}"
161-
164+
162165
# Determine platform string based on OS and version
163166
case "${os_id}" in
164167
"ubuntu")
@@ -200,22 +203,22 @@ platform_detect() {
200203
return 1
201204
;;
202205
esac
203-
206+
204207
log_info "Successfully detected platform: ${PLATFORM}"
205208
echo "Detected platform: ${PLATFORM}"
206-
209+
207210
log_info "Exiting platform_detect() function"
208211
return 0
209212
}
210213

211214
# Install SGX packages and SDK
212215
install_packages() {
213216
log_info "Entering install_packages() function"
214-
217+
215218
# Skip repo setup if already configured
216219
if ! check_sgx_repo; then
217220
log_info "Setting up SGX local repository..."
218-
221+
219222
pushd /tmp >> "${LOG_FILE}" 2>&1
220223
log_exec curl -fsSLO \
221224
https://download.01.org/intel-sgx/latest/linux-latest/distro/${PLATFORM}/sgx_debian_local_repo.tgz
@@ -257,7 +260,7 @@ install_packages() {
257260
# Install Intel SGX SDK only if missing
258261
if ! check_sgx_sdk; then
259262
log_info "Installing Intel SGX SDK for Application Developer..."
260-
263+
261264
pushd /opt/intel >> "${LOG_FILE}" 2>&1
262265
log_exec sudo curl -fsSLo sgx_linux_x64_sdk.bin \
263266
https://download.01.org/intel-sgx/latest/linux-latest/distro/${PLATFORM}/sgx_linux_x64_sdk_2.27.100.1.bin
@@ -279,66 +282,66 @@ install_packages() {
279282
# Install Developer packages for Intel SGX only if missing
280283
if ! check_sgx_packages; then
281284
log_info "Installing Intel SGX Developer packages..."
282-
285+
283286
log_exec sudo apt-get install -y libsgx-enclave-common-dev \
284287
libsgx-dcap-ql-dev \
285288
libsgx-dcap-default-qpl-dev \
286289
tee-appraisal-tool
287290
else
288291
log_info "SGX Developer packages already installed, skipping"
289292
fi
290-
293+
291294
log_info "Exiting install_packages() function"
292295
return 0
293296
}
294297

295298
# Validate the installation was successful
296299
validate_installation() {
297300
log_info "Entering validate_installation() function"
298-
301+
299302
local validation_failed=0
300-
303+
301304
# Re-check all components after installation
302305
if ! check_sgx_packages; then
303306
log_info "VALIDATION FAILED: SGX packages not properly installed"
304307
validation_failed=1
305308
fi
306-
309+
307310
if ! check_sgx_sdk; then
308311
log_info "VALIDATION FAILED: SGX SDK not properly installed"
309312
validation_failed=1
310313
fi
311-
314+
312315
if ! check_sgx_repo; then
313316
log_info "VALIDATION FAILED: SGX repository not properly configured"
314317
validation_failed=1
315318
fi
316-
319+
317320
if [ $validation_failed -eq 0 ]; then
318321
log_info "VALIDATION SUCCESS: All SGX components properly installed"
319322
else
320323
log_info "VALIDATION FAILED: Some SGX components failed installation"
321324
log_info "Exiting validate_installation() function"
322325
return 1
323326
fi
324-
327+
325328
log_info "Exiting validate_installation() function"
326329
return 0
327330
}
328331

329332
# Clean up temporary files
330333
cleanup() {
331334
log_info "Entering cleanup() function"
332-
335+
333336
# Clean up any temporary files in /tmp related to SGX installation
334337
if [ -f "/tmp/sgx_debian_local_repo.tgz" ]; then
335338
log_info "Removing temporary SGX repository archive"
336339
rm -f /tmp/sgx_debian_local_repo.tgz
337340
fi
338-
341+
339342
# Additional cleanup can be added here as needed
340343
log_info "Temporary file cleanup completed"
341-
344+
342345
log_info "Exiting cleanup() function"
343346
return 0
344347
}

0 commit comments

Comments
 (0)