Skip to content

Commit 6def76f

Browse files
authored
Merge pull request #22 from pangpang20/master
Refactor GaussDB Driver Installation into Reusable, Idempotent Script
2 parents b11a399 + 6c3491d commit 6def76f

File tree

4 files changed

+177
-33
lines changed

4 files changed

+177
-33
lines changed

README.rst

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,7 @@ In order to work on the GaussDB source code, you must have the
1212
``libpq`` GaussDB client library installed on the system. For instance, on
1313
EulerOS x86_64 systems, you can obtain it by running::
1414

15-
# Update the system package index
16-
sudo apt update
17-
18-
# Install required tools
19-
sudo apt install -y wget unzip
20-
21-
# Download the GaussDB driver package
22-
wget -O /tmp/GaussDB_driver.zip https://dbs-download.obs.cn-north-1.myhuaweicloud.com/GaussDB/1730887196055/GaussDB_driver.zip
23-
24-
# Extract the driver package and remove the zip file
25-
unzip /tmp/GaussDB_driver.zip -d /tmp/
26-
rm -rf /tmp/GaussDB_driver.zip
27-
28-
# Copy the Python driver tarball to /tmp
29-
\cp /tmp/GaussDB_driver/Centralized/Hce2_X86_64/GaussDB-Kernel*64bit_Python.tar.gz /tmp/
30-
31-
# Extract the driver tarball and clean up
32-
tar -zxvf /tmp/GaussDB-Kernel*64bit_Python.tar.gz -C /tmp/
33-
rm -rf /tmp/GaussDB-Kernel*64bit_Python.tar.gz
34-
rm -rf /tmp/_GaussDB
35-
rm -rf /tmp/GaussDB_driver
36-
37-
# Register /tmp/lib in the dynamic linker configuration
38-
echo /tmp/lib | sudo tee /etc/ld.so.conf.d/gauss-libpq.conf
39-
sudo sed -i '1s|^|/tmp/lib\n|' /etc/ld.so.conf
40-
41-
# Refresh the dynamic linker cache
42-
sudo ldconfig
43-
44-
# Verify libpq is registered, the first line should show the path:
45-
# libpq.so.5.5 (libc6,x86-64) => /tmp/lib/libpq.so.5.5
46-
ldconfig -p | grep pq
15+
sh tools/install_gaussdb_driver.sh
4716

4817
Installation from PyPI:
4918

tests/fix_db.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,16 @@ def conn(conn_cls, dsn, request, tracefile):
210210
conn = conn_cls.connect(dsn)
211211
with maybe_trace(conn.pgconn, tracefile, request.function):
212212
yield conn
213-
conn.close()
213+
try:
214+
if getattr(conn, "in_transaction", False):
215+
conn.rollback()
216+
except Exception:
217+
pass
218+
finally:
219+
try:
220+
conn.close()
221+
except Exception:
222+
pass
214223

215224

216225
@pytest.fixture(params=[True, False], ids=["pipeline=on", "pipeline=off"])

tests/test_typeinfo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def exit(self, exc_type, exc_val, exc_tb):
104104
assert conn.info.transaction_status == status
105105
assert info is None
106106

107+
if conn.info.transaction_status != TransactionStatus.IDLE:
108+
conn.rollback()
109+
107110

108111
@_name
109112
@_status

