Changes uploader to use the insights api directly

This commit is contained in:
Ben Thomasson
2019-08-23 16:19:51 -04:00
committed by Christian Adams
parent f91a02a9e4
commit 10d53637ad

View File

@@ -5,17 +5,17 @@ import os
import os.path import os.path
import tempfile import tempfile
import shutil import shutil
import subprocess import requests
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_str
from django.utils.timezone import now, timedelta from django.utils.timezone import now, timedelta
from rest_framework.exceptions import PermissionDenied from rest_framework.exceptions import PermissionDenied
from awx.conf.license import get_license from awx.conf.license import get_license
from awx.main.models import Job from awx.main.models import Job, Credential
from awx.main.access import access_registry from awx.main.access import access_registry
from awx.main.models.ha import TowerAnalyticsState from awx.main.models.ha import TowerAnalyticsState
from awx.main.utils import decrypt_field
__all__ = ['register', 'gather', 'ship', 'table_version'] __all__ = ['register', 'gather', 'ship', 'table_version']
@@ -146,30 +146,23 @@ def gather(dest=None, module=None, collection_type='scheduled'):
def ship(path): def ship(path):
""" """
Ship gathered metrics via the Insights agent Ship gathered metrics via the Insights API
""" """
try: try:
agent = 'insights-client'
if shutil.which(agent) is None:
logger.error('could not find {} on PATH'.format(agent))
return
logger.debug('shipping analytics file: {}'.format(path)) logger.debug('shipping analytics file: {}'.format(path))
try: url = settings.INSIGHTS_URL_BASE + '/api/ingress/v1/upload'
cmd = [ with open(path, 'rb') as f:
agent, '--payload', path, '--content-type', settings.INSIGHTS_AGENT_MIME files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
] creds = Credential.objects.get(name=settings.INSIGHTS_URL_BASE)
output = smart_str(subprocess.check_output(cmd, timeout=60 * 5)) response = requests.post(url, files=files, auth=(creds.inputs['username'],
logger.debug(output) decrypt_field(creds, 'password')))
# reset the `last_run` when data is shipped if response.status_code != 202:
run_now = now() logger.exception('Upload failure status {} text {}'.format(response.status_code,
state = TowerAnalyticsState.get_solo() response.text))
state.last_run = run_now run_now = now()
state.save() state = TowerAnalyticsState.get_solo()
state.last_run = run_now
except subprocess.CalledProcessError: state.save()
logger.exception('{} failure:'.format(cmd))
except subprocess.TimeoutExpired:
logger.exception('{} timeout:'.format(cmd))
finally: finally:
# cleanup tar.gz # cleanup tar.gz
os.remove(path) os.remove(path)