|
| 1 | +:_content-type: REFERENCE |
| 2 | +[id="automating-quay-using-the-api"] |
| 3 | += Automating {productname} processes by using the API |
| 4 | + |
| 5 | +With the API, {productname} administrators and users with access to the API can automate repetitive tasks such as repository management or image pruning. The following example shows you how you might use a Python script and a cron job to to automate image pruning. |
| 6 | + |
| 7 | +.Prerequisites |
| 8 | + |
| 9 | +* You have access to the {productname} API, which entails having already created an OAuth 2 access token. |
| 10 | +* You have set `BROWSER_API_CALLS_XHR_ONLY: false` in your `config.yaml` file. |
| 11 | +* You have installed the Python `requests` library using. |
| 12 | +* You have enabled cron jobs on your machine. |
| 13 | +
|
| 14 | +.Procedure |
| 15 | + |
| 16 | +. Create a Python script that executes an API command. The following example is used to prune images using the link:https://docs.redhat.com/en/documentation/red_hat_quay/{producty}/html-single/red_hat_quay_api_guide/index#deletefulltag[`DELETE /api/v1/repository/{repository}/tag/{tag}`] API endpoint. |
| 17 | ++ |
| 18 | +[source,python] |
| 19 | +---- |
| 20 | +import requests <1> |
| 21 | + |
| 22 | +# Hard-coded values |
| 23 | +API_BASE_URL = "http://<quay-server.example.com>/api/v1" <2> |
| 24 | +ACCESS_TOKEN = "<access_token>" <3> |
| 25 | +NAMESPACE = "<namespace_name>" <4> |
| 26 | +REPO_NAME = "<repository_name>" <5> |
| 27 | +TAG = "<tag_name>" <6> |
| 28 | + |
| 29 | +def delete_image_tag(): |
| 30 | + # Construct the full API URL for deleting the tag |
| 31 | + url = f"{API_BASE_URL}/repository/{NAMESPACE}/{REPO_NAME}/tag/{TAG}" |
| 32 | + headers = { |
| 33 | + "Authorization": f"Bearer {ACCESS_TOKEN}", |
| 34 | + "Content-Type": "application/json" |
| 35 | + } |
| 36 | + |
| 37 | + # Send the DELETE request to the API |
| 38 | + response = requests.delete(url, headers=headers) |
| 39 | + |
| 40 | + # Check the response and print appropriate messages |
| 41 | + if response.status_code == 200: |
| 42 | + print("Tag deleted successfully") |
| 43 | + else: |
| 44 | + print("Failed to delete tag:", response.json()) |
| 45 | + |
| 46 | +# Execute the function |
| 47 | +delete_image_tag() |
| 48 | +---- |
| 49 | +<1> Includes the `import` library in your Python code. |
| 50 | +<2> The URL of your registry appended with `/api/v1`. |
| 51 | +<3> Your OAuth 2 access token. |
| 52 | +<4> The namespace that holds the image tag. |
| 53 | +<5> The repository that holds the image tag. |
| 54 | +<6> The tag name of the image. |
| 55 | +
|
| 56 | +. Save the script as `prune_images.py`. |
| 57 | +
|
| 58 | +. Create a cron job that automatically runs the script: |
| 59 | +
|
| 60 | +.. Open the crontab editor by running the following command: |
| 61 | ++ |
| 62 | +[source,terminal] |
| 63 | +---- |
| 64 | +$ crontab -e |
| 65 | +---- |
| 66 | +
|
| 67 | +.. In the editor, add the cron job for running the script. The following example runs the script every minute: |
| 68 | ++ |
| 69 | +[source,text] |
| 70 | +---- |
| 71 | +* * * * * sudo python /path/to/prune_images.py >> /var/log/prune_images.log 2>&1 |
| 72 | +---- |
| 73 | +
|
0 commit comments