本文整理汇总了Python中boto.ec2.blockdevicemapping.EBSBlockDeviceType.size方法的典型用法代码示例。如果您正苦于以下问题:Python EBSBlockDeviceType.size方法的具体用法?Python EBSBlockDeviceType.size怎么用?Python EBSBlockDeviceType.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.ec2.blockdevicemapping.EBSBlockDeviceType
的用法示例。
在下文中一共展示了EBSBlockDeviceType.size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_encryptor_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [as 别名]
def run_encryptor_instance(aws_svc, encryptor_image_id, snapshot, root_size,
guest_image_id, sg_id, update_ami=False):
bdm = BlockDeviceMapping()
guest_unencrypted_root = EBSBlockDeviceType(
volume_type='gp2',
snapshot_id=snapshot,
delete_on_termination=True)
# Use gp2 for fast burst I/O copying root drive
bdm['/dev/sda4'] = guest_unencrypted_root
if not update_ami:
log.info('Launching encryptor instance with snapshot %s', snapshot)
# They are creating an encrypted AMI instead of updating it
# Use gp2 for fast burst I/O copying root drive
guest_encrypted_root = EBSBlockDeviceType(
volume_type='gp2',
delete_on_termination=True)
guest_encrypted_root.size = 2 * root_size + 1
bdm['/dev/sda5'] = guest_encrypted_root
else:
log.info('Launching encryptor instance for updating %s',
guest_image_id)
guest_encrypted_root = EBSBlockDeviceType(
volume_type='gp2',
snapshot_id=snapshot,
delete_on_termination=True)
guest_encrypted_root.size = root_size
bdm['/dev/sda5'] = guest_encrypted_root
instance = aws_svc.run_instance(encryptor_image_id,
security_group_ids=[sg_id],
block_device_map=bdm)
aws_svc.create_tags(
instance.id,
name=NAME_ENCRYPTOR,
description=DESCRIPTION_ENCRYPTOR % {'image_id': guest_image_id}
)
instance = _wait_for_instance(aws_svc, instance.id)
log.info('Launched encryptor instance %s', instance.id)
# Tag volumes.
bdm = instance.block_device_mapping
if not update_ami:
aws_svc.create_tags(
bdm['/dev/sda5'].volume_id, name=NAME_ENCRYPTED_ROOT_VOLUME)
aws_svc.create_tags(
bdm['/dev/sda2'].volume_id, name=NAME_METAVISOR_ROOT_VOLUME)
aws_svc.create_tags(
bdm['/dev/sda1'].volume_id, name=NAME_METAVISOR_GRUB_VOLUME)
aws_svc.create_tags(
bdm['/dev/sda3'].volume_id, name=NAME_METAVISOR_LOG_VOLUME)
return instance
示例2: create_node
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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)
示例3: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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
示例4: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [as 别名]
def launch_instance(skip_updates=False):
'''
Launch an Oracle database instance.
'''
# Assume the keypair name is based on our env.key_filename.
instance_key_name = os.path.basename(env.key_filename).replace('.pem', '')
# Check that we have a security group configured already.
security_group_list = ec2_connection.get_all_security_groups()
security_group_found = False
for security_group in security_group_list:
if security_group.name == security_group_name:
security_group_found = True
break
# If we didn't find it, create it.
if not security_group_found:
create_security_group()
# We want a larger EBS root volume, so override /dev/sda1.
# Create an EBS device with 40GB allocated.
dev_root = EBSBlockDeviceType()
dev_root.size = 40
# Create the mapping.
dev_mapping = BlockDeviceMapping()
dev_mapping['/dev/sda1'] = dev_root
reservation = ec2_connection.run_instances(ami_id,
instance_type=instance_type, key_name=instance_key_name,
security_groups=[security_group_name],
block_device_map = dev_mapping)
# This is hacky but (mostly) works.
instance = reservation.instances[0]
print(green("Launching instance on reservation {}.".format(instance, reservation)))
'''
Wait for instance state to change;
if it doesn't change to running, then fail.
'''
print(yellow('Waiting for instance to start...'))
set_tags = False
while instance.state == u'pending':
# Try to set tags.
if set_tags == False:
try:
ec2_connection.create_tags([instance.id], {"Name": instance_name})
set_tags = True
print(green("Instance {} tagged.".format(instance)))
except EC2ResponseError, e:
print(red("Tagging failed; sleeping, updating instance, and trying again."))
# Check up on its status every so often
time.sleep(10)
instance.update()
示例5: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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 size [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 size [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: startInstance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [as 别名]
def startInstance(ec2connection, hardwareProfile, ARCH, RHEL, AMI, SSHKEYNAME):
conn_region = ec2connection
map = BlockDeviceMapping()
t = EBSBlockDeviceType()
t.size = "15"
# map = {'DeviceName':'/dev/sda','VolumeSize':'15'}
map["/dev/sda1"] = t
# blockDeviceMap = []
# blockDeviceMap.append( {'DeviceName':'/dev/sda', 'Ebs':{'VolumeSize' : '100'} })
if ARCH == "i386" and RHEL == "6.1":
reservation = conn_region.run_instances(
AMI, instance_type=hardwareProfile, key_name=SSHKEYNAME, block_device_map=map
)
elif ARCH == "x86_64" and RHEL == "6.1":
reservation = conn_region.run_instances(
AMI, instance_type=hardwareProfile, key_name=SSHKEYNAME, block_device_map=map
)
elif ARCH == "i386":
reservation = conn_region.run_instances(
AMI, instance_type=hardwareProfile, key_name=SSHKEYNAME, block_device_map=map
)
elif ARCH == "x86_64":
reservation = conn_region.run_instances(
AMI, instance_type=hardwareProfile, key_name=SSHKEYNAME, block_device_map=map
)
else:
print "arch type is neither i386 or x86_64.. will exit"
exit(1)
myinstance = reservation.instances[0]
time.sleep(5)
while not myinstance.update() == "running":
time.sleep(5)
print myinstance.update()
instanceDetails = myinstance.__dict__
pprint(instanceDetails)
# region = instanceDetails['placement']
# print 'region =' + region
publicDNS = instanceDetails["public_dns_name"]
print "public hostname = " + publicDNS
# check for console output here to make sure ssh is up
return publicDNS
示例9: create_server
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [as 别名]
def create_server():
"""
Creates EC2 Instance and saves it state in a local json file
"""
# looks for an existing 'data.json' file, so that we don't start
# additional ec2 instances when we don't need them.
#
if is_there_state():
return True
else:
conn = connect_to_ec2()
print(_green("Started..."))
print(_yellow("...Creating EC2 instance..."))
# we need a larger boot device to store our cached images
dev_sda1 = EBSBlockDeviceType()
dev_sda1.size = 120
bdm = BlockDeviceMapping()
bdm['/dev/sda1'] = dev_sda1
# get an ec2 ami image object with our choosen ami
image = conn.get_all_images(env.ec2_ami)[0]
# start a new instance
reservation = image.run(1, 1,
key_name=env.ec2_key_pair,
security_groups=env.ec2_security,
block_device_map = bdm,
instance_type=env.ec2_instancetype)
# and get our instance_id
instance = reservation.instances[0]
# add a tag to our instance
conn.create_tags([instance.id], {"Name": env.ec2_instance_name})
# and loop and wait until ssh is available
while instance.state == u'pending':
yellow("Instance state: %s" % instance.state)
sleep(10)
instance.update()
wait_for_ssh(instance.public_dns_name)
green("Instance state: %s" % instance.state)
green("Public dns: %s" % instance.public_dns_name)
# finally save the details or our new instance into the local state file
save_state_locally(instance.id)
示例10: startInstance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [as 别名]
def startInstance(self, ami, ec2_keyName, sec_group, hwp):
map = BlockDeviceMapping()
t = EBSBlockDeviceType()
t.size = '15'
#map = {'DeviceName':'/dev/sda','VolumeSize':'15'}
map['/dev/sda1'] = t
reservation = self.connection.run_instances(ami,
instance_type=hwp, key_name=ec2_keyName,
security_groups=sec_group, block_device_map=map)
myinstance = reservation.instances[0]
time.sleep(5)
while(not myinstance.update() == 'running'):
time.sleep(5)
print myinstance.update()
#pprint(instanceDetails)
return myinstance
示例11: parse_block_device_args
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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
示例12: launch_instance
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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()
示例13: launch_cluster
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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: build
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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:
#.........这里部分代码省略.........
示例15: launch_cluster
# 需要导入模块: from boto.ec2.blockdevicemapping import EBSBlockDeviceType [as 别名]
# 或者: from boto.ec2.blockdevicemapping.EBSBlockDeviceType import size [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)
#.........这里部分代码省略.........