Skip to content

Commit 6d3bd1b

Browse files
stevsmitSteven Smith
andauthored
Adds cron job example (quay#1190)
Co-authored-by: Steven Smith <[email protected]>
1 parent aa59a69 commit 6d3bd1b

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

api/master.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ include::modules/creating-v2-oauth-access-token.adoc[leveloffset=+2]
4646
include::modules/enabling-using-the-api.adoc[leveloffset=+1]
4747
include::modules/configuring-api-calls.adoc[leveloffset=+2]
4848
include::modules/using-the-api.adoc[leveloffset=+2]
49-
49+
include::modules/automating-quay-using-the-api.adoc[leveloffset=+2]
5050
5151
5252
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)