欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)Harbor私有鏡像倉(cāng)庫(kù)垃圾自動(dòng)化清理詳情

 更新時(shí)間:2022年05月24日 17:02:08   作者:鍵客李大白  
這篇文章主要介紹了Python實(shí)現(xiàn)Harbor私有鏡像倉(cāng)庫(kù)垃圾自動(dòng)化清理詳情,文章圍繞主題分享相關(guān)詳細(xì)代碼,需要的小伙伴可以參考一下

一、編寫Python腳本

[root@lidabai ~]# vim harbor_clearimage.py
# -*- coding:utf-8 -*-
import requests
from requests.auth import HTTPBasicAuth
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import os
import time
import logging
from logging.handlers import RotatingFileHandler

class Harbor(object):
def __init__(self, api_url, api_user, api_passwd, tag_num, proj_exclude):
self.api_url = api_url
self.api_user = api_user
self.api_passwd = api_passwd
self.api_auth = HTTPBasicAuth(self.api_user, self.api_passwd)
self.tag_num = int(tag_num)
self.proj_exclude = proj_exclude
self.proj_url = self.api_url + "/projects"
self.repos_url = self.api_url + "/repositories"
self.header_dict = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
self.deldata = []
self.session = requests.Session()
self.session.auth = self.api_auth
retry = Retry(connect=3, backoff_factor=1)
adapter = HTTPAdapter(max_retries=retry)
self.session.mount('http://', adapter)
self.session.keep_alive = False

def soft_del_repos(self):
try:
projresp = self.session.get(self.proj_url, headers=self.header_dict)
if projresp.status_code == 200:
projdata = projresp.json()
for proj in projdata:
if proj['name'] not in self.proj_exclude # and proj['name'] == "gxjxhwebtest-28003" :
try:
reporesp = self.session.get(self.repos_url, params={"project_id": proj['project_id']} , headers=self.header_dict)
if reporesp.status_code == 200:
repodata = reporesp.json()
for repo in repodata:
if repo["tags_count"] > self.tag_num:
tag_url = self.repos_url + "/" + repo['name'] + "/tags"
tags = self.session.get(tag_url).json()
tagdata = sorted(tags, key=lambda a: a["created"])
del_tags = tagdata[0:len(tagdata) - self.tag_num]
for tag in del_tags:
del_repo_tag_url = tag_url + "/" + tag['name']
cmd = 'curl -v -X DELETE -u "' + self.api_user + ":" + self.api_passwd + '" -H "accept: application/json" ' + del_repo_tag_url
try:
#del_resp = self.session.delete(del_repo_tag_url,headers=self.header_dict)
ok = os.system(cmd)
if ok == 0 :
logging.info("httpdel:" + del_repo_tag_url )
_deldata = {"project_id":proj['project_id'],"project_name":proj['name'],"repo_name":repo['name'],"tag_name":tag['name']}
self.deldata.append(_deldata)
logging.info("httpdel project_id=" + str(proj['project_id']) + ",project_name=" + proj['name'] + ",repo_name=" + repo['name'] + ",tag_name=" + tag['name'])
else:
logging.error("exec_cmd fail:" + cmd )
except:
logging.error("exec_cmd fail:" + cmd )
except:
logging.error("httpget fail:" + self.repos_url)
else:
logging.error("httpget fail:" + self.proj_url )
except:
logging.error("apilogin fail:" + self.api_url )
return self.deldata

def hard_del_repo(self):
pwd_cmd = "cd /dcos/app/harbor/ " #進(jìn)入到Harbor安裝目錄
stop_cmd = "docker-compose stop" #停止Harbor服務(wù)
del_cmd = "docker run -it --name gc --rm --volumes-from registry goharbor/registry-photon:v2.7.1-patch-2819-v1.8.6 garbage-collect /etc/registryctl/config.yml"
start_cmd = "docker-compose start" #啟動(dòng)Harbor服務(wù)
os.system(pwd_cmd)
ok1 = os.system(stop_cmd)
if ok1 == 0 :
time.sleep(10)
ok2 = os.system(del_cmd)
ok3 = os.system(start_cmd)
if ok3 == 0 :
logging.info("hard_del_repo ok:")
else:
logging.error("hard_del_repo fail:")

