Passed
Pull Request — master (#681)
by Aldo
04:55
created

000_quinq_evcs.redeploy_affected_evcs()   B

Complexity

Conditions 5

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 36
nop 0
dl 0
loc 49
rs 8.5493
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
import sys
5
import os
6
import httpx
7
8
from kytos.core.db import Mongo
9
10
11
def number_evcs_affected():
12
    mongo = Mongo()
13
    evcs_collection = mongo.client[mongo.db_name]["evcs"]
14
    n_documents = evcs_collection.count_documents({
15
        "$and": [
16
            {"$expr": {
17
                "$ne": ["$uni_a.tag.value", "$uni_z.tag.value"]
18
            }},
19
            {"$or":[
20
                {"$and":[
21
                    {"uni_a.tag.value": {"$type": "number"}},
22
                    {"uni_z.tag.value": {"$type": "number"}}
23
                ]},
24
                {"$and":[
25
                    {"uni_a.tag.value": {"$type": "number"}},
26
                    {"uni_z.tag.value": {"$eq": "untagged"}}
27
                ]},
28
                {"$and":[
29
                    {"uni_a.tag.value": {"$eq": "untagged"}},
30
                    {"uni_z.tag.value": {"$type": "number"}}
31
                ]}]
32
            },
33
            {"archived": {"$eq": False}}
34
        ]
35
    })
36
    print(f"There are {n_documents} that need to be redeploy")
37
    return
38
39
def redeploy_affected_evcs():
40
    mongo = Mongo()
41
    evcs_collection = mongo.client[mongo.db_name]["evcs"]
42
    documents = evcs_collection.find({
43
        "$and": [
44
            {"$expr": {
45
                "$ne": ["$uni_a.tag.value", "$uni_z.tag.value"]
46
            }},
47
            {"$or":[
48
                {"$and":[
49
                    {"uni_a.tag.value": {"$type": "number"}},
50
                    {"uni_z.tag.value": {"$type": "number"}}
51
                ]},
52
                {"$and":[
53
                    {"uni_a.tag.value": {"$type": "number"}},
54
                    {"uni_z.tag.value": {"$eq": "untagged"}}
55
                ]},
56
                {"$and":[
57
                    {"uni_a.tag.value": {"$eq": "untagged"}},
58
                    {"uni_z.tag.value": {"$type": "number"}}
59
                ]}]
60
            },
61
            {"archived": {"$eq": False}}
62
        ]
63
    })
64
65
    evc_count = 0
66
    evc_ids: list[str] = []
67
    for evc in documents:
68
        evc_count += 1
69
        evc_ids.append(evc["id"])
70
    print(f"{evc_count} EVCs to be redeploy.")
71
    print("Redeploying...")
72
73
    evc_deleted_count = 0
74
    evc_not_deleted: list[str] = []
75
    MEF_URL = "http://localhost:8181/api/kytos/mef_eline/v2/evc/"
76
    for evc in evc_ids:
77
        response = httpx.patch(MEF_URL+evc+"/redeploy", timeout=60)
78
        if response.status_code//100 == 2:
79
            evc_deleted_count += 1
80
81
    if evc_count != evc_deleted_count:
82
        print("Not redeployed EVCs: ", evc_not_deleted)
83
84
    else:
85
        print("All EVCs were redeployed successfully.")
86
87
    return 0
88
89
if __name__ == "__main__":
90
    cmds = {
91
        "number_evcs_affected": number_evcs_affected,
92
        "redeploy_affected_evcs": redeploy_affected_evcs,
93
    }
94
    try:
95
        cmd = os.environ["CMD"]
96
        command = cmds[cmd]
97
    except KeyError:
98
        print(
99
            f"Please set the 'CMD' env var. \nIt has to be one of these: {list(cmds.keys())}"
100
        )
101
        sys.exit(1)
102
        
103
    command()
104
    
105