Last active
August 29, 2015 14:24
-
-
Save bungoume/18c59be0098f702ad411 to your computer and use it in GitHub Desktop.
AWS ELB付け直しのスクリプト (MIT LIcense)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"elasticloadbalancing:DescribeInstanceHealth", | |
"elasticloadbalancing:DescribeLoadBalancerAttributes", | |
"elasticloadbalancing:DescribeLoadBalancers" | |
], | |
"Resource": [ | |
"*" | |
] | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer", | |
"elasticloadbalancing:RegisterInstancesWithLoadBalancer" | |
], | |
"Resource": [ | |
"arn:aws:elasticloadbalancing:<region>:<account_id>:loadbalancer/*" | |
] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@reboot python /path/to/elb-reregistration.py >> /var/log/elb-reregistration.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import logging | |
import sys | |
import time | |
import boto.ec2.elb | |
import requests | |
verbose_format = '%(asctime)s %(name)s %(levelname)s %(message)s' | |
logging.basicConfig(format=verbose_format, level=logging.WARNING, stream=sys.stdout) | |
logging.getLogger("boto").propagate = False | |
logger = logging.getLogger("elb-reregistration") | |
logger.setLevel(logging.INFO) | |
def retry(num, func, *args, **kwargs): | |
for i in range(1, num + 1): | |
try: | |
return func(*args, **kwargs) | |
except Exception as e: | |
logger.info("Function failed:%s(%s) tries:%s, %s", func, args, i, e) | |
if(i >= num): | |
raise | |
time.sleep(2**i) | |
continue | |
def check(load_balancers, my_instance_id): | |
outofservice_load_balancers = [] | |
for load_balancer in load_balancers: | |
instances = load_balancer.instances | |
if my_instance_id not in map(lambda i: i.id, instances): | |
continue | |
# instance_health = load_balancer.get_instance_health([my_instance_id])[0] | |
instance_health = retry(5, load_balancer.get_instance_health, [my_instance_id])[0] | |
if instance_health.state == 'InService': | |
continue | |
outofservice_load_balancers.append(load_balancer) | |
return outofservice_load_balancers | |
def reregistration(load_balancers, my_instance_id): | |
for load_balancer in load_balancers: | |
logger.info("Reregistration ELB:%s instance:%s", load_balancer.name, my_instance_id) | |
# load_balancer.deregister_instances([my_instance_id]) | |
retry(5, load_balancer.deregister_instances, [my_instance_id]) | |
time.sleep(1) | |
# load_balancer.register_instances([my_instance_id]) | |
retry(5, load_balancer.register_instances, [my_instance_id]) | |
time.sleep(1) | |
def main(): | |
try: | |
conn = boto.ec2.elb.connect_to_region('ap-northeast-1') | |
all_load_balancers = conn.get_all_load_balancers() | |
my_instance_id = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text | |
for retries in range(5): | |
time.sleep(60*retries*retries) | |
outofservice_load_balancers = check(all_load_balancers, my_instance_id) | |
if not outofservice_load_balancers: | |
break | |
reregistration(outofservice_load_balancers, my_instance_id) | |
for load_balancer in check(all_load_balancers, my_instance_id): | |
logger.warn("NotRegistrated ELB:%s instance:%s", load_balancer.name, my_instance_id) | |
except Exception as e: | |
logger.error("Exception occurred, %s", e, exc_info=True) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment