From daa602baa900530fbd0a49ad67e7e1b5e0ae9343 Mon Sep 17 00:00:00 2001 From: Brian Rosmaita Date: Tue, 1 Mar 2022 09:13:09 -0500 Subject: [PATCH] Change default value for [wsgi]/python_interpreter The default value is being set too early. We don't want to use the value of sys.executable at the time the config is generated, because that's unlikely to map to an existing interpreter in an actual deployment. Change-Id: Ic40f582f83e04c6915a3fcb231d6d95ca071c100 Closes-bug: #1962581 --- .../async_/flows/plugins/image_conversion.py | 3 ++- glance/common/config.py | 11 +++++----- .../flows/plugins/test_image_conversion.py | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/glance/async_/flows/plugins/image_conversion.py b/glance/async_/flows/plugins/image_conversion.py index 32c7b7fe09..b74fea1f3d 100644 --- a/glance/async_/flows/plugins/image_conversion.py +++ b/glance/async_/flows/plugins/image_conversion.py @@ -15,6 +15,7 @@ import json import os +import sys from oslo_concurrency import processutils as putils from oslo_config import cfg @@ -69,7 +70,7 @@ class _ConvertImage(task.Task): self.action_wrapper = action_wrapper self.image_id = action_wrapper.image_id self.dest_path = "" - self.python = CONF.wsgi.python_interpreter + self.python = CONF.wsgi.python_interpreter or sys.executable super(_ConvertImage, self).__init__( name='%s-Convert_Image-%s' % (task_type, task_id)) diff --git a/glance/common/config.py b/glance/common/config.py index 5f7a02f4ca..59091422b2 100644 --- a/glance/common/config.py +++ b/glance/common/config.py @@ -19,7 +19,6 @@ Routines for configuring Glance import logging import os -import sys from oslo_config import cfg from oslo_middleware import cors @@ -641,13 +640,13 @@ may overwhelm other system resources such as disk or outbound network bandwidth. If this is too small, image import requests will have to wait until a thread becomes available to begin processing.""")), cfg.StrOpt('python_interpreter', - default=sys.executable, + default=None, help=_(""" Path to the python interpreter to use when spawning external -processes. By default this is sys.executable, which should be the -same interpreter running Glance itself. However, in some situations -(i.e. uwsgi) this may not actually point to a python interpreter -itself.""")), +processes. If left unspecified, this will be sys.executable, which should +be the same interpreter running Glance itself. However, in some situations +(for example, uwsgi) sys.executable may not actually point to a python +interpreter and an alternative value must be set.""")), ] diff --git a/glance/tests/unit/async_/flows/plugins/test_image_conversion.py b/glance/tests/unit/async_/flows/plugins/test_image_conversion.py index eaa84f43db..c5ca29b56a 100644 --- a/glance/tests/unit/async_/flows/plugins/test_image_conversion.py +++ b/glance/tests/unit/async_/flows/plugins/test_image_conversion.py @@ -15,6 +15,7 @@ import json import os +import sys from unittest import mock import glance_store @@ -264,3 +265,22 @@ class TestConvertImageTask(test_utils.BaseTestCase): os_exists_mock.return_value = True image_convert.revert(result=mock.MagicMock()) self.assertEqual(1, mock_os_remove.call_count) + + def test_image_convert_interpreter_resolution(self): + # By default, wsgi.python_interpreter is None, which we should + # translate to sys.executable at runtime. + convert = image_conversion._ConvertImage(self.context, + self.task.task_id, + self.task_type, + self.wrapper) + self.assertEqual(sys.executable, convert.python) + + # If overridden, we should take the interpreter from config. + fake_interpreter = '/usr/bin/python2.7' + self.config(python_interpreter=fake_interpreter, + group='wsgi') + convert = image_conversion._ConvertImage(self.context, + self.task.task_id, + self.task_type, + self.wrapper) + self.assertEqual(fake_interpreter, convert.python)