Skip to content

Commit 6ba7567

Browse files
authored
Merge pull request #15 from hcguersoy/feature/py3-ghactions
Feature/py3 ghactions
2 parents 8d6ddaa + 0e0c396 commit 6ba7567

19 files changed

+1012
-571
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Build and Publish cleanreg
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
- 'pr/**'
8+
- 'feature/**'
9+
paths-ignore:
10+
- 'README.md'
11+
12+
release:
13+
types: [published]
14+
15+
workflow_dispatch:
16+
17+
jobs:
18+
19+
test-cleanreg:
20+
runs-on: 'ubuntu-20.04'
21+
22+
steps:
23+
24+
- uses: actions/checkout@v2
25+
26+
- run: |
27+
cd test
28+
chmod +x runAllTests.sh
29+
./runAllTests.sh
30+
name: Execute tests
31+
shell: bash --noprofile --norc {0}
32+
33+
package:
34+
runs-on: 'ubuntu-20.04'
35+
needs: test-cleanreg
36+
# create the images and push them only if on a release tag, starting with a 'v'
37+
if: startsWith(github.ref, 'refs/tags/v')
38+
steps:
39+
40+
# here we create the version information based on the tag and the commit sha
41+
# but only if we are building from a tag reference
42+
- name: Create tags
43+
id: create_tags
44+
run: |
45+
DOCKER_IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/cleanreg
46+
SHORTREF=${GITHUB_SHA::8}
47+
VERSION=${{ github.ref_name }}
48+
RUN_NUMBER=${{ github.run_number }}
49+
50+
TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:${VERSION}-${SHORTREF}-${RUN_NUMBER}"
51+
52+
echo "::set-output name=tags::${TAGS}"
53+
shell: bash
54+
55+
- name: Set up QEMU
56+
uses: docker/setup-qemu-action@v1
57+
58+
- name: Set up Docker Buildx
59+
uses: docker/setup-buildx-action@v1
60+
61+
- name: Login to DockerHub
62+
uses: docker/login-action@v1
63+
with:
64+
username: ${{ secrets.DOCKERHUB_USERNAME }}
65+
password: ${{ secrets.DOCKERHUB_TOKEN }}
66+
67+
- name: Build and push
68+
uses: docker/build-push-action@v2
69+
with:
70+
push: true
71+
platforms: linux/amd64,linux/arm64
72+
tags: ${{ steps.create_tags.outputs.tags }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
test/testlog.txt
33
.DS_Store
44
venv
5+
.python-version

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM python:2.7-alpine
1+
FROM python:3.10.2-alpine3.15
2+
RUN python -m pip install --upgrade pip
23
RUN pip install requests PyYAML
34
COPY cleanreg.py /cleanreg.py
45
COPY LICENSE /LICENSE

Dockerfile.dummybox

Lines changed: 0 additions & 3 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2022 All cleanreg authors
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build Status](https://travis-ci.org/hcguersoy/cleanreg.svg?branch=master)](https://travis-ci.org/hcguersoy/cleanreg)
1+
22
# Registry Cleaner
33

44
This is a small tool to delete tags, or, to be more correct, delete image manifests, from a Docker Registry implementing the API v2.
@@ -9,7 +9,8 @@ Information about the needed garbage collection is described at [https://docs.do
99

1010
## History
1111

12-
* v0.7.1 - Added a `--skip-tls-verify` attribute
12+
* v0.8.0 - Finally find the time to migrate to Python 3 and Github Actions. This comes this many changes in the code and the surrounding build system. Support for `linux\arm64` added.
13+
* v0.7.1 - Added a `--skip-tls-verify` attribute (_unreleased_)
1314
* v0.7 - This is a release which breaks some stuff (configuration file is now yaml based), adding new options for keeping images (e.g. `--since`, `--regex`) (thanks to @JulianSauer for his [PR10](https://github.com/hcguersoy/cleanreg/pull/10))
1415
* v0.6 - add `-cf` flag which allows to clean up all repos in a registry (thanks @kekru for his PR)
1516
* 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
@@ -19,32 +20,34 @@ Information about the needed garbage collection is described at [https://docs.do
1920
* v0.2 - added support for registry server using self signed certificates
2021
* v0.1 - first version with basics
2122

22-
## Prerequisites and supported Plattform
23+
## Prerequisites and supported Platform
2324

2425
You need Docker and a Docker registry system which implements the Registry API v2.
2526
For Docker Registry v2 API specification see [https://docs.docker.com/registry/spec/api/](https://docs.docker.com/registry/spec/api/).
2627

27-
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)).
28+
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)).
2829

2930
## Usage
3031

31-
Download the file *cleanreg.py* or clone this repository to a local directory or pull the docker image. This is the suggested way to run `cleanreg`!
32+
Download the file _cleanreg.py_ or clone this repository to a local directory or pull the docker image.
33+
Beginning with version `v0.0.8`, images are supported for `linux/amd64` and (this is new) `linux\arm64`.
34+
This is the suggested way to run `cleanreg`!
3235

33-
```
34-
docker pull hcguersoy/cleanreg:v0.7.0
36+
```shell
37+
docker pull hcguersoy/cleanreg:v0.8.0
3538
```
3639

3740
The image is hosted here: [https://hub.docker.com/r/hcguersoy/cleanreg/](https://hub.docker.com/r/hcguersoy/cleanreg/ "")
3841

39-
*Hint:* `latest` tag is not supported (you know, `latest` is evil 😈).
42+
_Hint:_ `latest` tag is not supported (you know, `latest` is evil :imp:).
4043

4144
To get a immediate help simply run it without any parameters:
4245

43-
```
44-
$ docker run --rm hcguersoy/cleanreg:v0.7.0
46+
```shell
47+
$ docker run --rm hcguersoy/cleanreg:v0.8.0
4548
usage: cleanreg.py [-h] [-v] -r REGISTRY [-p] [-y] [-q] [-n REPONAME:TAG]
4649
[-k KEEPIMAGES] [-re] [-d DATE]
47-
[-f REPOSFILE] [-c CACERT] [-i] [-u BASICAUTHUSER]
50+
[-f REPOSFILE] [-c CACERT] [-sv] [-i] [-u BASICAUTHUSER]
4851
[-pw BASICAUTHPW] [-w MD_WORKERS]
4952

5053
Removes images on a docker registry (v2).
@@ -117,68 +120,67 @@ optional arguments:
117120

118121
```
119122
120-
121123
If you've to use a configuration file (parameter `-f`) you should mount that file into your container:
122124
123-
```
125+
```shell
124126
docker run --rm -it -v $(pwd)/cleanreg-example.conf:/cleanreg-example.yaml hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -f cleanreg-example.yaml -i
125127
```
126128
127129
As an alternative, you can create your own image, inheriting from `cleanreg`:
128130
129-
```
130-
FROM hcguersoy/cleanreg:v0.7.0
131+
```shell
132+
FROM hcguersoy/cleanreg:<version>
131133
ADD myconfig.yaml /config.yaml
132134
```
133135
134136
## Examples
135137
136-
**Attention:** It is strongly recommended that you use the *-i* flag even it is more time and memory consuming. If not you can delete images / layers which you not wanted to delete because registry itself doesn't check if a digest is referenced by multiple tags!
138+
**Attention:** It is strongly recommended that you use the _-i_ flag even it is more time and memory consuming. If not you can delete images / layers which you not wanted to delete because registry itself doesn't check if a digest is referenced by multiple tags!
137139
138140
Cleaning up a single repository called mysql on registry server 192.168.56.2:5000 and keeping 5 of the latest images:
139141
140-
```
142+
```shell
141143
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -n mysql -k 5
142144
```
145+
143146
Be aware that you don't keep here the five last tags but digests/images. As a digest can be associated with multiple tags this can result in deletion of images which you not intended in!
144147
Again: to be secure use the `-i` flag:
145148
146-
```
149+
```shell
147150
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -n mysql -k 5 -i
148151
```
149152
150153
Same as above but ignore images which are associated with multiple tags.
151154
152-
```
155+
```shell
153156
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -n mysql:latest -i
154157
```
155158
156159
Will only delete the image mysql which is tagged as latest.
157160
158-
```
161+
```shell
159162
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -n mysql:.*temp.* -re -d 2018-01-01 -k 5 -i
160163
```
161164
162-
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.
165+
Removes all images that contain the word "temp" in their tag's and if they were created before 2018 but at least 5 will be kept in total.
163166
164-
```
167+
```shell
165168
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -n myalpine -k 50 -i -w 12
166169
```
167170
168-
If you have a very large registry and enough bandwidth you can increase the parallel workers to retrieve the image metadata. The default is *6*. Be aware that you can generate a *DoS* on your registry server by increasing to much.
169-
171+
If you have a very large registry and enough bandwidth you can increase the parallel workers to retrieve the image metadata. The default is _6_. Be aware that you can generate a _DoS_ on your registry server by increasing to much.
170172
171173
Cleaning up all repositories of the registry:
172174
173-
```
175+
```shell
174176
docker run --rm -it hcguersoy/cleanreg:<version> -r http://192.168.56.2:5000 -cf -k 5 -i
175177
```
176-
This will clean up all repositories, keeping 5 images per repository.
177178
179+
This will clean up all repositories, keeping 5 images per repository.
178180
179181
Cleaning up multiple repositories defined in a configuration file:
180182
181-
```
183+
```shell
182184
docker run --rm -it \
183185
-v $(pwd)/cleanreg-example.conf:/cleanreg-example.yaml \
184186
hcguersoy/cleanreg:<version> \
@@ -187,9 +189,10 @@ docker run --rm -it \
187189
-re \
188190
-i
189191
```
192+
190193
The configuration file has the format
191194
192-
```
195+
```yaml
193196
<repository name>:
194197
tag: <tag>
195198
keepimages: <number of images to keep>
@@ -198,7 +201,7 @@ The configuration file has the format
198201
199202
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. A simple example for the configuration file:
200203
201-
```
204+
```yaml
202205
consul:
203206
tag: OnlyThisTag
204207
keepimages: 20
@@ -210,36 +213,38 @@ dummybox:
210213
keepimages: 0
211214
```
212215
213-
214216
The configuration file can be used together with the clean-full-catalog option:
215217
216-
```
218+
```shell
217219
./cleanreg.py -r http://192.168.56.2:5000 -cf -d 20180101 -f cleanreg-example.conf -i
218220
```
219-
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.
220221
222+
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.
221223
222224
There is a simple script added to create multiple image tags (based on `busybox`) on your registry server.
223225
224-
If you have installed a *semi secure* registry server using TLS and self signed certificates you have to provide the path to the CA certificate file:
226+
If you have installed a _semi secure_ registry server using TLS and self signed certificates you have to provide the path to the CA certificate file:
225227
226-
```
227-
./cleanreg.py -r https://192.168.56.3:5000 -c /my/certifacates/ca.pem -f cleanreg-example.conf -i
228+
```shell
229+
./cleanreg.py -r https://192.168.56.3:5000 -c /my/certificates/ca.pem -f cleanreg-example.conf -i
228230
```
229231
230-
If you run *cleanreg* in a container you should not forget to mount the certificate file into the container like the configuration file above.
232+
If you run _cleanreg_ in a container you should not forget to mount the certificate file into the container like the configuration file above.
233+
Alternatively you can set the option `--skip-tls-verify`. In this case, you don't need to provide a certificate.
231234
232-
If your registry is protected with basic auth and the username is `test` and the password is `secret`, you have to pass these credentials to *cleanreg*.
235+
> :exclamation: be aware that this should only used for (local) testing but not for productive environments.
233236
234-
```
237+
If your registry is protected with basic auth and the username is `test` and the password is `secret`, you have to pass these credentials to _cleanreg_.
238+
239+
```shell
235240
./cleanreg.py -r https://192.168.56.3:5000 -u test -pw secret -f cleanreg-example.conf
236241
```
237242
238-
## Runing Garbage Collection
243+
## Running Garbage Collection
239244
240245
Example on running the garbage collection:
241246
242-
```
247+
```shell
243248
$ docker run --rm \
244249
-v /docker/registry2:/var/lib/registry:rw \
245250
registry:latest bin/registry \
@@ -254,27 +259,28 @@ The registry itself should be stopped before running this.
254259
255260
Feel free to contribute your changes as a PR. Please ensure that the tests run without errors and provide tests for additional functionality.
256261
257-
This tool was implemented and tested on Ubuntu Linux 16.04 and on MacOS 10.13 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/)).
262+
This tool was initially implemented and tested on Ubuntu Linux 16.04 and on MacOS 10.13 using Python 2.7 and migrated to Python 3.10 on macOS 12 (arm64).
263+
It is developed and tested against Docker Registry versions [v2.7.1](https://github.com/docker/distribution/releases/tag/v2.7.1) and [v2.8.0](https://github.com/docker/distribution/releases/tag/v2.8.8).
264+
It should work with older registry versions which are supporting the API, but they are not tested anymore.
258265
259-
You need to install the Python modules *requests* and *PyYAML*:
266+
You need to install the Python modules _requests_ and _PyYAML_:
260267
261-
```
268+
```shell
262269
$ pip install requests PyYAML
263270
```
264271
265-
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)).
266-
272+
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)).
267273
268-
### Run tests
274+
### Run tests locally
269275
270276
Prerequisites:
271277
272278
* Bash
273-
* Locally installed Docker engine (remote execution is not yet implemented; runs with Docker for MacOS fine)
279+
* Locally installed Docker engine. Remote execution is not yet implemented. Runs with [Rancher Desktop](https://rancherdesktop.io) on macOS fine (configured to use _dockerd_ as runtime).
274280
275-
You can run all tests, with the *runAllTests.sh* script:
281+
You can run all tests, with the _runAllTests.sh_ script:
276282
277-
```
283+
```shell
278284
cd test
279285
./runAllTests.sh
280286
```
@@ -283,14 +289,25 @@ This will run all tests and repeat them for different versions of the Docker Reg
283289
284290
To run a single test, change to the `test/tests` directory and run a test script:
285291
286-
```
292+
```shell
287293
cd test/tests
288294
./simple_clean.sh
289295
```
290-
By default the test will start the Docker Registry from Docker Hub with the tag `latest`. To specify another registry version, write its Docker Hub tag to the environment variable `REGISTRYTAG`.
291296
292-
```
297+
By default the test will start the Docker Registry from Docker Hub with the tag `latest`.
298+
To specify another registry version, set it using the environment variable `REGISTRYTAG`.
299+
300+
```shell
293301
cd test/tests
294302
export REGISTRYTAG=2.5.1
295303
./simple_clean.sh
296304
```
305+
306+
### GitHub Actions
307+
308+
After pushing to `master` or a branch `feature\*` or `pr\*` a GitHub Action will be triggered which runs the tests defined in `test/runAllTests.sh`. If one of these tests fail, the action will fail, too.
309+
If the build was triggered due to the creation of a release, a GitHub Action will be triggered, too. In this case, after running successfully the tests, the Docker image will be build with the help of _buildx_.
310+
Currently, two kinds of architectures are supported: `linux/amd64` and `linux/arm64`.
311+
The later one could be used on Apple Silicon systems this the according runtime, e. g. Rancher Desktop.
312+
313+
> Due to lack of knowledge regarding other ARM systems (e. g. Raspberry Pi) you are very welcome to provide a PR.

0 commit comments

Comments
 (0)