17
17
DestinationCidrBlock = '172.18.0.253/32' #修改为VIP
18
18
thisInstanceId = '1cf963ff-7847-4859-8462-5405f0facc1d' #当前主机的Id
19
19
thatInstanceId = 'b141da5f-8e3e-44c0-ac0f-a0feccba78c7' #迁移前所在主机Id
20
- interface = { "eth0" : " 172.18.0.13"} #当前机器主网卡和主IP
20
+ thisInstanceIp = " 172.18.0.13" #当前机器IP
21
21
##################需修改部分End######################
22
22
23
23
log = open ('/var/log/keepalived.log' , 'a+' )
@@ -29,57 +29,26 @@ def get_now_time():
29
29
def log_write (message = '' ):
30
30
log .write (get_now_time () + " " + str (message ) + "\n " )
31
31
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 ()
35
32
36
33
def findRoute ():
37
34
for route in vpcClient .describe_routes ()['RouteSet' ]:
38
35
if route ['DestinationCidrBlock' ] == DestinationCidrBlock :
39
- print 'current route found'
36
+ log_write ( 'an existing route found')
40
37
return route ['RouteId' ]
41
-
42
- print 'route not found'
38
+ log_write ('route not found' )
43
39
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." )
55
45
time .sleep (0.5 )
56
46
r = findRoute ()
57
47
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' )
83
52
84
53
def print_help ():
85
54
log_write (
0 commit comments