本文整理汇总了Python中boto.ec2方法的典型用法代码示例。如果您正苦于以下问题:Python boto.ec2方法的具体用法?Python boto.ec2怎么用?Python boto.ec2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto
的用法示例。
在下文中一共展示了boto.ec2方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aws_client
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def aws_client(region, service='ec2', profile=None):
""" Set the boto3 client with the correct service and AWS profile.
Args:
region (str): The AWS region you want this client to connect to.
example us-west-2
Kwargs:
service (str): The service this client will connect to.
profile (str): The aws profile name that is set in ~/.aws/credentials
Basic Usage:
>>> client = aws_client('us-west-2', 'kinesis', profile='prod')
Returns:
botocore.client.EC2
"""
try:
session = boto3.Session(region_name=region, profile_name=profile)
return session.client(service)
except botocore.exceptions.ClientError as e:
raise e
示例2: zones
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def zones(region=None, profile=None):
""" Retrieve a list of available zones in a region.
Kwargs:
region (str): The AWS region.
profile (str): The ~/.aws/credentials profile name to use
Basic Usage:
>>> az = zones('us-west-2')
['us-west-2a', 'us-west-2b', 'us-west-2c']
Returns:
List
"""
client = aws_client(region, 'ec2', profile)
zone_names = (
map(
lambda x: x['ZoneName'],
client.describe_availability_zones()['AvailabilityZones']
)
)
zone_names.sort()
return zone_names
示例3: get_vpc_ids_from_names
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def get_vpc_ids_from_names(vpc_names, region=None, profile=None):
"""Return a list of vpc ids from the list of vpc names that were matched.
Args:
vpc_names (list): List of vpc names you are searching for.
client (Boto3.Client): The instantiated boto3 client.
"""
vpc_ids = list()
client = aws_client(region, 'ec2', profile)
vpcs = client.describe_vpcs()
for vpc in vpcs['Vpcs']:
if 'Tags' in vpc:
for tag in vpc['Tags']:
if tag['Key'] == 'Name':
for name in vpc_names:
if re.search(name, tag['Value'], re.IGNORECASE):
vpc_ids.append(vpc['VpcId'])
return vpc_ids
示例4: login_to_aws
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def login_to_aws():
logger.info(u"logging in to aws")
conn = boto.ec2.connect_to_region('us-west-2')
# instance = conn.get_all_instances()[0].instances[0]
ssh_client = None
for reservation in conn.get_all_instances():
instance = reservation.instances[0]
try:
if not ssh_client:
ssh_client = sshclient_from_instance(instance, "data/key.pem", user_name="ec2-user")
print u"this instance worked: {}".format(instance)
except Exception:
pass
return (conn, ssh_client)
# clarivate
# python queue_separate_table.py --export_with_versions --week
# or, run this on aws
# create table export_main_changed_with_versions_20170118 as (select * from export_main_changed_with_versions where last_changed_date > '2018-01-10'::timestamp)
# it takes about 45 minutes
# then this
# python queue_separate_table.py --export --view=export_main_changed_with_versions_20180118
# which takes about 5 minutes
# mv all_dois*.csv datasets_for_clarivate
# mv all_dois*.csv.gz datasets_for_clarivate
示例5: __init__
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def __init__(self,
name,
region=None,
vpc_id=None,
subnet_id=None,
security_group_id=None,
instance_type=None,
open_public_port=False,
commit_to_build=None):
assert vpc_id is None or subnet_id is not None
self.cluster_name = name
self.region = region
self.vpc_id = vpc_id
self.subnet_id = subnet_id
self.security_group_id = security_group_id
self.instance_type = instance_type
self.open_public_port = open_public_port
self.ec2 = None
self.commit_to_build = commit_to_build
示例6: get_reservations
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def get_reservations(self):
if not self.connected:
self.connect()
filters = {'tag:pyfora_cluster': self.cluster_name}
reservations = self.ec2.get_all_reservations(filters=filters)
instances = {i.id: i for r in reservations for i in r.instances}
spot_requests = [
r for r in self.ec2.get_all_spot_instance_requests(filters=filters)
if r.state != 'cancelled'
]
spot_instance_ids = [r.instance_id for r in spot_requests
if r.instance_id and r.instance_id not in instances]
unfulfilled_spot_requests = [r for r in spot_requests if not r.instance_id]
all_instances = instances.values() + \
(self.get_only_instances(instance_ids=spot_instance_ids)
if spot_instance_ids else [])
return {
'instances': all_instances,
'unfulfilled_spot_requests': unfulfilled_spot_requests
}
示例7: get_format_args
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def get_format_args(self, updates):
assert self.connected
args = {
'aws_access_key': self.ec2.provider.access_key,
'aws_secret_key': self.ec2.provider.secret_key,
'aws_region': self.region,
'aws_credentials': get_aws_credentials_docker_env(),
'container_env': '',
'image_version': pyfora_version,
'commit_to_build': str(self.commit_to_build) if self.commit_to_build is not None else "",
"needs_cuda": "1" if self.instance_type[:2] == "g2" else ""
}
args.update(updates)
return args
示例8: find_ufora_security_group
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def find_ufora_security_group(self):
filters = None
if self.vpc_id is not None:
filters = {'vpc-id': self.vpc_id}
try:
groups = self.ec2.get_all_security_groups(
groupnames=[self.security_group_name],
filters=filters
)
except boto.exception.EC2ResponseError as e:
if e.error_code == 'InvalidGroup.NotFound':
return None
raise
assert len(groups) < 2, "More than one pyfora security groups exist"
if len(groups) == 0:
return None
return groups[0]
示例9: create_ufora_security_group
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def create_ufora_security_group(self):
security_group = self.ec2.create_security_group(
name=self.security_group_name,
description=self.ufora_security_group_description,
vpc_id=self.vpc_id
)
security_group.authorize(src_group=security_group,
ip_protocol='tcp',
from_port=30002,
to_port=30010)
security_group.authorize(cidr_ip='0.0.0.0/0',
ip_protocol='tcp',
from_port=22,
to_port=22)
if self.open_public_port:
security_group.authorize(cidr_ip='0.0.0.0/0',
ip_protocol='tcp',
from_port=30000,
to_port=30000)
return security_group
示例10: get_stack_instance_details
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def get_stack_instance_details(stack):
global ec2
global elb_conn
instances_info = \
ec2.get_only_instances(filters={'tag:aws:cloudformation:stack-id': stack.stack_id})
instances_health = elb_conn.describe_instance_health(stack.stack_name)
instances = list()
for ii in instances_info:
for ih in instances_health:
if ih.instance_id == ii.id:
instance = {'instance_id': ii.id, 'private_ip': ii.private_ip_address,
'launch_time': parse_time(ii.launch_time)}
if ih.state == 'InService':
instance['role'] = 'MASTER'
else:
instance['role'] = 'REPLICA'
instances.append(instance)
instances.sort(key=lambda k: (k['role'], k['instance_id']))
return instances
示例11: __init__
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def __init__(self, region):
self.uid='ut_{}'.format(random.randint(1, 10000000000))
self.region_name = region
self.ec2_conn = boto.ec2.connect_to_region(region)
self.vpc_conn = boto.vpc.connect_to_region(region)
self.key_name = self.uid+'_kp'
self.vpc_id = ''
self.vpc_name = self.uid+'_vpc'
self.vpc_netmask = '10.0.0.0/24'
self.subnet_id = ''
self.subnet_name = self.uid+'_subnet'
self.subnet_netmask = self.vpc_netmask
示例12: get_stack_name
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def get_stack_name(node_addr, aws_config):
"""Get the name of the CloudFormation stack a node belongs to."""
import boto.ec2
conn = boto.ec2.connect_to_region(
aws_config['ec2_region'],
aws_access_key_id=aws_config['ec2_access_key'],
aws_secret_access_key=aws_config['ec2_secret_key'])
reservations = conn.get_all_reservations()
for resv in reservations:
for inst in resv.instances:
# Non-HA MGTs don't have a tagged interface.
if inst.private_ip_address == node_addr:
return inst.tags['aws:cloudformation:stack-name']
for iface in inst.interfaces:
iface.update()
if iface.private_ip_address == node_addr:
return inst.tags.get('aws:cloudformation:stack-name')
示例13: _get_mgt_ip_addr
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def _get_mgt_ip_addr(stack_name, aws_config):
import boto.ec2
conn = boto.ec2.connect_to_region(
aws_config['ec2_region'],
aws_access_key_id=aws_config['ec2_access_key'],
aws_secret_access_key=aws_config['ec2_secret_key'])
reservations = conn.get_all_reservations(
filters={
'tag:Name': 'mgt*',
'tag:aws:cloudformation:stack-name': stack_name,
}
)
for resv in reservations:
for inst in resv.instances:
for iface in inst.interfaces:
iface.update()
if iface.tags.get('lustre:server_role') == 'mgt':
# HA MGTs have a tagged interface.
return iface.private_ip_address
# Non-HA MGTs don't.
return inst.private_ip_address
return None
示例14: _setup_placment_group
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def _setup_placment_group(args, vpc_info):
import boto
import boto.ec2
cluster_config = common.ecluster_config(args.econfig, args.cluster)
ec2_conn = boto.ec2.connect_to_region(args.region)
conn = boto.connect_vpc(
aws_access_key_id=cluster_config['cloud']['ec2_access_key'],
aws_secret_access_key=cluster_config['cloud']['ec2_secret_key'],
region=ec2_conn.region)
pgname = "{}_cluster_pg".format(args.cluster)
pgs = conn.get_all_placement_groups()
if vpc_info.get("created") or pgname not in [x.name for x in pgs]:
if pgname in [x.name for x in pgs]:
print("Refreshing placement group %s." % pgname)
conn.delete_placement_group(pgname)
conn.create_placement_group(pgname)
print("Placement group %s created." % pgname)
else:
print("Placement group %s already exists. Skipping" % pgname)
示例15: attach_network_interface
# 需要导入模块: import boto [as 别名]
# 或者: from boto import ec2 [as 别名]
def attach_network_interface(ec2region,instance):
conn = boto.ec2.connect_to_region(ec2region,
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'))
instance = conn.get_only_instances(instance_ids=[instance])[0]
network_interface = conn.create_network_interface(instance.subnet_id,description="internal",groups=[ group.id for group in instance.groups])
print 'created network interface: ',network_interface.id
if network_interface.attach(instance.id,1)== False:
raise Exception('could not attach network interface to instance');
network_interface = conn.get_all_network_interfaces(network_interface_ids=[network_interface.id])[0]
if conn.modify_network_interface_attribute(network_interface.id,'deleteOnTermination',True, attachment_id=network_interface.attachment.id) == False:
raise Exception('could not update network interface delete on instance');