Merge pull request #4627 from rooftopcellist/upload_directly

Upload directly

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2019-09-12 14:40:53 +00:00
committed by GitHub
2 changed files with 34 additions and 27 deletions

View File

@@ -5,10 +5,9 @@ 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
@@ -85,13 +84,12 @@ def gather(dest=None, module=None, collection_type='scheduled'):
if last_run < max_interval or not last_run: if last_run < max_interval or not last_run:
last_run = max_interval last_run = max_interval
if _valid_license() is False: if _valid_license() is False:
logger.exception("Invalid License provided, or No License Provided") logger.exception("Invalid License provided, or No License Provided")
return "Error: Invalid License provided, or No License Provided" return "Error: Invalid License provided, or No License Provided"
if not settings.INSIGHTS_TRACKING_STATE: if not settings.INSIGHTS_TRACKING_STATE:
logger.error("Insights analytics not enabled") logger.error("Automation Analytics not enabled")
return return
if module is None: if module is None:
@@ -146,30 +144,39 @@ 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 to the Insights API
""" """
if not path:
logger.error('Automation Analytics TAR not found')
return
if "Error:" in str(path):
return
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 = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
cmd = [ if not url:
agent, '--payload', path, '--content-type', settings.INSIGHTS_AGENT_MIME logger.error('AUTOMATION_ANALYTICS_URL is not set')
] return
output = smart_str(subprocess.check_output(cmd, timeout=60 * 5)) rh_user = getattr(settings, 'REDHAT_USERNAME', None)
logger.debug(output) rh_password = getattr(settings, 'REDHAT_PASSWORD', None)
# reset the `last_run` when data is shipped if not rh_user:
run_now = now() return logger.error('REDHAT_USERNAME is not set')
state = TowerAnalyticsState.get_solo() if not rh_password:
state.last_run = run_now return logger.error('REDHAT_PASSWORD is not set')
state.save() with open(path, 'rb') as f:
files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
except subprocess.CalledProcessError: response = requests.post(url,
logger.exception('{} failure:'.format(cmd)) files=files,
except subprocess.TimeoutExpired: verify=True,
logger.exception('{} timeout:'.format(cmd)) auth=(rh_user, rh_password),
timeout=(31, 31))
if response.status_code != 202:
return logger.exception('Upload failed with status {}, {}'.format(response.status_code,
response.text))
run_now = now()
state = TowerAnalyticsState.get_solo()
state.last_run = run_now
state.save()
finally: finally:
# cleanup tar.gz # cleanup tar.gz
os.remove(path) os.remove(path)

View File

@@ -153,7 +153,7 @@ register(
register( register(
'AUTOMATION_ANALYTICS_URL', 'AUTOMATION_ANALYTICS_URL',
field_class=fields.URLField, field_class=fields.URLField,
default='https://cloud.redhat.com', default='https://example.com',
schemes=('http', 'https'), schemes=('http', 'https'),
allow_plain_hostname=True, # Allow hostname only without TLD. allow_plain_hostname=True, # Allow hostname only without TLD.
label=_('Automation Analytics upload URL.'), label=_('Automation Analytics upload URL.'),