zadig把服务的yaml同步到gitlab

需求:把zadgi服务的yaml文件同步到gitlab仓库
在这里插入图片描述

python3 代码:

import requests
import sys
import os
import subprocess
from loguru import logger
logger.add("create_jenkins_job.log", rotation="100 MB")


def yaml_create(name_proect,xinagmu_name):
    HEADERS = {
    
    'Content-Type': 'application/x-www-form-urlencoded',
               "Authorization": "Bearer iOiJsdW96aXh1In0sImF1ZCI6lMcOTSdm0ETsdVLecdr6GdoSpu1pY1KoPIwU",
               }

    rul = "http://zadig.buydance.com/api/aslan/service/services/{}/k8s?projectName=ceshihehuisechuangjianxinxiangmu".format(
        xinagmu_name)

    conten = requests.get(url=rul, headers=HEADERS)
    if conten.status_code != 200:
        logger.error("项目请求失败,状态码非200")
    else:
        logger.debug("状态码200,开始流程")
        podra_yaml = conten.json()
        zhuan_huan = podra_yaml["yaml"]
        ksdf = zhuan_huan.split('---')
        if not os.path.exists(name_proect):
            os.makedirs(name_proect)
            logger.debug("创建路径--{}".format(name_proect))
            for i in ksdf:
                if "kind: Deployment" in i:
                    xiangmu_path = name_proect + "/deployment.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建deployment.yaml文件")
                if "kind: Service" in i:
                    xiangmu_path = name_proect + "/service.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建service.yaml文件")
                if "kind: ConfigMap" in i:
                    xiangmu_path = name_proect + "/configmap.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建configmap.yaml文件")
            git_push(xianmug_name)

def git_push(xiangmu_name):
    cmd_copy = "cp -r /home/dtk-kubernetes-test/app/{}  /home/dtk-kubernetes-test/z-huise/app; cd /home/dtk-kubernetes-test/;git pull ;git add . ;git commit -m 'xinzeng';git push  ".format(xiangmu_name)
    subprocess.run(f"{
      
      cmd_copy}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode("utf-8")
    logger.debug("执行命令{},推送到gitlab仓库".format(cmd_copy))

if __name__ == '__main__':
    xianmug_name = sys.argv[1]
    yaml_create("/home/dtk-kubernetes-test/app/{}".format(xianmug_name),xianmug_name)

在这里插入图片描述

在这里插入图片描述
从gitlab仓库拉下来的:
在这里插入图片描述
问:为啥要同步到gitlab仓库呢?直接在zadig上管理不行吗?
答:个人业务问题,需要使用gitlab修改configmap文件,所以有了这个功能。

猜你喜欢

转载自blog.csdn.net/weixin_43606975/article/details/127690183