forked from apache/cloudberry-devops-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudberry-utils.sh
More file actions
executable file
·158 lines (146 loc) · 5.32 KB
/
Copy pathcloudberry-utils.sh
File metadata and controls
executable file
·158 lines (146 loc) · 5.32 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
#!/bin/bash
# --------------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed
# with this work for additional information regarding copyright
# ownership. The ASF licenses this file to You under the Apache
# License, Version 2.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of the
# License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# --------------------------------------------------------------------
#
# Library: cloudberry-utils.sh
# Description: Common utility functions for Apache Cloudberry build
# and test scripts
#
# Required Environment Variables:
# SRC_DIR - Root source directory
#
# Optional Environment Variables:
# LOG_DIR - Directory for logs (defaults to ${SRC_DIR}/build-logs)
#
# Functions:
# init_environment "Script Name" "Log File" "Build Destination"
# - Initialize logging and verify environment
# - Parameters:
# * script_name: Name of the calling script
# * log_file: Path to log file
# * build_destination: Path to Cloudberry destination, by default is /usr/local/cloudberry-db
# - Returns: 0 on success, 1 on failure
#
# execute_cmd command [args...]
# - Execute command with logging
# - Parameters: Command and its arguments
# - Returns: Command's exit code
#
# run_psql_cmd "sql_command"
# - Execute PostgreSQL command with logging
# - Parameters: SQL command string
# - Returns: psql command's exit code
#
# source_cloudberry_env
# - Source Cloudberry environment files
# - Returns: 0 on success
#
# log_section "section_name"
# - Log section start
# - Parameters: Name of the section
#
# log_section_end "section_name"
# - Log section end
# - Parameters: Name of the section
#
# log_completion "script_name" "log_file"
# - Log script completion
# - Parameters:
# * script_name: Name of the calling script
# * log_file: Path to log file
#
# Usage:
# source ./cloudberry-utils.sh
#
# Example:
# source ./cloudberry-utils.sh
# init_environment "My Script" "${LOG_FILE}" "${BUILD_DESTINATION}"
# execute_cmd make clean
# log_section "Build Process"
# execute_cmd make -j$(nproc)
# log_section_end "Build Process"
# log_completion "My Script" "${LOG_FILE}"
#
# --------------------------------------------------------------------
DEFAULT_BUILD_DESTINATION=/usr/local/cloudberry-db
# Initialize logging and environment
init_environment() {
local script_name=$1
local log_file=$2
local build_destination=$3
if [ -z "$build_destination" ]; then
build_destination=${DEFAULT_BUILD_DESTINATION}
fi
export BUILD_DESTINATION=$build_destination
echo "=== Initializing environment for ${script_name} ==="
echo "${script_name} executed at $(date)" | tee -a "${log_file}"
echo "Whoami: $(whoami)" | tee -a "${log_file}"
echo "Hostname: $(hostname)" | tee -a "${log_file}"
echo "Working directory: $(pwd)" | tee -a "${log_file}"
echo "Source directory: ${SRC_DIR}" | tee -a "${log_file}"
echo "Log directory: ${LOG_DIR}" | tee -a "${log_file}"
echo "Build destination: ${BUILD_DESTINATION}" | tee -a "${log_file}"
if [ -z "${SRC_DIR:-}" ]; then
echo "Error: SRC_DIR environment variable is not set" | tee -a "${log_file}"
exit 1
fi
mkdir -p "${LOG_DIR}"
}
# Function to echo and execute command with logging
execute_cmd() {
local cmd_str="$*"
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
echo "Executing at ${timestamp}: $cmd_str" | tee -a "${LOG_DIR}/commands.log"
"$@" 2>&1 | tee -a "${LOG_DIR}/commands.log"
return ${PIPESTATUS[0]}
}
# Function to run psql commands with logging
run_psql_cmd() {
local cmd=$1
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
echo "Executing psql at ${timestamp}: $cmd" | tee -a "${LOG_DIR}/psql-commands.log"
psql -P pager=off template1 -c "$cmd" 2>&1 | tee -a "${LOG_DIR}/psql-commands.log"
return ${PIPESTATUS[0]}
}
# Function to source Cloudberry environment
source_cloudberry_env() {
echo "=== Sourcing Cloudberry environment ===" | tee -a "${LOG_DIR}/environment.log"
source /usr/local/cloudberry-db/greenplum_path.sh
source ${SRC_DIR}/../cloudberry/gpAux/gpdemo/gpdemo-env.sh
}
# Function to log section start
log_section() {
local section_name=$1
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
echo "=== ${section_name} started at ${timestamp} ===" | tee -a "${LOG_DIR}/sections.log"
}
# Function to log section end
log_section_end() {
local section_name=$1
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
echo "=== ${section_name} completed at ${timestamp} ===" | tee -a "${LOG_DIR}/sections.log"
}
# Function to log script completion
log_completion() {
local script_name=$1
local log_file=$2
local timestamp=$(date "+%Y.%m.%d-%H.%M.%S")
echo "${script_name} execution completed successfully at ${timestamp}" | tee -a "${log_file}"
}