本文整理汇总了Python中boto.connect_ec2方法的典型用法代码示例。如果您正苦于以下问题:Python boto.connect_ec2方法的具体用法?Python boto.connect_ec2怎么用?Python boto.connect_ec2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto
的用法示例。
在下文中一共展示了boto.connect_ec2方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_resources
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def get_resources(self):
ResourceLocator.get_resources(self)
ec2 = boto.connect_ec2()
#search for the backend servers
# Build set of filters for instance lookup
filterDict = {}
# Add tag filters from config
for ignore, kvmap in self._dataMap['aws_tags'].items():
print kvmap['key'] + "=>" + kvmap['value']
tagKey = "tag:" + kvmap['key']
filterDict[tagKey] = kvmap['value']
# Add instance state filter
filterDict['instance-state-name'] = 'running'
reservations = ec2.get_all_instances(filters = filterDict)
instances = [i for reservation in reservations for i in reservation.instances]
backends = []
for server in instances:
ip_address = IPResource(server.private_ip_address)
backends.append(ip_address)
return backends
示例2: do_start
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def do_start(self):
ami_id = self.sd.get('ami_id')
instance_type = self.sd.get('instance_type', 'm1.small')
security_group = self.sd.get('security_group', 'default')
if not ami_id:
self.parser.error('ami_id option is required when starting the service')
ec2 = boto.connect_ec2()
if not self.sd.has_section('Credentials'):
self.sd.add_section('Credentials')
self.sd.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id)
self.sd.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key)
s = StringIO()
self.sd.write(s)
rs = ec2.get_all_images([ami_id])
img = rs[0]
r = img.run(user_data=s.getvalue(), key_name=self.options.keypair,
max_count=self.options.num_instances,
instance_type=instance_type,
security_groups=[security_group])
print('Starting AMI: %s' % ami_id)
print('Reservation %s contains the following instances:' % r.id)
for i in r.instances:
print('\t%s' % i.id)
示例3: do_start
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def do_start(self):
ami_id = self.sd.get('ami_id')
instance_type = self.sd.get('instance_type', 'm1.small')
security_group = self.sd.get('security_group', 'default')
if not ami_id:
self.parser.error('ami_id option is required when starting the service')
ec2 = boto.connect_ec2()
if not self.sd.has_section('Credentials'):
self.sd.add_section('Credentials')
self.sd.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id)
self.sd.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key)
s = StringIO.StringIO()
self.sd.write(s)
rs = ec2.get_all_images([ami_id])
img = rs[0]
r = img.run(user_data=s.getvalue(), key_name=self.options.keypair,
max_count=self.options.num_instances,
instance_type=instance_type,
security_groups=[security_group])
print 'Starting AMI: %s' % ami_id
print 'Reservation %s contains the following instances:' % r.id
for i in r.instances:
print '\t%s' % i.id
示例4: get_ami_map
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def get_ami_map(self,
aws_region=None, image_names=None):
"""
Method iterates on all AWS regions for a given set of AMI names to gather AMI IDs and
to create a regionmap for CloudFormation templates.
@param aws_region [string] - optionally provides the region to start querying when gathering the list of regions globally.
"""
if aws_region is None:
aws_region = self.configuration.get('boto', {}).get('region_name', 'us-east-1')
logging.debug('Setting default AWS Region for API access from overall configuration [' + aws_region + ']')
region_map = {}
vpc_conn = boto.connect_vpc(aws_region)
logging.debug('Connected to VPC in region [' + aws_region + ']')
for region in vpc_conn.get_all_regions():
if region.name not in region_map.keys():
logging.debug('Adding region [' + region.name + '] to region map.')
region_map[region.name] = {}
ec2_conn = boto.connect_ec2(region.name)
logging.debug('Connected to EC2 API in region [' + region.name + ']')
for k, v in image_names:
logging.debug('Looking for Image [' + k + ': ' + v + '] in region [' + region.name + ']')
images = ec2_conn.get_all_images(filters={'name': v})
if len(images) == 0:
logging.warn('No image found for [' + k + ': ' + v + '] in region [' + region.name + ']')
elif len(images) > 1:
logging.warn('Found ' + str(len(images)) + ' images for [' + k + ': ' + v + '] in region [' + region.name + ']')
else:
logging.debug('Adding image [' + images[0].id + '] to region [' + region.name + '] for key [' + k + ']')
region_map[region.name][k] = images[0].id
logging.debug('AMI Region Map Contents: ' + json.dumps(region_map))
return region_map
示例5: create_keypair
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def create_keypair(econfig_file=None, region=None, keyname="bcbio"):
"""Create a bcbio keypair and import to ec2. Gives us access to keypair locally and at AWS.
"""
import boto
import boto.ec2
if econfig_file:
keypair_dir = os.path.dirname(econfig_file).replace("elasticluster", "aws_keypairs")
else:
keypair_dir = os.path.join(os.getcwd(), "aws_keypairs")
if not os.path.exists(keypair_dir):
os.makedirs(keypair_dir)
private_key = os.path.join(os.path.join(keypair_dir, keyname))
new_key = not os.path.exists(private_key)
if new_key:
cmd = ["ssh-keygen", "-t", "rsa", "-N", "", "-f", private_key, "-C", "bcbio_aws_keypair"]
subprocess.check_call(cmd)
public_key = private_key + ".pub"
if region:
ec2 = boto.ec2.connect_to_region(region)
else:
ec2 = boto.connect_ec2()
key = ec2.get_key_pair(keyname)
if key and new_key:
print("Non matching key %s found in AWS, removing." % keyname)
ec2.delete_key_pair(keyname)
key = None
if not key:
print("Key %s not found in AWS, importing created key" % keyname)
with open(public_key) as in_handle:
body = in_handle.read()
try:
ec2.import_key_pair(keyname, body)
except TypeError as e:
body = body.encode('utf-8')
ec2.import_key_pair(keyname, body)
return {"user_key_name": keyname, "user_key_private": private_key,
"user_key_public": public_key}
示例6: main
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def main(self):
fp = StringIO.StringIO()
boto.config.dump_safe(fp)
self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue())
if self.src and self.dst:
self.copy_keys()
if self.dst:
self.copy_log()
self.notify('%s (%s) Stopping' % (self.name, self.instance_id),
'Copy Operation Complete')
if boto.config.getbool(self.name, 'exit_on_completion', True):
ec2 = boto.connect_ec2()
ec2.terminate_instances([self.instance_id])
示例7: attach
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def attach(self):
ec2 = boto.connect_ec2()
if self.logical_volume_name:
# if a logical volume was specified, override the specified volume_id
# (if there was one) with the current AWS volume for the logical volume:
logical_volume = next(Volume.find(name=self.logical_volume_name))
self.volume_id = logical_volume._volume_id
volume = ec2.get_all_volumes([self.volume_id])[0]
# wait for the volume to be available. The volume may still be being created
# from a snapshot.
while volume.update() != 'available':
boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status))
time.sleep(5)
instance = ec2.get_only_instances([self.instance_id])[0]
attempt_attach = True
while attempt_attach:
try:
ec2.attach_volume(self.volume_id, self.instance_id, self.device)
attempt_attach = False
except EC2ResponseError as e:
if e.error_code != 'IncorrectState':
# if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2
# to realize the instance is running, then try again. Otherwise, raise the error:
boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors))
time.sleep(2)
else:
raise e
boto.log.info('Attached volume %s to instance %s as device %s' % (self.volume_id, self.instance_id, self.device))
# now wait for the volume device to appear
while not os.path.exists(self.device):
boto.log.info('%s still does not exist, waiting 2 seconds' % self.device)
time.sleep(2)
示例8: shutdown
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def shutdown(self):
on_completion = self.sd.get('on_completion', 'shutdown')
if on_completion == 'shutdown':
if self.instance_id:
time.sleep(60)
c = boto.connect_ec2()
c.terminate_instances([self.instance_id])
示例9: ec2
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def ec2(self):
if self._ec2 is None:
self._ec2 = boto.connect_ec2()
return self._ec2
示例10: AwsConnect
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def AwsConnect(configSpec,awsKey,awsSecret):
if configSpec['Verbose']:
logging.info("[AwsConnect]")
targetRegion = None
if 'Region' in configSpec:
targetRegion = boto.ec2.get_region(configSpec['Region'])
conn = boto.connect_ec2(awsKey,awsSecret,region = targetRegion)
return conn
示例11: attach
# 需要导入模块: import boto [as 别名]
# 或者: from boto import connect_ec2 [as 别名]
def attach(self):
ec2 = boto.connect_ec2()
if self.logical_volume_name:
# if a logical volume was specified, override the specified volume_id
# (if there was one) with the current AWS volume for the logical volume:
logical_volume = Volume.find(name = self.logical_volume_name).next()
self.volume_id = logical_volume._volume_id
volume = ec2.get_all_volumes([self.volume_id])[0]
# wait for the volume to be available. The volume may still be being created
# from a snapshot.
while volume.update() != 'available':
boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status))
time.sleep(5)
instance = ec2.get_all_instances([self.instance_id])[0].instances[0]
attempt_attach = True
while attempt_attach:
try:
ec2.attach_volume(self.volume_id, self.instance_id, self.device)
attempt_attach = False
except EC2ResponseError, e:
if e.error_code != 'IncorrectState':
# if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2
# to realize the instance is running, then try again. Otherwise, raise the error:
boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors))
time.sleep(2)
else:
raise e