-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver_lifecycle.py
89 lines (61 loc) · 2.22 KB
/
server_lifecycle.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
from pymongo import MongoClient
import json
from kafka import KafkaProducer
import time
from flask import Flask, request, jsonify
client = MongoClient(
"mongodb+srv://mongo2mongo:[email protected]/myFirstDatabase?retryWrites=true&w=majority")
db = client["initialiser_db"]
collection = db["services"]
services = []
for x in collection.find():
services.append(x["service"])
# services = set(services)
print(services)
# print("i am here")
# get dynamic ip
# ip1 = "52.140.61.100"
ip1 = "104.211.205.232"
ip2 = "52.172.3.77"
ip3 = "104.211.227.22"
def send_message(topic_name, message):
producer = KafkaProducer(bootstrap_servers=[
'52.172.25.250:9092'], value_serializer=lambda v: json.dumps(v).encode('utf-8'))
producer.send(topic_name, message)
no_of_services = len(services)
for i in range(0, len(services)):
data1 = {
"command": "START",
"service": services[i]
}
if i % 3==0:
send_message("service_{}".format(ip1), data1)
print("service_{}".format(ip1), services[i])
elif i%3==1:
send_message("service_{}".format(ip2), data1)
print("service_{}".format(ip2), services[i])
else:
send_message("service_{}".format(ip3), data1)
print("service_{}".format(ip3), services[i])
time.sleep(2)
app = Flask(__name__)
collection1 = db["running_services"]
@app.route('/initialiser/getService/<serviceName>', methods=['GET'])
def getServiceAddress(serviceName):
# print("hello")
doc = collection1.find_one({"service": serviceName})
ip = doc["ip"]
port = doc["port"]
return jsonify({"ip": ip, "port": port})
@app.route('/initialiser/getDeploymentNodes', methods=['GET'])
def getDeploymentNodes():
cursor = collection1.find({"type": "node-agent"})
node_agent_addresses = []
for doc in cursor:
node_agent_address = {}
node_agent_address["ip"] = doc['ip']
node_agent_address["port"] = doc['port']
node_agent_addresses.append(node_agent_address)
return jsonify({"ips": node_agent_addresses})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5003)