diff --git a/dockermake/builds.py b/dockermake/builds.py index 068172b..e17a492 100644 --- a/dockermake/builds.py +++ b/dockermake/builds.py @@ -15,6 +15,7 @@ import os from builtins import object +from termcolor import cprint from dockermake.step import FileCopyStep from . import utils @@ -51,15 +52,11 @@ def write_dockerfile(self, output_dir): lines.extend(step.dockerfile_lines) else: lines.extend(step.dockerfile_lines[1:]) - path = os.path.join(output_dir, 'Dockerfile.%s' % self.imagename) - with open(path, 'w') as outfile: outfile.write('\n'.join(lines)) - print('Wrote %s' % path) - def build(self, client, nobuild=False, keepbuildtags=False, @@ -80,14 +77,18 @@ def build(self, client, usecache=usecache, pull=pull) - print('\n' + '-'*utils.get_console_width()) - print(' STARTING BUILD for "%s" (image definition "%s" from %s)\n' % ( - self.targetname, self.imagename, self.steps[-1].sourcefile)) + width = utils.get_console_width() + cprint('\n' + '='*width, + color='white', attrs=['bold']) + + line = 'STARTING BUILD for "%s" (image definition "%s" from %s)\n' % ( + self.targetname, self.imagename, self.steps[-1].sourcefile) + + cprint(_centered(line, width), color='blue', attrs=['bold']) for istep, step in enumerate(self.steps): - print(' * Building %s, Step %d/%d:' % (self.imagename, - istep+1, - len(self.steps))) + cprint('* Building image "%s", Step %d/%d:' % (self.imagename, istep+1, len(self.steps)), + color='blue') if not nobuild: if step.bust_cache: @@ -96,7 +97,8 @@ def build(self, client, step.bust_cache = False step.build(client, usecache=usecache) - print(" - Created intermediate image %s\n" % step.buildname) + cprint("* Created intermediate image \"%s\"\n" % step.buildname, + 'green') if step.bust_cache: _rebuilt.add(stackkey) @@ -105,7 +107,11 @@ def build(self, client, if not nobuild: self.finalizenames(client, finalimage, keepbuildtags) - print(' *** Successfully built image %s\n' % self.targetname) + line = 'FINISHED BUILDING "%s" (image definition "%s" from %s)'%( + self.targetname, self.imagename, self.steps[-1].sourcefile) + cprint(_centered(line, width), + color='green', attrs=['bold']) + cprint('=' * width, color='white', attrs=['bold'], end='\n\n') def _get_stack_key(self, istep): names = [self.from_image] @@ -120,20 +126,31 @@ def update_source_images(self, client, usecache, pull): for build in self.sourcebuilds: if build.targetname in _updated_staging_images: continue - print('\nUpdating source image %s' % build.targetname) + cprint('\nUpdating source image %s' % build.targetname, + 'blue') build.build(client, usecache=usecache, pull=pull) - print(' *** Done with source image %s\n' % build.targetname) + cprint('Finished with build image "%s"\n' % build.targetname, + color='green') def finalizenames(self, client, finalimage, keepbuildtags): """ Tag the built image with its final name and untag intermediate containers """ client.tag(finalimage, *self.targetname.split(':')) - print('Tagged final image as %s' % self.targetname) + cprint('Tagged final image as "%s"' % self.targetname, + 'green') if not keepbuildtags: print('Untagging intermediate containers:', end='') for step in self.steps: client.remove_image(step.buildname, force=True) print(step.buildname, end=',') print() + + +def _centered(s, w): + leftover = w - len(s) + if leftover < 0: + return s + else: + return ' '*(leftover//2) + s \ No newline at end of file diff --git a/dockermake/staging.py b/dockermake/staging.py index fe7222b..060bfaf 100644 --- a/dockermake/staging.py +++ b/dockermake/staging.py @@ -16,6 +16,7 @@ import docker.errors from builtins import object +from termcolor import cprint import os import tempfile @@ -33,10 +34,10 @@ def clear_copy_cache(): for path in (BUILD_CACHEDIR, BUILD_TEMPDIR): if os.path.exists(path): assert os.path.isdir(path), "'%s' is not a directory!" - print('Removing docker-make cache %s' % path) + cprint('Removing docker-make cache %s' % path, 'yellow') shutil.rmtree(path) else: - print('Cache directory %s does not exist.' % path) + cprint('Cache directory %s does not exist.' % path, 'red') class StagedFile(object): @@ -64,8 +65,9 @@ def stage(self, startimage, newimage): from .step import BuildError client = utils.get_client() - print(' * Copying FROM "%s:/%s" TO "%s://%s/"'%(self.sourceimage, self.sourcepath, - startimage, self.destpath)) + cprint(' Copying file from "%s:/%s" \n to "%s://%s/"' + % (self.sourceimage, self.sourcepath, startimage, self.destpath), + 'blue') # copy build artifacts from the container if necessary cachedir = self._setcache(client) @@ -93,7 +95,7 @@ def stage(self, startimage, newimage): os.mkdir(cachedir) os.rename(tempdir, cachedir) else: - print(' * Using cached files from %s' % cacherelpath) + print(' Using cached files from %s' % cacherelpath) # write Dockerfile for the new image and then build it dockerfile = 'FROM %s\nADD content.tar %s' % (startimage, self.destpath) diff --git a/dockermake/step.py b/dockermake/step.py index e89de22..47e4a53 100644 --- a/dockermake/step.py +++ b/dockermake/step.py @@ -123,6 +123,7 @@ def write_dockerfile(self, dockerfile): def build_external_dockerfile(client, image): import docker.errors cprint(" Building base image from %s" % image, 'blue') + assert not image.built stream = client.build(path=os.path.dirname(image.path), dockerfile=os.path.basename(image.path), @@ -136,7 +137,7 @@ def build_external_dockerfile(client, image): raise errors.ExternalBuildError( 'Error building Dockerfile at %s. ' % image.path + 'Please check it for errors\n. Docker API error message:' + str(e)) - + image.built = True cprint(" Finished building Dockerfile at %s" % image.path, 'green')