|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +#/etc/keepalived/nexthop.py |
| 4 | + |
| 5 | +import os |
| 6 | +import time |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from kscore.session import get_session |
| 10 | + |
| 11 | +##################需修改部分Begin#################### |
| 12 | +region='cn-beijing-6' #region code |
| 13 | +vpcId = '1858a08a-6cc9-4278-8d0c-d536f441fe8e' #vpcId |
| 14 | +ks_access_key_id = '您的ak' |
| 15 | +ks_secret_access_key = '您的sk' |
| 16 | +vip = "172.18.0.253" #改成您的本机内网 VIP |
| 17 | +DestinationCidrBlock = '172.18.0.253/32' #修改为VIP |
| 18 | +thisInstanceId = '1cf963ff-7847-4859-8462-5405f0facc1d' #当前主机的Id |
| 19 | +thatInstanceId = 'b141da5f-8e3e-44c0-ac0f-a0feccba78c7' #迁移前所在主机Id |
| 20 | +interface = {"eth0":"172.18.0.13"} #当前机器主网卡和主IP |
| 21 | +##################需修改部分End###################### |
| 22 | + |
| 23 | +log = open('/var/log/keepalived.log', 'a+') |
| 24 | +state_file = open('/var/keepalived/state', 'r') |
| 25 | + |
| 26 | +def get_now_time(): |
| 27 | + return time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time())) + '[pid' + str(os.getpid()) + ']' |
| 28 | + |
| 29 | +def log_write(message=''): |
| 30 | + log.write(get_now_time() + " " + str(message) + "\n") |
| 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 | + |
| 36 | +def findRoute(): |
| 37 | + for route in vpcClient.describe_routes()['RouteSet']: |
| 38 | + if route['DestinationCidrBlock'] == DestinationCidrBlock: |
| 39 | + print 'current route found' |
| 40 | + return route['RouteId'] |
| 41 | + |
| 42 | + print 'route not found' |
| 43 | +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 |
| 55 | + time.sleep(0.5) |
| 56 | + r = findRoute() |
| 57 | + 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 |
| 83 | + |
| 84 | +def print_help(): |
| 85 | + log_write( |
| 86 | + ''' |
| 87 | + ./nexthop.py migrate |
| 88 | + migrate your vip |
| 89 | + ''') |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + s = get_session() |
| 93 | + s.set_credentials(ks_access_key_id,ks_secret_access_key) |
| 94 | + vpcClient = s.create_client("vpc", region, use_ssl=True) |
| 95 | + if len(sys.argv) == 1: |
| 96 | + log_write("nexthop.py: parameter num is 0") |
| 97 | + print_help() |
| 98 | + elif sys.argv[1] == 'migrate': |
| 99 | + migrateVip() |
| 100 | + log_write() |
| 101 | + else: |
| 102 | + log_write("nexthop.py: misMatched parameter") |
| 103 | + print_help() |
| 104 | + |
0 commit comments