Wechat official account: Operation and maintenance development story, author; Andy

The cause of

The company is promoting Kubernetes, and has built a private mirror warehouse called Harbor. The code will be submitted many times every day for development, and the number of mirrors built is increasing, and the mirrors in the warehouse are also increasing gradually. However, the disk space of the server where Harbor is located is limited, and many mirrors are no longer needed, so manual deletion on harborUI takes time and energy. So, I decided to replace the manual operation with the next script. The shell script I used before had some problems. Learning Python recently, so I plan to use Python to solve.

Train of thought

For example, 20190411.11.23 20181212.12.12, that is, year, month and day. When the points. Each time delete is to delete a whole month, and is a long time. Delete the image by interactively selecting the project repository and the image type, which is the first six bits of the label, such as 201904 201812.

Removal process

Select the project → Select the repository under the project → select the image type → Delete

insufficient

The script is an initial version, with a lot of functionality unfinished and not even a bit of optimization, all in the name of results. I look forward to your comments. The following is the specific content of the script, which is divided into two scripts and used together. I used PYTHon3.6: clean_harbor_image.py and changed it to my own harbor address

#!/usr/bin/env python#--coding:utf-8--import requests# import ast# import configimport sys,json,reimport testclass Harbor_API:
    def __init__(self):
        self.login_user = 'admin'
        self.login_password = 'Harbor12345'
        ## Harbor相关登录配置
        self.login_url = 'https://xxx.xx.x.xxx/login'
        self.projects_url = 'https://xxx.xx.x.xxx/api/projects'
        self.repo_url = "https://xxx.xx.x.xxx/api/repositories?project_id="
        self.image_url = "https://xxx.xx.x.xxx/api/repositories/"
        # "https://192.168.0.1/api/repositories/testrepo%2Fcentos/tags/
        self.headers = {            'Host':'xxx.xx.x.xxx',            'Origin':'https://xxx.xx.x.xxx',            'Referer':'https://xxx.xx.x.xxx/harbor/sign-in',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
        }
        self.data = { 'principal': self.login_user, 'password': self.login_password }
        self.s = requests.Session()    # 获取仓库
    def get_repo(self,project_id):
        # 用join
        url = self.repo_url + str(project_id)        # print(url)
        res = self.s.get(url)

        all_repo = json.loads(res.text)
        repo_data = []        for repo in all_repo:
            data = {}
            data["id"] = repo["id"]
            data["name"] = repo["name"]
            repo_data.append(data)        return repo_data    # 获取镜像数量
    def get_image(self,project_id,repo_name):
        # 用join
        url = self.repo_url+str(project_id)+("&q=%s") % repo_name        # print(url)
        res = self.s.get(url)
        count = json.loads(res.text)[0]["tags_count"]        return count    # 获取镜像标签
    def get_image_tags(self,project_name,repo_name):
        # 用join
        url = self.image_url+project_name+"%2F"+repo_name+"/tags/"
        # print(url)
        res = self.s.get(url)
        all_tags = json.loads(res.text)
        tags_data = []        for tag in all_tags:
            tags_data.append(tag["name"])        #print("tags_data",tags_data)
        # 获取类型
        tags_seri1 = []        for i in tags_data:
            tags_seri1.append(i[0:6])        # 类型去重
        tags_seri2 = []
        [tags_seri2.append(i) for i in tags_seri1 if not i in tags_seri2]        # 根绝类型统计个数
        tags_seri3 = []        for i in tags_seri2:
            data = {}
            data["name"] = i
            data["count"] = tags_seri1.count(i)
            tags_seri3.append(data)        return tags_seri3    # 获取项目
    def get_projects(self):
        #res = self.s.get(self.projects_url)
        # res = self.s.get(self.projects_url)
        res = self.s.get(self.projects_url)        # repo_project = ast.literal_eval(res.text.encode("utf-8"))
        repo_project = json.loads(res.text)
        project_data = []        for project in repo_project:
            data = {}            # project_data[str(project['project_id']] = project['project_id']
            data["id"] = project["project_id"]
            data["name"] = project["name"]
            project_data.append(data)        return project_data    # 删除镜像
    def delete_image(self,all_tags,project_name,repo_name,seri):
        url = self.image_url+project_name+"%2F"+repo_name+"/tags/"
        # print(url)
        res = self.s.get(url)
        all_tags = json.loads(res.text)
        tags_data = []        for tag in all_tags:
            tags_data.append(tag["name"])        #print("tags_data",tags_data)
        # 用join
        url2 = self.image_url + project_name + "%2F" + repo_name + "/tags/"
        # all_tags = self.get_image_tags(project_name,repo_name)
        tag = []        for i in tags_data:
            ret = re.findall(r"%s.*" % seri,i)            #print(ret)
            if not ret:                continue
            else:
                tag.append(ret[0])        #print(tag)
        for i in tag:
            url2 = url + i
            print(url2)
            ret = self.s.delete(url2)        return ret    def login(self):
        ## 创建Session登录
        res = self.s.post(self.login_url, headers=self.headers, data=self.data,verify=False)        return res.status_codedef run():
    
    ss = Harbor_API()
    status_code = ss.login()    #print(status_code)
    if status_code == 200:
        all_projects = ss.get_projects()        # print(all_projects)
        print("--------当前harbor下以下项目-------")
        id_list = []        for i in all_projects: 

            print("id:%s-----项目名:%s" % (i["id"],i["name"]))
            id_list.append(i["id"])        while True:
            project_id = input("请输入上面的项目id,查看该项目下的镜像仓库:")
            project_id = int(project_id.strip())
            flag = project_id in id_list            if flag:
                all_repo = ss.get_repo(project_id)
                print("--------当前项目下有以下镜像仓库-------")
                repo_id_list = []                for i in all_repo:
                    print("id:%s-----仓库名:%s" % (i["id"],i["name"]))
                    repo_id_list.append(i["id"])                # print(repo_id_list)
                while True:
                    repo_id = input("请输入上面的仓库id,查看该项目下的镜像类别和数量:")
                    repo_id = repo_id.strip()                    # print(repo_id)
                    # print(repo_id_list)
                    # print(all_repo)
                    # flag = repo_id in repo_id_list

                    if int(repo_id) in repo_id_list:                        for i in all_repo:                            if i["id"] == int(repo_id):
                                repo_name = i["name"]
                                count = ss.get_image(project_id,repo_name)
                                repo_name = test.tt(repo_name)                        # print(repo_name)
                        # print(count)
                        for i in all_projects:                            if i["id"] == project_id:
                                project_name = i["name"]                        # print(project_name)
                        all_tags = ss.get_image_tags(project_name,repo_name)                        #print(all_tags)
                        for i in all_tags:
                            print("项目为%s,仓库为%s的镜像类型有%s,数量为%s" %(project_name,repo_name,i["name"],i["count"]))
                        seri_name = []                        for i in all_tags:
                            seri_name.append(i["name"])                        while True:
                            seri = input("请输入要删除的镜像类型,如201805:").strip()

                            flag = seri in seri_name                            if flag:                                for i in all_tags:                                    if i["name"] == seri:
                                        print("类型为%s,其数量为%s个" %(seri,i["count"]))
                                        print("project %s  repo %s seri %s" %(project_name,repo_name,seri))                                        #print(all_tags)
                                        ret = ss.delete_image(all_tags,project_name,repo_name,seri)                                        if ret:
                                            print("删除成功")
                                            quit()                            else:
                                print("false")                    else:
                        print("该仓库id不存在,请重新输入。")
            else:
                print("该id不存在,请重新输入。")
## 入口if __name__ == '__main__':
    run()
Copy the code

For-clean_harbor_image.py, to get the repository

#! The/usr/bin/env pythonimport redef tt (STR) : ret = re. The.findall (" {1, 20} \ w/(\ w {1, 20} [_]? {1, 20 \ w} [_]? {1, 20} \ w /? \ w {1, 5}) ", STR) return ret [0] # ss = tt (sc/CMCCSQ - v2 - "device") # print (ss)Copy the code

This article uses the article synchronization assistant to synchronize