Skip to content

Commit 009b170

Browse files
committed
Merge branch 'feature-py-556-fix-cli-install' into '5.5.12'
<fix>[ansible]: support python3 ansible install See merge request zstackio/zstack!9259
2 parents 04d705f + 29e6c83 commit 009b170

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

conf/tools/install.sh

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ if [ -z $tool ]; then
2222
fi
2323

2424
install_pip() {
25-
pip3.11 --version | grep 22.3.1 >/dev/null || yum install python3.11-pip
25+
pip3.11 --version | grep 22.3.1 >/dev/null || yum install -y python3.11-pip
26+
}
27+
28+
# Ensure the virtualenv at $1 is a Python 3.11 venv.
29+
# If it does not exist or is a legacy Python 2 venv, recreate it.
30+
ensure_python3_venv() {
31+
local venv_path=$1
32+
local allowed_prefix="/var/lib/zstack/virtualenv"
33+
34+
if [[ "$venv_path" != "$allowed_prefix"* || "$venv_path" == *".."* ]]; then
35+
echo "Error: Path must start with $allowed_prefix. Provided: $venv_path" >&2
36+
exit 1
37+
fi
38+
39+
if [ -d "$venv_path" ] && [ -x "$venv_path/bin/python3.11" ]; then
40+
return 0
41+
fi
42+
rm -rf "$venv_path" && python3.11 -m venv "$venv_path" || exit 1
2643
}
2744

2845

@@ -34,13 +51,7 @@ cd /tmp
3451
if [ $tool = 'zstack-cli' ]; then
3552
CLI_VIRENV_PATH=/var/lib/zstack/virtualenv/zstackcli
3653
[ ! -z $force ] && rm -rf $CLI_VIRENV_PATH
37-
if [ ! -d "$CLI_VIRENV_PATH" ]; then
38-
python3.11 -m venv $CLI_VIRENV_PATH
39-
if [ $? -ne 0 ]; then
40-
rm -rf $CLI_VIRENV_PATH
41-
exit 1
42-
fi
43-
fi
54+
ensure_python3_venv "$CLI_VIRENV_PATH"
4455
. $CLI_VIRENV_PATH/bin/activate
4556
cd $cwd
4657
pip install -i $pypi_path --trusted-host localhost --ignore-installed zstackcli-*.tar.gz apibinding-*.tar.gz
@@ -71,7 +82,7 @@ if [ $tool = 'zstack-cli' ]; then
7182

7283
elif [ $tool = 'zstack-ctl' ]; then
7384
CTL_VIRENV_PATH=/var/lib/zstack/virtualenv/zstackctl
74-
rm -rf $CTL_VIRENV_PATH && python3.11 -m venv $CTL_VIRENV_PATH || exit 1
85+
ensure_python3_venv "$CTL_VIRENV_PATH"
7586
. $CTL_VIRENV_PATH/bin/activate
7687
cd $cwd
7788
TMPDIR=/usr/local/zstack/ pip install -i $pypi_path --trusted-host localhost --ignore-installed zstackctl-*.tar.gz || exit 1
@@ -82,18 +93,15 @@ elif [ $tool = 'zstack-ctl' ]; then
8293

8394
elif [ $tool = 'zstack-sys' ]; then
8495
SYS_VIRENV_PATH=/var/lib/zstack/virtualenv/zstacksys
85-
NEED_INSTALL=false
86-
if [ -d $SYS_VIRENV_PATH ]; then
87-
. $SYS_VIRENV_PATH/bin/activate
88-
if ! ansible --version | grep -q 'core 2.11.12.3'; then
89-
deactivate
90-
NEED_INSTALL=true
91-
fi
92-
else
93-
NEED_INSTALL=true
96+
ensure_python3_venv "$SYS_VIRENV_PATH"
97+
RE_INSTALL=false
98+
. $SYS_VIRENV_PATH/bin/activate
99+
if ! ansible --version | grep -q 'core 2.16.14'; then
100+
deactivate
101+
RE_INSTALL=true
94102
fi
95-
if $NEED_INSTALL; then
96-
rm -rf $SYS_VIRENV_PATH && python3.11 -m venv /var/lib/zstack/virtualenv/zstacksys || exit 1
103+
if $RE_INSTALL; then
104+
rm -rf $SYS_VIRENV_PATH && python3.11 -m venv $SYS_VIRENV_PATH || exit 1
97105
. $SYS_VIRENV_PATH/bin/activate
98106
cd $cwd
99107
#TMPDIR=/usr/local/zstack/ pip install -i $pypi_path --trusted-host localhost --ignore-installed setuptools==65.5.1 || exit 1
@@ -112,7 +120,7 @@ LC_ALL=en_US.utf8
112120
export LANG LC_ALL
113121
. ${VIRTUAL_ENV}/bin/activate
114122
115-
ansible \$@
123+
\${VIRTUAL_ENV}/bin/ansible \$@
116124
EOF
117125
chmod +x /usr/bin/ansible
118126

@@ -129,7 +137,7 @@ LC_ALL=en_US.utf8
129137
export LANG LC_ALL
130138
. ${VIRTUAL_ENV}/bin/activate
131139
132-
ansible-playbook \$@
140+
\${VIRTUAL_ENV}/bin/ansible-playbook \$@
133141
EOF
134142
chmod +x /usr/bin/ansible-playbook
135143
fi

0 commit comments

Comments
 (0)