Skip to content

Commit ce67d5f

Browse files
littleyaMatheMatrix
authored andcommitted
<fix>[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
1 parent ecdf9cd commit ce67d5f

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

conf/tools/install.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,60 @@ elif [ $tool = 'zstack-ctl' ]; then
8181
chmod +x /usr/bin/zstack-ctl
8282
python $CTL_VIRENV_PATH/lib/python2.7/site-packages/zstackctl/generate_zstackctl_bash_completion.py
8383

84+
elif [ $tool = 'zstack-sys' ]; then
85+
SYS_VIRENV_PATH=/var/lib/zstack/virtualenv/zstacksys
86+
NEED_INSTALL=false
87+
if [ -d $SYS_VIRENV_PATH ]; then
88+
. $SYS_VIRENV_PATH/bin/activate
89+
if ! ansible --version | grep -q 'core 2.11.12.3'; then
90+
deactivate
91+
NEED_INSTALL=true
92+
fi
93+
else
94+
NEED_INSTALL=true
95+
fi
96+
if $NEED_INSTALL; then
97+
rm -rf $SYS_VIRENV_PATH && virtualenv $SYS_VIRENV_PATH --python=python2.7 || exit 1
98+
. $SYS_VIRENV_PATH/bin/activate
99+
cd $cwd
100+
pip install -i $pypi_path --trusted-host localhost --ignore-installed setuptools==39.2.0 || exit 1
101+
pip install -i $pypi_path --trusted-host localhost --ignore-installed ansible==4.10.0 || exit 1
102+
103+
cat > /usr/bin/ansible << EOF
104+
#! /bin/sh
105+
VIRTUAL_ENV=/var/lib/zstack/virtualenv/zstacksys
106+
if [ ! -d $VIRTUAL_ENV ]; then
107+
echo "Need to install zstacksys before using it"
108+
exit 1
109+
fi
110+
111+
LANG=en_US.UTF-8
112+
LC_ALL=en_US.utf8
113+
export LANG LC_ALL
114+
. ${VIRTUAL_ENV}/bin/activate
115+
116+
ansible \$@
117+
EOF
118+
chmod +x /usr/bin/ansible
119+
120+
cat > /usr/bin/ansible-playbook << EOF
121+
#! /bin/sh
122+
VIRTUAL_ENV=/var/lib/zstack/virtualenv/zstacksys
123+
if [ ! -d $VIRTUAL_ENV ]; then
124+
echo "Need to install zstacksys before using it"
125+
exit 1
126+
fi
127+
128+
LANG=en_US.UTF-8
129+
LC_ALL=en_US.utf8
130+
export LANG LC_ALL
131+
. ${VIRTUAL_ENV}/bin/activate
132+
133+
ansible-playbook \$@
134+
EOF
135+
chmod +x /usr/bin/ansible-playbook
136+
fi
137+
84138
elif [ $tool = 'zstack-dashboard' ]; then
85139
UI_VIRENV_PATH=/var/lib/zstack/virtualenv/zstack-dashboard
86140
[ ! -z $force ] && rm -rf $UI_VIRENV_PATH

core/src/main/java/org/zstack/core/ansible/AnsibleFacadeImpl.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,23 @@ void init() {
131131
placePip703();
132132
placeAnsible196();
133133

134-
ShellUtils.run(String.format("if ! sudo ansible --version | grep -q 1.9.6; then " +
135-
"if grep -i -s centos /etc/system-release; then " +
136-
"sudo yum remove -y ansible; " +
137-
"elif grep -i -s ubuntu /etc/issue; then " +
138-
"sudo apt-get --assume-yes remove ansible; " +
139-
"else echo \"Warning: can't remove ansible from unknown platform\"; " +
134+
ShellUtils.run(String.format(
135+
"NEED_INSTALL=false; " +
136+
"if [ -d /var/lib/zstack/virtualenv/zstacksys ]; then " +
137+
". /var/lib/zstack/virtualenv/zstacksys/bin/activate; " +
138+
"if ! ansible --version | grep -q 'core 2.11.12'; then " +
139+
"deactivate; " +
140+
"NEED_INSTALL=true; " +
140141
"fi; " +
141-
"sudo pip install -i file://%s --trusted-host localhost -I ansible==1.9.6; " +
142-
"fi", AnsibleConstant.PYPI_REPO), false);
142+
"else " +
143+
"NEED_INSTALL=true; "+
144+
"fi; " +
145+
"if $NEED_INSTALL; then " +
146+
"sudo bash -c 'rm -rf /var/lib/zstack/virtualenv/zstacksys && virtualenv /var/lib/zstack/virtualenv/zstacksys --python=python2.7; "+
147+
". /var/lib/zstack/virtualenv/zstacksys/bin/activate; "+
148+
"pip install -i file://%s --trusted-host localhost -I setuptools==39.2.0; "+
149+
"pip install -i file://%s --trusted-host localhost -I ansible==4.10.0'; "+
150+
"fi" , AnsibleConstant.PYPI_REPO, AnsibleConstant.PYPI_REPO), false);
143151

144152
deployModule("ansible/zstacklib", "zstacklib.py");
145153
} catch (IOException e) {
@@ -236,15 +244,15 @@ private void run(Completion completion) {
236244
try {
237245
String output;
238246
if (AnsibleGlobalProperty.DEBUG_MODE2) {
239-
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s' | tee -a %s",
247+
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",
240248
AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments), AnsibleConstant.LOG_PATH),
241249
AnsibleConstant.ROOT_DIR);
242250
} else if (AnsibleGlobalProperty.DEBUG_MODE) {
243-
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s -vvvv --private-key %s -e '%s'",
251+
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'\\'",
244252
AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments)),
245253
AnsibleConstant.ROOT_DIR);
246254
} else {
247-
output = ShellUtils.run(String.format("PYTHONPATH=%s timeout %d %s %s -i %s --private-key %s -e '%s'",
255+
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'\\'",
248256
AnsibleConstant.ZSTACKLIB_ROOT, timeout, executable, playBookPath, AnsibleConstant.INVENTORY_FILE, msg.getPrivateKeyFile(), JSONObjectUtil.dumpPretty(arguments)),
249257
AnsibleConstant.ROOT_DIR);
250258
}

0 commit comments

Comments
 (0)