Skip to content

Commit 7aa81d6

Browse files
committed
精简了部分内容
1 parent a6c5863 commit 7aa81d6

File tree

1 file changed

+12
-43
lines changed

1 file changed

+12
-43
lines changed

examples/nexthop.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
DestinationCidrBlock = '172.18.0.253/32' #修改为VIP
1818
thisInstanceId = '1cf963ff-7847-4859-8462-5405f0facc1d' #当前主机的Id
1919
thatInstanceId = 'b141da5f-8e3e-44c0-ac0f-a0feccba78c7' #迁移前所在主机Id
20-
interface = {"eth0":"172.18.0.13"} #当前机器主网卡和主IP
20+
thisInstanceIp = "172.18.0.13" #当前机器IP
2121
##################需修改部分End######################
2222

2323
log = open('/var/log/keepalived.log', 'a+')
@@ -29,57 +29,26 @@ def get_now_time():
2929
def log_write(message=''):
3030
log.write(get_now_time() + " " + str(message) + "\n")
3131

32-
def get_ip():
33-
f = os.popen('ip addr show dev %s | grep %s | awk \'{print $2}\' | awk -F/ \'{print $1}\'' % (interface.keys()[0] , interface.values()[0]))
34-
return f.read().strip()
3532

3633
def findRoute():
3734
for route in vpcClient.describe_routes()['RouteSet']:
3835
if route['DestinationCidrBlock'] == DestinationCidrBlock:
39-
print 'current route found'
36+
log_write('an existing route found')
4037
return route['RouteId']
41-
42-
print 'route not found'
38+
log_write('route not found')
4339
def migrateVip():
44-
params = {
45-
'vpcId': vpcId,
46-
'privateIpAddress': vip,
47-
'thatInstanceId': thatInstanceId,
48-
'thisInstanceId': thisInstanceId
49-
}
50-
51-
log_write(" try set vip.")
52-
retry_times_when_mgr_ip_got = 4
53-
exceptimes = 0
54-
get_ip_times = 0
40+
param={'VpcId':vpcId,
41+
'DestinationCidrBlock':DestinationCidrBlock,
42+
'RouteType':'Host',
43+
'InstanceId':thisInstanceId}
44+
log_write("migrating vip to another host.")
5545
time.sleep(0.5)
5646
r = findRoute()
5747
if r:
58-
vpcClient.delete_route(**{'RouteId':r})
59-
log_write(" now change the nexthop of vip to this host." + get_ip())
60-
vpcClient.create_route(**{'VpcId':vpcId,'DestinationCidrBlock':DestinationCidrBlock,'RouteType':'Host','InstanceId':thisInstanceId})
61-
while get_ip_times < 5:
62-
log_write(" get_ip=" + get_ip())
63-
if get_ip()==interface.values()[0]:
64-
try:
65-
i = 0
66-
while i < retry_times_when_mgr_ip_got:
67-
state_file.seek(0)
68-
state = state_file.readline()
69-
if state == 'MASTER':
70-
break
71-
i = i + 1
72-
time.sleep(2)
73-
if i >= retry_times_when_mgr_ip_got:
74-
log_write(" set vip failed")
75-
break
76-
except Exception, e:
77-
log_write(' exception:' + str(e))
78-
exceptimes = exceptimes + 1
79-
if exceptimes > 3:
80-
break
81-
time.sleep(0.5)
82-
get_ip_times = get_ip_times + 1
48+
print vpcClient.delete_route(RouteId=r)
49+
log_write(" now change the nexthop of vip to this host." + thisInstanceIp)
50+
if vpcClient.create_route(**param):
51+
log_write('migrating vip success')
8352

8453
def print_help():
8554
log_write(

0 commit comments

Comments
 (0)