Skip to content
代码片段 群组 项目
tasks_sync_scripts_able.py 16.1 KB
Newer Older
spiritysdx's avatar
spiritysdx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
# -*- coding:utf-8 -*-
# 作者仓库:https://jihulab.com/spiritlhl/qinglong_auto_tools.git
# 觉得不错麻烦点个star谢谢
# 频道:https://t.me/qinglong_auto_tools
# 根据主青龙的启用任务和对应脚本同步到副青龙中去,不存在于副青龙中的任务和对应脚本会自动新增,如果启用的任务对应的脚本内容有更新,也会同步更新

# 只同步更新主青龙中已经启用的任务和对应脚本

'''
cron: 1
new Env('任务 二叉树同步脚本文件');
'''

# 在脚本管理里修改这个文件的配置,然后保存,然后禁用 二叉树同步脚本文件 这个任务,有需要再点运行
# 主青龙,任务获取更新的容器,事先需要在容器里创建应用,给所有权限,然后重启容器,应用设置才会生效,
# 副青龙,被同步的任务容器,事先需要在容器里创建应用,给所有权限,然后重启容器,应用设置才会生效,
# 按照格式有几个写几个,没有的空的删除
'''
# ec_config.txt中填写如下设置

# 二叉树同步脚本文件
### 主青龙
tasks_sync_scripts_able_cilent_id1="xxxxxxxx"
tasks_sync_scripts_able_cilent_secret1="xxxxxxxxxx"
tasks_sync_scripts_able_url1="http://xxxxxxxx:xxxx/"

### 副青龙
tasks_sync_scripts_able_client_ids=["",""]
tasks_sync_scripts_able_client_secrets=["",""]
tasks_sync_scripts_able_urllist=["http://xxxxxxxxx:xxxx/",""]

'''
# client_id1=""
# client_secret1=""
# url1 = ""
# client_ids=['','','','']
# client_secrets=['','','','']
# urllist = ["http://xxxx:xxxx/","","",'']

import re
import time
import json

try:
    import requests
except Exception as e:
    print(e, "\n缺少requests 模块,请执行命令安装:python3 -m pip install requests")
    exit(3)

try:
    with open("ec_config.txt", "r", encoding="utf-8") as fp:
        t = fp.readlines()
    try:
        for i in t:
            try:
                temp = re.findall(r"tasks_sync_scripts_able_cilent_id1=\"(.*?)\"", i)[0]
                client_id1 = temp
                if client_id1 == "":
                    print("tasks_sync_scripts_able_cilent_id1 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_cilent_id1 未创建")
        exit(3)

    try:
        for i in t:
            try:
                temp = re.findall(r"tasks_sync_scripts_able_cilent_secret1=\"(.*?)\"", i)[0]
                client_secret1 = temp
                if client_secret1 == "":
                    print("tasks_sync_scripts_able_cilent_secret1 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_cilent_secret1 未创建")
        exit(3)

    try:
        for i in t:
            try:
                temp = re.findall(r"tasks_sync_scripts_able_url1=\"(.*?)\"", i)[0]
                url1 = temp
                if url1 == "":
                    print("tasks_sync_scripts_able_url1 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_url1 未创建")
        exit(3)
except:
    print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")

try:
    try:
        for i in t:
            try:
                temp = "[" + re.findall(r"tasks_sync_scripts_able_client_ids=\[(.*?)\]", i)[0] + "]"
                try:
                    client_ids = json.loads(temp)
                except:
                    print("tasks_sync_scripts_able_client_ids 填写有误")
                if client_ids == []:
                    print("tasks_sync_scripts_able_client_ids 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_client_ids 未创建")
        exit(3)

    try:
        for i in t:
            try:
                temp = "[" + re.findall(r"tasks_sync_scripts_able_client_secrets=\[(.*?)\]", i)[0] + "]"
                try:
                    client_secrets = json.loads(temp)
                except:
                    print("tasks_sync_scripts_able_client_secrets 填写有误")
                if client_secrets == []:
                    print("tasks_sync_scripts_able_client_secrets 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_client_secrets 未创建")
        exit(3)

    try:
        for i in t:
            try:
                temp = "[" + re.findall(r"tasks_sync_scripts_able_urllist=\[(.*?)\]", i)[0] + "]"
                try:
                    urllist = json.loads(temp)
                except:
                    print("tasks_sync_scripts_able_urllist 填写有误")
                if urllist == []:
                    print("tasks_sync_scripts_able_urllist 未填写")
            except:
                pass
    except:
        print("tasks_sync_scripts_able_urllist 未创建")
        exit(3)
except:
    print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")

requests.packages.urllib3.disable_warnings()


def gettimestamp():
    return str(int(time.time() * 1500))


def gettoken(self, url_token):
    r = requests.get(url_token).text
    res = json.loads(r)["data"]["token"]
    self.headers.update({"Authorization": "Bearer " + res})


