1. Python脚本
import argparse
import requests
def create_project(harbor_url, username, password, project_name):
headers = {
"Content-Type": "application/json"
}
auth = (username, password)
response = requests.get(f"{harbor_url}/api/v2.0/projects", auth=auth)
projects = response.json()
project_exists = any(project["name"] == project_name for project in projects)
if not project_exists:
data = {
"project_name": project_name,
}
response = requests.post(f"{harbor_url}/api/v2.0/projects", json=data, auth=auth, headers=headers)
if response.status_code == 201:
print(f"项目 {project_name} 创建成功")
else:
print(f"项目 {project_name} 创建失败:{response.text}")
else:
print(f"项目 {project_name} 已经存在")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create project in Harbor")
parser.add_argument("--harbor_url", type=str, help="Harbor host URL")
parser.add_argument("--username", type=str, help="Username for authentication")
parser.add_argument("--password", type=str, help="Password for authentication")
parser.add_argument("--project_name", type=str, help="Name of the project to create")
args = parser.parse_args()
create_project(args.harbor_url, args.username, args.password, args.project_name)
2. 用法
python3 create_harbor_project.py --harbor_url=改为自己harbor地址 --username=admin --password=Harbor12345 --project_name=test111
脚本新增功能
调用harbor 的 retentions接口,执行添加保留策略
说明:
在新建项目的基础上,增加策略中的保留规则和定时执行
保留规则:只保留最新推送的3个镜像
定时执行:定时每天0点执行
用法与上面一致
import argparse
import requests
def create_project(harbor_url, username, password, project_name):
headers = {
"Content-Type": "application/json"
}
auth = (username, password)
response = requests.get(f"{harbor_url}/api/v2.0/projects", auth=auth)
projects = response.json()
project_exists = any(project["name"] == project_name for project in projects)
if not project_exists:
data = {
"project_name": project_name,
}
response = requests.post(f"{harbor_url}/api/v2.0/projects", json=data, auth=auth, headers=headers)
if response.status_code == 201:
print(f"Harbor项目 {project_name} 创建成功")
response = requests.get(f"{harbor_url}/api/v2.0/projects", auth=auth)
projects = response.json()
for i in projects:
if i['name'] == project_name:
print(f"{project_name}项目ID为:{i['project_id']}")
policy_data = {
"algorithm": "or",
"rules": [
{
"action": "retain",
"params": {"latestPushedK": 3},
"scope_selectors": {
"repository": [
{"decoration": "repoMatches", "kind": "doublestar", "pattern": "**"}
]
},
"tag_selectors": [
{"decoration": "matches", "extras": "{\"untagged\":true}", "kind": "doublestar", "pattern": "**"}
],
"template": "latestPushedK"
}
],
"scope": {"level": "project", "ref": i['project_id']},
"trigger": {
"kind": "Schedule",
"settings": {"cron": "0 0 0 * * *"}
}
}
response = requests.post(f"{harbor_url}/api/v2.0/retentions", json=policy_data, auth=auth, headers=headers)
if response.status_code == 201:
print(f"{project_name}项目保留策略添加成功:每天执行一次,保留最近推送的3个镜像")
else:
print(f"{project_name}项目保留策略添加出错:{response.text}")
else:
print(f"Harbor项目 {project_name} 创建失败:{response.text}")
else:
print(f"Harbor项目 {project_name} 已经存在")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create project in Harbor")
parser.add_argument("--harbor_url", type=str, help="Harbor host URL")
parser.add_argument("--username", type=str, help="Username for authentication")
parser.add_argument("--password", type=str, help="Password for authentication")
parser.add_argument("--project_name", type=str, help="Name of the project to create")
args = parser.parse_args()
create_project(args.harbor_url, args.username, args.password, args.project_name)