From ce67d5f45e49746b53c889117b4b1f8494404b5a Mon Sep 17 00:00:00 2001 From: littleya Date: Tue, 13 Jun 2023 21:20:35 +0800 Subject: [PATCH] [conf]: Isolating ansible with python venv Since there will be manually installed python pacakges at the system level, this may cause ansible installnation failed due to conflicts. Therefore, install ansible into a new python virtual environment at /var/lib/zstack/virtualenv/zstacksys. Resolves: ZSTAC-56494 Change-Id: I6a6165736a62686b61736c6963787765636c7670 --- conf/tools/install.sh | 54 +++++++++++++++++++ .../core/ansible/AnsibleFacadeImpl.java | 30 +++++++---- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/conf/tools/install.sh b/conf/tools/install.sh index b79bb019117..12a92dbe913 100755 --- a/conf/tools/install.sh +++ b/conf/tools/install.sh @@ -81,6 +81,60 @@ elif [ $tool = 'zstack-ctl' ]; then chmod +x /usr/bin/zstack-ctl python $CTL_VIRENV_PATH/lib/python2.7/site-packages/zstackctl/generate_zstackctl_bash_completion.py +elif [ $tool = 'zstack-sys' ]; then + SYS_VIRENV_PATH=/var/lib/zstack/virtualenv/zstacksys + NEED_INSTALL=false + if [ -d $SYS_VIRENV_PATH ]; then + . $SYS_VIRENV_PATH/bin/activate + if ! ansible --version | grep -q 'core 2.11.12.3'; then + deactivate + NEED_INSTALL=true + fi + else + NEED_INSTALL=true + fi + if $NEED_INSTALL; then + rm -rf $SYS_VIRENV_PATH && virtualenv $SYS_VIRENV_PATH --python=python2.7 || exit 1 + . $SYS_VIRENV_PATH/bin/activate + cd $cwd + pip install -i $pypi_path --trusted-host localhost --ignore-installed setuptools==39.2.0 || exit 1 + pip install -i $pypi_path --trusted-host localhost --ignore-installed ansible==4.10.0 || exit 1 + + cat > /usr/bin/ansible << EOF +#! /bin/sh +VIRTUAL_ENV=/var/lib/zstack/virtualenv/zstacksys +if [ ! -d $VIRTUAL_ENV ]; then + echo "Need to install zstacksys before using it" + exit 1 +fi + +LANG=en_US.UTF-8 +LC_ALL=en_US.utf8 +export LANG LC_ALL +. ${VIRTUAL_ENV}/bin/activate + +ansible \$@ +EOF + chmod +x /usr/bin/ansible + + cat > /usr/bin/ansible-playbook << EOF +#! /bin/sh +VIRTUAL_ENV=/var/lib/zstack/virtualenv/zstacksys +if [ ! -d $VIRTUAL_ENV ]; then + echo "Need to install zstacksys before using it" + exit 1 +fi + +LANG=en_US.UTF-8 +LC_ALL=en_US.utf8 +export LANG LC_ALL +. ${VIRTUAL_ENV}/bin/activate + +ansible-playbook \$@ +EOF + chmod +x /usr/bin/ansible-playbook + fi + elif [ $tool = 'zstack-dashboard' ]; then UI_VIRENV_PATH=/var/lib/zstack/virtualenv/zstack-dashboard [ ! -z $force ] && rm -rf $UI_VIRENV_PATH diff --git a/core/src/main/java/org/zstack/core/ansible/AnsibleFacadeImpl.java b/core/src/main/java/org/zstack/core/ansible/AnsibleFacadeImpl.java index 400420cfd8e..95278943347 100755 --- a/core/src/main/java/org/zstack/core/ansible/AnsibleFacadeImpl.java +++ b/core/src/main/java/org/zstack/core/ansible/AnsibleFacadeImpl.java @@ -131,15 +131,23 @@ void init() { placePip703(); placeAnsible196(); - ShellUtils.run(String.format("if ! sudo ansible --version | grep -q 1.9.6; then " + - "if grep -i -s centos /etc/system-release; then " + - "sudo yum remove -y ansible; " + - "elif grep -i -s ubuntu /etc/issue; then " + - "sudo apt-get --assume-yes remove ansible; " + - "else echo \"Warning: can't remove ansible from unknown platform\"; " + + ShellUtils.run(String.format( + "NEED_INSTALL=false; " + + "if [ -d /var/lib/zstack/virtualenv/zstacksys ]; then " + + ". /var/lib/zstack/virtualenv/zstacksys/bin/activate; " + + "if ! ansible --version | grep -q 'core 2.11.12'; then " + + "deactivate; " + + "NEED_INSTALL=true; " + "fi; " + - "sudo pip install -i file://%s --trusted-host localhost -I ansible==1.9.6; " + - "fi", AnsibleConstant.PYPI_REPO), false); + "else " + + "NEED_INSTALL=true; "+ + "fi; " + + "if $NEED_INSTALL; then " + + "sudo bash -c 'rm -rf /var/lib/zstack/virtualenv/zstacksys && virtualenv /var/lib/zstack/virtualenv/zstacksys --python=python2.7; "+ + ". /var/lib/zstack/virtualenv/zstacksys/bin/activate; "+ + "pip install -i file://%s --trusted-host localhost -I setuptools==39.2.0; "+ + "pip install -i file://%s --trusted-host localhost -I ansible==4.10.0'; "+ + "fi" , AnsibleConstant.PYPI_REPO, AnsibleConstant.PYPI_REPO), false); deployModule("ansible/zstacklib", "zstacklib.py"); } catch (IOException e) { @@ -236,15 +244,15 @@ private void run(Completion completion) { try { String output; if (AnsibleGlobalProperty.DEBUG_MODE2) { - output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s' | tee -a %s", + output = ShellUtils.run(String.format("bash -c '. /var/lib/zstack/virtualenv/zstacksys/bin/activate; PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '\\''%s'\\' | tee -a %s", AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments), AnsibleConstant.LOG_PATH), AnsibleConstant.ROOT_DIR); } else if (AnsibleGlobalProperty.DEBUG_MODE) { - output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s'", + output = ShellUtils.run(String.format("bash -c '. /var/lib/zstack/virtualenv/zstacksys/bin/activate; PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '\\''%s'\\'", AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments)), AnsibleConstant.ROOT_DIR); } else { - output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s --private-key %s -e '%s'", + output = ShellUtils.run(String.format("bash -c '. /var/lib/zstack/virtualenv/zstacksys/bin/activate; PYTHONPATH=%s timeout %d %s %s -i %s --private-key %s -e '\\''%s'\\'", AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments)), AnsibleConstant.ROOT_DIR); }