本文整理汇总了Python中boto.ec2.blockdevicemapping.EBSBlockDeviceType.delete_on_termination方法的典型用法代码示例。如果您正苦于以下问题:Python EBSBlockDeviceType.delete_on_termination方法的具体用法?Python EBSBlockDeviceType.delete_on_termination怎么用?Python EBSBlockDeviceType.delete_on_termination使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.ec2.blockdevicemapping.EBSBlockDeviceType
的用法示例。
在下文中一共展示了EBSBlockDeviceType.delete_on_termination方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register_ebs_ami
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def register_ebs_ami(self, snapshot_id, arch = 'x86_64', default_ephem_map = True,
img_name = None, img_desc = None):
# register against snapshot
try:
aki=PVGRUB_AKIS[self.region.name][arch]
except KeyError:
raise Exception("Unable to determine pvgrub hd00 AKI for region (%s) arch (%s)" % (self.region.name, arch))
if not img_name:
rand_id = random.randrange(2**32)
# These names need to be unique, hence the pseudo-uuid
img_name='EBSHelper AMI - %s - uuid-%x' % (snapshot_id, rand_id)
if not img_desc:
img_desc='Created directly from volume snapshot %s' % (snapshot_id)
self.log.debug("Registering snapshot (%s) as new EBS AMI" % (snapshot_id))
ebs = EBSBlockDeviceType()
ebs.snapshot_id = snapshot_id
ebs.delete_on_termination = True
block_map = BlockDeviceMapping()
block_map['/dev/sda'] = ebs
# The ephemeral mappings are automatic with S3 images
# For EBS images we need to make them explicit
# These settings are required to make the same fstab work on both S3 and EBS images
if default_ephem_map:
e0 = EBSBlockDeviceType()
e0.ephemeral_name = 'ephemeral0'
e1 = EBSBlockDeviceType()
e1.ephemeral_name = 'ephemeral1'
block_map['/dev/sdb'] = e0
block_map['/dev/sdc'] = e1
result = self.conn.register_image(name=img_name, description=img_desc,
architecture=arch, kernel_id=aki,
root_device_name='/dev/sda', block_device_map=block_map)
return str(result)
示例2: _register_image
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def _register_image(self, snapshot_id):
conn = self.platform.new_ec2_conn()
instance_id = self.platform.get_instance_id()
instance = conn.get_all_instances([instance_id])[0].instances[0]
block_device_map = BlockDeviceMapping(conn)
root_vol = EBSBlockDeviceType(snapshot_id=snapshot_id)
root_vol.delete_on_termination = True
# Adding ephemeral devices
for eph, device in EPH_STORAGE_MAPPING[linux.os['arch']].items():
bdt = EBSBlockDeviceType(conn)
bdt.ephemeral_name = eph
block_device_map[device] = bdt
root_partition = instance.root_device_name[:-1]
if root_partition in self.platform.get_block_device_mapping().values():
block_device_map[root_partition] = root_vol
else:
block_device_map[instance.root_device_name] = root_vol
return conn.register_image(
name=self.image_name,
root_device_name=instance.root_device_name,
block_device_map=block_device_map,
kernel_id=instance.kernel,
virtualization_type=instance.virtualization_type,
ramdisk_id=self.platform.get_ramdisk_id(),
architecture=instance.architecture)
示例3: create_node
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def create_node(self, name, distribution, metadata={}):
size = self._default_size
disk_size = 8
with start_action(
action_type=u"flocker:provision:aws:create_node",
name=name,
distribution=distribution,
image_size=size,
disk_size=disk_size,
metadata=metadata,
):
metadata = metadata.copy()
metadata["Name"] = name
disk1 = EBSBlockDeviceType()
disk1.size = disk_size
disk1.delete_on_termination = True
diskmap = BlockDeviceMapping()
diskmap["/dev/sda1"] = disk1
images = self._connection.get_all_images(filters={"name": IMAGE_NAMES[distribution]})
# Retry several times, no sleep between retries is needed.
instance = poll_until(
lambda: self._get_node(images[0].id, size, diskmap, metadata), repeat(0, 10), lambda x: None
)
return AWSNode(name=name, _provisioner=self, _instance=instance, distribution=distribution)
示例4: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_instance(self):
if not self.verify_settings():
return
is_instance_store = self.conn.get_all_images(self.config['ec2_ami_id'], filters={'root-device-type': 'instance-store'})
if is_instance_store:
block_map = None
else:
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
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'],
placement_group=self.config['ec2_placement_group'],
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 {0} failed after {1} seconds".format(
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".format(
'-p {0} '.format(port) if port else '',
user,
self.instance.public_dns_name)
msg3 = "To terminate: shaker-terminate {0}".format(
self.instance.id)
LOG.info(msg2)
LOG.info(msg3)
print msg2
print msg3
示例5: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
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
示例6: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_instance(self):
if not self.verify_settings():
return
is_instance_store = self.conn.get_all_images(self.config['ec2_ami_id'], filters={'root-device-type': 'instance-store'})
if is_instance_store:
block_map = None
else:
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
opts = {
'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'],
'placement_group': self.config['ec2_placement_group'],
'monitoring_enabled': self.config['ec2_monitoring_enabled'],
'block_device_map': block_map,
'user_data': self.user_data
}
if self.config.get('ec2_subnet_id',False):
# when providing subnet_id, must use security_group_ids and not
# named security_groups or API call will fail.
opts.pop('security_groups',None)
opts['security_group_ids'] = self.config['ec2_security_group_ids'] or [self.config['ec2_security_group_id']]
if not opts['security_group_ids']:
raise AssertionError('Must specify ec2_security_group_id or ec2_security_group_ids with subnet_id')
opts['subnet_id'] = self.config['ec2_subnet_id']
reservation = self.conn.run_instances(self.config['ec2_ami_id'], **opts)
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 {0} failed after {1} seconds".format(
self.instance.id, RUN_INSTANCE_TIMEOUT)
LOG.error(errmsg)
else:
if self.config['hostname']:
self.assign_name_tag()
示例7: get_block_device
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def get_block_device(instance_type, ebs_vol_size):
block_map = BlockDeviceMapping()
if ebs_vol_size > 0:
device = EBSBlockDeviceType()
device.size = ebs_vol_size
device.delete_on_termination = True
block_map['/dev/sdv'] = device
for i in range(get_num_disks(instance_type)):
dev = BlockDeviceType()
dev.ephemeral_name = 'ephemeral%d' % i
# The first ephemeral drive is /dev/sdb.
name = '/dev/sd' + string.ascii_letters[i + 1]
block_map[name] = dev
return block_map
示例8: register_ebs_ami
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def register_ebs_ami(self, snapshot_id, arch="x86_64", default_ephem_map=True, img_name=None, img_desc=None):
# register against snapshot
try:
aki = PVGRUB_AKIS[self.region.name][arch]
except KeyError:
raise Exception("Unable to find pvgrub hd00 AKI for %s, arch (%s)" % (self.region.name, arch))
if not img_name:
rand_id = random.randrange(2 ** 32)
# These names need to be unique, hence the pseudo-uuid
img_name = "EBSHelper AMI - %s - uuid-%x" % (snapshot_id, rand_id)
if not img_desc:
img_desc = "Created directly from volume snapshot %s" % snapshot_id
self.log.debug("Registering %s as new EBS AMI" % snapshot_id)
self.create_sgroup("ec2helper-vnc-ssh-%x" % random.randrange(2 ** 32), allow_vnc=True)
ebs = EBSBlockDeviceType()
ebs.snapshot_id = snapshot_id
ebs.delete_on_termination = True
block_map = BlockDeviceMapping()
block_map["/dev/sda"] = ebs
# The ephemeral mappings are automatic with S3 images
# For EBS images we need to make them explicit
# These settings are required to make the same fstab work on both S3
# and EBS images
if default_ephem_map:
e0 = EBSBlockDeviceType()
e0.ephemeral_name = "ephemeral0"
e1 = EBSBlockDeviceType()
e1.ephemeral_name = "ephemeral1"
block_map["/dev/sdb"] = e0
block_map["/dev/sdc"] = e1
result = self.conn.register_image(
name=img_name,
description=img_desc,
architecture=arch,
kernel_id=aki,
root_device_name="/dev/sda",
block_device_map=block_map,
)
sleep(10)
new_amis = self.conn.get_all_images([result])
new_amis[0].add_tag("Name", resource_tag)
return str(result)
示例9: parse_block_device_args
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def parse_block_device_args(self, block_device_maps_args):
block_device_map = BlockDeviceMapping()
for block_device_map_arg in block_device_maps_args:
parts = block_device_map_arg.split('=')
if len(parts) > 1:
device_name = parts[0]
block_dev_type = EBSBlockDeviceType()
value_parts = parts[1].split(':')
if value_parts[0].startswith('snap'):
block_dev_type.snapshot_id = value_parts[0]
else:
if value_parts[0].startswith('ephemeral'):
block_dev_type.ephemeral_name = value_parts[0]
if len(value_parts) > 1:
block_dev_type.size = int(value_parts[1])
if len(value_parts) > 2:
if value_parts[2] == 'true':
block_dev_type.delete_on_termination = True
block_device_map[device_name] = block_dev_type
return block_device_map
示例10: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_instance(self):
if not self.verify_settings():
return
is_instance_store = self.conn.get_all_images(self.config['ec2_ami_id'], filters={'root-device-type': 'instance-store'})
if is_instance_store:
block_map = None
else:
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
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'],
placement_group=self.config['ec2_placement_group'],
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 {0} failed after {1} seconds".format(
self.instance.id, RUN_INSTANCE_TIMEOUT)
LOG.error(errmsg)
else:
if self.config['hostname']:
self.assign_name_tag()
示例11: launch_cluster
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_cluster(conn, opts, cluster_name):
print "Setting up security groups..."
master_group = get_or_make_group(conn, "shark-exp-master")
slave_group = get_or_make_group(conn, "shark-exp-slaves")
zoo_group = get_or_make_group(conn, "ampcamp-zoo")
if master_group.rules == []: # Group was just now created
master_group.authorize(src_group=master_group)
master_group.authorize(src_group=slave_group)
master_group.authorize(src_group=zoo_group)
master_group.authorize('tcp', 22, 22, '0.0.0.0/0')
master_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
if opts.cluster_type == "mesos":
master_group.authorize('tcp', 50030, 50030, '0.0.0.0/0')
master_group.authorize('tcp', 50070, 50070, '0.0.0.0/0')
master_group.authorize('tcp', 60070, 60070, '0.0.0.0/0')
master_group.authorize('tcp', 38090, 38090, '0.0.0.0/0')
# hbase
master_group.authorize('tcp', 60010, 60010, '0.0.0.0/0')
master_group.authorize('tcp', 60050, 60050, '0.0.0.0/0')
if slave_group.rules == []: # Group was just now created
slave_group.authorize(src_group=master_group)
slave_group.authorize(src_group=slave_group)
slave_group.authorize(src_group=zoo_group)
slave_group.authorize('tcp', 22, 22, '0.0.0.0/0')
slave_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
if opts.cluster_type == "mesos":
slave_group.authorize('tcp', 50060, 50060, '0.0.0.0/0')
slave_group.authorize('tcp', 50075, 50075, '0.0.0.0/0')
slave_group.authorize('tcp', 60060, 60060, '0.0.0.0/0')
slave_group.authorize('tcp', 60075, 60075, '0.0.0.0/0')
# hbase
slave_group.authorize('tcp', 60050, 60050, '0.0.0.0/0')
if zoo_group.rules == []: # Group was just now created
zoo_group.authorize(src_group=master_group)
zoo_group.authorize(src_group=slave_group)
zoo_group.authorize(src_group=zoo_group)
zoo_group.authorize('tcp', 22, 22, '0.0.0.0/0')
zoo_group.authorize('tcp', 2181, 2181, '0.0.0.0/0')
zoo_group.authorize('tcp', 2888, 2888, '0.0.0.0/0')
zoo_group.authorize('tcp', 3888, 3888, '0.0.0.0/0')
# Check if instances are already running in our groups
print "Checking for running cluster..."
reservations = conn.get_all_instances()
for res in reservations:
for instance in res.instances:
if 'tags' in instance.__dict__ and 'cluster' in instance.tags:
if instance.tags['cluster'] == cluster_name and is_active(instance):
print >> stderr, ("ERROR: Instances %s is already running in cluster %s"
% (instance.id, cluster_name))
sys.exit(1)
if opts.ami in ["latest", "standalone"]:
opts.ami = get_ami(opts.ami)
print "Launching instances..."
try:
image = conn.get_all_images(image_ids=[opts.ami])[0]
except:
print >> stderr, "Could not find AMI " + opts.ami
sys.exit(1)
# Create block device mapping so that we can add an EBS volume if asked to
block_map = BlockDeviceMapping()
if opts.ebs_vol_size > 0:
device = EBSBlockDeviceType()
device.size = opts.ebs_vol_size
device.delete_on_termination = True
block_map["/dev/sdv"] = device
# Launch slaves
if opts.spot_price != None:
# Launch spot instances with the requested price
print ("Requesting %d slaves as spot instances with price $%.3f" %
(opts.slaves, opts.spot_price))
slave_reqs = conn.request_spot_instances(
price = opts.spot_price,
image_id = opts.ami,
launch_group = "launch-group-%s" % cluster_name,
placement = opts.zone,
count = opts.slaves,
key_name = opts.key_pair,
security_groups = [slave_group],
instance_type = opts.instance_type,
block_device_map = block_map)
my_req_ids = [req.id for req in slave_reqs]
print "Waiting for spot instances to be granted..."
while True:
time.sleep(10)
reqs = conn.get_all_spot_instance_requests()
id_to_req = {}
for r in reqs:
id_to_req[r.id] = r
active = 0
instance_ids = []
for i in my_req_ids:
if id_to_req[i].state == "active":
active += 1
#.........这里部分代码省略.........
示例12: build
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def build(hosts, cred, dry, inventory='hosts'):
hret = {}
old_state = {}
con = None
for h in hosts:
logger.info(" Run action on host [%s]" % (h))
hret[h] = {}
hv = {}
hv = vmbuilder.utils.load_host_vars(h, inventory=inventory)
hvars = hv['VM_PROVIDER']
if con is None:
con = _connect(hvars['region'], cred)
reservations = con.get_all_reservations(filters={"tag:Name": h})
old_state[h] = "absent"
for reservation in reservations:
instance = reservation.instances[0]
if instance.state != 'terminated':
hret[h]['instance'] = instance
old_state[h] = "present"
logger.info(" Server [%s] is already present" % (h))
if old_state[h] == 'present':
continue
bdm = None
if 'disk_size' in hvars:
try:
dev_sda1 = EBSBlockDeviceType()
dev_sda1.size = hvars['disk_size']
dev_sda1.delete_on_termination = True
bdm = BlockDeviceMapping()
bdm['/dev/sda1'] = dev_sda1
except Exception as e:
logger.error("Error building block device for server: %s" % (e))
exit(1)
try:
reservation = con.run_instances(
hvars['ami'],
key_name=hvars['key'],
instance_type=hvars['vmtype'],
security_group_ids=[hvars['security']],
subnet_id=hvars['subnet'],
block_device_map=bdm,
dry_run=dry
)
hret[h]['instance'] = reservation.instances[0]
except Exception as e:
logger.error("Error building server: %s" % (e))
exit(1)
for h in hosts:
hv = vmbuilder.utils.load_host_vars(h, inventory=inventory)
hvars = hv['VM_PROVIDER']
instance = hret[h]['instance']
status = instance.update()
if old_state[h] == 'absent':
logger.info(" Waiting for [%s] to be launched..." % (h))
while status == 'pending':
time.sleep(5)
status = instance.update()
if old_state[h] == 'present':
logger.info(" State is running with IP [%s]" % (instance.private_ip_address))
elif status == 'running':
logger.info(" State changed to running with IP [%s]" % (instance.private_ip_address))
else:
logger.error(" Status of [%s] is [%s]" % (h, status))
instance.add_tag("Name", "%s" % (h))
for cur_tag in hvars['tags']:
instance.add_tag(cur_tag, hvars['tags'][cur_tag])
if 'extra_disks' in hvars and old_state[h] == 'absent':
try:
for cur_disk in hvars['extra_disks']:
cur_vol = con.create_volume(cur_disk['size'], instance.placement)
status = cur_vol.status
while status != 'available':
logger.info(" Waiting for volume [%s] to be launched..." % (cur_vol))
time.sleep(10)
status = cur_vol.update()
con.attach_volume(cur_vol.id, instance.id, '/dev/' + cur_disk['device'])
except Exception as e:
logger.error("Error Attaching new disks: %s" % (e))
exit(1)
instance_volumes = con.get_all_volumes(filters={'attachment.instance-id': instance.id})
for counter, cur_vol in enumerate(instance_volumes):
cur_vol.add_tag("Name", "%s_disk%d" % (h.split('.')[0], counter))
hret[h]['private_ip_address'] = instance.private_ip_address
# If requested assosiate an new elastic IP for the host and create a security group to whitelist external IPs
if 'assosiate_eip' in hvars and hvars['assosiate_eip'] is True:
if instance.ip_address is None:
eip = con.allocate_address()
con.associate_address(instance.id, eip.public_ip)
logger.info(" Adding public IP [%s]" % (eip.public_ip))
hret[h]['public_ip_address'] = eip.public_ip
if 'whitelisted_ips' in hvars:
#.........这里部分代码省略.........
示例13: launch_cluster
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_cluster(conn, opts, cluster_name):
print "Setting up security groups..."
master_group = get_or_make_group(conn, cluster_name + "-master")
slave_group = get_or_make_group(conn, cluster_name + "-slaves")
zoo_group = get_or_make_group(conn, cluster_name + "-zoo")
if master_group.rules == []: # Group was just now created
master_group.authorize(src_group=master_group)
master_group.authorize(src_group=slave_group)
master_group.authorize(src_group=zoo_group)
master_group.authorize('tcp', 22, 22, '0.0.0.0/0')
master_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
master_group.authorize('tcp', 50030, 50030, '0.0.0.0/0')
master_group.authorize('tcp', 50070, 50070, '0.0.0.0/0')
master_group.authorize('tcp', 60070, 60070, '0.0.0.0/0')
master_group.authorize('tcp', 38090, 38090, '0.0.0.0/0')
if slave_group.rules == []: # Group was just now created
slave_group.authorize(src_group=master_group)
slave_group.authorize(src_group=slave_group)
slave_group.authorize(src_group=zoo_group)
slave_group.authorize('tcp', 22, 22, '0.0.0.0/0')
slave_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
slave_group.authorize('tcp', 50060, 50060, '0.0.0.0/0')
slave_group.authorize('tcp', 50075, 50075, '0.0.0.0/0')
slave_group.authorize('tcp', 60060, 60060, '0.0.0.0/0')
slave_group.authorize('tcp', 60075, 60075, '0.0.0.0/0')
if zoo_group.rules == []: # Group was just now created
zoo_group.authorize(src_group=master_group)
zoo_group.authorize(src_group=slave_group)
zoo_group.authorize(src_group=zoo_group)
zoo_group.authorize('tcp', 22, 22, '0.0.0.0/0')
zoo_group.authorize('tcp', 2181, 2181, '0.0.0.0/0')
zoo_group.authorize('tcp', 2888, 2888, '0.0.0.0/0')
zoo_group.authorize('tcp', 3888, 3888, '0.0.0.0/0')
# Check if instances are already running in our groups
print "Checking for running cluster..."
reservations = conn.get_all_instances()
for res in reservations:
group_names = [g.id for g in res.groups]
if master_group.name in group_names or slave_group.name in group_names or zoo_group.name in group_names:
active = [i for i in res.instances if is_active(i)]
if len(active) > 0:
print >> stderr, ("ERROR: There are already instances running in " +
"group %s, %s or %s" % (master_group.name, slave_group.name, zoo_group.name))
sys.exit(1)
print "Launching instances..."
try:
image = conn.get_all_images(image_ids=[opts.ami])[0]
except:
print >> stderr, "Could not find AMI " + opts.ami
sys.exit(1)
# Create block device mapping so that we can add an EBS volume if asked to
block_map = BlockDeviceMapping()
if opts.ebs_vol_size > 0:
device = EBSBlockDeviceType()
device.size = opts.ebs_vol_size
device.delete_on_termination = True
block_map["/dev/sdv"] = device
# Launch slaves
if opts.spot_price != None:
# Launch spot instances with the requested price
print ("Requesting %d slaves as spot instances with price $%.3f" %
(opts.slaves, opts.spot_price))
slave_reqs = conn.request_spot_instances(
price = opts.spot_price,
image_id = opts.ami,
launch_group = "launch-group-%s" % cluster_name,
placement = opts.zone,
count = opts.slaves,
key_name = opts.key_pair,
security_groups = [slave_group],
instance_type = opts.instance_type,
block_device_map = block_map)
my_req_ids = [req.id for req in slave_reqs]
print "Waiting for spot instances to be granted..."
while True:
time.sleep(10)
reqs = conn.get_all_spot_instance_requests()
id_to_req = {}
for r in reqs:
id_to_req[r.id] = r
active = 0
instance_ids = []
for i in my_req_ids:
if id_to_req[i].state == "active":
active += 1
instance_ids.append(id_to_req[i].instance_id)
if active == opts.slaves:
print "All %d slaves granted" % opts.slaves
reservations = conn.get_all_instances(instance_ids)
slave_nodes = []
for r in reservations:
slave_nodes += r.instances
break
else:
print "%d of %d slaves granted, waiting longer" % (active, opts.slaves)
else:
# Launch non-spot instances
#.........这里部分代码省略.........
示例14: node_install
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def node_install(cn=def_cn,inst_type_idx=def_inst_type,idn=0,
avz=def_default_avz,rt=def_default_requesttype,
group_name='oggmssh',
ssh_port=22,
cidr='0.0.0.0/0'):
"""
Request and prepare single instance
"""
# FSO---connect
cloud = boto.ec2.connect_to_region(avz[:-1],profile_name=ec2Profile)
aminfo = cloud.get_image(def_ami[avz[:-1]])
# FSO---check if node with same name already exists
if node_exists(cn + '_node' + str(idn)):
print("Node already exists")
sys.exit()
# Check if ssh keypair exists
key_name = get_keypair_name()
check_keypair(cloud, key_name)
# FSO---create a bigger root device
dev_sda1 = EBSBlockDeviceType()
dev_sda1.size = rootfs_size_gb
dev_sda1.delete_on_termination = True
bdm = BlockDeviceMapping()
bdm['/dev/sda1'] = dev_sda1
dev_sdf_vol = get_user_persist_ebs(cloud, avz)
# Check to see if specified security group already exists.
# If we get an InvalidGroup.NotFound error back from EC2,
# it means that it doesn't exist and we need to create it.
try:
group = cloud.get_all_security_groups(groupnames=[group_name])[0]
except cloud.ResponseError as e:
if e.code == 'InvalidGroup.NotFound':
print('Creating Security Group: %s' % group_name)
# Create a security group to control access to instance via SSH.
group = cloud.create_security_group(group_name, 'A group that allows SSH access')
else:
raise
# Add a rule to the security group to authorize SSH traffic
# on the specified port.
try:
group.authorize('tcp', ssh_port, ssh_port, cidr)
except cloud.ResponseError as e:
if e.code == 'InvalidPermission.Duplicate':
print('Security Group: %s already authorized' % group_name)
else:
raise
log_with_ts("request node "+str(idn))
print('Reserving instance for node', aminfo.id, instance_infos[inst_type_idx]['type'], aminfo.name, aminfo.region)
if rt == 'spot':
print("placing node in ",avz)
requests = cloud.request_spot_instances(def_price,
def_ami[avz[:-1]],
count=1,
type='one-time',
security_groups=[group_name],
key_name=key_name,
placement=avz,
instance_type=instance_infos[inst_type_idx]['type'],
block_device_map=bdm)
req_ids = [request.id for request in requests]
instance_ids = wait_for_fulfillment(cloud,req_ids)
instances = cloud.get_only_instances(instance_ids=instance_ids)
node = instances[0]
log_with_ts("fullfilled spot node "+str(idn))
else:
print("placing node in ",avz)
reservation = cloud.run_instances(image_id=def_ami[avz[:-1]],
key_name=key_name,
placement = avz,
security_groups=[group_name],
instance_type=instance_infos[inst_type_idx]['type'],
block_device_map= bdm)
node = reservation.instances[0]
log_with_ts("fullfilled ondemand node "+str(idn))
time.sleep(2)
while not node.update() == 'running':
print('waiting for', cn, 'node', idn, 'to boot...')
time.sleep(5)
log_with_ts("booted node "+str(idn))
if dev_sdf_vol is not None:
cloud.attach_volume(dev_sdf_vol.id, node.id, "/dev/sdf")
node.add_tag('Name', cn+'_node'+str(idn))
node.add_tag('type', cn+'node')
node.add_tag('node-owner', user_identifier)
# FSO---set delete on termination flag to true for ebs block device
node.modify_attribute('blockDeviceMapping', { '/dev/sda1' : True })
#.........这里部分代码省略.........
示例15: launch_cluster
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import delete_on_termination [as 别名]
def launch_cluster(conn, opts, cluster_name):
conn = AWSConnection(conn, VPCConnection(region=conn.region))
print "Setting up VPC..."
vpc = get_or_make_vpc(conn, cluster_name, 'mesos-vpc')
print "Using vpc: %s" % (vpc.id)
print "Setting up subnet..."
subnet = get_or_make_subnet(conn, vpc.id, opts.zone, cluster_name, 'mesos-subnet')
print "Using subnet: %s" % (subnet.id)
# Add internet gateway to VPC.
print "Creating internet gateway"
ig = get_or_make_ig(conn, vpc.id, cluster_name, 'mesos-vpc')
print "Using internet gateway: %s" % (ig.id)
# Add route to route table
rt = get_or_make_rt(conn, vpc.id, cluster_name, 'mesos-rt')
conn.vpc.create_route(rt.id, '0.0.0.0/0', gateway_id=ig.id)
print "Setting up security groups..."
master_group = get_or_make_group(conn, cluster_name, vpc.id, "mesos-masters")
slave_group = get_or_make_group(conn, cluster_name, vpc.id, "mesos-slaves")
zoo_group = get_or_make_group(conn, cluster_name, vpc.id, "mesos-zoo")
if master_group.rules == []: # Group was just now created
master_group.authorize('tcp', 22, 22, '0.0.0.0/0')
master_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
master_group.authorize('tcp', 50030, 50030, '0.0.0.0/0')
master_group.authorize('tcp', 50070, 50070, '0.0.0.0/0')
master_group.authorize('tcp', 60070, 60070, '0.0.0.0/0')
master_group.authorize('tcp', 38090, 38090, '0.0.0.0/0')
if slave_group.rules == []: # Group was just now created
slave_group.authorize('tcp', 22, 22, '0.0.0.0/0')
slave_group.authorize('tcp', 8080, 8081, '0.0.0.0/0')
slave_group.authorize('tcp', 50060, 50060, '0.0.0.0/0')
slave_group.authorize('tcp', 50075, 50075, '0.0.0.0/0')
slave_group.authorize('tcp', 60060, 60060, '0.0.0.0/0')
slave_group.authorize('tcp', 60075, 60075, '0.0.0.0/0')
if zoo_group.rules == []: # Group was just now created
zoo_group.authorize('tcp', 22, 22, '0.0.0.0/0')
zoo_group.authorize('tcp', 2181, 2181, '0.0.0.0/0')
zoo_group.authorize('tcp', 2888, 2888, '0.0.0.0/0')
zoo_group.authorize('tcp', 3888, 3888, '0.0.0.0/0')
# Check if instances are already running in our groups
print "Checking for running cluster..."
reservations = conn.ec2.get_all_instances()
for res in reservations:
group_names = [g.name for g in res.groups]
if master_group.name in group_names or slave_group.name in group_names or zoo_group.name in group_names:
active = [i for i in res.instances if is_active(i)]
if len(active) > 0:
print >> stderr, ("ERROR: There are already instances running in " +
"group %s, %s or %s" % (master_group.name, slave_group.name, zoo_group.name))
sys.exit(1)
print "Launching instances..."
if opts.ami == "latest":
# Figure out the latest AMI from our static URL
try:
opts.ami = urllib2.urlopen(LATEST_AMI_URL).read().strip()
except:
print >> stderr, "Could not read " + LATEST_AMI_URL
try:
image = conn.ec2.get_all_images(image_ids=[opts.ami])[0]
except:
print >> stderr, "Could not find AMI " + opts.ami
sys.exit(1)
# Create block device mapping so that we can add an EBS volume if asked to
block_map = BlockDeviceMapping()
if opts.ebs_vol_size > 0:
device = EBSBlockDeviceType()
device.size = opts.ebs_vol_size
device.delete_on_termination = True
block_map["/dev/sdv"] = device
# Launch slaves
if opts.spot_price != None:
# Launch spot instances with the requested price
print ("Requesting %d slaves as spot instances with price $%.3f" %
(opts.slaves, opts.spot_price))
slave_reqs = conn.ec2.request_spot_instances(
price = opts.spot_price,
image_id = opts.ami,
launch_group = "launch-group-%s" % cluster_name,
placement = opts.zone,
count = opts.slaves,
key_name = opts.key_pair,
security_groups = [slave_group],
instance_type = opts.instance_type,
block_device_map = block_map)
my_req_ids = [req.id for req in slave_reqs]
print "Waiting for spot instances to be granted..."
while True:
time.sleep(10)
#.........这里部分代码省略.........