Beispiel #1
0
def get_dockerfile(location, echo=print):
    """
    Return a Dockerfile data dictionary if the location is a Dockerfile,
    otherwise return None.
    """
    fn = fileutils.file_base_name(location)
    if not 'Dockerfile' in fn:
        return {}

    echo('Found Dockerfile at: %(location)r' % locals())

    try:
        # TODO: keep comments instead of ignoring them:
        # assign the comments before an instruction line to a line "comment" attribute
        # assign end of line comment to the line
        # assign top of file and  end of file comments to file level comment attribute
        df = dockerfile_parse.DockerfileParser(location)

        df_data = OrderedDict()
        df_data['location'] = location
        df_data['base_image'] = df.baseimage
        df_data['instructions'] = []

        for entry in df.structure:
            entry = OrderedDict([(k, v) for k, v in sorted(entry.items())
                                 if k in (
                                     'instruction',
                                     'startline',
                                     'value',
                                 )])
            df_data['instructions'].append(entry)
        return {location: df_data}
    except:
        echo('Error parsing Dockerfile at: %(location)r' % locals())
        return {}
Beispiel #2
0
def write_docker_cfg(
    dockerfile_path: str,
    docker_cfg_path: str
):
    with open(dockerfile_path) as f:
        parser = dockerfile_parse.DockerfileParser(fileobj=f)
        relevant_image_refs = parser.parent_images

    # use dict to deduplicate by cfg-name (which we otherwise do not care about)
    container_registry_cfgs = {
        c.name(): c for c
        in (mc.find_config(img_ref) for img_ref in relevant_image_refs)
        if c is not None
    }.values()

    docker_cfg_auths = {}

    for container_registry_cfg in container_registry_cfgs:
        docker_cfg_auths.update(
            mc.find_config('eu.gcr.io/sap-se-gcp-scp-k8s/landscape').as_docker_auths()
        )

    docker_cfg = {'auths': docker_cfg_auths}

    with open(docker_cfg_path, 'w') as f:
        json.dump(docker_cfg, f)
 def post(self, tag):
     query_args = self.request.query_arguments
     #if not pull if whitelisted fetch localhost:.../pull/image
     name, body = None, None
     for key, value in self.request.files.items():
         name = key
         body = value[0]['body']
     if name and body:
         dfp = dockerfile_parse.DockerfileParser()
         dfp.content = body
         if dfp.baseimage.split(':')[0] in self.whitelisted_base_images:
             output = ''
             f = BytesIO(body)
             for line in self.docker_env.build(fileobj=f, rm=True, tag=tag):
                 output += line.decode('utf-8') + '\n'
             self.write(output)
         else:
             self.write('Can\'t build')
     else:
         self.write('Can\'t build')