-
Notifications
You must be signed in to change notification settings - Fork 0
/
.automation.py
112 lines (84 loc) · 2.9 KB
/
.automation.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
import requests
import re
import time
import subprocess
checker = "domain1.com"
backend1_address = "127.0.0.1:81"
backend2_address = "127.0.0.1:82"
backend3_address = "127.0.0.1:83"
def requster(url):
try:
response = requests.get(url)
match = re.search(r"<div class=\"fullscreen-text\">\s*(.*?)\s*</div>", response.text)
return match.group(1)
except:
print("\n=========> server changed the IP addresses")
def update_haproxy_config(backend1_add, backend2_add, backend3_add):
config = f'''frontend http_front
bind *:80
mode http
acl domain1 hdr(host) -i domain1.com
acl domain2 hdr(host) -i domain2.com
acl domain3 hdr(host) -i domain3.com
use_backend backend1 if domain1
use_backend backend2 if domain2
use_backend backend3 if domain3
default_backend default_backend
backend backend1
mode http
server server1 {backend1_add}
backend backend2
mode http
server server2 {backend2_add}
backend backend3
mode http
server server3 {backend3_add}
backend default_backend
mode http
server default_server 127.0.0.1:81
'''
with open('haproxy.cfg', 'w') as file:
file.write(config)
def changeIP():
#1
if checker == "domain1.com":
backend1_address = "127.0.0.1:81"
elif checker == "domain2.com":#backend2 deer bichegdene
backend2_address = "127.0.0.1:81"
elif checker == "domain3.com":
backend3_address = "127.0.0.1:81"
#2
req2 = requster("http://127.0.0.1:82")
if req2 == "domain1.com":
backend1_address = "127.0.0.1:82"
elif req2 == "domain2.com":
backend2_address = "127.0.0.1:82"
elif req2 == "domain3.com":
backend3_address = "127.0.0.1:82"
#3
req3 = requster("http://127.0.0.1:83")
if req3 == "domain1.com":
backend1_address = "127.0.0.1:83"
elif req3 == "domain2.com":
backend2_address = "127.0.0.1:83"
elif req3 == "domain3.com":
backend3_address = "127.0.0.1:83"
update_haproxy_config(backend1_address, backend2_address, backend3_address)
print("\nserver1 ==> ", backend1_address,"\nserver2 ==> ", backend2_address, "\nserver3 ==> ",backend3_address,"\n")
subprocess.Popen(["haproxy", "-f", "haproxy.cfg"])
while True:
value = requster("http://127.0.0.1:81")
if value != checker:
checker = value
try :
changeIP()
subprocess.run(["pkill", "-f", "haproxy"])
command = ["haproxy", "-f", "haproxy.cfg"]
subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("===================================================================")
print("= ===> changing servers IP and restart proxy !!! <=== =")
print("===================================================================")
except:
time.sleep(3)
# time.sleep(1)
#########