Skip to content

Instantly share code, notes, and snippets.

@matetsu
Created December 28, 2012 03:56
Show Gist options
  • Save matetsu/4394294 to your computer and use it in GitHub Desktop.
Save matetsu/4394294 to your computer and use it in GitHub Desktop.
VPCのroute tableにVIPのルーティング先に自インスタンスIDを指定する。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import boto.ec2
from boto.ec2.connection import EC2Connection
from boto.vpc import VPCConnection
import logging
LOGFILE="/var/log/redis-ha.log"
# AWS CONFIGURATION
DEFAULT_REGION_NAME = "ap-northeast-1"
AWS_ACCESS_KEY_ID = "[YOUR_AWS_ACCESS_KEY_ID]"
AWS_SECRET_ACCESS_KEY = "[YOUR_AWS_SECRET_ACCESS_KEY]"
# use for vip of redis
REDIS_VIP = '172.16.0.10/32'
# route table id
ROUTE_TABLE_ID = '[ROUTE_TABLE_ID]'
def main():
# logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh = logging.FileHandler(filename=LOGFILE)
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("Start to change vpc route...")
# main
region_info = boto.ec2.get_region(region_name=DEFAULT_REGION_NAME, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
vpc_conn = VPCConnection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region=region_info)
route_table = vpc_conn.get_all_route_tables(route_table_ids=[ROUTE_TABLE_ID])
metadata = boto.utils.get_instance_metadata()
instance_id = metadata["instance-id"]
# change source/dest check to disabled
ec2_conn = EC2Connection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region=region_info)
attr = ec2_conn.get_instance_attribute(instance_id, "sourceDestCheck")
if attr['sourceDestCheck'] == True:
# Disable Source / Destination Cehck
set_sourceDestCheck_result = ec2_conn.modify_instance_attribute(instance_id, "sourceDestCheck", False)
if set_sourceDestCheck_result:
logger.info("Succeeded to modify sourceDestCheck to disabled (%s)." % instance_id)
else:
logger.critical("Failed to modify sourceDestCheck to disabled (%s)." % instance_id)
# change route of REDIS_VIP to own instance_id
has_route = False
for rt in route_table[0].routes:
if rt.destination_cidr_block == REDIS_VIP:
has_route = True
if rt.instance_id != instance_id:
delete_result = vpc_conn.delete_route(ROUTE_TABLE_ID, REDIS_VIP)
if delete_result:
logger.info("Succeeded to delete route: %s from %s." % (REDIS_VIP, ROUTE_TABLE_ID))
create_result = vpc_conn.create_route(ROUTE_TABLE_ID, REDIS_VIP, instance_id=instance_id)
if create_result:
logger.info("Succeeded to create route: [%s -> %s] to %s." % (REDIS_VIP, instance_id, ROUTE_TABLE_ID))
else:
logger.info("Failed to create route: %s to %s." % (REDIS_VIP, ROUTE_TABLE_ID))
else:
logger.critcal("Failed to delete route: %s from %s." % (REDIS_VIP, ROUTE_TABLE_ID))
if not has_route:
create_result = vpc_conn.create_route(ROUTE_TABLE_ID, REDIS_VIP, instance_id=instance_id)
if create_result:
logger.info("Succeeded to create route(no delete): %s to %s." % (REDIS_VIP, ROUTE_TABLE_ID))
else:
logger.info("Failed to create route(no delete): %s to %s." % (REDIS_VIP, ROUTE_TABLE_ID))
logger.info("Finished to change vpc route.")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment