Skip to content

Commit

Permalink
[flare] add source install compatibility
Browse files Browse the repository at this point in the history
- add flare option to centos and source install commands
- rewrite the analyse_result part (dealing first with our custom 400
  errors, then with standard errors, and then with success)
- fix endpoint test
  • Loading branch information
degemer committed Mar 18, 2015
1 parent 9b12fb8 commit 8b10531
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
7 changes: 7 additions & 0 deletions packaging/centos/datadog-agent.init
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ case "$1" in
su $AGENTUSER -c "$AGENTPATH jmx $@"
exit $?
;;

flare)
shift
su $AGENTUSER -c "$AGENTPATH flare $@"
exit $?
;;

*)
echo "Usage: $0 {start|stop|restart|info|status|configcheck|configtest|jmx}"
exit 2
Expand Down
8 changes: 8 additions & 0 deletions packaging/datadog-agent/source/agent
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ case $action in
exit $?
;;


flare)
shift
python agent/agent.py flare $@
exit $?
;;


*)
echo "Usage: $0 {start|stop|restart|info|status|configcheck|check|jmx}"
exit 2
Expand Down
8 changes: 7 additions & 1 deletion tests/test_flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __init__(self, status_code=200):
self.status_code = status_code
self.text = '{"case_id":1337}'

def json(self):
return {'case_id': 1337}

def raise_for_status(self):
return None

class FlareTest(unittest.TestCase):

@mock.patch('utils.flare.strftime', side_effect=mocked_strftime)
Expand Down Expand Up @@ -99,4 +105,4 @@ def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
f.upload(confirmation=False)
raise Exception('Should fail before')
except Exception, e:
self.assertEqual(str(e), "Invalid inputs: {'email': None}")
self.assertEqual(str(e), "Your request is incorrect: Invalid inputs: {'email': None}")
105 changes: 65 additions & 40 deletions utils/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os.path
import re
import simplejson as json
import subprocess
import sys
import tarfile
Expand Down Expand Up @@ -65,7 +64,7 @@ class Flare(object):
COMPRESSED_FILE = 'datadog-agent-{0}.tar.bz2'
# We limit to 10MB arbitrary
MAX_UPLOAD_SIZE = 10485000
TIMEOUT = 15
TIMEOUT = 30


def __init__(self, cmdline=False, case_id=None):
Expand Down Expand Up @@ -193,13 +192,11 @@ def _add_conf_tar(self):
def _strip_comment(self, file_path):
_, temp_path = tempfile.mkstemp(prefix='dd')
atexit.register(os.remove, temp_path)
temp_file = open(temp_path, 'w')
orig_file = open(file_path, 'r').read()

for line in orig_file.splitlines(True):
if not self.COMMENT_REGEX.match(line):
temp_file.write(re.sub(self.APIKEY_REGEX, self.REPLACE_APIKEY, line))
temp_file.close()
with open(temp_path, '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):
temp_file.write(re.sub(self.APIKEY_REGEX, self.REPLACE_APIKEY, line))

return temp_path

Expand All @@ -218,17 +215,16 @@ def _add_clean_confd(self, file_path):
def _strip_password(self, file_path):
_, temp_path = tempfile.mkstemp(prefix='dd')
atexit.register(os.remove, temp_path)
temp_file = open(temp_path, 'w')
orig_file = open(file_path, 'r').read()
password_found = ''
for line in orig_file.splitlines(True):
if self.PASSWORD_REGEX.match(line):
line = re.sub(self.PASSWORD_REGEX, r'\1 ********', line)
password_found = ' - this file contains a password which '\
'has been removed in the version collected'
if not self.COMMENT_REGEX.match(line):
temp_file.write(line)
temp_file.close()
with open(temp_path, 'w') as temp_file:
with open(file_path, 'r') as orig_file:
password_found = ''
for line in orig_file.readlines():
if self.PASSWORD_REGEX.match(line):
line = re.sub(self.PASSWORD_REGEX, r'\1 ********', line)
password_found = ' - this file contains a password which '\
'has been removed in the version collected'
if not self.COMMENT_REGEX.match(line):
temp_file.write(line)

return temp_path, password_found

Expand All @@ -250,13 +246,45 @@ def _supervisor_status(self):
if Platform.is_windows():
print 'Windows - status not implemented'
else:
print '/etc/init.d/datadog-agent status'
self._print_output_command(['/etc/init.d/datadog-agent', 'status'])
print 'supervisorctl status'
self._print_output_command(['/opt/datadog-agent/bin/supervisorctl',
'-c', '/etc/dd-agent/supervisor.conf',
agent_exec = self._get_path_agent_exec()
print '{0} status'.format(agent_exec)
self._print_output_command([agent_exec, 'status'])
supervisor_exec = self._get_path_supervisor_exec()
print '{0} status'.format(supervisor_exec)
self._print_output_command([supervisor_exec,
'-c', self._get_path_supervisor_conf(),
'status'])

# Find the agent exec (package or source)
def _get_path_agent_exec(self):
agent_exec = '/etc/init.d/datadog-agent'
if not os.path.isfile(agent_exec):
agent_exec = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../../bin/agent'
)
return agent_exec

# Find the supervisor exec (package or source)
def _get_path_supervisor_exec(self):
supervisor_exec = '/opt/datadog-agent/bin/supervisorctl'
if not os.path.isfile(supervisor_exec):
supervisor_exec = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../../venv/bin/supervisorctl'
)
return supervisor_exec

# Find the supervisor conf (package or source)
def _get_path_supervisor_conf(self):
supervisor_conf = '/etc/init.d/datadog-agent'
if not os.path.isfile(supervisor_conf):
supervisor_conf = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../../supervisord/supervisord.conf'
)
return supervisor_conf

# Print output of command
def _print_output_command(self, command):
try:
Expand Down Expand Up @@ -303,20 +331,17 @@ def _ask_for_email(self):

# Print output (success/error) of the request
def _analyse_result(self):
# First catch our custom explicit 400
if self._resp.status_code == 400:
raise Exception('Your request is incorrect: {0}'.format(self._resp.json()['error']))
# Then raise potential 500 and 404
self._resp.raise_for_status()
try:
json_resp = json.loads(self._resp.text)
json_resp = self._resp.json()
# Failed parsing
except ValueError, e:
raise Exception('An unknown error has occured: {0}\n'\
'Please contact support by email'.format(self._resp.text))
if self._resp.status_code in range(200, 203):
log.info("Your logs were successfully uploaded. For future reference,"\
" your internal case id is {0}".format(json_resp['case_id']))
elif self._resp.status_code in range(400, 405):
raise Exception('Your request is incorrect: {0}'.format(json_resp['error']))
elif self._resp.status_code in range(500, 506):
raise Exception('An error has occurred while uploading: {0}'.format(
json_resp['error']))
else:
raise Exception('An unknown error has occured: {0} - {1}\n'\
'Please contact support by email'.format(
self._resp.status_code, self._resp.text))
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']))

0 comments on commit 8b10531

Please sign in to comment.