def _make_spot_request(self, num, instance_type, price, worker_ud): worker_ud_str = yaml.dump(worker_ud) reqs = None try: ec2_conn = self.get_ec2_connection() if self.get_subnet_id(): log.debug( "Making a spot instance request, using groups: %s, subnet=%s" % (self.get_security_group_ids(), self.get_subnet_id())) interface = boto.ec2.networkinterface.NetworkInterfaceSpecification( subnet_id=self.get_subnet_id(), groups=self.get_security_group_ids(), associate_public_ip_address=True) interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection( interface) reqs = ec2_conn.request_spot_instances( price=price, image_id=self.get_ami(), count=num, key_name=self.get_key_pair_name(), instance_type=instance_type, placement=self.get_zone(), user_data=worker_ud_str, network_interfaces=interfaces, ) else: log.debug( "Making a Spot request with the following command: " "ec2_conn.request_spot_instances(price='{price}', image_id='{iid}', " "count='{num}', key_name='{key}', security_groups=['{sgs}'], " "instance_type='{type}', placement='{zone}', user_data='{ud}')" .format(price=price, iid=self.get_ami(), num=num, key=self.get_key_pair_name(), sgs=", ".join(self.get_security_groups()), type=instance_type, zone=self.get_zone(), ud=worker_ud_str)) reqs = ec2_conn.request_spot_instances( price=price, image_id=self.get_ami(), count=num, key_name=self.get_key_pair_name(), security_groups=self.get_security_groups(), instance_type=instance_type, placement=self.get_zone(), user_data=worker_ud_str) if reqs is not None: for req in reqs: i = Instance(app=self.app, spot_request_id=req.id) log.debug("Adding Spot request {0} as an Instance".format( req.id)) self.app.manager.worker_instances.append(i) except EC2ResponseError, e: log.error("Trouble issuing a spot instance request: {0}".format( e.message)) return False
def _launch_instances(self, num, instance_type, worker_ud, min_num=1): """ Actually launch the `num` instance(s) of type `instance_type` and using the provided `worker_ud` dict that contains the instance user data. """ worker_ud_str = yaml.dump(worker_ud) try: reservation = None ec2_conn = self.get_ec2_connection() log.debug( "Starting instance(s) with the following command: ec2_conn.run_instances(" "image_id='{iid}', min_count='{min_num}', max_count='{num}', " "key_name='{key}', security_groups=['{sgs}'], " "user_data(with password/secret_key filtered out)=[{ud}], " "instance_type='{type}', placement='{zone}')".format( iid=self.get_ami(), min_num=min_num, num=num, key=self.get_key_pair_name(), sgs=", ".join(self.get_security_groups()), ud="\n".join([ '%s: %s' % (key, value) for key, value in worker_ud.iteritems() if key not in ['password', 'secret_key'] ]), type=instance_type, zone=self.get_zone())) reservation = ec2_conn.run_instances( image_id=self.get_ami(), min_count=min_num, max_count=num, key_name=self.get_key_pair_name(), security_groups=self.get_security_groups(), user_data=worker_ud_str, instance_type=instance_type, placement=self.get_zone()) # Occasionally, instances take a bit to register, so wait a few seconds time.sleep(3) if reservation: for instance in reservation.instances: i = Instance(app=self.app, inst=instance, m_state=instance.state) log.debug("Adding Instance %s to the list of workers" % instance) self.app.manager.worker_instances.append(i) log.debug("Started %s instance(s)" % num) return True except EC2ResponseError, e: err = "EC2 response error when starting worker nodes: %s" % str(e) log.error(err) return False
def _run_ondemand_instances(self, num, instance_type, spot_price, worker_ud, min_num=1): # log.debug("Setting boto's logger to DEBUG mode") # logging.getLogger('boto').setLevel(logging.DEBUG) worker_ud_str = yaml.dump(worker_ud) try: # log.debug( "Would be starting worker instance(s)..." ) reservation = None ec2_conn = self.get_ec2_connection() if self.running_in_vpc: log.debug("Starting instance(s) in VPC with the following command : ec2_conn.run_instances( " "image_id='{iid}', min_count='{min_num}', max_count='{num}', key_name='{key}', " "security_group_ids={sgs}, user_data(with sensitive info filtered out)=[{ud}], " "instance_type='{type}', placement='{zone}', subnet_id='{subnet_id}')" .format(iid=self.get_ami(), min_num=min_num, num=num, key=self.get_key_pair_name(), sgs=self.get_security_group_ids(), ud=("\n".join(['%s: %s' % (key, value) for key, value in worker_ud.iteritems() if key not in['password', 'freenxpass', 'secret_key']])), type=instance_type, zone=self.get_zone(), subnet_id=self.get_subnet_id())) interface = boto.ec2.networkinterface.NetworkInterfaceSpecification( subnet_id=self.get_subnet_id(), groups=self.get_security_group_ids(), associate_public_ip_address=True) interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface) reservation = ec2_conn.run_instances(image_id=self.get_ami(), min_count=min_num, max_count=num, key_name=self.get_key_pair_name(), user_data=worker_ud_str, instance_type=instance_type, network_interfaces=interfaces, ) else: log.debug("Starting instance(s) with the following command : ec2_conn.run_instances( " "image_id='{iid}', min_count='{min_num}', max_count='{num}', key_name='{key}', " "security_groups=['{sgs}'], user_data(with sensitive info filtered out)=[{ud}], " "instance_type='{type}', placement='{zone}')" .format(iid=self.get_ami(), min_num=min_num, num=num, key=self.get_key_pair_name(), sgs=", ".join(self.get_security_groups()), ud=("\n".join(['%s: %s' % (key, value) for key, value in worker_ud.iteritems() if key not in['password', 'freenxpass', 'secret_key']])), type=instance_type, zone=self.get_zone())) reservation = ec2_conn.run_instances(image_id=self.get_ami(), min_count=min_num, max_count=num, key_name=self.get_key_pair_name(), security_groups=self.get_security_groups(), user_data=worker_ud_str, instance_type=instance_type, placement=self.get_zone()) # Rarely, instances take a bit to register, # so wait a few seconds (although this is a very poor # 'solution') time.sleep(3) if reservation: for instance in reservation.instances: # At this point in the launch, tag only amazon instances if 'amazon' in self.app.config.get('cloud_name', 'amazon').lower(): self.add_tag(instance, 'clusterName', self.app.config['cluster_name']) self.add_tag(instance, 'role', worker_ud['role']) self.add_tag(instance, 'Name', "Worker: {0}".format(self.app.config['cluster_name'])) i = Instance(app=self.app, inst=instance, m_state=instance.state) log.debug("Adding Instance %s" % instance) self.app.manager.worker_instances.append(i) except EC2ResponseError, e: err = "EC2 response error when starting worker nodes: %s" % str(e) log.error(err) return False
def __setup_app(self, ud={}): self.app = TestApp(ud=ud) self.inst = MockBotoInstance() self.instance = Instance(self.app, inst=self.inst) self.app.manager.worker_instances = [self.instance]