本文整理汇总了Python中boto.ec2.blockdevicemapping.BlockDeviceType类的典型用法代码示例。如果您正苦于以下问题:Python BlockDeviceType类的具体用法?Python BlockDeviceType怎么用?Python BlockDeviceType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BlockDeviceType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BlockDeviceTypeTests
class BlockDeviceTypeTests(unittest.TestCase):
def setUp(self):
self.block_device_type = BlockDeviceType()
def check_that_attribute_has_been_set(self, name, value, attribute):
self.block_device_type.endElement(name, value, None)
self.assertEqual(getattr(self.block_device_type, attribute), value)
def test_endElement_sets_correct_attributes_with_values(self):
for arguments in [("volumeId", 1, "volume_id"),
("virtualName", "some name", "ephemeral_name"),
("snapshotId", 1, "snapshot_id"),
("volumeSize", 1, "size"),
("status", "some status", "status"),
("attachTime", 1, "attach_time"),
("somethingRandom", "somethingRandom", "somethingRandom")]:
self.check_that_attribute_has_been_set(arguments[0], arguments[1], arguments[2])
def test_endElement_with_name_NoDevice_value_true(self):
self.block_device_type.endElement("NoDevice", 'true', None)
self.assertEqual(self.block_device_type.no_device, True)
def test_endElement_with_name_NoDevice_value_other(self):
self.block_device_type.endElement("NoDevice", 'something else', None)
self.assertEqual(self.block_device_type.no_device, False)
def test_endElement_with_name_deleteOnTermination_value_true(self):
self.block_device_type.endElement("deleteOnTermination", "true", None)
self.assertEqual(self.block_device_type.delete_on_termination, True)
def test_endElement_with_name_deleteOnTermination_value_other(self):
self.block_device_type.endElement("deleteOnTermination", 'something else', None)
self.assertEqual(self.block_device_type.delete_on_termination, False)
示例2: run_instance
def run_instance(conn, ami_id, key_name, instance_type,
sec_group, zone="us-east-1d", vol_size=None):
"""
@param connection: python boto connection
@param ami_id: AMI ID
@param key_name: SSH key name
@param instance_type: instance type, example 'm1.large'
@param sec_group: security group
@param zone: optional, defaults to 'us-east-1d'
@param vol_size: optional integer, if specified will change size of root volume to this size
@return boto.ec2.instance.Instance
"""
bdm = None
if vol_size:
# Create block device mapping info
dev_sda1 = BlockDeviceType()
dev_sda1.size = int(vol_size)
dev_sda1.delete_on_termination = True
bdm = BlockDeviceMapping()
bdm['/dev/sda1'] = dev_sda1
# Run instance
reservation = conn.run_instances(
ami_id,
key_name=key_name,
instance_type=instance_type,
placement=zone,
instance_initiated_shutdown_behavior="stop",
security_groups=[sec_group],
block_device_map=bdm)
return reservation.instances[0]
示例3: build_block_device_map
def build_block_device_map(ephemeral, number_ephemeral_disks=1, ebs_size=None, iops=None, number_ebs_volumes=1):
bdm = blockdevicemapping.BlockDeviceMapping()
if ephemeral:
# The ephemeral disk
xvdb = BlockDeviceType()
xvdb.ephemeral_name = 'ephemeral0'
bdm['/dev/xvdb'] = xvdb
if number_ephemeral_disks == 2:
xvdc = BlockDeviceType()
xvdc.ephemeral_name = 'ephemeral1'
bdm['/dev/xvdc'] = xvdc
if ebs_size:
for disks in range(0, number_ebs_volumes):
xvd_n = blockdevicemapping.EBSBlockDeviceType(delete_on_termination=True)
xvd_n.size = int(ebs_size) # size in Gigabytes
if iops:
xvd_n.iops = 500
xvd_n.volume_type = 'io1'
else:
xvd_n.volume_type = 'gp2'
last_char = chr(ord('f') + disks)
bdm['/dev/xvd' + last_char] = xvd_n
return bdm
示例4: register_snapshot_by_id
def register_snapshot_by_id( self, snap_id, rdn="/dev/sda1", description="bfebs", windows=False, bdmdev=None, name=None, ramdisk=None, kernel=None, dot=True ):
'''
Register an image snapshot
snap_id (mandatory string) snapshot id
name (mandatory string) name of image to be registered
description (optional string) description of image to be registered
bdmdev (optional string) block-device-mapping device for image
rdn (optional string) root-device-name for image
dot (optional boolean) Delete On Terminate boolean
windows (optional boolean) Is windows image boolean
kernel (optional string) kernal (note for windows this name should be "windows"
'''
if (bdmdev is None):
bdmdev=rdn
if (name is None):
name="bfebs_"+ snap_id
if ( windows is True ) and ( kernel is not None):
kernel="windows"
bdmap = BlockDeviceMapping()
block_dev_type = BlockDeviceType()
block_dev_type.snapshot_id = snap_id
block_dev_type.delete_on_termination = dot
bdmap[bdmdev] = block_dev_type
self.debug("Register image with: snap_id:"+str(snap_id)+", rdn:"+str(rdn)+", desc:"+str(description)+", windows:"+str(windows)+", bdname:"+str(bdmdev)+", name:"+str(name)+", ramdisk:"+str(ramdisk)+", kernel:"+str(kernel))
image_id = self.ec2.register_image(name=name, description=description, kernel_id=kernel, ramdisk_id=ramdisk, block_device_map=bdmap, root_device_name=rdn)
self.debug("Image now registered as " + image_id)
return image_id
示例5: snapshot_register
def snapshot_register(self):
snapshot_id = self.snapshot.id
name = self.request.params.get('name')
description = self.request.params.get('description')
dot = self.request.params.get('dot')
reg_as_windows = self.request.params.get('reg_as_windows')
root_vol = BlockDeviceType(snapshot_id=snapshot_id)
root_vol.delete_on_termination = dot
bdm = BlockDeviceMapping()
root_device_name = '/dev/sda' if self.cloud_type == 'euca' else '/dev/sda1'
bdm[root_device_name] = root_vol
location = self.request.route_path('snapshot_view', id=snapshot_id)
if self.snapshot and self.register_form.validate():
with boto_error_handler(self.request, location):
self.log_request(_(u"Registering snapshot {0} as image {1}").format(snapshot_id, name))
self.snapshot.connection.register_image(
name=name, description=description,
root_device_name=root_device_name,
kernel_id=('windows' if reg_as_windows else None),
block_device_map=bdm)
prefix = _(u'Successfully registered snapshot')
msg = u'{prefix} {id}'.format(prefix=prefix, id=snapshot_id)
# Clear images cache
self.invalidate_images_cache()
self.request.session.flash(msg, queue=Notification.SUCCESS)
return HTTPFound(location=location)
return self.render_dict
示例6: test_create_launch_configuration_with_block_device_mappings
def test_create_launch_configuration_with_block_device_mappings():
block_device_mapping = BlockDeviceMapping()
ephemeral_drive = BlockDeviceType()
ephemeral_drive.ephemeral_name = 'ephemeral0'
block_device_mapping['/dev/xvdb'] = ephemeral_drive
snapshot_drive = BlockDeviceType()
snapshot_drive.snapshot_id = "snap-1234abcd"
snapshot_drive.volume_type = "standard"
block_device_mapping['/dev/xvdp'] = snapshot_drive
ebs_drive = BlockDeviceType()
ebs_drive.volume_type = "io1"
ebs_drive.size = 100
ebs_drive.iops = 1000
ebs_drive.delete_on_termination = False
block_device_mapping['/dev/xvdh'] = ebs_drive
conn = boto.connect_autoscale(use_block_device_types=True)
config = LaunchConfiguration(
name='tester',
image_id='ami-abcd1234',
instance_type='m1.small',
key_name='the_keys',
security_groups=["default", "default2"],
user_data="This is some user_data",
instance_monitoring=True,
instance_profile_name='arn:aws:iam::123456789012:instance-profile/testing',
spot_price=0.1,
block_device_mappings=[block_device_mapping]
)
conn.create_launch_configuration(config)
launch_config = conn.get_all_launch_configurations()[0]
launch_config.name.should.equal('tester')
launch_config.image_id.should.equal('ami-abcd1234')
launch_config.instance_type.should.equal('m1.small')
launch_config.key_name.should.equal('the_keys')
set(launch_config.security_groups).should.equal(set(['default', 'default2']))
launch_config.user_data.should.equal("This is some user_data")
launch_config.instance_monitoring.enabled.should.equal('true')
launch_config.instance_profile_name.should.equal('arn:aws:iam::123456789012:instance-profile/testing')
launch_config.spot_price.should.equal(0.1)
len(launch_config.block_device_mappings).should.equal(3)
returned_mapping = launch_config.block_device_mappings
set(returned_mapping.keys()).should.equal(set(['/dev/xvdb', '/dev/xvdp', '/dev/xvdh']))
returned_mapping['/dev/xvdh'].iops.should.equal(1000)
returned_mapping['/dev/xvdh'].size.should.equal(100)
returned_mapping['/dev/xvdh'].volume_type.should.equal("io1")
returned_mapping['/dev/xvdh'].delete_on_termination.should.be.false
returned_mapping['/dev/xvdp'].snapshot_id.should.equal("snap-1234abcd")
returned_mapping['/dev/xvdp'].volume_type.should.equal("standard")
returned_mapping['/dev/xvdb'].ephemeral_name.should.equal('ephemeral0')
示例7: vol2ami
def vol2ami(self, volume_id, name, description=None):
snap = self.ctxt.cnx_ec2.create_snapshot(volume_id, 'boto snapshot for %s' % name)
block_map = BlockDeviceMapping()
sda = BlockDeviceType()
sda.snapshot_id = snap.id
sda.ebs = True
root_device_name = '/dev/sda1'
block_map[root_device_name] = sda
return self.ctxt.cnx_ec2.register_image(name=name, architecture='x86_64', root_device_name=root_device_name, block_device_map=block_map, description=description)
示例8: create_mapping
def create_mapping(config):
if 'mapping' not in config:
return None
mapping = BlockDeviceMapping()
for ephemeral_name, device_path in config['mapping'].iteritems():
ephemeral = BlockDeviceType()
ephemeral.ephemeral_name = ephemeral_name
mapping[device_path] = ephemeral
return mapping
示例9: spotrequest
def spotrequest(ec2, nodetype, count, testname, testdate, threads, userdata = None):
global selected_availability_zone
maxprice = "0.40"
if nodetype == 'client':
instancetype = 'c3.xlarge'
else:
# For data nodes
instancetype = 'c3.4xlarge'
# Allow an explicit selection if needed...
#selected_availability_zone = "us-east-1e"
if not selected_availability_zone:
selected_availability_zone = random.choice([
'us-east-1a',
#'us-east-1b',
'us-east-1d',
'us-east-1e',
])
availability_zone = selected_availability_zone
if userdata == None:
userdata = """#!/bin/bash
echo {0} > /etc/node_testname
echo {1} > /etc/node_testdate
echo {2} > /etc/node_threads
echo {3} > /etc/node_role
#echo 10.136.71.116 > /etc/node_headnode
echo 400 > /etc/node_swap # MB of swap created
echo 1 > /etc/node_mongo_uselocal # Use local mongos shard server on each client
""".format(testname, testdate, threads, nodetype)
# For some tests we may not need any nodes of this type
if count == 0:
return []
# Default AMI
ami = 'ami-XXXXXXXX' # Current versions
# Specify ephemeral block devices...
bdmap = BlockDeviceMapping()
sdb = BlockDeviceType()
sdb.ephemeral_name = 'ephemeral0'
bdmap['/dev/sdb'] = sdb
sdc = BlockDeviceType()
sdc.ephemeral_name = 'ephemeral1'
bdmap['/dev/sdc'] = sdc
#sdd = BlockDeviceType()
#sdd.ephemeral_name = 'ephemeral2'
#bdmap['/dev/sdd'] = sdd
#sde = BlockDeviceType()
#sde.ephemeral_name = 'ephemeral3'
#bdmap['/dev/sde'] = sde
return ec2.request_spot_instances(maxprice, ami, count=count, launch_group=testdate, availability_zone_group=testdate, security_groups=['epstatic'], user_data=userdata, instance_type=instancetype, block_device_map=bdmap)
示例10: _create_device_map
def _create_device_map(ephemeral_disk_count):
"""Creates a block device out of the ephemeral disks on this instance."""
device_map = BlockDeviceMapping()
device_paths = _get_device_paths(ephemeral_disk_count)
for index, device_path in enumerate(device_paths):
device = BlockDeviceType()
device.ephemeral_name = "ephemeral{}".format(index)
device_map[device_path] = device
return device_map
示例11: _parse_block_device_mappings
def _parse_block_device_mappings(user_input):
"""
Parse block device mappings per AWS CLI tools syntax (modified to add IOPS)
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
Syntax:
/dev/xvd[a-z]=[snapshot-id|ephemeral]:[size in GB]:[Delete on Term]:[IOPS]
- Leave inapplicable fields blank
- Delete on Termination defaults to True
- IOPS limits are not validated
- EBS sizing is not validated
Mount an Ephemeral Drive:
/dev/xvdb1=ephemeral0
Mount multiple Ephemeral Drives:
/dev/xvdb1=ephemeral0,/dev/xvdb2=ephemeral1
Mount a Snapshot:
/dev/xvdp=snap-1234abcd
Mount a Snapshot to a 100GB drive:
/dev/xvdp=snap-1234abcd:100
Mount a Snapshot to a 100GB drive and do not delete on termination:
/dev/xvdp=snap-1234abcd:100:false
Mount a Fresh 100GB EBS device
/dev/xvdp=:100
Mount a Fresh 100GB EBS Device and do not delete on termination:
/dev/xvdp=:100:false
Mount a Fresh 100GB EBS Device with 1000 IOPS
/dev/xvdp=:100::1000
"""
block_device_map = BlockDeviceMapping()
mappings = user_input.split(",")
for mapping in mappings:
block_type = BlockDeviceType()
mount_point, drive_type, size, delete, iops = _parse_drive_mapping(mapping)
if 'ephemeral' in drive_type:
block_type.ephemeral_name = drive_type
elif 'snap' in drive_type:
block_type.snapshot_id = drive_type
block_type.volume_type = "standard"
else:
block_type.volume_type = "standard"
block_type.size = size
block_type.delete_on_termination = delete
if iops:
block_type.iops = iops
block_type.volume_type = "io1"
block_device_map[mount_point] = block_type
return block_device_map
示例12: launch_instance
def launch_instance(self):
if not self.verify_settings():
return
block_map = BlockDeviceMapping()
root_device = self.config["ec2_root_device"]
block_map[root_device] = EBSBlockDeviceType()
if self.config["ec2_size"]:
block_map[root_device].size = self.config["ec2_size"]
block_map[root_device].delete_on_termination = True
for num, device_location in enumerate(self.config["ec2_ephemeral_devices"]):
device = BlockDeviceType()
device.ephemeral_name = "ephemeral%d" % num
block_map[device_location] = device
reservation = self.conn.run_instances(
self.config["ec2_ami_id"],
key_name=self.config["ec2_key_name"],
security_groups=self.config["ec2_security_groups"] or [self.config["ec2_security_group"]],
instance_type=self.config["ec2_instance_type"],
placement=self.config["ec2_zone"],
monitoring_enabled=self.config["ec2_monitoring_enabled"],
block_device_map=block_map,
user_data=self.user_data,
)
self.instance = reservation.instances[0]
secs = RUN_INSTANCE_TIMEOUT
rest_interval = 5
while secs and not self.instance.state == "running":
time.sleep(rest_interval)
secs = secs - rest_interval
try:
self.instance.update()
except boto.exception.EC2ResponseError:
pass
if secs <= 0:
errmsg = "run instance %s failed after %d seconds" % (self.instance.id, RUN_INSTANCE_TIMEOUT)
LOG.error(errmsg)
else:
if self.config["hostname"]:
self.assign_name_tag()
msg1 = "Started Instance: {0}\n".format(self.instance.id)
LOG.info(msg1)
print msg1
p = int(self.config["ssh_port"])
port = "-p {0} ".format(p) if p and not p == 22 else ""
## change user to 'root' for all non-Ubuntu systems
user = self.config["sudouser"] if self.config["sudouser"] and self.config["ssh_import"] else "ubuntu"
# XXX - TODO: replace public dns with fqdn, where appropriate
msg2 = "To access: ssh {0}{1}@{2}\n" "To terminate: shaker-terminate {3}".format(
port, user, self.instance.public_dns_name, self.instance.id
)
LOG.info(msg2)
print msg2
示例13: demandrequest
def demandrequest(ec2, nodetype, testname, testdate, threads, userdata = None):
global selected_availability_zone
if nodetype == 'client':
instancetype = 'c3.xlarge'
else:
instancetype = 'i2.xlarge'
# Allow an explicit selection if needed...
#selected_availability_zone = 'us-east-1e'
if not selected_availability_zone:
selected_availability_zone = random.choice([
'us-east-1a',
#'us-east-1b',
'us-east-1d',
'us-east-1e',
])
availability_zone = selected_availability_zone
if userdata == None:
userdata = """#!/bin/bash
echo {0} > /etc/node_testname
echo {1} > /etc/node_testdate
echo {2} > /etc/node_threads
echo {3} > /etc/node_role
echo 10.136.71.116 > /etc/node_headnode
echo 400 > /etc/node_swap # MB of swap created
echo 1 > /etc/node_mongo_uselocal # Use local mongos shard server on each client
""".format(testname, testdate, threads, nodetype)
# Default AMI
ami = 'ami-XXXXXXXX' # Current versions
# Specify ephemeral block devices...
bdmap = BlockDeviceMapping()
sdb = BlockDeviceType()
sdb.ephemeral_name = 'ephemeral0'
bdmap['/dev/sdb'] = sdb
#sdc = BlockDeviceType()
#sdc.ephemeral_name = 'ephemeral1'
#bdmap['/dev/sdc'] = sdc
#sdd = BlockDeviceType()
#sdd.ephemeral_name = 'ephemeral2'
#bdmap['/dev/sdd'] = sdd
#sde = BlockDeviceType()
#sde.ephemeral_name = 'ephemeral3'
#bdmap['/dev/sde'] = sde
return ec2.run_instances(ami, placement=availability_zone, security_groups=['epstatic'], user_data=userdata, instance_type=instancetype, block_device_map=bdmap)
示例14: clone_instance
def clone_instance(instance):
new_bdm = None
ec2 = instance.connection
if instance.block_device_mapping:
root_device_name = instance.get_attribute('rootDeviceName')['rootDeviceName']
user_data = instance.get_attribute('userData')['userData']
# user_data comes back base64 encoded. Need to decode it so it
# can get re-encoded by run_instance !
user_data = base64.b64decode(user_data)
new_bdm = BlockDeviceMapping()
for dev in instance.block_device_mapping:
# if this entry is about the root device, skip it
if dev != root_device_name:
bdt = instance.block_device_mapping[dev]
if bdt.volume_id:
volume = ec2.get_all_volumes([bdt.volume_id])[0]
snaps = volume.snapshots()
if len(snaps) == 0:
print 'No snapshots available for %s' % volume.id
else:
# sort the list of snapshots, newest is at the end now
snaps.sort(key=lambda snap: snap.start_time)
latest_snap = snaps[-1]
new_bdt = BlockDeviceType()
new_bdt.snapshot_id = latest_snap.id
new_bdm[dev] = new_bdt
return ec2.run_instances(
instance.image_id,
key_name=instance.key_name,
security_groups=[g.name for g in instance.groups],
user_data=user_data,
instance_type=instance.instance_type,
kernel_id=instance.kernel,
ramdisk_id=instance.ramdisk,
monitoring_enabled=instance.monitored,
placement=instance.placement,
block_device_map=new_bdm
).instances[0]
示例15: _get_bmap
def _get_bmap(self, params):
bmap = BlockDeviceMapping()
for device in params['bmap']:
if not 'name' in device.keys():
self.logger.debug('bad device ' + str(device))
continue
dev = BlockDeviceType()
if 'size' in device.keys():
dev.size = device['size']
if 'delete_on_termination' in device.keys():
dev.delete_on_termination = device['delete_on_termination']
if 'ephemeral_name' in device.keys():
dev.ephemeral_name = device['ephemeral_name']
bmap[device['name']] = dev
return bmap