mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Acquire & assign elastic IPs the boto3 way
This commit is contained in:
parent
810b4bf120
commit
8cebcfb195
@ -1,16 +1,17 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# create Elastic IPs and assign them to instances if needed
|
""" Create Elastic IPs and assign them to instances if needed.
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import boto.ec2
|
import boto3
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
import time
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"]
|
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
|
||||||
AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"]
|
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
|
||||||
|
AWS_REGION = os.environ['AWS_REGION']
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--tag", help="tag instances in aws")
|
parser.add_argument("--tag", help="tag instances in aws")
|
||||||
@ -20,46 +21,45 @@ if args.tag:
|
|||||||
tag = args.tag
|
tag = args.tag
|
||||||
else:
|
else:
|
||||||
# reading credentials from config for remote connection
|
# reading credentials from config for remote connection
|
||||||
print('usage: python3 get_elastic_ips.py --tag <tag>')
|
print('usage: python get_elastic_ips.py --tag <tag>')
|
||||||
print('reason: tag missing!!!')
|
print('reason: tag missing!!!')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
conn = boto.ec2.connect_to_region("eu-central-1",
|
# Connect to Amazon EC2
|
||||||
|
ec2 = boto3.resource(service_name='ec2',
|
||||||
|
region_name=AWS_REGION,
|
||||||
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
||||||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
|
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
|
||||||
|
|
||||||
INSTANCE_IDS = []
|
|
||||||
|
|
||||||
|
# Get a list of all instances with the specified tag.
|
||||||
|
# (Technically, instances_with_tag is an ec2.instancesCollection.)
|
||||||
|
instances_with_tag = ec2.instances.filter(
|
||||||
|
Filters=[{'Name': 'tag:Name', 'Values': [tag]}]
|
||||||
|
)
|
||||||
|
|
||||||
def prepare_list(tag):
|
print('Allocating elastic IP addresses and assigning them to the instances...')
|
||||||
reservations = conn.get_all_instances(filters={"tag:Name" : tag})
|
|
||||||
instances = [i for r in reservations for i in r.instances]
|
|
||||||
for i in instances:
|
|
||||||
inst = i.__dict__
|
|
||||||
#print (inst)
|
|
||||||
#break
|
|
||||||
inst_id = inst.get('id')
|
|
||||||
|
|
||||||
INSTANCE_IDS.append(inst_id)
|
for instance in instances_with_tag:
|
||||||
return INSTANCE_IDS
|
# Create a client from the ec2 resource
|
||||||
|
# See http://boto3.readthedocs.org/en/latest/guide/clients.html
|
||||||
|
client = ec2.meta.client
|
||||||
|
|
||||||
|
# Acquire an Elastic IP address
|
||||||
|
# response is a dict. See http://tinyurl.com/z2n7u9k
|
||||||
|
response = client.allocate_address(DryRun=False, Domain='standard')
|
||||||
|
public_ip = response['PublicIp']
|
||||||
|
print('public_ip = {}'.format(public_ip))
|
||||||
|
|
||||||
def get_new_pubDNS():
|
# Associate that Elastic IP address with an instance
|
||||||
eip = conn.allocate_address()
|
response2 = client.associate_address(
|
||||||
return eip
|
DryRun=False,
|
||||||
|
InstanceId=instance.instance_id,
|
||||||
|
PublicIp=public_ip
|
||||||
|
)
|
||||||
|
print('was associated with the instance with id {}'.
|
||||||
|
format(instance.instance_id))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# Make sure all IP addresses are assigned...
|
||||||
# hostlist.tmp (JSON) erzeugen
|
print('Waiting 30 seconds to make sure all IP addresses are assigned...')
|
||||||
instlist = prepare_list(tag)
|
|
||||||
|
|
||||||
for entry in range(0,len(instlist)):
|
|
||||||
|
|
||||||
instance_id = instlist[entry]
|
|
||||||
print(instance_id)
|
|
||||||
newpubDNS = get_new_pubDNS()
|
|
||||||
inID = str(newpubDNS).split(':')[1]
|
|
||||||
print(inID)
|
|
||||||
conn.associate_address(instance_id, public_ip=inID)
|
|
||||||
|
|
||||||
# make sure all addresse are assigned...
|
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
@ -62,9 +62,8 @@ python run_and_tag.py --tag $TAG --nodes $NODES
|
|||||||
python wait_until_all_running.py --tag $TAG
|
python wait_until_all_running.py --tag $TAG
|
||||||
|
|
||||||
# in case of elastic ips...
|
# in case of elastic ips...
|
||||||
python3 get_elastic_ips.py --tag $TAG
|
python get_elastic_ips.py --tag $TAG
|
||||||
|
|
||||||
# everything prepared. now wait until instances up and running!
|
|
||||||
# generate hostlist.py and add_keys.sh
|
# generate hostlist.py and add_keys.sh
|
||||||
python3 create_hostlist.py --tag $TAG > hostlist.py
|
python3 create_hostlist.py --tag $TAG > hostlist.py
|
||||||
# make add_keys executable and execute
|
# make add_keys executable and execute
|
||||||
|
Loading…
x
Reference in New Issue
Block a user