tools/install_gaussdb_driver.sh

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/bin/bash
2+
# install_gaussdb_driver.sh
3+
# Automatically download, install, and configure GaussDB driver, supporting HCE, CentOS (Hce2), Euler, Kylin systems
4+
# Idempotent and repeatable execution
5+
6+
set -euo pipefail
7+
8+
#===================
9+
# Basic Configuration
10+
#===================
11+
DOWNLOAD_URL="https://dbs-download.obs.cn-north-1.myhuaweicloud.com/GaussDB/1730887196055/GaussDB_driver.zip"
12+
HOME_DIR="$HOME"
13+
ZIP_FILE="$HOME_DIR/GaussDB_driver.zip"
14+
DRIVER_DIR="$HOME_DIR/GaussDB_driver/Centralized"
15+
LIB_DIR="$HOME_DIR/GaussDB_driver_lib"
16+
LOG_FILE="/tmp/gaussdb_driver_install_$(date +%Y%m%d_%H%M%S).log"
17+
18+
#===================
19+
# Utility Functions
20+
#===================
21+
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"; }
22+
23+
cleanup() {
24+
log "Cleaning up temporary files..."
25+
[[ -f "$ZIP_FILE" ]] && rm -rf "$ZIP_FILE" 2>/dev/null
26+
[[ -d "$HOME_DIR/GaussDB_driver" ]] && rm -rf "$HOME_DIR/GaussDB_driver" 2>/dev/null
27+
}
28+
29+
#===================
30+
# Parameter Checks
31+
#===================
32+
command -v wget >/dev/null || { log "Error: wget is missing"; exit 1; }
33+
command -v unzip >/dev/null || { log "Error: unzip is missing"; exit 1; }
34+
command -v tar >/dev/null || { log "Error: tar is missing"; exit 1; }
35+
command -v ldconfig >/dev/null || { log "Error: ldconfig is missing"; exit 1; }
36+
37+
log "Starting GaussDB driver installation..."
38+
39+
#===================
40+
# Download and Extract
41+
#===================
42+
if [[ ! -f "$ZIP_FILE" ]]; then
43+
log "Downloading driver file..."
44+
wget -O "$ZIP_FILE" "$DOWNLOAD_URL" >> "$LOG_FILE" 2>&1 || { log "Error: Download failed"; exit 1; }
45+
else
46+
log "Driver file already exists, skipping download"
47+
fi
48+
49+
log "Extracting driver file..."
50+
unzip -o "$ZIP_FILE" -d "$HOME_DIR/" >> "$LOG_FILE" 2>&1 || { log "Error: Extraction failed"; exit 1; }
51+
52+
53+
#===================
54+
# Detect System and Architecture
55+
#===================
56+
ARCH=$(uname -m)
57+
case "$ARCH" in
58+
x86_64) ARCH_TYPE="X86_64" ;;
59+
aarch64) ARCH_TYPE="arm_64" ;;
60+
*) log "Error: Unsupported architecture: $ARCH"; exit 1 ;;
61+
esac
62+
log "architecture: $ARCH_TYPE"
63+
OS_TYPE=""
64+
if [[ -f /etc/os-release ]]; then
65+
. /etc/os-release
66+
case "$ID" in
67+
centos|hce)
68+
if [[ -d "$DRIVER_DIR/Hce2_$ARCH_TYPE" ]]; then
69+
OS_TYPE="Hce2_$ARCH_TYPE"
70+
fi
71+
;;
72+
euler)
73+
VERSION=$(grep -oP 'VERSION_ID="\K[^"]+' /etc/os-release)
74+
case "$VERSION" in
75+
2.5*)
76+
if [[ -d "$DRIVER_DIR/Euler2.5_$ARCH_TYPE" ]]; then
77+
OS_TYPE="Euler2.5_$ARCH_TYPE"
78+
fi
79+
;;
80+
2.9*)
81+
if [[ -d "$DRIVER_DIR/Euler2.9_$ARCH_TYPE" ]]; then
82+
OS_TYPE="Euler2.9_$ARCH_TYPE"
83+
fi
84+
;;
85+
esac
86+
;;
87+
kylin)
88+
if [[ -d "$DRIVER_DIR/Kylinv10_$ARCH_TYPE" ]]; then
89+
OS_TYPE="Kylinv10_$ARCH_TYPE"
90+
fi
91+
;;
92+
*)
93+
log "Error: Unsupported operating system: $ID"; exit 1
94+
;;
95+
esac
96+
else
97+
log "Warning: Unable to read /etc/os-release, attempting to infer system type from directory structure"
98+
if [[ -d "$DRIVER_DIR/Hce2_$ARCH_TYPE" ]]; then
99+
OS_TYPE="Hce2_$ARCH_TYPE"
100+
elif [[ -d "$DRIVER_DIR/Euler2.5_$ARCH_TYPE" ]]; then
101+
OS_TYPE="Euler2.5_$ARCH_TYPE"
102+
elif [[ -d "$DRIVER_DIR/Euler2.9_$ARCH_TYPE" ]]; then
103+
OS_TYPE="Euler2.9_$ARCH_TYPE"
104+
elif [[ -d "$DRIVER_DIR/Kylinv10_$ARCH_TYPE" ]]; then
105+
OS_TYPE="Kylinv10_$ARCH_TYPE"
106+
else
107+
log "Error: Unsupported operating system or architecture: $ARCH_TYPE"; exit 1
108+
fi
109+
fi
110+
log "Detected system: $OS_TYPE"
111+
if [[ -z "$OS_TYPE" ]]; then
112+
log "Error: No matching driver directory found: $DRIVER_DIR/*_$ARCH_TYPE"; exit 1
113+
fi
114+
115+
116+
117+
#===================
118+
# Copy Driver Package
119+
#===================
120+
mkdir -p "$LIB_DIR"
121+
DRIVER_PACKAGE=$(find "$DRIVER_DIR/$OS_TYPE" -name "*Python.tar.gz" | head -n 1)
122+
if [[ -z "$DRIVER_PACKAGE" ]]; then
123+
log "Error: No driver package found for $OS_TYPE"; exit 1
124+
fi
125+
126+
log "Copying driver package: $DRIVER_PACKAGE to $LIB_DIR"
127+
sudo cp "$DRIVER_PACKAGE" "$LIB_DIR/" || { log "Error: Failed to copy driver package"; exit 1; }
128+
129+
#===================
130+
# Extract Driver Package
131+
#===================
132+
log "Extracting driver package to $LIB_DIR..."
133+
tar -zxvf "$LIB_DIR/$(basename "$DRIVER_PACKAGE")" -C "$LIB_DIR/" >> "$LOG_FILE" 2>&1 || { log "Error: Failed to extract driver package"; exit 1; }
134+
rm -f "$LIB_DIR/$(basename "$DRIVER_PACKAGE")"
135+
sudo chmod 755 -R $LIB_DIR
136+
137+
#===================
138+
# Configure Dynamic Link Library
139+
#===================
140+
log "Configuring dynamic link library path..."
141+
echo "$LIB_DIR/lib" | sudo tee /etc/ld.so.conf.d/gauss-libpq.conf >/dev/null
142+
if ! grep -Fx "$LIB_DIR/lib" /etc/ld.so.conf >/dev/null; then
143+
sudo sed -i "1s|^|$LIB_DIR/lib\n|" /etc/ld.so.conf
144+
fi
145+
sudo sed -i '/gauss/d' /etc/ld.so.conf
146+
sudo ldconfig
147+
148+
149+
150+
#===================
151+
# Verify Installation
152+
#===================
153+
if sudo ldconfig -p | grep -q libpq; then
154+
cleanup
155+
log "============================================================="
156+
log "GaussDB driver installed successfully!"
157+
log "Dynamic link library configured: $LIB_DIR/lib"
158+
log "Log file: $LOG_FILE"
159+
log "============================================================="
160+
else
161+
log "Error: Dynamic link library verification failed"
162+
exit 1
163+
fi

0 commit comments

Comments
 (0)