-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sam build - image package type - streaming progress bar (#3215)
* fix: add log streaming to `sam build` - IMAGE functions * fix: get closer to 95% test coverage * fix: address comments - Add type hinting - Add additional tests * fix: make mypy and lint happy * fix: address feedback
- Loading branch information
Showing
14 changed files
with
224 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
""" | ||
|
||
# Expose the cli object here | ||
from .command import cli # noqa | ||
from .command import cli # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Log streaming utilities when streaming logs from Docker | ||
""" | ||
import os | ||
from typing import Dict | ||
|
||
import docker | ||
|
||
from samcli.lib.package.stream_cursor_utils import ( | ||
CursorUpFormatter, | ||
CursorDownFormatter, | ||
CursorLeftFormatter, | ||
ClearLineFormatter, | ||
) | ||
from samcli.lib.utils.stream_writer import StreamWriter | ||
|
||
|
||
class LogStreamError(Exception): | ||
def __init__(self, msg: str) -> None: | ||
Exception.__init__(self, msg) | ||
|
||
|
||
class LogStreamer: | ||
def __init__(self, stream: StreamWriter): | ||
self._stream = stream | ||
self._cursor_up_formatter = CursorUpFormatter() | ||
self._cursor_down_formatter = CursorDownFormatter() | ||
self._cursor_left_formatter = CursorLeftFormatter() | ||
self._cursor_clear_formatter = ClearLineFormatter() | ||
|
||
def stream_progress(self, logs: docker.APIClient.logs): | ||
""" | ||
Stream progress from docker push logs and move the cursor based on the log id. | ||
:param logs: generator from docker_clent.APIClient.logs | ||
""" | ||
ids: Dict[str, int] = dict() | ||
for log in logs: | ||
_id = log.get("id", "") | ||
status = log.get("status", "") | ||
stream = log.get("stream", "") | ||
progress = log.get("progress", "") | ||
error = log.get("error", "") | ||
change_cursor_count = 0 | ||
if _id: | ||
if _id not in ids: | ||
ids[_id] = len(ids) | ||
else: | ||
curr_log_line_id = ids[_id] | ||
change_cursor_count = len(ids) - curr_log_line_id | ||
self._stream.write( | ||
self._cursor_up_formatter.cursor_format(change_cursor_count) | ||
+ self._cursor_left_formatter.cursor_format(), | ||
encode=True, | ||
) | ||
|
||
self._stream_write(_id, status, stream, progress, error) | ||
|
||
if _id: | ||
self._stream.write( | ||
self._cursor_down_formatter.cursor_format(change_cursor_count) | ||
+ self._cursor_left_formatter.cursor_format(), | ||
encode=True, | ||
) | ||
self._stream.write(os.linesep, encode=True) | ||
|
||
def _stream_write(self, _id: str, status: str, stream: bytes, progress: str, error: str): | ||
""" | ||
Write stream information to stderr, if the stream information contains a log id, | ||
use the carriage return character to rewrite that particular line. | ||
:param _id: docker log id | ||
:param status: docker log status | ||
:param stream: stream, usually stderr | ||
:param progress: docker log progress | ||
:param error: docker log error | ||
""" | ||
if error: | ||
raise LogStreamError(msg=error) | ||
if not status and not stream: | ||
return | ||
|
||
# NOTE(sriram-mv): Required for the purposes of when the cursor overflows existing terminal buffer. | ||
if not stream: | ||
self._stream.write(os.linesep, encode=True) | ||
self._stream.write( | ||
self._cursor_up_formatter.cursor_format() + self._cursor_left_formatter.cursor_format(), encode=True | ||
) | ||
self._stream.write(self._cursor_clear_formatter.cursor_format(), encode=True) | ||
|
||
if not _id: | ||
self._stream.write(stream, encode=True) | ||
self._stream.write(status, encode=True) | ||
else: | ||
self._stream.write(f"\r{_id}: {status} {progress}", encode=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from unittest import TestCase | ||
|
||
from samcli.lib.utils.stream_writer import StreamWriter | ||
from samcli.lib.utils.osutils import stderr | ||
from samcli.lib.docker.log_streamer import LogStreamer | ||
|
||
|
||
from docker.errors import APIError | ||
|
||
|
||
class TestLogStreamer(TestCase): | ||
def setUp(self): | ||
self.stream = StreamWriter(stream=stderr(), auto_flush=True) | ||
self.error_class = APIError | ||
self.image = "image:v1" | ||
|
||
def test_logstreamer_init(self): | ||
LogStreamer(stream=self.stream) | ||
|
||
def test_logstreamer_stream_progress(self): | ||
log_streamer = LogStreamer(stream=self.stream) | ||
log_streamer.stream_progress( | ||
iter( | ||
[ | ||
{"status": "Pushing to xyz"}, | ||
{"id": "1", "status": "Preparing", "progress": ""}, | ||
{"id": "2", "status": "Preparing", "progress": ""}, | ||
{"id": "3", "status": "Preparing", "progress": ""}, | ||
{"id": "1", "status": "Pushing", "progress": "[====> ]"}, | ||
{"id": "3", "status": "Pushing", "progress": "[====> ]"}, | ||
{"id": "2", "status": "Pushing", "progress": "[====> ]"}, | ||
{"id": "3", "status": "Pushed", "progress": "[========>]"}, | ||
{"id": "1", "status": "Pushed", "progress": "[========>]"}, | ||
{"id": "2", "status": "Pushed", "progress": "[========>]"}, | ||
{"status": f"image {self.image} pushed digest: a89q34f"}, | ||
{}, | ||
] | ||
) | ||
) |
Oops, something went wrong.