Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1d84542
Add options to filter for dates and regular expressions
JulianSauer Apr 23, 2018
beb43d6
Fix date parsing
JulianSauer Apr 27, 2018
dba7e4c
Update readme
JulianSauer Apr 27, 2018
605e8bd
Add test for regex
JulianSauer Apr 30, 2018
709e3c7
Fix error when using -n without -k
JulianSauer Apr 30, 2018
f5a1649
Improve regex test and add test for date
JulianSauer Apr 30, 2018
df687e9
Add config options for regex and date
JulianSauer May 4, 2018
613fc74
Fix parsing of parameters
JulianSauer May 7, 2018
93fc41b
Add checks for empty repos
JulianSauer May 7, 2018
4eeaa84
Add exception for empty regex
JulianSauer May 7, 2018
0c697d9
Update example config
JulianSauer May 7, 2018
5353297
Add tests for configs with regex and date
JulianSauer May 11, 2018
5b08825
Update tests cases
JulianSauer May 11, 2018
2744fb8
Update readme
JulianSauer May 11, 2018
534050a
Code cleanup
JulianSauer May 23, 2018
906c431
Fix date test
JulianSauer Jul 12, 2018
643edab
Use image:tag syntax
JulianSauer Jul 12, 2018
64a7e7c
Change format
JulianSauer Jul 13, 2018
a45a056
Fix indent
JulianSauer Jul 13, 2018
8e61477
Fix regex test
JulianSauer Jul 13, 2018
864eed2
Fix cf test
JulianSauer Jul 13, 2018
f469830
Update config check tests
JulianSauer Jul 13, 2018
509aae2
Switch to yaml format for repo files
JulianSauer Jul 13, 2018
fbe0c7d
Update tests for yaml configs
JulianSauer Jul 13, 2018
b66fd54
Update descriptions
JulianSauer Jul 13, 2018
41d61a9
Fix package in docker image
JulianSauer Jul 19, 2018
616d18a
Rename parameter
JulianSauer Jul 19, 2018
8a37dc4
Update config tests
JulianSauer Jul 19, 2018
576e8d8
Change date format
JulianSauer Jul 19, 2018
90a18b6
Add comments
JulianSauer Aug 2, 2018
04b85cc
Typo
JulianSauer Aug 2, 2018
3a6a201
Move date parsing to separate function
JulianSauer Aug 2, 2018
8d31700
Simplified loop syntax
JulianSauer Aug 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:2.7-alpine
RUN pip install requests
RUN pip install requests PyYAML
COPY cleanreg.py /cleanreg.py
COPY LICENSE /LICENSE
ENTRYPOINT ["/cleanreg.py"]
Expand Down
75 changes: 55 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ Information about the needed garbage collection is described at [https://docs.do

## History

* v0.6 - add `-cf` flag which allows to clean up all repos in a registry (thnks @kekru for his PR)
* v0.6 - add `-cf` flag which allows to clean up all repos in a registry (thnks @kekru for his PR)
* v0.5 - fix for issue [#8](https://github.com/hcguersoy/cleanreg/issues/8) which resulted in deleting more layers then intended; performance improvements; added `--metadata-workers` attribute
* v0.4.1 - added `--assume-yes` and deprecated `--quiet` flag
* v0.4 - added support for basic auth secured registry servers, introducing `--basicauth-user` and `--basicauth-pw` (thanks to @kekru for his pull request)
* v0.3 - fixing deletion if a digest is associated with multiple tags, introducing the `--ignore-ref-tags` flag.
* v0.3 - fixing deletion if a digest is associated with multiple tags, introducing the `--ignore-ref-tags` flag.
* v0.2 - added support for registry server using self signed certificates
* v0.1 - first version with basics
* v0.1 - first version with basics

## Prerequisites and supported Plattform

This tool was implemented and tested on Ubuntu Linux 14.04, 16.04 and on MacOS 10.12 using Python 2.7. It is developed against Docker Registry version [2.5.1](https://github.com/docker/distribution/releases/tag/v2.5.1), but tested against [2.6.1](https://github.com/docker/distribution/releases/tag/v2.6.1) and *latest* (see [https://hub.docker.com/r/library/registry/](https://hub.docker.com/r/library/registry/)).

You need to install the Python module *requests*:
You need to install the Python modules *requests* and *PyYAML*:

```
$ pip install requests
$ pip install requests PyYAML
```

Be sure to configure your registry server to allow deletion (see [https://docs.docker.com/registry/configuration/#/delete](https://docs.docker.com/registry/configuration/#/delete)).
Expand All @@ -36,9 +36,10 @@ Be sure to configure your registry server to allow deletion (see [https://docs.d
Download the file *cleanreg.py* or clone this repository to a local directory.

```
usage: cleanreg.py [-h] [-v] -r REGISTRY [-p] [-y] [-q] [-n REPONAME]
[-k KEEPIMAGES] [-f REPOSFILE] [-c CACERT] [-i]
[-u BASICAUTHUSER] [-pw BASICAUTHPW] [-w MD_WORKERS]
usage: cleanreg.py [-h] [-v] -r REGISTRY [-p] [-y] [-q] [-n REPONAME:TAG]
[-k KEEPIMAGES] [-re] [-d DATE]
[-f REPOSFILE] [-c CACERT] [-i] [-u BASICAUTHUSER]
[-pw BASICAUTHPW] [-w MD_WORKERS]

Removes images on a docker registry (v2).

Expand All @@ -57,19 +58,34 @@ optional arguments:
will be answered with YES
-q, --quiet [deprecated] If set no user action will appear and all
questions will be answered with YES
-n REPONAME, --reponame REPONAME
The name of the repo which should be cleaned up
-n REPONAME, --reponame REPONAME:TAG
The name of the repo which should be cleaned up. Tags
are optional.
-cf, --clean-full-catalog
If set all repos of the registry will be cleaned up,
keeping the amount of images specified in -k option.
The amount for each repo can be overridden in the repofile (-f).
considering the -k, -re and -d options.
These can be overridden for each repo in the repofile (-f).
-k KEEPIMAGES, --keepimages KEEPIMAGES
Amount of images (not tags!) which should be kept for
the given repo (if -n is set) or for each repo of the
the given repo (if -n is set) or for each repo of the
registry (if -cf is set).
-re, --regex
Interpret tagnames as regular expressions for the given
repo (if -n is set) or for each repo of the registry (if
-cf is set).
-s DATE, --since DATE
Keeps images which were created since then for
the given repo (if -n is set) or for each repo of the
registry (if -cf is set).
Format: YYYYMMDD, YYYYMMDDThhmmss, YYYY-MM-DD or YYYY-MM-DDThh:mm:ss
-f REPOSFILE, --reposfile REPOSFILE
A file containing the list of Repositories and how
many images should be kept.
A yaml file containing the list of Repositories with
additional information regarding tags, dates and how many
images to keep.
Format: REPONAME:
tag: TAG
keepimages: KEEPIMAGES
keepsince: DATE
-c CACERT, --cacert CACERT
Path to a valid CA certificate file. This is needed if
self signed TLS is used in the registry server.
Expand Down Expand Up @@ -120,6 +136,18 @@ Again: to be secure use the `-i` flag:

Same as above but ignore images which are associated with multiple tags.

```
./cleanreg.py -r http://192.168.56.2:5000 -n mysql:latest -i
```

Will only delete the image mysql which is tagged as latest.

```
./cleanreg.py -r http://192.168.56.2:5000 -n mysql:.*temp.* -re -d 2018-01-01 -k 5 -i
```

Removes all images that contain the word "temp" in their tagnames and if they were created before 2018 but at least 5 will be kept in total.

```
./cleanreg.py -r http://192.168.56.2:5000 -n myalpine -k 50 -i -w 12
```
Expand All @@ -138,16 +166,23 @@ This will clean up all repositories, keeping 5 images per repository.
Cleaning up multiple repositories defined in a configuration file:

```
./cleanreg.py -r http://192.168.56.2:5000 -f cleanreg-example.conf -i
./cleanreg.py -r http://192.168.56.2:5000 -f cleanreg-example.conf -re -i
```
The configuration file has the format `<repository name> <images to keep>`. An example file can be found in the repository.
The configuration file has the format
```
<repository name>:
tag: <tag>
keepimages: <number of images to keep>
keepsince: <date>
```
The values for tag, keepimages and keepsince are optional. If the tag should be parsed as a regular expression use the -re flag as shown above. An example file can be found in the repository.

The configuration file can be used together with the clean-full-catalog option:

```
./cleanreg.py -r http://192.168.56.2:5000 -cf -k 5 -f cleanreg-example.conf -i
./cleanreg.py -r http://192.168.56.2:5000 -cf -d 20180101 -f cleanreg-example.conf -i
```
This will clean the repositories with images to keep as defined in the configuration file and it will additionally clean all other repositories of the registry, keeping 5 images per repository.
This will clean the repositories with images to keep as defined in the configuration file and it will additionally clean all other repositories of the registry, keeping images per repository that were created since 2018.


If you've to use a repositories definition file (parameter `-f`) while using the image distribution you should mount that file into your container:
Expand Down Expand Up @@ -200,7 +235,7 @@ Prerequisites:
* Locally installed Docker engine (remote execution is not yet implemented; runs with Docker for MacOS fine)

You can run all tests, with the *runAllTests.sh* script:

```
cd test
./runAllTests.sh
Expand Down
3 changes: 0 additions & 3 deletions cleanreg-example.conf

This file was deleted.

9 changes: 9 additions & 0 deletions cleanreg-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
consul:
tag: OnlyThisTag
keepimages: 20
keepsince: 2000-12-24T12:31:59
elasticsearch:
keepimages: 20
keepsince: 20000101
dummybox:
keepimages: 0
Loading