Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import boto3 | ||
|
||
class Instance: | ||
def __init__(self, name, ip, port): | ||
self.name = name | ||
self.ip = ip | ||
self.port = port | ||
|
||
class RetrieveInstanceIPs(): | ||
def getIps(self,service,port): | ||
raise NotImplementedError("Subclass must implement abstract method") | ||
|
||
class ManualInstances(RetrieveInstanceIPs): | ||
def getIps(self,service,ips,port): | ||
instances=[] | ||
for ip in ips.split(','): | ||
instances.append(Instance(name=service,ip=ip,port=port)) | ||
return instances | ||
|
||
class AWSInstances(RetrieveInstanceIPs): | ||
def __init__(self,profile,region): | ||
self.profile = profile | ||
self.region = region | ||
boto3.setup_default_session(profile_name=profile) | ||
def getIps(self,service,port): | ||
instances=[] | ||
for ec2_ip in self.req_aws_ips(service, self.region): | ||
instances.append(Instance(name=service,ip=ec2_ip,port=port)) | ||
return instances | ||
def req_aws_ips(self,service, region): | ||
ips=[] | ||
aws_filter = lambda name,value: [{'Name':'tag:'+name,'Values':[value]}] | ||
client = boto3.client('ec2') | ||
response = client.describe_instances(Filters=aws_filter('Name',service)) | ||
for res in response.get('Reservations'): | ||
for instance in res.get('Instances'): | ||
ips.append(instance.get(u'PrivateIpAddress')) | ||
return ips |
Binary file not shown.
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
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
Binary file not shown.
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