def login(self, baseurl, client_id_temp, client_secret_temp):
    url_token = baseurl + 'open/auth/token?client_id=' + client_id_temp + '&client_secret=' + client_secret_temp
    gettoken(self, url_token)


def getitem(self, baseurl, typ):
    url = baseurl + typ + "/scripts/files?t={}".format(gettimestamp())
    r = self.get(url)
    item = json.loads(r.text)["data"]
    return item


def getscript(self, baseurl, typ, filename, path):
    url = baseurl + typ + "/scripts/" + filename + "?t=%s" % gettimestamp()
    r = self.get(url)
    response = json.loads(r.text)["code"]
    if response == 500:
        url = baseurl + typ + "/scripts/" + filename + "?path=" + path
        r = self.get(url)
    script = json.loads(r.text)["data"]
    return script


def pushscript(self, baseurl, typ, data, path):
    url = baseurl + typ + "/scripts?t=%s" % gettimestamp()
    self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
    r = self.put(url, data=json.dumps(data))
    response = json.loads(r.text)["code"]
    if response == 500:
        data["path"] = path
        r = self.put(url, data=json.dumps(data))
    return r.text


def getcrons(self, baseurl, typ):
    url = baseurl + typ + "/crons?t=%s" % gettimestamp()
    r = self.get(url)
    item = json.loads(r.text)["data"]
    return item


def addcron(self, baseurl, typ, data):
    url = baseurl + typ + "/crons?t=%s" % gettimestamp()
    self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
    r = self.post(url, data=json.dumps(data))
    if json.loads(r.text)["code"] == 200:
        return True
    else:
        return r.text


