-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-1852] Allow hostname to be overridable.
This allows hostnames to be overridable to facilitate service discovery requirements in common production deployments. Closes #3036 from thekashifmalik/hostnames
- Loading branch information
1 parent
fc26cad
commit 6c93460
Showing
11 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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 importlib | ||
import socket | ||
from airflow.configuration import (conf, AirflowConfigException) | ||
|
||
|
||
def get_hostname(): | ||
""" | ||
Fetch the hostname using the callable from the config or using | ||
`socket.getfqdn` as a fallback. | ||
""" | ||
# First we attempt to fetch the callable path from the config. | ||
try: | ||
callable_path = conf.get('core', 'hostname_callable') | ||
except AirflowConfigException: | ||
callable_path = None | ||
|
||
# Then we handle the case when the config is missing or empty. This is the | ||
# default behavior. | ||
if not callable_path: | ||
return socket.getfqdn() | ||
|
||
# Since we have a callable path, we try to import and run it next. | ||
module_path, attr_name = callable_path.split(':') | ||
module = importlib.import_module(module_path) | ||
callable = getattr(module, attr_name) | ||
return callable() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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 unittest | ||
import mock | ||
|
||
from airflow.utils import net | ||
|
||
|
||
def get_hostname(): | ||
return 'awesomehostname' | ||
|
||
|
||
class GetHostname(unittest.TestCase): | ||
|
||
@mock.patch('airflow.utils.net.socket') | ||
@mock.patch('airflow.utils.net.conf') | ||
def test_get_hostname_unset(self, patched_conf, patched_socket): | ||
patched_conf.get = mock.Mock(return_value=None) | ||
patched_socket.getfqdn = mock.Mock(return_value='first') | ||
self.assertTrue(net.get_hostname() == 'first') | ||
|
||
@mock.patch('airflow.utils.net.conf') | ||
def test_get_hostname_set(self, patched_conf): | ||
patched_conf.get = mock.Mock( | ||
return_value='tests.utils.test_net:get_hostname' | ||
) | ||
self.assertTrue(net.get_hostname() == 'awesomehostname') | ||
|
||
@mock.patch('airflow.utils.net.conf') | ||
def test_get_hostname_set_incorrect(self, patched_conf): | ||
patched_conf.get = mock.Mock( | ||
return_value='tests.utils.test_net' | ||
) | ||
with self.assertRaises(ValueError): | ||
net.get_hostname() | ||
|
||
@mock.patch('airflow.utils.net.conf') | ||
def test_get_hostname_set_missing(self, patched_conf): | ||
patched_conf.get = mock.Mock( | ||
return_value='tests.utils.test_net:missing_func' | ||
) | ||
with self.assertRaises(AttributeError): | ||
net.get_hostname() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters