def build_dockerfile(self, path, name, nocache=False): """ Build dockerfile """ instanceName = name.replace('/', '-').replace(':', '-') # cmdStop = ['docker','buildx','stop',instanceName] # Command.execute(cmdStop) # cmdRm = ['docker','buildx','rm',instanceName] # Command.execute(cmdRm) cmdBuilderInstance = [ 'docker', 'buildx', 'create', '--name', instanceName ] Command.execute(cmdBuilderInstance) cmdBuilderInstanceUse = ['docker', 'buildx', 'use', instanceName] Command.execute(cmdBuilderInstanceUse) cmd = [ 'docker', 'buildx', 'build', '--platform', 'linux/arm64,linux/arm/v7', '--tag', name, os.path.dirname(path) ] if nocache: cmd.append('--no-cache') return Command.execute(cmd)
def build_dockerfile(self, path, name, nocache=False): """ Build dockerfile """ cmd = ['docker', 'build', '--tag', name, os.path.dirname(path)] if nocache: cmd.append('--no-cache') return Command.execute(cmd)
def run_task(self, configuration): dockerfile_list = DockerfileUtility.find_dockerfiles_in_path( base_path=configuration.get('dockerPath'), path_regex=configuration.get('docker.pathRegex'), image_prefix=configuration.get('docker.imagePrefix'), whitelist=configuration.get('whitelist'), blacklist=configuration.get('blacklist'), ) image_fail_list = [] docker_command = self.argument('docker command') for dockerfile in dockerfile_list: title = dockerfile['image']['fullname'] print title print '~' * len(title) if configuration['dryRun']: print ' exec: %s' % (docker_command) else: cmd = [ 'docker', 'run', '-t', '--rm', dockerfile['image']['fullname'], 'bash', '-c', '%s' % (' '.join(docker_command)) ] status = Command.execute(cmd) if status: print colored(' -> successfull', 'green') else: print colored(' -> failed', 'red') image_fail_list.append(dockerfile['image']['fullname']) print '' print '' if len(image_fail_list) >= 1: print '' print colored(' => failed images (%s):' % (str(len(image_fail_list))), 'red') for image in image_fail_list: print ' - %s ' % image print '' return False else: return True
def run_task(self, configuration): dockerfile_list = DockerfileUtility.find_dockerfiles_in_path( base_path=configuration.get('dockerPath'), path_regex=configuration.get('docker.pathRegex'), image_prefix=configuration.get('docker.imagePrefix'), whitelist=configuration.get('whitelist'), blacklist=configuration.get('blacklist'), ) image_fail_list = [] docker_command = self.argument('docker command') for dockerfile in dockerfile_list: title = dockerfile['image']['fullname'] print title print '~' * len(title) if configuration['dryRun']: print ' exec: %s' % (docker_command) else: cmd = [ 'docker', 'run', '-t', '--rm', dockerfile['image']['fullname'], 'bash', '-c', '%s' % (' '.join(docker_command)) ] status = Command.execute(cmd) if status: print colored(' -> successfull', 'green') else: print colored(' -> failed', 'red') image_fail_list.append(dockerfile['image']['fullname']) print '' print '' if len(image_fail_list) >= 1: print '' print colored( ' => failed images (%s):' % (str(len(image_fail_list))), 'red') for image in image_fail_list: print ' - %s ' % image print '' return False else: return True
def task_run(dockerfile, configuration, task): """ Run test """ # check if dockerfile is symlink, skipping tests if just a duplicate image # image is using the same hashes if dockerfile['image']['duplicate']: print ' Docker image %s is build from symlink and duplicate of %s' % ( dockerfile['image']['fullname'], dockerfile['image']['from']) print ' -> skipping tests' BaseTaskLoader.set_task_status(task, 'skipped (symlink)', 'skipped') return True # Check if current image is a toolimage (no daemon) is_toolimage = False for term in configuration.get('dockerTest.toolImages', {}): if term in dockerfile['image']['fullname']: is_toolimage = True # rspec spec file settings spec_path = configuration.get('dockerTest.serverspec.specPath' ) % dockerfile['image']['imageName'] spec_abs_path = os.path.join(configuration.get('serverspecPath'), spec_path) # create dockerfile tmp_suffix = '.%s_%s_%s.tmp' % (dockerfile['image']['repository'], dockerfile['image']['imageName'], dockerfile['image']['tag']) tmp_suffix = tmp_suffix.replace('/', '_') test_dockerfile = tempfile.NamedTemporaryFile( prefix='Dockerfile.', suffix=tmp_suffix, dir=configuration.get('serverspecPath'), bufsize=0, delete=False) # serverspec conf serverspec_conf = DockerTestServerspecTaskLoader.generate_serverspec_configuration( path=os.path.basename(test_dockerfile.name), dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage) # serverspec options serverspec_opts = [] serverspec_opts.extend([ spec_path, dockerfile['image']['fullname'], base64.b64encode(json.dumps(serverspec_conf)), os.path.basename(test_dockerfile.name) ]) # dockerfile content dockerfile_content = DockerTestServerspecTaskLoader.generate_dockerfile( dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage) # DryRun if configuration.get('dryRun'): if not os.path.isfile(spec_abs_path): print ' no tests found' print ' image: %s' % (dockerfile['image']['fullname']) print ' path: %s' % (spec_path) print ' args: %s' % (' '.join(serverspec_opts)) print '' print 'spec configuration:' print '-------------------' print json.dumps(serverspec_conf, indent=4, sort_keys=True) print '' print 'Dockerfile:' print '-----------' print dockerfile_content return True # check if we have any tests if not os.path.isfile(spec_abs_path): print ' no tests defined (%s)' % (spec_path) BaseTaskLoader.set_task_status(task, 'skipped (no test)', 'skipped') return True # build rspec/serverspec command cmd = ['bash', 'serverspec.sh'] cmd.extend(serverspec_opts) # create Dockerfile with open(test_dockerfile.name, mode='w', buffering=0) as f: f.write(dockerfile_content) f.flush() os.fsync(f.fileno()) f.close() test_status = False for retry_count in range(0, configuration.get('retry')): try: test_status = Command.execute( cmd, cwd=configuration.get('serverspecPath')) except Exception as e: print e pass if test_status: break elif retry_count < (configuration.get('retry') - 1): print ' failed, retrying... (try %s)' % (retry_count + 1) else: print ' failed, giving up' return test_status
def task_run(dockerfile, configuration, task): """ Run test """ # Check if current image is a toolimage (no daemon) is_toolimage = False for term in configuration.get("dockerTest.toolImages", {}): if term in dockerfile["image"]["fullname"]: is_toolimage = True # rspec spec file settings spec_path = configuration.get("dockerTest.serverspec.specPath") % dockerfile["image"]["imageName"] spec_abs_path = os.path.join(configuration.get("serverspecPath"), spec_path) # create dockerfile tmp_suffix = ".%s_%s_%s.tmp" % ( dockerfile["image"]["repository"], dockerfile["image"]["imageName"], dockerfile["image"]["tag"], ) test_dockerfile = tempfile.NamedTemporaryFile( prefix="Dockerfile.", suffix=tmp_suffix, dir=configuration.get("serverspecPath"), bufsize=0, delete=False ) # serverspec options serverspec_opts = [] serverspec_opts.extend(["--pattern", spec_path]) # serverspec env serverspec_env = DockerTestServerspecTaskLoader.generate_serverspec_environment( path=os.path.basename(test_dockerfile.name), dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage, ) # dockerfile content dockerfile_content = DockerTestServerspecTaskLoader.generate_dockerfile( dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage ) # DryRun if configuration.get("dryRun"): if not os.path.isfile(spec_abs_path): print " no tests found" print " image: %s" % (dockerfile["image"]["fullname"]) print " path: %s" % (spec_path) print " args: %s" % (" ".join(serverspec_opts)) print "" print "environment:" print "------------" print json.dumps(serverspec_env, indent=4, sort_keys=True) print "" print "Dockerfile:" print "-----------" print dockerfile_content os.remove(test_dockerfile.name) return True # check if we have any tests if not os.path.isfile(spec_abs_path): print " no tests defined (%s)" % (spec_path) return True # build rspec/serverspec command cmd = ["bundle", "exec", "rspec"] cmd.extend(serverspec_opts) # Set environment variables env = os.environ.copy() env.update(serverspec_env) # create Dockerfile with open(test_dockerfile.name, mode="w", buffering=0) as f: f.write(dockerfile_content) f.flush() f.close() test_status = False for retry_count in range(0, configuration.get("retry")): try: test_status = Command.execute(cmd, cwd=configuration.get("serverspecPath"), env=env) except Exception as e: print e pass if test_status: break elif retry_count < (configuration.get("retry") - 1): print " failed, retrying... (try %s)" % (retry_count + 1) else: print " failed, giving up" os.remove(test_dockerfile.name) return test_status
def push_image(self, name): """ Push one Docker image to registry """ cmd = ['docker', 'push', name] return Command.execute(cmd)
def pull_image(self, name, tag): """ Build dockerfile """ cmd = ['docker', 'pull', '%s:%s' % (name, tag)] return Command.execute(cmd)
def task_run(dockerfile, configuration, task): """ Run test """ # check if dockerfile is symlink, skipping tests if just a duplicate image # image is using the same hashes if dockerfile['image']['duplicate']: print ' Docker image %s is build from symlink and duplicate of %s' % (dockerfile['image']['fullname'], dockerfile['image']['from']) print ' -> skipping tests' BaseTaskLoader.set_task_status(task, 'skipped (symlink)', 'skipped') return True # Check if current image is a toolimage (no daemon) is_toolimage = False for term in configuration.get('dockerTest.toolImages', {}): if term in dockerfile['image']['fullname']: is_toolimage = True # rspec spec file settings spec_path = configuration.get('dockerTest.serverspec.specPath') % dockerfile['image']['imageName'] spec_abs_path = os.path.join(configuration.get('serverspecPath'), spec_path) # create dockerfile tmp_suffix = '.%s_%s_%s.tmp' % (dockerfile['image']['repository'], dockerfile['image']['imageName'], dockerfile['image']['tag']) tmp_suffix = tmp_suffix.replace('/', '_') test_dockerfile = tempfile.NamedTemporaryFile(prefix='Dockerfile.', suffix=tmp_suffix, dir=configuration.get('serverspecPath'), bufsize=0, delete=False) # serverspec conf serverspec_conf = DockerTestServerspecTaskLoader.generate_serverspec_configuration( path=os.path.basename(test_dockerfile.name), dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage ) # serverspec options serverspec_opts = [] serverspec_opts.extend([spec_path, dockerfile['image']['fullname'], base64.b64encode(json.dumps(serverspec_conf)), os.path.basename(test_dockerfile.name)]) # dockerfile content dockerfile_content = DockerTestServerspecTaskLoader.generate_dockerfile( dockerfile=dockerfile, configuration=configuration, is_toolimage=is_toolimage ) # DryRun if configuration.get('dryRun'): if not os.path.isfile(spec_abs_path): print ' no tests found' print ' image: %s' % (dockerfile['image']['fullname']) print ' path: %s' % (spec_path) print ' args: %s' % (' '.join(serverspec_opts)) print '' print 'spec configuration:' print '-------------------' print json.dumps(serverspec_conf, indent=4, sort_keys=True) print '' print 'Dockerfile:' print '-----------' print dockerfile_content return True # check if we have any tests if not os.path.isfile(spec_abs_path): print ' no tests defined (%s)' % (spec_path) BaseTaskLoader.set_task_status(task, 'skipped (no test)', 'skipped') return True # build rspec/serverspec command cmd = ['bash', 'serverspec.sh'] cmd.extend(serverspec_opts) # create Dockerfile with open(test_dockerfile.name, mode='w', buffering=0) as f: f.write(dockerfile_content) f.flush() os.fsync(f.fileno()) f.close() test_status = False for retry_count in range(0, configuration.get('retry')): try: test_status = Command.execute(cmd, cwd=configuration.get('serverspecPath')) except Exception as e: print e pass if test_status: break elif retry_count < (configuration.get('retry') - 1): print ' failed, retrying... (try %s)' % (retry_count + 1) else: print ' failed, giving up' return test_status