if __name__ == '__main__':
    # 主容器
    s = requests.session()
    login(s, url1, client_id1, client_secret1)

    # 获取主青龙任务
    print("=========== 主青龙 信息获取中 =============")
    print()
    ztasks = getcrons(s, url1, "open")
    zenable_list = []
    zdisable_list = []
    for i in ztasks:
        if i['isDisabled'] == 0:
            zenable_list.append(i)
        else:
            continue
    # enable_tlid = []
    zenable_tname = []
    zenable_tcommand = []
    zenable_tschedule = []
    for j in zenable_list:
        # enable_tlid.append(j['_id'])
        zenable_tname.append(j['name'])
        zenable_tcommand.append(j['command'])
        zenable_tschedule.append(j['schedule'])
    print("主青龙任务数量:{},启用任务{}".format(len(ztasks), len(zenable_tname)))
    print()

    # 获取主青龙的脚本名
    ## 根目录
    zscripts = getitem(s, url1, "open")
    zscripts_list = []
    for i in zscripts:
        zscripts_list.append(i["key"])

    # script根目录默认存在的文件夹,放入其中的文件夹不再检索
    or_list = ['node_modules', '__pycache__', 'utils', '.pnpm-store', 'function', 'tools', 'backUp', '.git', '.idea']
    zpath_list = []
    zpath_scripts_list = []
    for i in zscripts:
        try:
            i["children"]
            if i["title"] not in or_list:
                zpath_list.append(i["title"])
                zpath_scripts_list.append(i["children"])
        except:
            pass

    print("主青龙根目录脚本文件数量:{},".format(len(zscripts_list)))
    print("主青龙任务拉取的仓库对应目录文件夹数量:{},".format(len(zpath_list)))
    print()
    print()

    # 创建对应文件夹字典
    dict_z = {}
    for i in zpath_list:
        dict_z[i] = []

    # 主青龙启用的任务对应的脚本
    zscripts_enable = []
    for i in zenable_tcommand:
        if i[-2:] == "js" or i[-2:] == "py":
            ti = i.replace("task ", "").split("/")
            if len(ti) == 1:
                zscripts_enable.append(ti[0])
            elif len(ti) == 2:
                dict_z[ti[0]].append(ti[1])

    t = 0
    for k in urllist:
        # 分容器
        print("=========== 副青龙{} 信息获取中 =============".format(t + 1))
        print()
        a = requests.session()
        login(a, urllist[t], client_ids[t], client_secrets[t])

        tasks = getcrons(a, urllist[t], "open")

        # 增加新任务
        enable_list = []
        disable_list = []
        for i in tasks:
            if i['isDisabled'] == 0:
                enable_list.append(i)
            else:
                continue
        # enable_tlid = []
        enable_tname = []
        enable_tcommand = []
        enable_tschedule = []
        for j in enable_list:
            # enable_tlid.append(j['_id'])
            enable_tname.append(j['name'])
            enable_tcommand.append(j['command'])
            enable_tschedule.append(j['schedule'])
        print("副青龙任务数量:{}".format(len(tasks)))
        print()

        print("同步任务中")
        print()

        count = 0
        ct = 0

        for i in zenable_tname:
            if zenable_tname[count] not in enable_tname and zenable_tcommand[count] not in enable_tcommand:
                data_cron = {
                    "command": zenable_tcommand[count],
                    "schedule": zenable_tschedule[count],
                    "name": zenable_tname[count]
                }
                addcron(a, urllist[t], "open", data_cron)
            else:
                ct += 1
            count += 1
        xc = count - ct
        print("新增启用任务数量:{}".format(xc))
        print()

        # 获取副青龙根目录脚本名
        scripts = getitem(a, urllist[t], "open")
        scripts_list = []
        for i in scripts:
            scripts_list.append(i["key"])

        # 获取副青龙仓库目录脚本名字典
        dict_cs_name = {}
        for i in zpath_list:
            dict_cs_name[i] = []
            for j in scripts:
                if j["key"] == i:
                    for k in j["children"]:
                        dict_cs_name[i].append(k["value"])

        # 筛选需要添加或更改的脚本名
        ## 根目录
        add_list = []
        change_list = []
        for j in zscripts_enable:
            if j not in scripts_list:
                add_list.append(j)
            else:
                change_list.append(j)

        ## 仓库目录
        dict_add_f = {}
        dict_change_f = {}
        for j in dict_z:
            dict_add_f[j] = []
            dict_change_f[j] = []
            for k in dict_z[j]:  # 在主青龙
                if k not in dict_cs_name[j]:  # 不在副青龙的文件
                    dict_add_f[j].append(k)
                else:
                    dict_change_f[j].append(k)

        print(dict_add_f)
        print(dict_change_f)

        print("同步脚本文件中")
        print()

        # 查询新增脚本内容
        ## 根目录
        data_script_list = []
        for i in add_list:
            content = getscript(s, url1, "open", i, "")
            data_script = {
                "filename": i,
                "content": content,
            }
            data_script_list.append(data_script)

        ## 仓库文件夹
        data_script_add_dict = {}
        for i in dict_add_f:
            data_script_add_dict[i] = []
            for j in dict_add_f[i]:  # j是文件名,i 是文件夹名
                content = getscript(s, url1, "open", j, i)
                data_script = {
                    "filename": j,
                    "content": content,
                    "path": i
                }
                data_script_add_dict[i].append(data_script)

        # 写入新增内容
        ## 根目录更新
        for i in data_script_list:
            pushscript(a, urllist[t], "open", i, "")

        ## 仓库文件夹更新
        cotu = 0
        for i in data_script_add_dict:
            for j in data_script_add_dict[i]:
                rc = pushscript(a, urllist[t], "open", j, j["path"])
                print(rc)
                cotu += 1
        print("新增启用任务对应脚本文件数量:{}".format(xc))
        print("新增启用任务对应仓库文件脚本数量:{}".format(cotu))
        print()

        print("同步脚本文件中")
        print()

        # 查询需要更改的脚本内容
        ## 根目录
        change_script_list = []
        for i in change_list:
            content = getscript(s, url1, "open", i, "")
            data_script = {
                "filename": i,
                "content": content,
            }
            change_script_list.append(data_script)

        origin_script_list = []
        for i in change_list:
            content = getscript(a, urllist[t], "open", i, "")
            data_script = {
                "filename": i,
                "content": content,
            }
            origin_script_list.append(data_script)

        ## 仓库文件字典
        change_script_dict = {}
        for i in dict_change_f:
            change_script_dict[i] = []
            for j in dict_change_f[i]:  # j是文件名,i 是文件夹名
                content = getscript(s, url1, "open", j, i)
                data_script = {
                    "filename": j,
                    "content": content,
                    "path": i
                }
                change_script_dict[i].append(data_script)

        origin_script_dict = {}
        for i in dict_change_f:
            origin_script_dict[i] = []
            for j in dict_change_f[i]:  # j是文件名,i 是文件夹名
                content = getscript(a, urllist[t], "open", j, i)
                data_script = {
                    "filename": j,
                    "content": content,
                    "path": i
                }
                origin_script_dict[i].append(data_script)

        # 比较后更新
        ## 根目录
        count = 0
        ct = 0
        while True:
            if count <= (len(change_script_list) - 1):
                if change_script_list[count] != origin_script_list[count]:
                    pushscript(a, urllist[t], "open", change_script_list[count], "")
                    print("更新脚本文件  {}".format(change_script_list[count]["filename"]))
                else:
                    ct += 1
                count += 1
            else:
                break
        ## 仓库文件
        for i, k in zip(change_script_dict, origin_script_dict):
            for j, l in zip(change_script_dict[i], origin_script_dict[k]):
                if j != l:
                    pushscript(a, urllist[t], "open", j, j["path"])
                    print("更新脚本文件  {}".format(j["filename"]))

        print()

        print("同步脚本文件完毕")
        t += 1

        print('========= 副青龙{} 同步信息完毕 ============='.format(t))
        print()