Add scripts for Harbor user and project management: - register.py: Bulk user registration from Excel - delete_users.py: Complete user deletion with resource cleanup - delete_projects.py: Targeted deletion of student projects (stu01-stu49) - CLAUDE.md: Project documentation and API guidance - requirements.txt: Python dependencies 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import pandas as pd
|
||
import json
|
||
import requests
|
||
import getpass
|
||
import sys
|
||
from urllib3.exceptions import InsecureRequestWarning
|
||
from requests.auth import HTTPBasicAuth
|
||
|
||
# 禁用不安全HTTPS警告
|
||
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||
|
||
HARBOR_URL = "https://harbor.seahi.me"
|
||
|
||
def get_admin_credentials():
|
||
print("请输入Harbor管理员账号信息:")
|
||
username = input("用户名: ")
|
||
password = getpass.getpass("密码: ")
|
||
return username, password
|
||
|
||
def create_harbor_user(auth, user_data):
|
||
"""创建Harbor用户"""
|
||
url = f"{HARBOR_URL}/api/v2.0/users"
|
||
headers = {
|
||
'Content-Type': 'application/json'
|
||
}
|
||
payload = {
|
||
"username": user_data["username"],
|
||
"email": user_data["email"],
|
||
"realname": user_data["realname"],
|
||
"password": user_data["password"],
|
||
}
|
||
|
||
try:
|
||
response = requests.post(url, json=payload, headers=headers, auth=auth, verify=False)
|
||
if response.status_code == 201:
|
||
print(f"✓ 成功创建用户: {user_data['username']}")
|
||
return True
|
||
elif response.status_code == 409:
|
||
print(f"! 用户已存在: {user_data['username']}")
|
||
return True
|
||
else:
|
||
print(f"✗ 创建用户失败 {user_data['username']}: {response.status_code} - {response.text}")
|
||
return False
|
||
except Exception as e:
|
||
print(f"✗ 创建用户出错 {user_data['username']}: {str(e)}")
|
||
return False
|
||
|
||
def convert_excel_to_json():
|
||
try:
|
||
# 读取Excel文件,跳过第一行(标题行)
|
||
df = pd.read_excel('users.xlsx', skiprows=1)
|
||
|
||
# 获取管理员凭据
|
||
admin_username, admin_password = get_admin_credentials()
|
||
|
||
# 创建认证对象
|
||
auth = HTTPBasicAuth(admin_username, admin_password)
|
||
|
||
# 测试认证
|
||
test_response = requests.get(f"{HARBOR_URL}/api/v2.0/users", auth=auth, verify=False)
|
||
if test_response.status_code == 401:
|
||
print("认证失败:用户名或密码错误")
|
||
return
|
||
|
||
print("\n开始创建用户...")
|
||
success_count = 0
|
||
total_count = 0
|
||
|
||
# 将DataFrame转换为JSON格式并创建用户
|
||
for _, row in df.iterrows():
|
||
# 确保所有值都转换为字符串,并处理 NaN 值
|
||
username = str(row.iloc[0]).strip()
|
||
realname = str(row.iloc[1]).strip()
|
||
password = str(row.iloc[2]).strip()
|
||
email = str(row.iloc[4]).strip()
|
||
|
||
# 跳过空行
|
||
if username == 'nan' or realname == 'nan':
|
||
continue
|
||
|
||
user_dict = {
|
||
"username": username,
|
||
"realname": realname,
|
||
"password": password,
|
||
"email": email
|
||
}
|
||
|
||
total_count += 1
|
||
if create_harbor_user(auth, user_dict):
|
||
success_count += 1
|
||
|
||
print(f"\n完成!成功创建 {success_count}/{total_count} 个用户")
|
||
|
||
except Exception as e:
|
||
print(f"发生错误: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
convert_excel_to_json()
|