本文整理汇总了Python中boto.ec2.volume.Volume.attach方法的典型用法代码示例。如果您正苦于以下问题:Python Volume.attach方法的具体用法?Python Volume.attach怎么用?Python Volume.attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.ec2.volume.Volume
的用法示例。
在下文中一共展示了Volume.attach方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EucaCloudPlugin
# 需要导入模块: from boto.ec2.volume import Volume [as 别名]
# 或者: from boto.ec2.volume.Volume import attach [as 别名]
class EucaCloudPlugin(EC2CloudPlugin):
_name = 'euca'
def add_plugin_args(self, *args, **kwargs):
context = self._config.context
EC2CloudPlugin.add_plugin_args(self)
cloud = self._parser.add_argument_group(title='EC2 Options', description='EC2 Connection Information')
cloud.add_argument('--ec2-endpoint', dest='ec2_endpoint', help='EC2 endpoint to connect to',
action=conf_action(config=context.cloud))
def configure(self, config, parser):
super(EucaCloudPlugin, self).configure(config, parser)
host = config.context.web_log.get('host', False)
if not host:
md = get_instance_metadata()
pub, ipv4 = 'public-hostname', 'local-ipv4'
config.context.web_log['host'] = md[pub] if pub in md else md[ipv4]
def connect(self, **kwargs):
if self._connection:
log.warn('Already connected to Euca')
else:
log.info('Connecting to Euca')
self._connect(**kwargs)
def _connect(self, **kwargs):
cloud_config = self._config.plugins[self.full_name]
context = self._config.context
self._instance_metadata = get_instance_metadata()
euca_path = "/services/Eucalyptus"
euca_port = 8773
ec2_region = RegionInfo()
ec2_region.name = 'eucalyptus'
ec2_region.endpoint = context.cloud.ec2_endpoint
connection_args = { 'is_secure': False,
'debug': 0,
'port' : 8773,
'path' : euca_path,
'host' : context.cloud.ec2_endpoint,
'api_version': '2012-07-20',
'region': ec2_region }
if float(boto.__version__[0:3]) >= 2.6:
connection_args['validate_certs'] = False
self._connection = boto.connect_ec2(**connection_args)
log.info('Aminating in region {0}: http://{1}:{2}{3}'.format(ec2_region.name,
context.cloud.ec2_endpoint,
euca_port,
euca_path))
def allocate_base_volume(self, tag=True):
cloud_config = self._config.plugins[self.full_name]
context = self._config.context
self._volume = Volume(connection=self._connection)
rootdev = context.base_ami.block_device_mapping[context.base_ami.root_device_name]
self._volume.id = self._connection.create_volume(size=rootdev.size, zone=self._instance.placement,
snapshot=rootdev.snapshot_id).id
if not self._volume_available():
log.critical('{0}: unavailable.')
return False
if tag:
tags = {
'purpose': cloud_config.get('tag_ami_purpose', 'amination'),
'status': 'busy',
'ami': context.base_ami.id,
'ami-name': context.base_ami.name,
'arch': context.base_ami.architecture,
}
self._connection.create_tags([self._volume.id], tags)
self._volume.update()
log.debug('Volume {0} created'.format(self._volume.id))
@retry(VolumeException, tries=2, delay=1, backoff=1, logger=log)
def attach_volume(self, blockdevice, tag=True):
self.allocate_base_volume(tag=tag)
# must do this as amazon still wants /dev/sd*
ec2_device_name = blockdevice.replace('sd', 'vd')
log.debug('Attaching volume {0} to {1}:{2}({3})'.format(self._volume.id, self._instance.id, ec2_device_name,
blockdevice))
self._volume.attach(self._instance.id, ec2_device_name)
attached_device = self.is_volume_attached(ec2_device_name)
if attached_device != ec2_device_name:
log.debug('{0} attachment to {1}:{2}({3}) timed out'.format(self._volume.id, self._instance.id,
ec2_device_name, blockdevice))
self._volume.add_tag('status', 'used')
# trigger a retry
raise VolumeException('Timed out waiting for {0} to attach to {1}:{2}'.format(self._volume.id,
self._instance.id,
blockdevice))
log.debug('Volume {0} attached to {1}:{2}'.format(self._volume.id, self._instance.id, blockdevice))
return blockdevice
def is_volume_attached(self, ec2_device_name):
try:
attached_device = self._volume_attached(ec2_device_name)
#.........这里部分代码省略.........
示例2: VolumeTests
# 需要导入模块: from boto.ec2.volume import Volume [as 别名]
# 或者: from boto.ec2.volume.Volume import attach [as 别名]
class VolumeTests(unittest.TestCase):
def setUp(self):
self.attach_data = AttachmentSet()
self.attach_data.id = 1
self.attach_data.instance_id = 2
self.attach_data.status = "some status"
self.attach_data.attach_time = 5
self.attach_data.device = "/dev/null"
self.volume_one = Volume()
self.volume_one.id = 1
self.volume_one.create_time = 5
self.volume_one.status = "one_status"
self.volume_one.size = "one_size"
self.volume_one.snapshot_id = 1
self.volume_one.attach_data = self.attach_data
self.volume_one.zone = "one_zone"
self.volume_two = Volume()
self.volume_two.connection = mock.Mock()
self.volume_two.id = 1
self.volume_two.create_time = 6
self.volume_two.status = "two_status"
self.volume_two.size = "two_size"
self.volume_two.snapshot_id = 2
self.volume_two.attach_data = None
self.volume_two.zone = "two_zone"
@mock.patch("boto.ec2.volume.TaggedEC2Object.startElement")
def test_startElement_calls_TaggedEC2Object_startElement_with_correct_args(self, startElement):
volume = Volume()
volume.startElement("some name", "some attrs", None)
startElement.assert_called_with(volume, "some name", "some attrs", None)
@mock.patch("boto.ec2.volume.TaggedEC2Object.startElement")
def test_startElement_retval_not_None_returns_correct_thing(self, startElement):
tag_set = mock.Mock(TagSet)
startElement.return_value = tag_set
volume = Volume()
retval = volume.startElement(None, None, None)
self.assertEqual(retval, tag_set)
@mock.patch("boto.ec2.volume.TaggedEC2Object.startElement")
@mock.patch("boto.resultset.ResultSet")
def test_startElement_with_name_tagSet_calls_ResultSet(self, ResultSet, startElement):
startElement.return_value = None
result_set = mock.Mock(ResultSet([("item", Tag)]))
volume = Volume()
volume.tags = result_set
retval = volume.startElement("tagSet", None, None)
self.assertEqual(retval, volume.tags)
@mock.patch("boto.ec2.volume.TaggedEC2Object.startElement")
def test_startElement_with_name_attachmentSet_returns_AttachmentSet(self, startElement):
startElement.return_value = None
attach_data = AttachmentSet()
volume = Volume()
volume.attach_data = attach_data
retval = volume.startElement("attachmentSet", None, None)
self.assertEqual(retval, volume.attach_data)
@mock.patch("boto.ec2.volume.TaggedEC2Object.startElement")
def test_startElement_else_returns_None(self, startElement):
startElement.return_value = None
volume = Volume()
retval = volume.startElement("not tagSet or attachmentSet", None, None)
self.assertEqual(retval, None)
def check_that_attribute_has_been_set(self, name, value, attribute):
volume = Volume()
volume.endElement(name, value, None)
self.assertEqual(getattr(volume, attribute), value)
def test_endElement_sets_correct_attributes_with_values(self):
for arguments in [("volumeId", "some value", "id"),
("createTime", "some time", "create_time"),
("status", "some status", "status"),
("size", 5, "size"),
("snapshotId", 1, "snapshot_id"),
("availabilityZone", "some zone", "zone"),
("someName", "some value", "someName")]:
self.check_that_attribute_has_been_set(arguments[0], arguments[1], arguments[2])
def test_endElement_with_name_status_and_empty_string_value_doesnt_set_status(self):
volume = Volume()
volume.endElement("status", "", None)
self.assertNotEqual(volume.status, "")
def test_update_with_result_set_greater_than_0_updates_dict(self):
self.volume_two.connection.get_all_volumes.return_value = [self.volume_one]
self.volume_two.update()
assert all([self.volume_two.create_time == 5,
self.volume_two.status == "one_status",
self.volume_two.size == "one_size",
self.volume_two.snapshot_id == 1,
self.volume_two.attach_data == self.attach_data,
self.volume_two.zone == "one_zone"])
def test_update_with_validate_true_raises_value_error(self):
#.........这里部分代码省略.........
示例3: EC2CloudPlugin
# 需要导入模块: from boto.ec2.volume import Volume [as 别名]
# 或者: from boto.ec2.volume.Volume import attach [as 别名]
class EC2CloudPlugin(BaseCloudPlugin):
_name = 'ec2'
def add_metrics(self, metric_base_name, cls, func_name):
newfunc = succeeds("{0}.count".format(metric_base_name), self)(raises("{0}.error".format(metric_base_name), self)(timer("{0}.duration".format(metric_base_name), self)(getattr(cls, func_name))))
setattr(cls, func_name, newfunc)
def __init__(self):
super(EC2CloudPlugin, self).__init__()
# wrap each of the functions so we can get timer and error metrics
for ec2func in ["create_volume", "create_tags", "register_image", "get_all_images"]:
self.add_metrics("aminator.cloud.ec2.connection.{0}".format(ec2func), EC2Connection, ec2func)
for volfunc in ["add_tag", "attach", "create_snapshot", "delete", "detach", "update"]:
self.add_metrics("aminator.cloud.ec2.volume.{0}".format(volfunc), Volume, volfunc)
for imgfunc in ["update"]:
self.add_metrics("aminator.cloud.ec2.image.{0}".format(imgfunc), Image, imgfunc)
for insfunc in ["update"]:
self.add_metrics("aminator.cloud.ec2.instance.{0}".format(insfunc), Instance, insfunc)
def add_plugin_args(self, *args, **kwargs):
context = self._config.context
base_ami = self._parser.add_argument_group(
title='Base AMI', description='EITHER AMI id OR name, not both!')
base_ami_mutex = base_ami.add_mutually_exclusive_group(required=True)
base_ami_mutex.add_argument(
'-b', '--base-ami-name', dest='base_ami_name',
action=conf_action(config=context.ami),
help='The name of the base AMI used in provisioning')
base_ami_mutex.add_argument(
'-B', '--base-ami-id', dest='base_ami_id',
action=conf_action(config=context.ami),
help='The id of the base AMI used in provisioning')
cloud = self._parser.add_argument_group(
title='EC2 Options', description='EC2 Connection Information')
cloud.add_argument(
'-r', '--region', dest='region',
help='EC2 region (default: us-east-1)',
action=conf_action(config=context.cloud))
cloud.add_argument(
'--boto-secure', dest='is_secure', help='Connect via https',
action=conf_action(config=context.cloud, action='store_true'))
cloud.add_argument(
'--boto-debug', dest='boto_debug', help='Boto debug output',
action=conf_action(config=context.cloud, action='store_true'))
volume_mutex = cloud.add_mutually_exclusive_group()
volume_mutex.add_argument(
'-V', '--volume-id', dest='volume_id',
action=conf_action(config=context.ami),
help='The Base AMI volume id already attached to the system')
volume_mutex.add_argument(
'--provisioner-ebs-type', dest='provisioner_ebs_type',
action=conf_action(config=context.cloud),
help='The type of EBS volume to create from the Base AMI snapshot')
cloud.add_argument(
'--register-ebs-type', dest='register_ebs_type',
action=conf_action(config=context.cloud),
help='The root volume EBS type for AMI registration')
cloud.add_argument(
'--root-volume-size', dest='root_volume_size',
action=conf_action(config=context.ami),
help='Root volume size (in GB). The default is to inherit from the base AMI.')
def configure(self, config, parser):
super(EC2CloudPlugin, self).configure(config, parser)
host = config.context.web_log.get('host', False)
if not host:
md = get_instance_metadata()
pub, ipv4 = 'public-hostname', 'local-ipv4'
config.context.web_log['host'] = md[pub] if pub in md else md[ipv4]
def connect(self, **kwargs):
if self._connection:
log.warn('Already connected to EC2')
else:
log.info('Connecting to EC2')
self._connect(**kwargs)
def _connect(self, **kwargs):
cloud_config = self._config.plugins[self.full_name]
context = self._config.context
self._instance_metadata = get_instance_metadata()
instance_region = self._instance_metadata['placement']['availability-zone'][:-1]
region = kwargs.pop('region', context.get('region', cloud_config.get('region', instance_region)))
log.debug('Establishing connection to region: {0}'.format(region))
context.cloud.setdefault('boto_debug', False)
if context.cloud.boto_debug:
from aminator.config import configure_datetime_logfile
configure_datetime_logfile(self._config, 'boto')
kwargs['debug'] = 1
log.debug('Boto debug logging enabled')
else:
logging.getLogger('boto').setLevel(logging.INFO)
if 'is_secure' not in kwargs:
kwargs['is_secure'] = context.get('is_secure', cloud_config.get('is_secure', True))
self._connection = connect_to_region(region, **kwargs)
log.info('Aminating in region {0}'.format(region))
def allocate_base_volume(self, tag=True):
cloud_config = self._config.plugins[self.full_name]
#.........这里部分代码省略.........