-
Notifications
You must be signed in to change notification settings - Fork 0
/
ha-nat-eip
executable file
·105 lines (82 loc) · 3.79 KB
/
ha-nat-eip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
# coding: UTF-8
import boto
import boto.ec2
import boto.vpc
from boto.utils import get_instance_metadata
import argparse
import os
import sys
import time
def main():
try:
ec2_conn = boto.ec2.connect_to_region(args.region,aws_access_key_id=args.aws_access_key_id,aws_secret_access_key=args.aws_secret_access_key,profile_name=args.profile)
vpc_conn = boto.vpc.connect_to_region(args.region,aws_access_key_id=args.aws_access_key_id,aws_secret_access_key=args.aws_secret_access_key,profile_name=args.profile)
except:
print "UNKNOWN: Unable to connect to reqion %s" %(args.region)
sys.exit(1)
change_privateip(vpc_conn)
change_eip(ec2_conn)
sys.exit(0)
def change_privateip(vpc_conn):
try:
rt = vpc_conn.get_all_route_tables(filters={'tag:Name':args.routetable})[0]
except:
print "UNKNOWN: Was not found %s" %(args.routetable)
sys.exit(1)
has_default_route = False
for route in rt.routes:
if route.destination_cidr_block == args.network:
has_default_route = True
if has_default_route:
vpc_conn.replace_route(
route_table_id=rt.id,
destination_cidr_block=args.network,
instance_id=get_instance_metadata()['instance-id'])
else:
vpc_conn.create_route(
route_table_id=rt.id,
destination_cidr_block=args.network,
instance_id=get_instance_metadata()['instance-id'])
try:
rt = vpc_conn.get_all_route_tables(filters={'tag:Name':args.routetable})[0]
except:
print "UNKNOWN: Was not found %s" %(args.routetable)
sys.exit(1)
has_default_route = False
for route in rt.routes:
if route.destination_cidr_block == args.network:
has_default_route = True
prt=route
if has_default_route:
print "%s cidr:%s gateway:%s" %(time.strftime("%c"),prt.destination_cidr_block,prt.instance_id)
else:
print "ERROR: Route is not registered %s" %(args.network)
sys.exit(1)
def change_eip(ec2_conn):
eip=ec2_conn.get_all_addresses(allocation_ids=args.allocation_id)[0]
try:
ec2_conn.disassociate_address(eip.public_ip)
except:
print "ERROR: Allocation-id is not registered %s" %(args.allocation_id)
sys.exit(1)
try:
ec2_conn.associate_address(instance_id=get_instance_metadata()['instance-id'],
allocation_id=args.allocation_id)
except:
print "ERROR: Registration error of ElasticIP %s" %(args.allocation_id)
sys.exit(1)
print "%s InstanceID:%s EIP:%s" %(time.strftime("%c"),eip.instance_id,eip.public_ip)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Instance Start Stop')
parser.add_argument('-a', '--aws-access-key-id', required=False, dest='aws_access_key_id', help='AWS Access Key')
parser.add_argument('-s', '--aws-secret-access-key', required=False, dest='aws_secret_access_key', help='AWS Secret Access Key')
parser.add_argument('--profile', required=False, dest='profile', help=' Profile name of AWS shared credential file entry.')
parser.add_argument('-r', '--region', required=True, dest='region', help='AWS Region')
parser.add_argument('-n', '--name', required=False, dest='tagname', help='Name_Tag')
parser.add_argument('-f', '--filename', required=False, dest='filename', help='List File')
parser.add_argument('-R', '--route-table', required=True, dest='routetable', help='AWS Route Table Name')
parser.add_argument('-N', '--network', required=True, dest='network', help='AWS Network Address')
parser.add_argument('-E', '--allocation-id', required=True, dest='allocation_id', help='AWS EIP Allocation ID')
args = parser.parse_args()
main()