Revert "Revert "Add the process environment to zuul.conf parser""

Updates the environment variable processing to only affect variables
prefixed with ZUUL_.

Adds a test showing the os.environ with % in it.

This reverts commit b3929b5633.

Change-Id: Ic6c3dd0327ef70dc1375486827e4503a4cea9bfc
This commit is contained in:
Monty Taylor 2019-11-01 09:18:46 +09:00
parent 1986d263c8
commit 8014a8286c
6 changed files with 60 additions and 2 deletions

View File

@ -75,6 +75,11 @@ location may be supplied on the command line) which uses an INI file
syntax. Each component may have its own configuration file, though syntax. Each component may have its own configuration file, though
you may find it simpler to use the same file for all components. you may find it simpler to use the same file for all components.
Zuul will interpolate environment variables starting with the ``ZUUL_``
prefix given in the config file escaped as python string expansion.
``foo=%(ZUUL_HOME)s`` will set the value of ``foo`` to the same value
as the environment variable named ``ZUUL_HOME``.
An example ``zuul.conf``: An example ``zuul.conf``:
.. code-block:: ini .. code-block:: ini

View File

@ -46,6 +46,7 @@ services:
- http_proxy - http_proxy
- https_proxy - https_proxy
- no_proxy=${no_proxy},gerrit - no_proxy=${no_proxy},gerrit
- ZUUL_MYSQL_PASSWORD=secret
command: "sh -c '/var/playbooks/wait-to-start.sh && zuul-scheduler -d'" command: "sh -c '/var/playbooks/wait-to-start.sh && zuul-scheduler -d'"
# FIXME: The scheduler has no ansible anymore so use the executor image. # FIXME: The scheduler has no ansible anymore so use the executor image.
# This needs to be changes such that ansible is not required for startup. # This needs to be changes such that ansible is not required for startup.
@ -62,6 +63,8 @@ services:
ports: ports:
- "9000:9000" - "9000:9000"
image: zuul/zuul-web image: zuul/zuul-web
environment:
ZUUL_MYSQL_PASSWORD: secret
volumes: volumes:
- "./etc_zuul/:/etc/zuul/:z" - "./etc_zuul/:/etc/zuul/:z"
- "./playbooks/:/var/playbooks/:z" - "./playbooks/:/var/playbooks/:z"
@ -71,6 +74,7 @@ services:
- http_proxy - http_proxy
- https_proxy - https_proxy
- no_proxy=${no_proxy},gerrit,scheduler - no_proxy=${no_proxy},gerrit,scheduler
- ZUUL_MYSQL_PASSWORD=secret
command: "zuul-executor -d" command: "zuul-executor -d"
depends_on: depends_on:
- scheduler - scheduler

View File

@ -28,7 +28,7 @@ baseurl=https://opendev.org
[connection "mysql"] [connection "mysql"]
name=mysql name=mysql
driver=sql driver=sql
dburi=mysql+pymysql://zuul:secret@mysql/zuul dburi=mysql+pymysql://zuul:%(ZUUL_MYSQL_PASSWORD)s@mysql/zuul
[web] [web]
listen_address=0.0.0.0 listen_address=0.0.0.0

42
tests/unit/test_cmd.py Normal file
View File

@ -0,0 +1,42 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import fixtures
import tempfile
import testtools
import zuul.cmd
FAKE_CONFIG = b'''
[DEFAULT]
foo=%(ZUUL_ENV_TEST)s
'''
class TestCmd(testtools.TestCase):
def test_read_config_with_environment(self):
"Test that readConfig interpolates environment vars"
self.useFixture(fixtures.EnvironmentVariable(
'HISTTIMEFORMAT', '%Y-%m-%dT%T%z '))
self.useFixture(fixtures.EnvironmentVariable(
'ZUUL_ENV_TEST', 'baz'))
with tempfile.NamedTemporaryFile() as test_config:
test_config.write(FAKE_CONFIG)
test_config.flush()
app = zuul.cmd.ZuulApp()
app.parseArguments(['-c', test_config.name])
app.readConfig()
self.assertEquals('baz', app.config.get('DEFAULT', 'foo'))

View File

@ -39,7 +39,10 @@ class Dummy(object):
class TestJob(BaseTestCase): class TestJob(BaseTestCase):
def setUp(self): def setUp(self):
self._env_fixture = self.useFixture(
fixtures.EnvironmentVariable('HISTTIMEFORMAT', '%Y-%m-%dT%T%z '))
super(TestJob, self).setUp() super(TestJob, self).setUp()
# Toss in % in env vars to trigger the configparser issue
self.connections = zuul.lib.connections.ConnectionRegistry() self.connections = zuul.lib.connections.ConnectionRegistry()
self.addCleanup(self.connections.stop) self.addCleanup(self.connections.stop)
self.connection = Dummy(connection_name='dummy_connection') self.connection = Dummy(connection_name='dummy_connection')

View File

@ -123,7 +123,11 @@ class ZuulApp(object):
return parser return parser
def readConfig(self): def readConfig(self):
self.config = configparser.ConfigParser() safe_env = {
k: v for k, v in os.environ.items()
if k.startswith('ZUUL_')
}
self.config = configparser.ConfigParser(safe_env)
if self.args.config: if self.args.config:
locations = [self.args.config] locations = [self.args.config]
else: else: