-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNa.py
149 lines (117 loc) · 5.08 KB
/
Na.py
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
"""
At a newly founded startup, one of the developers Amir has introduced Kafka and because of
his adventure every developer has to start using it in their microservice.
Kafka, though it solves hundreds of problems for developers but it brings ten more. One of the
tedious problems is maintenance of topics.
To solve this problem, Amir introduced a csv file containing list of topics in below format:
TOPICS,PARTITIONS,REPLICATION_FACTOR,RETENTION_TIME_MS,MESSAGE_BYTES
dev-eod-enach-for-dpd-accounts,2,3,10800000,5242880
qa-eod-enach-for-dpd-accounts,2,3,10800000,5242880
dev-campaigns-association-updated_two,2,3,10800000,5242880
credit_underwriting.public.batch_text_messages,12,3,10800000,5242880
All that's left is writing the script. Given you are a star scripter, write a script to help Amir.
The script should maintain the topics in kafka based on the list. What that means is
1) If a topic is added to the list, it should be added in Kafka as well.
2) If a topic is deleted from the list, it should be deleted from Kafka as well.
3) If any of the factors are modified like retention_ms, it should be reflected in Kafka.
Note:
1) You can assume that script is run once after every file change from some kind of ci/cd
system.
2) You have to install kafka locally for testing.
"""
# VCS any change in this should call the commands
# Start time 11:30
# if a topic added, and it has all the values like partition count etc --> create it
# GET THE GIT diff
#
from calendar import c
import pwd
import subprocess
import os
from webbrowser import get
import git
# start kafka bby running this
# /opt/homebrew/opt/kafka/bin/kafka-server-start /opt/homebrew/etc/kafka/server.properties
# go to the path
def getModifications():
os.chdir('/Users/amartyajha/Learning/Python/devops-learning')
# get the last two revision for the same file
last2commits=subprocess.run('git rev-list HEAD -n 2 topics.csv',capture_output=True, shell=True, text=True, check=True)
data=last2commits.stdout.split('\n')
diff_commits=subprocess.run(['git', 'diff', data[1], data[0]],capture_output=True,text=True)
diff=(diff_commits.stdout.split('\n'))
modifiedArray=[]
for i in diff:
if((i.startswith('+') and not i.startswith('+++')) or (i.startswith('-') and not i.startswith('---'))):
modifiedArray.append(i)
print(modifiedArray)
applyModifications(modifiedArray)
def applyModifications(modifiedArray):
topicName={}
for i in modifiedArray:
if(i[1:].split(',')[0] in topicName):
topicName[i[1:].split(',')[0]]+=1
else:
topicName[i[1:].split(',')[0]]=1
print(topicName)
for i in topicName:
if(topicName[i]>1):
getTopicValues(i,modifiedArray)
#subprocess.run(['kafka-topics','--bootstrap-server','localhost:9092','--alter','--topic',i,'--partitions',topicValues[1], '--replication-factor', topicValues[2]],capture_output=True, text=True, check=True)
else:
for j in modifiedArray:
if(j.startswith("+"+i)):
topicValues=j.split(',')
print(topicValues)
#subprocess.run(['kafka-topics','--bootstrap-server','localhost:9092','--create','--topic',i,'--partitions',topicValues[1], '--replication-factor', topicValues[2]],capture_output=True, text=True, check=True)
elif(j.startswith("-"+i)):
topicValues=j.split(',')
print(topicValues)
#subprocess.run(['kafka-topics','--bootstrap-server','localhost:9092','--delete','--topic',i,'--partitions',topicValues[1], '--replication-factor', topicValues[2]],capture_output=True, text=True, check=True)
def getTopicValues(i,modifiedArray):
for j in modifiedArray:
if(j.startswith("+"+i)):
topicValues=j.split(',')
print(topicValues)
getModifications()
"""
{
'dev-campaigns-association-updated_two': [4,3,10800000,5242880, 5,3,10800000,5242880],
'credit_underwriting.public.test': [10,3,10800000,52428880]
}
"""
"""
p1=subprocess.run('''
for i in $data
do
echo $i
git diff $i $i | grep "^+" | grep -v "^+++"
done
''',capture_output=True, shell=True, text=True, check=True)
print(p1.stdout)
"""
import os
import git
"""def getModifications():
repo = git.Repo('.')
listofChanges=(repo.git.diff('HEAD~1').split('\n'))
modifiedArray=[]
for i in listofChanges:
if((i.startswith('+') and not i.startswith('+++')) or (i.startswith('-') and not i.startswith('---'))):
modifiedArray.append(i)
#print(modifiedArray)
applyModifications(modifiedArray)
def applyModifications(modifiedArray):
topicName=[]
for i in modifiedArray:
print(i)
if(i.startswith('-')):
topicName.append(i[1:].split(',')[0])
else:
topicName.append(i.split('+')[1].split(',')[0])
#print(i.split('-'))
# check if a new topic is created or is it modified
#topicName.append(i.split('-').split('+')[1].split(',')[0])
#print(topicName)
78bab6fd08a6bdc1859e66f84efa1dec72879226
getModifications()"""