Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for expanding environment variables in configuration files. #378

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/build-artifacts-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ docker run --name ${CONTAINER_NAME} -v ${ROOT_DIR}:${CONTAINER_DIR} --rm -i ${BA
pip install Babel==2.5.3
pip install cffi==1.11.5
pip install PyNaCl==1.2.1
pip install idna==2.7
pip install cryptography==2.1.1
pip install -r requirements.txt
export PYTHONPATH=${PYTHONPATH}:$(pwd)
Expand Down
8 changes: 3 additions & 5 deletions packaging/bdist_prestoadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ def package_dependencies(self, build_dir):
thirdparty_dir = os.path.join(build_dir, 'third-party')

requirements = self.distribution.install_requires
for requirement in requirements:
pip.main(['wheel',
'--wheel-dir={0}'.format(thirdparty_dir),
'--no-cache',
requirement])
pip.main(['wheel',
'--wheel-dir={0}'.format(thirdparty_dir),
'--no-cache'] + requirements)

pip.main(['install',
'-d',
Expand Down
6 changes: 6 additions & 0 deletions prestoadmin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def get_conf_from_properties_data(data):
if len(line) > 0 and line[0] not in COMMENT_CHARS:
pair = split_to_pair(line)
props[pair[0]] = pair[1]
return expand_environment_variables(props)


def expand_environment_variables(props):
for key in props:
props[key] = os.path.expandvars(props[key])
return props


Expand Down
3 changes: 2 additions & 1 deletion tests/product/prestoadmin_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def _build_installer_in_docker(self, cluster, online_installer=None,
# use explicit versions of dependent packages
'pip install --upgrade pycparser==2.18 cffi==1.11.5\n'
'pip install --upgrade pycparser==2.18 PyNaCl==1.2.1\n'
'pip install --upgrade pycparser==2.18 cryptography==2.1.1\n'
'pip install idna==2.7\n'
'pip install cryptography==2.1.1\n'
'pip install --upgrade pip==7.1.2\n'
'pip install --upgrade wheel==0.23.0\n'
'pip install --upgrade setuptools==20.1.1\n'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

master: Configuration file at /etc/presto/node.properties:
node.id=.*
catalog.config-dir=/etc/presto/catalog
Expand Down Expand Up @@ -32,6 +33,11 @@ query.max-memory-per-node=512MB
query.max-memory=50GB


master: Configuration file at /etc/presto/log.properties:
# Enable verbose logging from Presto
#com.facebook.presto=DEBUG


slave1: Configuration file at /etc/presto/node.properties:
node.id=.*
catalog.config-dir=/etc/presto/catalog
Expand Down Expand Up @@ -62,3 +68,8 @@ discovery.uri=http://master:7070
http-server.http.port=7070
query.max-memory-per-node=512MB
query.max-memory=50GB


slave1: Configuration file at /etc/presto/log.properties:
# Enable verbose logging from Presto
#com.facebook.presto=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ query.max-memory-per-node=512MB
query.max-memory=50GB


slave2: Configuration file at /etc/presto/log.properties:
# Enable verbose logging from Presto
#com.facebook.presto=DEBUG


slave3: Configuration file at /etc/presto/node.properties:
node.id=.*
catalog.config-dir=/etc/presto/catalog
Expand Down Expand Up @@ -60,3 +65,8 @@ discovery.uri=http://master:7070
http-server.http.port=7070
query.max-memory-per-node=512MB
query.max-memory=50GB


slave3: Configuration file at /etc/presto/log.properties:
# Enable verbose logging from Presto
#com.facebook.presto=DEBUG
1 change: 1 addition & 0 deletions tests/unit/resources/environment_variable.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=${ENVIRONMENT_VARIABLE}
3 changes: 2 additions & 1 deletion tests/unit/resources/valid.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
prop1
prop2
prop3
prop3
${ENVIRONMENT_VARIABLE}
2 changes: 1 addition & 1 deletion tests/unit/resources/valid.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ f===6
g:= 7
h=:8
i = 9

j=${ENVIRONMENT_VARIABLE}
11 changes: 9 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ def test_invalid_json(self):
def test_get_config(self):
config_file = os.path.join(DIR, 'resources', 'valid.config')
conf = config.get_conf_from_config_file(config_file)
self.assertEqual(conf, ['prop1', 'prop2', 'prop3'])
self.assertEqual(conf, ['prop1', 'prop2', 'prop3', '${ENVIRONMENT_VARIABLE}'])

def test_get_properties(self):
config_file = os.path.join(DIR, 'resources', 'valid.properties')
conf = config.get_conf_from_properties_file(config_file)
self.assertEqual(conf, {'a': '1', 'b': '2', 'c': '3',
'd\\=': '4', 'e\\:': '5', 'f': '==6',
'g': '= 7', 'h': ':8', 'i': '9'})
'g': '= 7', 'h': ':8', 'i': '9', 'j': '${ENVIRONMENT_VARIABLE}'})

@patch('prestoadmin.config.os.path.expandvars', return_value='environment_variable_value')
def test_expending_environment_variable(self, environment_variables):
config_file = os.path.join(DIR, 'resources', 'environment_variable.properties')
conf = config.get_conf_from_properties_file(config_file)
self.assertTrue(environment_variables.called)
self.assertEqual({'a': 'environment_variable_value'}, conf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have an real test that is using actual environment variable, to see the configuration with expanded environment variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply. I will work on this as well as automation failures.


@patch('__builtin__.open')
def test_get_properties_ignores_whitespace(self, open_mock):
Expand Down