Skip to content

Commit

Permalink
[windows] add flare function
Browse files Browse the repository at this point in the history
Add a Windows flare function inside the GUI, using only popups (because it's
the easiest way to do this).

Flare: reuse filehandle instead of file path (filehandle was not closed,
causing issues on Windows)
  • Loading branch information
degemer committed Jul 17, 2015
1 parent 8cb24a5 commit 279111e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
44 changes: 28 additions & 16 deletions utils/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,39 @@ def collect(self):
self._tar.close()

# Upload the tar file
def upload(self):
def upload(self, email=None):
self._check_size()

if self._cmdline:
self._ask_for_confirmation()

email = self._ask_for_email()
if not email:
email = self._ask_for_email()

log.info("Uploading {0} to Datadog Support".format(self._tar_path))
url = self._url
if self._case_id:
url = '{0}/{1}'.format(self._url, str(self._case_id))
url = "{0}?api_key={1}".format(url, self._api_key)
files = {'flare_file': open(self._tar_path, 'rb')}
data = {
'case_id': self._case_id,
'hostname': self._hostname,
'email': email
requests_options = {
'data': {
'case_id': self._case_id,
'hostname': self._hostname,
'email': email
},
'files': {'flare_file': open(self._tar_path, 'rb')},
'timeout': self.TIMEOUT
}
self._resp = requests.post(url, files=files, data=data,
timeout=self.TIMEOUT)
if Platform.is_windows():
requests_options['verify'] = os.path.realpath(os.path.join(
os.path.dirname(os.path.realpath(__file__)),
os.pardir, os.pardir,
'datadog-cert.pem'
))

self._resp = requests.post(url, **requests_options)
self._analyse_result()
return self._case_id

# Start by creating the tar file which will contain everything
def _init_tarfile(self):
Expand Down Expand Up @@ -318,9 +329,9 @@ def _can_read(cls, f, output=True, warn=True):

# Return path to a temp file without comment
def _strip_comment(self, file_path):
_, temp_path = tempfile.mkstemp(prefix='dd')
fh, temp_path = tempfile.mkstemp(prefix='dd')
atexit.register(os.remove, temp_path)
with open(temp_path, 'w') as temp_file:
with os.fdopen(fh, 'w') as temp_file:
with open(file_path, 'r') as orig_file:
for line in orig_file.readlines():
if not self.COMMENT_REGEX.match(line):
Expand All @@ -342,9 +353,9 @@ def _add_clean_confd(self, file_path):

# Return path to a temp file without password and comment
def _strip_password(self, file_path):
_, temp_path = tempfile.mkstemp(prefix='dd')
fh, temp_path = tempfile.mkstemp(prefix='dd')
atexit.register(os.remove, temp_path)
with open(temp_path, 'w') as temp_file:
with os.fdopen(fh, 'w') as temp_file:
with open(file_path, 'r') as orig_file:
password_found = ''
for line in orig_file.readlines():
Expand All @@ -360,8 +371,8 @@ def _strip_password(self, file_path):
# Add output of the command to the tarfile
def _add_command_output_tar(self, name, command, command_desc=None):
out, err, _ = self._capture_output(command, print_exc_to_stderr=False)
_, temp_path = tempfile.mkstemp(prefix='dd')
with open(temp_path, 'w') as temp_file:
fh, temp_path = tempfile.mkstemp(prefix='dd')
with os.fdopen(fh, 'w') as temp_file:
if command_desc:
temp_file.write(">>>> CMD <<<<\n")
temp_file.write(command_desc)
Expand Down Expand Up @@ -516,10 +527,11 @@ def _analyse_result(self):
self._resp.raise_for_status()
try:
json_resp = self._resp.json()
self._case_id = self._resp.json()['case_id']
# Failed parsing
except ValueError:
raise Exception('An unknown error has occured - '
'Please contact support by email')
# Finally, correct
log.info("Your logs were successfully uploaded. For future reference,"
" your internal case id is {0}".format(json_resp['case_id']))
" your internal case id is {0}".format(self._case_id))
36 changes: 35 additions & 1 deletion win32/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@
get_version
)
from util import yLoader
from utils.flare import Flare
from utils.platform import Platform


# Constants describing the agent state
AGENT_RUNNING = 0
AGENT_START_PENDING = 1
Expand Down Expand Up @@ -550,6 +550,7 @@ class Menu(QMenu):
START = "Start"
STOP = "Stop"
RESTART = "Restart"
FLARE = "Flare"
MAC_LOGIN = '{0} at login'
EXIT = "Exit"
SYSTEM_EVENTS_CMD = 'tell application "System Events" to {0}'
Expand All @@ -573,6 +574,8 @@ def __init__(self, parent=None):
if Platform.is_mac():
self.add_option(self.MAC_LOGIN.format(self.enable_or_disable_mac()),
lambda: self.enable_or_disable_login())
elif Platform.is_windows():
self.add_option(self.FLARE, lambda: thread.start_new_thread(windows_flare, ()))

# And finally the exit
self.add_option(self.EXIT, lambda: sys.exit(0))
Expand Down Expand Up @@ -750,6 +753,37 @@ def agent_manager(action, async=True):
thread.start_new_thread(manager, (action,))


def windows_flare():
case_id, ok = QInputDialog.getInteger(
None, "Flare",
"Your logs and configuration files are going to be collected and "
"sent to Datadog Support. Please enter your ticket number if you have one:",
value=0, min=0
)
if not ok:
info_popup("Flare cancelled")
return
case_id = int(case_id) if case_id != 0 else None
f = Flare(case_id=case_id)
f.collect()
email, ok = QInputDialog.getText(
None, "Your email",
"Logs and configuration files have been collected"
" Please enter your email address:"
)
if not ok:
info_popup("Flare cancelled")
return
try:
case_id = f.upload(email=str(email))
info_popup("Your logs were successfully uploaded. For future reference,"
" your internal case id is {0}".format(case_id))
except Exception, e:
warning_popup('The upload failed:\n{0}'.format(str(e)))
finally:
return


def warning_popup(message, parent=None):
QMessageBox.warning(parent, 'Message', message, QMessageBox.Ok)

Expand Down

0 comments on commit 279111e

Please sign in to comment.