From 7b390fa2fcb733ad297343a5bbc37a3629526e4e Mon Sep 17 00:00:00 2001 From: olia-dev Date: Wed, 10 Jul 2019 11:28:02 +0200 Subject: [PATCH] related #4274 - moved function 'create_temporary_fifo' to 'awx/main/utils/common.py' and referenced it in other plugins Signed-off-by: olia-dev --- awx/main/credential_plugins/aim.py | 22 ++++------------------ awx/main/credential_plugins/conjur.py | 23 +++++------------------ awx/main/credential_plugins/hashivault.py | 21 ++++----------------- awx/main/utils/common.py | 18 +++++++++++++++++- 4 files changed, 30 insertions(+), 54 deletions(-) diff --git a/awx/main/credential_plugins/aim.py b/awx/main/credential_plugins/aim.py index 8fde480b73..ae466c96ed 100644 --- a/awx/main/credential_plugins/aim.py +++ b/awx/main/credential_plugins/aim.py @@ -9,6 +9,10 @@ from urllib.parse import quote, urlencode, urljoin from django.utils.translation import ugettext_lazy as _ import requests +# AWX +from awx.main.utils import ( + create_temporary_fifo, +) aim_inputs = { 'fields': [{ @@ -60,24 +64,6 @@ aim_inputs = { } -def create_temporary_fifo(data): - """Open fifo named pipe in a new thread using a temporary file path. The - thread blocks until data is read from the pipe. - - Returns the path to the fifo. - - :param data(bytes): Data to write to the pipe. - """ - path = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) - os.mkfifo(path, stat.S_IRUSR | stat.S_IWUSR) - - threading.Thread( - target=lambda p, d: open(p, 'wb').write(d), - args=(path, data) - ).start() - return path - - def aim_backend(**kwargs): url = kwargs['url'] client_cert = kwargs.get('client_cert', None) diff --git a/awx/main/credential_plugins/conjur.py b/awx/main/credential_plugins/conjur.py index c2a60f72b6..8bb40be779 100644 --- a/awx/main/credential_plugins/conjur.py +++ b/awx/main/credential_plugins/conjur.py @@ -10,6 +10,11 @@ from urllib.parse import urljoin, quote_plus from django.utils.translation import ugettext_lazy as _ import requests +# AWX +from awx.main.utils import ( + create_temporary_fifo, +) + conjur_inputs = { 'fields': [{ @@ -51,24 +56,6 @@ conjur_inputs = { } -def create_temporary_fifo(data): - """Open fifo named pipe in a new thread using a temporary file path. The - thread blocks until data is read from the pipe. - - Returns the path to the fifo. - - :param data(bytes): Data to write to the pipe. - """ - path = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) - os.mkfifo(path, stat.S_IRUSR | stat.S_IWUSR) - - threading.Thread( - target=lambda p, d: open(p, 'wb').write(d), - args=(path, data) - ).start() - return path - - def conjur_backend(**kwargs): url = kwargs['url'] api_key = kwargs['api_key'] diff --git a/awx/main/credential_plugins/hashivault.py b/awx/main/credential_plugins/hashivault.py index 3f12aad4b4..f551c3992a 100644 --- a/awx/main/credential_plugins/hashivault.py +++ b/awx/main/credential_plugins/hashivault.py @@ -11,6 +11,10 @@ from .plugin import CredentialPlugin import requests from django.utils.translation import ugettext_lazy as _ +# AWX +from awx.main.utils import ( + create_temporary_fifo, +) base_inputs = { 'fields': [{ @@ -81,23 +85,6 @@ hashi_ssh_inputs['metadata'] = [{ }] hashi_ssh_inputs['required'].extend(['public_key', 'role']) - -def create_temporary_fifo(data): - """Open fifo named pipe in a new thread using a temporary file path. The - thread blocks until data is read from the pipe. - Returns the path to the fifo. - :param data(bytes): Data to write to the pipe. - """ - path = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) - os.mkfifo(path, stat.S_IRUSR | stat.S_IWUSR) - - threading.Thread( - target=lambda p, d: open(p, 'wb').write(d), - args=(path, data) - ).start() - return path - - def kv_backend(**kwargs): token = kwargs['token'] url = urljoin(kwargs['url'], 'v1') diff --git a/awx/main/utils/common.py b/awx/main/utils/common.py index 3038fbf4fd..b16b142bce 100644 --- a/awx/main/utils/common.py +++ b/awx/main/utils/common.py @@ -44,7 +44,7 @@ __all__ = ['get_object_or_400', 'camelcase_to_underscore', 'underscore_to_camelc 'wrap_args_with_proot', 'build_proot_temp_dir', 'check_proot_installed', 'model_to_dict', 'model_instance_diff', 'parse_yaml_or_json', 'RequireDebugTrueOrTest', 'has_model_field_prefetched', 'set_environ', 'IllegalArgumentError', 'get_custom_venv_choices', 'get_external_account', - 'task_manager_bulk_reschedule', 'schedule_task_manager', 'classproperty'] + 'task_manager_bulk_reschedule', 'schedule_task_manager', 'classproperty', 'create_temporary_fifo'] def get_object_or_400(klass, *args, **kwargs): @@ -1015,3 +1015,19 @@ class classproperty: def __get__(self, instance, ownerclass): return self.fget(ownerclass) + +def create_temporary_fifo(data): + """Open fifo named pipe in a new thread using a temporary file path. The + thread blocks until data is read from the pipe. + Returns the path to the fifo. + :param data(bytes): Data to write to the pipe. + """ + path = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) + os.mkfifo(path, stat.S_IRUSR | stat.S_IWUSR) + + threading.Thread( + target=lambda p, d: open(p, 'wb').write(d), + args=(path, data) + ).start() + return path +