Skip to content

Commit d20bd99

Browse files
Merge pull request #14 from agrasth/RTDEV-52487
Worker Sample Improvement
2 parents edcad42 + b848815 commit d20bd99

File tree

10 files changed

+332
-77
lines changed

10 files changed

+332
-77
lines changed
Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
11
# Copy the .deb and .rpm file to specific repository
22

3-
This worker will copy .deb and .rpm files from given repository set to specified repository.
3+
This worker script is designed to handle package copy operations between repositories in a JFrog Artifactory environment. It monitors specified repositories for newly created `.deb` and `.rpm` files, parses the file paths using a predefined layout, and copies the files to designated target repositories.
44

5-
NOTE: Don't forget to add source repositories in the worker's filters.
5+
## Features
66

7-
Also you should add following exclude patterns: repodata/** _tmp_/** and dists/**.
7+
- **Automatic Copying**: Detects newly created `.deb` and `.rpm` files in specified repositories and copies them to their respective destination repositories.
8+
- **Debian Metadata Update**: Updates Debian package properties before copying to ensure compliance with repository configurations.
9+
- **Retry Mechanism**: Implements a retry mechanism for handling transient errors during HTTP requests.
10+
- **Logging**: Provides informative logs for monitoring operations, including successes and failures.
811

12+
## Workflow
13+
14+
1. **Supported Repositories**:
15+
- Source repositories for `.deb` files: `["debian"]`
16+
- Source repositories for `.rpm` files: `["rpm"]`
17+
- Destination for `.deb` files: `["debian-dest"]`
18+
- Destination for `.rpm` files: `["rpm-dest"]`
19+
20+
2. **Debian Metadata Update**:
21+
For `.deb` files, the script updates properties such as:
22+
- Distribution: `trusty, jessie, xenial, stretch, bionic, buster, focal, jammy`
23+
- Component: `main`
24+
- Architecture: `all`
25+
26+
3. **Retry Logic**:
27+
- Retries failed operations (e.g., HTTP POST/PUT) up to 5 times for status codes 404, 409, or other transient errors.
28+
- Includes exponential backoff between retries.
29+
30+
4. **Renaming**:
31+
Replaces timestamps in file names (e.g., `20240824.123456-0400`) with `SNAPSHOT`.
32+
33+
## How to Use
34+
35+
1. **Configuration**:
36+
- Update the repository lists in the script:
37+
```javascript
38+
const debRepositoryList = ["debian"];
39+
const yumRepositoryList = ["rpm"];
40+
const yumRepoCopyList = ["rpm-dest"];
41+
const debRepoCopyList = ["debian-dest"];
42+
```
43+
44+
2. **Deploy the Worker**:
45+
- Ensure the script is deployed in the correct environment and linked to the JFrog platform.
46+
47+
3. **Testing**:
48+
- Test with a sample `.deb` or `.rpm` file.
49+
- Validate that the file is copied to the correct destination with updated properties or renamed as needed.
50+
51+
4. **Monitor Logs**:
52+
- Check the worker logs for success/failure messages:
53+
- **Success**: `Copy success`
54+
- **Failure**: `Unable to copy artifact to <destination>: <error_message>`
55+
56+
5. **Exclude Patterns**:
57+
Add the following exclude patterns to the worker to ensure unnecessary files are ignored: repodata/** tmp/** dists/**
58+
59+
## Example Log Output
60+
61+
```
62+
INFO: Copying package: rpm/rpm-4.20.0-6-omv2490.aarch64.rpm to rpm-dest/rpm/rpm-4.20.0-6-SNAPSHOT.aarch64.rpm
63+
INFO: Copy success
64+
WARN: Unable to rename Timestamp to Snapshot for debian/dummy-package.deb
65+
ERROR: Unable to copy artifact to debian-dest: File not found
66+
```
67+
68+
## Notes
69+
- Ensure that source repositories are added to the worker's filters.
70+
- Only files matching the expected layout regex will be processed.
71+
- Don't forget to add appropriate permissions for accessing and modifying repositories.

samples/artifactory/AFTER_CREATE/copy-deb-and-rpm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "copy-deb-and-rpm",
33
"description": "Run a script on AFTER_CREATE",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"scripts": {
66
"deploy": "jf worker deploy",
77
"undeploy": "jf worker rm \"copy-deb-and-rpm\"",

samples/artifactory/AFTER_CREATE/copy-deb-and-rpm/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const yumRepositoryList = ["rpm"] //if something is created here...
66
const yumRepoCopyList = ["rpm-dest"] //copy it to the corresponding repo here
77
const debRepoCopyList = ["debian-dest"] //copy it to the corresponding repo here
88

9-
const layOutRegex = /(?<orgPath>.+?)\/(?<module>[^/]+)\/(\2)-(?<baseRev>[^/]+?)\.(?<ext>(?:(?!\d))[^\-\/]+|7z)/;
9+
const layOutRegex = /(?<orgPath>.+?)\/(?<module>[^\/]+?)-(?<baseRev>[^\/]+)\.(?<ext>[a-z]+)/;
1010

1111
export default async (context: PlatformContext, data: AfterCreateRequest): Promise<AfterCreateResponse> => {
1212
await copyPackage(context, data.metadata.repoPath);
Lines changed: 139 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,87 @@
1-
Artifactory Clean Docker Images User Plugin
2-
===========================================
1+
# Artifactory Clean Docker Images Worker
32

4-
This worker code is used to clean Docker repositories based on configured cleanup policies.
3+
This worker script is designed to clean Docker repositories hosted in Artifactory based on configurable cleanup policies. It is useful for managing storage and maintaining repository hygiene.
54

6-
Configuration
7-
-------------
5+
---
86

9-
- `dockerRepos`: A list of Docker repositories to clean. If a repo is not in
10-
this list, it will not be cleaned.
11-
- `byDownloadDate`: An optional boolean flag (true/false).
12-
* **false** (default): retention will take into account only **creation** date of the image
13-
(technically, its manifest file). This is the original behaviour.
14-
* **true**: identify images to remove by their **last download date** or failing that,
15-
last **update** date. This mode of operation has been inspired by the 'artifactCleanup'
16-
plugin.
7+
## Key Features
178

18-
For example:
9+
- Cleans Docker repositories based on:
10+
- Maximum age of images
11+
- Maximum number of image versions
12+
- Supports cleanup based on:
13+
- Image creation date
14+
- Last download date
15+
- Provides optional dry-run mode for safe execution.
16+
- Handles complex repositories with multiple Docker files.
17+
- Logs processed repositories and cleanup details for transparency.
18+
19+
20+
## Configuration
21+
---
22+
The worker accepts the following configuration parameters:
23+
24+
### Required Parameters
25+
26+
* **`dockerRepos`**: A list of Docker repositories to clean. Only repositories listed here will be processed.
27+
28+
29+
### Optional Parameters
30+
31+
* **`byDownloadDate`** (boolean, default: `false`):
32+
33+
* **`false`**: Images are cleaned based on their creation date.
34+
35+
* **`true`**: Images are cleaned based on their last download date or, if unavailable, their last update date.
36+
37+
* **`dryRun`** (boolean, default: `false`):
38+
39+
* **`true`**: Simulates the cleanup process without deleting any files.
40+
41+
* **`false`**: Executes the cleanup, deleting the identified files.
42+
43+
44+
### Example Configuration
1945

2046
```json
2147
{
2248
"dockerRepos": ["example-docker-local", "example-docker-local-2"],
23-
"byDownloadDate": false
49+
"byDownloadDate": false,
50+
"dryRun": true
2451
}
2552
```
2653

27-
Usage
54+
### Usage
2855
-----
2956

30-
Cleanup policies are specified as labels on the Docker image. Currently, this
31-
plugin supports the following policies:
57+
### Cleanup Policies
58+
59+
Cleanup policies are defined using labels in the Docker image. The worker supports the following policies:
3260

33-
- `maxDays`: The maximum number of days a Docker image can exist in an
34-
Artifactory repository. Any images older than this will be deleted.
35-
* when `byDownloadDate=true`: images downloaded or updated within last `maxDays` will
36-
be preserved
37-
- `maxCount`: The maximum number of versions of a particular image which should
38-
exist. For example, if there are 10 versions of a Docker image and `maxCount`
39-
is set to 6, the oldest 4 versions of the image will be deleted.
40-
* when `byDownloadDate=true`: image age will be determined by first checking
41-
the _Last Downloaded Date_ and _Modification Date_ will be checked only when this image has never
42-
been downloaded.
61+
* **`maxDays`**: Specifies the maximum number of days an image can exist in the repository. Older images will be deleted.
62+
63+
* When `byDownloadDate=true`: Images downloaded or updated within the last `maxDays` will be preserved.
64+
65+
* **`maxCount`**: Specifies the maximum number of image versions to retain. Excess versions will be deleted, starting with the oldest.
66+
67+
* When `byDownloadDate=true`: Image age is determined first by the _Last Downloaded Date_ and then by the _Modification Date_ if the image has never been downloaded.
68+
4369

44-
To set these labels for an image, add them to the Dockerfile before building:
70+
### Adding Cleanup Labels to Docker Images
4571

46-
``` dockerfile
72+
Labels can be added to the Dockerfile before building the image. For example:
73+
74+
```dockerfile
4775
LABEL com.jfrog.artifactory.retention.maxCount="10"
4876
LABEL com.jfrog.artifactory.retention.maxDays="7"
4977
```
5078

51-
When a Docker image is deployed, Artifactory will automatically create
52-
properties reflecting each of its labels. These properties are read by the
53-
worker in order to decide on the cleanup policy for the image.
79+
When deployed, these labels are automatically converted into properties in Artifactory. The worker reads these properties to determine the cleanup policy for each image.
80+
81+
Execution
82+
---------
83+
84+
### JFrog CLI
5485

5586
Cleanup can be triggered using the JFrog CLI. For example:
5687

@@ -64,8 +95,81 @@ jf worker exec my-worker - <<EOF
6495
EOF
6596
```
6697

67-
Execute with the payload located into a file named `payload.json`:
98+
Alternatively, execute with a payload file:
6899

69100
```shell
70101
jf worker exec my-worker @payload.json
71-
```
102+
```
103+
104+
105+
Worker Timeout
106+
--------------
107+
108+
### Timeout Behavior
109+
110+
* The worker has a maximum execution timeout of **5 seconds**. If the cleanup process for a complex repository exceeds this limit:
111+
112+
* Files that can be deleted within the timeout are processed.
113+
114+
* A timeout error is returned:
115+
```json
116+
{ "message": "Worker execution timeout" }
117+
```
118+
119+
* Remaining files are **not** processed.
120+
121+
122+
### Implications
123+
124+
* Repositories with large numbers of images or complex cleanup requirements may require multiple executions to fully clean.
125+
126+
* It is recommended to periodically monitor and trigger the worker for such repositories.
127+
128+
129+
Logging
130+
-------
131+
132+
### Payload and Repository Logs
133+
134+
* The worker logs the received payload and the processed repositories for debugging purposes. Example:
135+
```
136+
Payload - {
137+
"dockerRepos": ["example-docker-local"],
138+
"byDownloadDate": false,
139+
"dryRun": false
140+
}
141+
Repos - ["example-docker-local"]
142+
```
143+
144+
* Errors and unprocessed files due to timeout are logged for transparency.
145+
146+
147+
Example Cleanup Workflow
148+
------------------------
149+
150+
1. Configure cleanup policies using labels in the Dockerfile.
151+
152+
2. Deploy the images to Artifactory.
153+
154+
3. Define the worker payload:
155+
```json
156+
{
157+
"dockerRepos": ["example-docker-local"],
158+
"byDownloadDate": true,
159+
"dryRun": false
160+
}
161+
```
162+
163+
4. Trigger the worker using the JFrog CLI.
164+
165+
5. Review the logs for details about the cleanup process.
166+
167+
168+
Notes
169+
-----
170+
171+
* **Timeout Management**: For large or complex repositories, consider breaking cleanup into smaller tasks.
172+
173+
* **Dry Run**: Always perform a dry run for initial testing to validate the cleanup logic.
174+
175+
* **Monitoring**: Regularly monitor the storage usage and worker logs to ensure efficient repository management.

samples/artifactory/GENERIC_EVENT/clean-docker-images/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "clean-docker-images",
33
"description": "Run a script on GENERIC_EVENT",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"scripts": {
66
"deploy": "jf worker deploy",
77
"undeploy": "jf worker rm \"clean-docker-images\"",

0 commit comments

Comments
 (0)