Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitlab-deps
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pipelines
tools
gitlab-deps
Commits
efd3b720
Commit
efd3b720
authored
4 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Remove obsolete docker_deps.py
parent
e27619fc
Branches
Branches containing commit
No related tags found
1 merge request
!1
Modular
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
gitlab_deps/docker_deps.py
+0
-100
0 additions, 100 deletions
gitlab_deps/docker_deps.py
gitlab_deps/docker_deps_test.py
+0
-19
0 additions, 19 deletions
gitlab_deps/docker_deps_test.py
with
0 additions
and
119 deletions
gitlab_deps/docker_deps.py
deleted
100644 → 0
+
0
−
100
View file @
e27619fc
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
)
This diff is collapsed.
Click to expand it.
gitlab_deps/docker_deps_test.py
deleted
100644 → 0
+
0
−
19
View file @
e27619fc
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment