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