From c139a998b82761cd5405ee1caa1847f3a23d89b9 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Fri, 23 Oct 2020 10:51:48 -0400 Subject: [PATCH] simplify how awx "open" licensing works --- awx/api/generics.py | 3 ++- awx/main/utils/common.py | 7 +++++-- awx/main/utils/licensing.py | 29 +++++++++++++---------------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/awx/api/generics.py b/awx/api/generics.py index 0c6f2637f6..b618a826fb 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -187,6 +187,7 @@ class APIView(views.APIView): ''' Log warning for 400 requests. Add header with elapsed time. ''' + from awx.main.utils.common import get_licenser # # If the URL was rewritten, and we get a 404, we should entirely @@ -223,7 +224,7 @@ class APIView(views.APIView): response = super(APIView, self).finalize_response(request, response, *args, **kwargs) time_started = getattr(self, 'time_started', None) response['X-API-Product-Version'] = get_awx_version() - response['X-API-Product-Name'] = 'AWX' if settings.LICENSE.get('license_type', 'UNLICENSED') in 'open' else 'Red Hat Ansible Tower' + response['X-API-Product-Name'] = 'AWX' if get_licenser().validate().get('license_type') == 'open' else 'Red Hat Ansible Tower' response['X-API-Node'] = settings.CLUSTER_HOST_ID if time_started: diff --git a/awx/main/utils/common.py b/awx/main/utils/common.py index cc2c78976d..1ec2812e12 100644 --- a/awx/main/utils/common.py +++ b/awx/main/utils/common.py @@ -202,10 +202,13 @@ def get_awx_http_client_headers(): def get_licenser(*args, **kwargs): + from awx.main.utils.licensing import Licenser, OpenLicense try: # Get License Config from db? - from awx.main.utils.licensing import Licenser - return Licenser(*args, **kwargs) + if os.path.exists('/var/lib/awx/.tower_version'): + return Licenser(*args, **kwargs) + else: + return OpenLicense() except Exception as e: raise ValueError(_('Error importing Tower License: %s') % e) diff --git a/awx/main/utils/licensing.py b/awx/main/utils/licensing.py index 67d3a4e4d0..a83947c7cf 100644 --- a/awx/main/utils/licensing.py +++ b/awx/main/utils/licensing.py @@ -17,7 +17,6 @@ import copy import io import json import logging -import os import re import requests import time @@ -68,6 +67,16 @@ def validate_entitlement_manifest(data): return json.loads(z.open(f).read()) +class OpenLicense(object): + def validate(self): + return dict( + license_type='open', + valid_key=True, + subscription_name='OPEN', + product_name="AWX", + ) + + class Licenser(object): # warn when there is a month (30 days) left on the license LICENSE_TIMEOUT = 60 * 60 * 24 * 30 @@ -98,23 +107,11 @@ class Licenser(object): if 'company_name' in kwargs: kwargs.pop('company_name') self._attrs.update(kwargs) - if os.path.exists('/var/lib/awx/.tower_version'): - if 'valid_key' in self._attrs: - if not self._attrs['valid_key']: - self._unset_attrs() - else: + if 'valid_key' in self._attrs: + if not self._attrs['valid_key']: self._unset_attrs() else: - self._generate_open_config() - - - def _generate_open_config(self): - self._attrs.update(dict(license_type='open', - valid_key=True, - subscription_name='OPEN', - product_name="AWX", - )) - settings.LICENSE = self._attrs + self._unset_attrs() def _unset_attrs(self):