if __name__ == "__main__":
Rthandler = RotatingFileHandler('harbor_repo_clear.log', maxBytes=10*1024*1024,backupCount=5)
logging.basicConfig(level=logging.INFO)
formatter = logging.Formatter('%(levelname)s %(asctime)s %(process)d %(thread)d %(pathname)s %(filename)s %(funcName)s[line:%(lineno)d] %(message)s')
Rthandler.setFormatter(formatter)
logging.getLogger('').addHandler(Rthandler)

api_url = "http://192.168,2,66:443/api" #Harbor服務(wù)的API URL
api_user = "admin" #超級(jí)管理員
api_passwd = "Harbor12345" #超級(jí)管理員的用戶密碼
tag_num = 20 #保留的tag數(shù)量
proj_exclude = ["library"]
harborClient = Harbor(api_url,api_user,api_passwd,tag_num,proj_exclude)
data = harborClient.soft_del_repos()
if len(data) > 0 :
#harborClient.hard_del_repos()
logging.info("hard_del_repo:")

二、執(zhí)行Python腳本

[root@lidabai ~]# python harbor_clearimage.py

到此這篇關(guān)于Python實(shí)現(xiàn)Harbor私有鏡像倉(cāng)庫(kù)垃圾自動(dòng)化清理詳情的文章就介紹到這了,更多相關(guān) Harbor垃圾清理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pandas DataFrame 交集并集補(bǔ)集的實(shí)現(xiàn)

    pandas DataFrame 交集并集補(bǔ)集的實(shí)現(xiàn)

    這篇文章主要介紹了pandas DataFrame 交集并集補(bǔ)集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Python操作MySQL的方法詳細(xì)解讀

    Python操作MySQL的方法詳細(xì)解讀

    這篇文章主要介紹了Python操作MySQL的方法詳細(xì)解讀,在Python中,通過使用第三方庫(kù):pymysql,完成對(duì)MySQL數(shù)據(jù)庫(kù)的操作,Python操作MySQL并不難,難點(diǎn)是如何編寫合適的SQL語句,需要的朋友可以參考下
    2023-11-11
  • python實(shí)現(xiàn)簡(jiǎn)單溫度轉(zhuǎn)換的方法

    python實(shí)現(xiàn)簡(jiǎn)單溫度轉(zhuǎn)換的方法

    這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單溫度轉(zhuǎn)換的方法,涉及Python操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • python使用opencv驅(qū)動(dòng)攝像頭的方法

    python使用opencv驅(qū)動(dòng)攝像頭的方法

    今天小編就為大家分享一篇python使用opencv驅(qū)動(dòng)攝像頭的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 關(guān)于TensorBoard可視化不顯示數(shù)據(jù)問題No scalar data was found

    關(guān)于TensorBoard可視化不顯示數(shù)據(jù)問題No scalar data was&nbs

    這篇文章主要介紹了如何解決TensorBoard可視化不顯示數(shù)據(jù)問題No scalar data was found,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Tkinter canvas的畫布參數(shù),刪除組件,添加垂直滾動(dòng)條詳解

    Tkinter canvas的畫布參數(shù),刪除組件,添加垂直滾動(dòng)條詳解

    這篇文章主要介紹了python tkinter 畫布參數(shù),刪除組件,添加垂直滾動(dòng)條使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-10-10
  • 在Python中如何優(yōu)雅地創(chuàng)建表格的實(shí)現(xiàn)

    在Python中如何優(yōu)雅地創(chuàng)建表格的實(shí)現(xiàn)

    本文主要介紹了在Python中如何優(yōu)雅地創(chuàng)建表格的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 詳解python的字典及相關(guān)操作

    詳解python的字典及相關(guān)操作

    本文主要介紹了python的字典及相關(guān)操作,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 5款最強(qiáng)且免費(fèi)的Python IDE小結(jié)

    5款最強(qiáng)且免費(fèi)的Python IDE小結(jié)

    開發(fā)工具在日常代碼編寫過程中起著至關(guān)重要的作用,一款優(yōu)秀的開發(fā)工具,不僅可以盡可能的減少你在配置方面耗費(fèi)的精力,本文主要介紹了5種,感興趣的可以了解一下
    2021-07-07
  • Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法實(shí)例

    Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法實(shí)例

    在數(shù)據(jù)處理的過程,有可能需要對(duì)一堆數(shù)據(jù)分組處理,例如對(duì)不同的列進(jìn)行agg聚合操作(mean,min,max等等),下面這篇文章主要給大家介紹了關(guān)于Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法的相關(guān)資料,需要的朋友可以參考下
    2023-06-06

最新評(píng)論