Skip to content
Snippets Groups Projects
Commit efd3b720 authored by ale's avatar ale
Browse files

Remove obsolete docker_deps.py

parent e27619fc
Branches
No related tags found
1 merge request!1Modular
import gitlab
import json
import logging
import re
import sys
_from_rx = re.compile(r'^FROM\s+(\S+).*$', re.MULTILINE)
def _parse_dockerfile(df):
return _from_rx.findall(df)
def _fetch_dockerfile(gl, project, ref):
try:
f = project.files.get(file_path='Dockerfile', ref=ref)
return f.decode()
except:
return None
def _has_gitlab_ci(gl, project, ref):
try:
project.files.get(file_path='.gitlab-ci.yml', ref=ref)
return True
except:
return False
def _remove_image_tag(name):
if ':' in name:
return name.split(':')[0]
return name
def build_docker_deps(gl, search_pattern=None, filter_pattern=None):
"""Build the project dependency map based on Dockerfiles.
This can be a fairly expensive (long) operation if the list of
projects is large. The 'search_pattern' argument allows for
filtering on the server side, using Gitlab search query syntax.
On the client side, the project list can be filtered with a
regular expression using the 'filter_pattern' argument, which will
be applied to the project's path_with_namespace.
Returns an {image_name: [projects]}, where 'projects' is the list
of projects that have 'image_name' as their base Docker
image. These are gitlab.Project instances.
We only examine Dockerfiles in the master branch of repositories.
"""
deps = {}
filter_rx = None
if filter_pattern:
filter_rx = re.compile(filter_pattern)
projects = gl.projects.list(all=True, search=search_pattern, as_list=False)
for project in projects:
project_name = project.path_with_namespace
project_url = project_name
if filter_rx is not None and not filter_rx.search(project.path_with_namespace):
continue
if not _has_gitlab_ci(gl, project, 'master'):
continue
df = _fetch_dockerfile(gl, project, 'master')
if not df:
continue
base_images = _parse_dockerfile(df.decode('utf-8'))
if not base_images:
logging.error('ERROR: could not find base image for %s',
project.path_with_namespace)
continue
for img in base_images:
deps.setdefault(_remove_image_tag(img), []).append(project_url)
return deps
def docker_deps_to_project_deps(deps, registry_hostname):
out = {}
for image_name in deps:
if image_name.startswith(registry_hostname):
project_name = image_name[len(registry_hostname)+1:]
out[project_name] = deps[image_name]
return out
def dump_deps(gitlab_url, registry_hostname, gitlab_token,
deps_match, deps_filter, project_deps=True):
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token)
if gitlab_token:
gl.auth()
deps = build_docker_deps(gl, deps_match, deps_filter)
if project_deps:
deps = docker_deps_to_project_deps(deps, registry_hostname)
json.dump(deps, sys.stdout, indent=2)
from .docker_deps import _parse_dockerfile
import unittest
class TestParseDockerfile(unittest.TestCase):
def test_parse_dockerfile(self):
dockerfile = '''
FROM baseimage1 AS build
RUN build
FROM baseimage2
COPY --from=build bin /usr/bin/bin
RUN fix-perms
'''
images = _parse_dockerfile(dockerfile)
self.assertEqual(['baseimage1', 'baseimage2'], images)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment