Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LogStore to retain build logs, and a S3LogStore implementation #967

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
45 changes: 43 additions & 2 deletions repo2docker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import logging
import os
import boto3
import getpass
import shutil
import tempfile
Expand Down Expand Up @@ -326,6 +327,12 @@ def _user_name_default(self):
config=True,
)

s3_logs_endpoint = Unicode("", help="S3 endpoint", config=True)
s3_logs_access_key = Unicode("", help="S3 access key ", config=True)
s3_logs_secret_key = Unicode("", help="S3 secret key", config=True)
s3_logs_bucket = Unicode("", help="S3 bucket", config=True)
s3_logs_region = Unicode("", help="S3 region", config=True)
manics marked this conversation as resolved.
Show resolved Hide resolved

all_ports = Bool(
False,
help="""
Expand Down Expand Up @@ -625,7 +632,7 @@ def find_image(self):
return True
return False

def build(self):
def build(self, logfile=None):
"""
Build docker image
"""
Expand Down Expand Up @@ -714,6 +721,8 @@ def build(self):
bp.__class__.__name__,
extra=dict(phase="building"),
)
if logfile:
logfile.write("Using %s builder\n" % bp.__class__.__name__)

for l in picked_buildpack.build(
docker_client,
Expand All @@ -725,8 +734,12 @@ def build(self):
):
if "stream" in l:
self.log.info(l["stream"], extra=dict(phase="building"))
if logfile:
logfile.write(l["stream"])
elif "error" in l:
self.log.info(l["error"], extra=dict(phase="failure"))
if logfile:
logfile.write(l["error"])
raise docker.errors.BuildError(l["error"], build_log="")
elif "status" in l:
self.log.info(
Expand All @@ -741,7 +754,35 @@ def build(self):
shutil.rmtree(checkout_path, ignore_errors=True)

def start(self):
self.build()
logfile = None
if (
self.s3_logs_endpoint
and self.s3_logs_access_key
and self.s3_logs_secret_key
and self.s3_logs_bucket
):
logfile = tempfile.NamedTemporaryFile("w", delete=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These builds logs have been useful in the past, maybe they should be kept anyway, even if not uploading to s3?

try:
self.build(logfile=logfile)
finally:
if logfile:
logfile.close()
if os.stat(logfile.name).st_size:
dest = f"buildlogs/{self.output_image_spec}/repo2docker.log"
self.log.info(f"Uploading log to {self.s3_logs_bucket}/{dest}")
s3 = boto3.resource(
"s3",
endpoint_url=self.s3_logs_endpoint,
aws_access_key_id=self.s3_logs_access_key,
aws_secret_access_key=self.s3_logs_secret_key,
config=boto3.session.Config(signature_version="s3v4"),
region_name=self.s3_logs_region,
)
s3.Bucket(self.s3_logs_bucket).upload_file(
logfile.name,
manics marked this conversation as resolved.
Show resolved Hide resolved
dest,
ExtraArgs={"ContentType": "text/plain"},
)

if self.push:
self.push_image()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def get_identifier(json):
name="jupyter-repo2docker",
version=versioneer.get_version(),
install_requires=[
"boto3",
manics marked this conversation as resolved.
Show resolved Hide resolved
"docker",
"traitlets",
"python-json-logger",
Expand Down