共计 10435 个字符,预计需要花费 27 分钟才能阅读完成。
性能展现
代码展现
- app.py
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
from flask import Flask, request, render_template, redirect, url_for, make_response ,escape, session, flash, g, current_app, abort, jsonify, send_file
import requests
#from werkzeug.utils import secure_filename
import os,time, platform
from flask_wtf import Form
#from wtforms import TextField
#from shell import os_scrpict, harbor
#import sqlite3
#from pathlib import Path
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# 本脚本只是 harbor 页面 tags 清理
# cd /usr/local/src/harbor && docker-compose stop #敞开 harbor
# 物理清理:docker run -it --name gc --rm --volumes-from registry goharbor/registry-photon:v2.6.2-v1.6.3 garbage-collect /etc/registry/config.yml
#docker-compose up -d #启动
class RequestClient(object):
def __init__(self, login_url, username, password):
self.username = username
self.password = password
self.login_url = login_url
self.session = requests.Session()
self.login()
def login(self):
requests.packages.urllib3.disable_warnings()
self.session.post(self.login_url, params={"principal": self.username, "password": self.password}, verify=False)
class ClearHarbor(object):
def __init__(self, harbor_domain, username, password, num, schema="http",):
self.num = num
self.schema = schema
self.harbor_domain = harbor_domain
self.harbor_url = self.schema + "://" + self.harbor_domain
self.login_url = self.harbor_url + "/login"
self.api_url = self.harbor_url + "/api"
self.pro_url = self.api_url + "/projects"
self.repos_url = self.api_url + "/repositories"
self.username = username
self.password = password
self.client = RequestClient(self.login_url, self.username, self.password)
def __fetch_pros_obj(self):
# 获取所有项目名称
self.pros_obj = self.client.session.get(self.pro_url).json()
# for n in self.pros_obj:
# print("分组:",n.get("name"))
print(self.pros_obj)
return self.pros_obj
def get_pros_id(self):
# 获取所有我的项目 ID
self.pros_id = []
pro_res = self.__fetch_pros_obj()
for i in pro_res:
self.pros_id.append(i['project_id'])
# print("所有我的项目 ID:",self.pros_id)
return self.pros_id
def get_del_repos_name(self, pro_id):
# 镜像 tag 数量大于 self.num 的镜像仓库名称
self.del_repos_name = []
repos_res = self.client.session.get(self.repos_url, params={"project_id": pro_id})
# print("我的项目信息:",repos_res.json())
for repo in repos_res.json():
if repo["tags_count"] > self.num:
# print("镜像仓库名称:",repo['name'])
self.del_repos_name.append(repo['name'])
return self.del_repos_name
def fetch_del_repos(self, repo_name):
# 删除镜像仓库 tag
self.del_res = []
tag_url = self.repos_url + "/" + repo_name + "/tags"
# 我的项目镜像仓库的所有 tags, 按创立工夫排序
tags = self.client.session.get(tag_url).json()
tags_sort = sorted(tags, key=lambda a: a["created"])
# print(len(tags_sort),tags_sort)
# 除了最新的 self.num 个,其余的 tag 都增加到待删除列表 del_tags
del_tags = tags_sort[0:len(tags_sort) - self.num]
# print(del_tags)
for tag in del_tags:
del_repo_tag_url = tag_url + "/" + tag['name']
# print(del_repo_tag_url)
del_res = self.client.session.delete(del_repo_tag_url)
self.del_res.append("镜像: %s 删除状态: %s" % (del_repo_tag_url,del_res))
return self.del_res
def work(self):
# 遍历 project id
for i in self.get_pros_id():
# 获取所有 tag 超过 self.num 的 repos
repos = self.get_del_repos_name(i)
if repos:
for repo in repos:
del_repos = self.fetch_del_repos(repo)
print(del_repos)
print(repo)
def list_repositories(self, pro_id):
# 镜像 tag 数量大于 self.num 的镜像仓库名称
self.del_repos_name1 = []
repos_res = self.client.session.get(self.repos_url, params={"project_id": pro_id})
# print("我的项目信息:",repos_res.json())
for repo in repos_res.json():
#if repo["tags_count"] > self.num:
#print(repo,"镜像仓库名称:",repo['name'])
#self.del_repos_name1.append({"name": repo["name"], "tags_count": repo["tags_count"]})
self.del_repos_name1.append({"name": repo["name"], "tags_count": repo["tags_count"],"project_id": repo["project_id"]})
#print(self.del_repos_name1)
return self.del_repos_name1
def list_pros(self):
# 获取所有我的项目 ID
self.pros_idname = []
pro_res = self.__fetch_pros_obj()
for i in pro_res:
self.pros_idname.append({"project_id": i["project_id"], "name": i["name"], "repo_count": i["repo_count"] })
#print(self.pros_idname)
#print(self.pros_idname[0]['name'])
return self.pros_idname
def work_list(self, pros_id_list):
# 遍历 project id
for i in pros_id_list:
# 获取所有 tag 超过 self.num 的 repos
repos = self.get_del_repos_name(i)
if repos:
for repo in repos:
# del_repos = self.fetch_del_repos(repo)
# print(del_repos)
print(repo)
@app.route('/harbor/', methods = ['GET', 'POST'])
def harbor_list():
del_images_tag = ClearHarbor(harbor_domain="172.16.167.11",
username="admin",
password="Harbor12345",
num=10)
harbor_project = del_images_tag.list_pros()
print("###########",harbor_project)
return render_template('harbor.html', harbor_project1 = harbor_project)
@app.route('/del_harbor_project_all/')
def del_harbor_project_all1():
del_images_tag = ClearHarbor(harbor_domain="172.16.167.11",
username="admin",
password="Harbor12345",
num=10)
del_images_tag.work()
return redirect('/harbor/')
@app.route('/del_harbor_project/<int:project_id>')
def del_harbor_project(project_id):
del_images_tag = ClearHarbor(harbor_domain="172.16.167.11",
username="admin",
password="Harbor12345",
num=10)
del_reposname = del_images_tag.get_del_repos_name(project_id)
print(del_reposname,"#####################",project_id)
del_reposname_status = del_images_tag.fetch_del_repos(del_reposname)
print(del_reposname_status)
return redirect('/harbor/')
@app.route('/del_harbor_project_list/', methods = ['GET', 'POST'])
def del_harbor_project_list():
if request.method == 'POST':
project_id_list = request.form.getlist('vals')
#print(project_id_list)
vals_list2 = []
for i in project_id_list:
if i == '':
print("i is none")
else:
vals_list2.append(i)
del_images_tag = ClearHarbor(harbor_domain="172.16.167.11",
username="admin",
password="Harbor12345",
num=10)
del_images_tag.work_list(vals_list2)
return redirect('/harbor/')
@app.route('/harbor_repositories/<int:project_id>')
def harbor_repositoriest(project_id):
del_images_tag = ClearHarbor(harbor_domain="192.168.1.17",
username="admin",
password="xxxxxx",
num=10)
harbor_project = del_images_tag.list_repositories(project_id)
print("###########",harbor_project)
return render_template('harbor_repositories.html', harbor_project1 = harbor_project)
@app.route('/del_repositories_url/<int:project_id>/<path:project_name>')
def del_repositories_url(project_id,project_name):
del_images_tag = ClearHarbor(harbor_domain="192.168.1.17",
username="admin",
password="xxxxxx",
num=10)
del_reposname_status = del_images_tag.fetch_del_repos(project_name)
print(del_reposname_status)
return redirect('/harbor_repositories/{tproject_id}'.format(tproject_id=project_id))
if __name__ == '__main__':
app.run()
#app.run(host="172.16.117.33",port=5888)
- harbor.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="/static/jquery-1.9.1.min.js"></script>
<script>
$('#out').click(function () {
$.ajax({
url: '/dns_list/',
type: 'GET',
data: {},
success: function (data) {
// 期待解决完结主动调用,data- 返回值
console.log(data);
window.location.href = "url"
}
})
});
$('#back').click(function () {
$.ajax({
url: '/dns_list/',
type: 'GET',
data: {},
success: function (data) {window.location.href = "/"}
})
});
function getValues_del_project() {var valArr=[];
var ones=document.getElementsByName('item');
for (var i=0;i<ones.length;i++){if (ones[i].checked==true){valArr[i]=ones[i].value
}
}
if (valArr.length!=0){// var vals = valArr.join(',');
// alert(valArr);
// var url2 = '/app_intstall/'
$.ajax({
url:"/del_harbor_project_list/",
type:'POST',
contenType:'application/json',
// #不加这个,ajax 会将后果后边加个 [],例如 {'vals[]':[4,6,8]}
traditional:true,
// #不加这个,会报服务器终止了一个在运行的程序
async: false,
data:{'vals':valArr},
success:function(){alert("批量革除 project 超过 10 个 tag 胜利");
// window.location.href =url2
},
error:function(){alert("批量革除 project 超过 10 个 tag 失败");
}
})
}
else {
var error_m="请抉择数据";
alert(error_m);
}
}
</script>
<div class="container" style="position: absolute;left: -2%;width: 1590px;height: 850px;margin-left: 95px;">
<h2>harbor</h2>
<div style="float: left; width:190px;height:70px">
<ul class='myul'>
<li><a class="btn btn-success input-sm" href="/harbor/"
onclick="getValues_del_project()"> 批量革除选中我的项目 images </a>
</li>
</ul>
</div>
<div style="float: left; width:290px;height:40px;display:block;overflow-y:auto;">
<ul class='myul'>
<li>
<a class="btn btn-primary input-sm" href="/del_harbor_project_all/"> 革除 all project >10 tag images </a>
</li>
</ul>
</div>
{# <div style="position: absolute;top: 12%;left: 65%;width: 590px;height: 650px;">#}
{# <p>text:<textarea rows="41" cols="80" name="conftext"> {{ conflook.conflook}} </textarea></p>#}
{# </div>#}
<table class="table table-condensed" id="table1">
<thead>
<tr>
<th width="25"><input id="checkAll" onclick="checkAll()" type="checkbox"></th>
<th>ID</th>
<th> 我的项目 </th>
<th> 镜像仓库数 </th>
<th> 操作 </th>
</tr>
</thead>
<tbody>
{% for ele in harbor_project1 %}
<tr>
<td><input name="item" onclick="checkOne()" type="checkbox" value="{{ele.project_id}}"></td>
<td>{{ele.project_id}}</td>
<td><a href="/harbor_repositories/{{ele.project_id}}"> {{ele.name}}</a></td>
<td>{{ele.repo_count}} </td>
<td><a href="/del_harbor_project/{{ele.project_id}}"> 革除 </a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
- harbor_repositories.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="/static/jquery-1.9.1.min.js"></script>
<script>
$('#out').click(function () {
$.ajax({
url: '/dns_list/',
type: 'GET',
data: {},
success: function (data) {
// 期待解决完结主动调用,data- 返回值
console.log(data);
window.location.href = "url"
}
})
});
$('#back').click(function () {
$.ajax({
url: '/dns_list/',
type: 'GET',
data: {},
success: function (data) {window.location.href = "/"}
})
});
function getValues_del_repositories() {var valArr=[];
var ones=document.getElementsByName('item');
for (var i=0;i<ones.length;i++){if (ones[i].checked==true){valArr[i]=ones[i].value
}
}
if (valArr.length!=0){// var vals = valArr.join(',');
// alert(valArr);
// var url2 = '/app_intstall/'
$.ajax({
url:"/del_harbor_repositories_list/",
type:'POST',
contenType:'application/json',
// #不加这个,ajax 会将后果后边加个 [],例如 {'vals[]':[4,6,8]}
traditional:true,
// #不加这个,会报服务器终止了一个在运行的程序
async: false,
data:{'vals':valArr},
success:function(){alert("批量革除 image 超过 10 个 tag 胜利");
// window.location.href =url2
},
error:function(){alert("批量革除 image 超过 10 个 tag 失败");
}
})
}
else {
var error_m="请抉择数据";
alert(error_m);
}
}
</script>
<div class="container" style="position: absolute;left: -2%;width: 1590px;height: 850px;margin-left: 95px;display:block;overflow-y:auto;">
<h2>harbor</h2>
{# <div style="position: absolute;top: 12%;left: 65%;width: 590px;height: 650px;">#}
{# <p>text:<textarea rows="41" cols="80" name="conftext"> {{ conflook.conflook}} </textarea></p>#}
{# </div>#}
<table class="table table-condensed" id="table1" >
<thead>
<tr>
<th width="25"><input id="checkAll" onclick="checkAll()" type="checkbox"></th>
<th> 我的项目 </th>
<th> 标签数 </th>
<th> 操作 </th>
</tr>
</thead>
<tbody>
{% for ele in harbor_project1 %}
<tr>
<td><input name="item" onclick="checkOne()" type="checkbox" value="{{ele.name}}"></td>
<td> {{ele.name}}</td>
<td>{{ele.tags_count}} </td>
<td><a href="/del_repositories_url/{{ele.project_id}}/{{ele.name}}"> 革除 </a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
正文完