Skip to content

Commit

Permalink
feat(controller): add a option to limit the number of log lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson committed Apr 19, 2015
1 parent 0a8c9e3 commit c27fc9c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
12 changes: 10 additions & 2 deletions client/deis.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,20 @@ def apps_logs(self, args):
Options:
-a --app=<app>
the uniquely identifiable name for the application.
-n --lines=<lines>
the number of lines to display
"""
app = args.get('--app')
if not app:
app = self._session.app
response = self._dispatch('get',
"/v1/apps/{}/logs".format(app))

url = "/v1/apps/{}/logs".format(app)

log_lines = args.get('--lines')
if log_lines:
url += "?log_lines={}".format(log_lines)

response = self._dispatch('get', url)
if response.status_code == requests.codes.ok:
# strip the last newline character
for line in response.json().split('\n')[:-1]:
Expand Down
4 changes: 2 additions & 2 deletions controller/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ def _default_scale(self, user, release):

self.scale(user, structure)

def logs(self):
def logs(self, log_lines):
"""Return aggregated log data for this application."""
path = os.path.join(settings.DEIS_LOG_DIR, self.id + '.log')
if not os.path.exists(path):
raise EnvironmentError('Could not locate logs')
data = subprocess.check_output(['tail', '-n', str(settings.LOG_LINES), path])
data = subprocess.check_output(['tail', '-n', log_lines, path])
return data

def run(self, user, command):
Expand Down
7 changes: 7 additions & 0 deletions controller/api/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def test_app_actions(self):
HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, FAKE_LOG_DATA)

# test with log_lines
response = self.client.get(url + "?log_lines=1",
HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, FAKE_LOG_DATA.splitlines(True)[4])

os.remove(path)
# TODO: test run needs an initial build

Expand Down
4 changes: 3 additions & 1 deletion controller/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ def scale(self, request, **kwargs):
def logs(self, request, **kwargs):
app = self.get_object()
try:
return Response(app.logs(), status=status.HTTP_200_OK, content_type='text/plain')
return Response(app.logs(request.query_params.get('log_lines',
str(settings.LOG_LINES))),
status=status.HTTP_200_OK, content_type='text/plain')
except EnvironmentError:
return Response("No logs for {}".format(app.id),
status=status.HTTP_204_NO_CONTENT,
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/api-v1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ What's New
----------

**New!** ``/users`` endpoint for listing users
**New!** ``/logs`` endpoint now has a option for limiting the number of log lines returned


Authentication
Expand Down Expand Up @@ -258,6 +259,12 @@ Example Request:
Host: deis.example.com
Authorization: token abc123
Optional URL Query Parameters:

.. code-block:: console
?log_lines=
Example Response:

.. code-block:: console
Expand Down
4 changes: 4 additions & 0 deletions tests/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
appsRunCmd = "apps:run echo Hello, 世界"
appsOpenCmd = "apps:open --app={{.AppName}}"
appsLogsCmd = "apps:logs --app={{.AppName}}"
appsLogsLimitCmd = "apps:logs --app={{.AppName}} -n 1"
appsInfoCmd = "apps:info --app={{.AppName}}"
appsDestroyCmd = "apps:destroy --app={{.AppName}} --confirm={{.AppName}}"
appsDestroyCmdNoApp = "apps:destroy --confirm={{.AppName}}"
Expand Down Expand Up @@ -97,6 +98,9 @@ func appsLogsTest(t *testing.T, params *utils.DeisTestConfig) {
utils.CurlApp(t, *params)
utils.Execute(t, cmd, params, false, "created initial release")
utils.Execute(t, cmd, params, false, "listening on 5000...")

utils.Execute(t, appsLogsLimitCmd, params, false, "")

if err := utils.Chdir(".."); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c27fc9c

Please sign in to comment.