Пример #1
0
 def _provision_on_demand_instances(
         provision_parameters: ProvisionParameters,
         instance_parameters: AWSInstanceParams, count: int,
         tags: List[TagsType]) -> List[Instance]:
     instance_parameters_dict = instance_parameters.dict(
         exclude_none=True,
         exclude_defaults=True,
         exclude_unset=True,
         encode_user_data=False)
     LOGGER.info(
         "[%s] Creating {count} on-demand instances using AMI id '%s' with following parameters:\n%s",
         provision_parameters.region_name, instance_parameters.ImageId,
         instance_parameters_dict)
     instances = ec2_services[
         provision_parameters.region_name].create_instances(
             **instance_parameters_dict, MinCount=count, MaxCount=count)
     LOGGER.info("Created instances: %s.", instances)
     if instances:
         for ind, instance in enumerate(instances):
             instance_tags = tags.pop()
             set_tags_on_instances(
                 region_name=provision_parameters.region_name,
                 instance_ids=[instance.instance_id],
                 tags={
                     'Name':
                     'spot_fleet_{}_{}'.format(instance.instance_id, ind)
                 } | instance_tags,
             )
     return instances
Пример #2
0
 def _execute_spot_instance_request(
         self,
         provision_parameters: ProvisionParameters,
         instance_parameters: AWSInstanceParams,
         count: int,
         tags: List[TagsType]) -> List[Instance]:
     request_ids = create_spot_instance_request(
         region_name=provision_parameters.region_name,
         count=count,
         price=getattr(provision_parameters, 'price', None),
         instance_parameters=instance_parameters.dict(exclude_none=True, exclude_unset=True, exclude_defaults=True),
         full_availability_zone=self._full_availability_zone_name(provision_parameters),
         valid_until=self._spot_valid_until,
         duration=self._spot_block_duration(provision_parameters),
     )
     instance_ids = wait_for_provision_request_done(
         region_name=provision_parameters.region_name,
         request_ids=request_ids,
         is_fleet=False,
     )
     if not instance_ids:
         with contextlib.suppress(Exception):
             self._ec2_client(provision_parameters).cancel_spot_instance_requests(SpotInstanceRequestIds=request_ids)
         return []
     for ind, instance_id in enumerate(instance_ids):
         instance_tags = tags.pop()
         set_tags_on_instances(
             region_name=provision_parameters.region_name,
             instance_ids=[instance_id],
             tags={'Name': 'spot_{}_{}'.format(instance_id, ind)} | instance_tags,
         )
     self._ec2_client(provision_parameters).cancel_spot_instance_requests(SpotInstanceRequestIds=request_ids)
     return [find_instance_by_id(
         region_name=provision_parameters.region_name, instance_id=instance_id) for instance_id in instance_ids]
Пример #3
0
 def _instance_parameters(self, region_id: int) -> AWSInstanceParams:
     params_builder = self._INSTANCE_PARAMS_BUILDER(  # pylint: disable=not-callable
         params=self.params,
         region_id=region_id,
         user_data_raw=self._user_data)
     return AWSInstanceParams(**params_builder.dict(
         exclude_none=True, exclude_unset=True, exclude_defaults=True))
Пример #4
0
 def _execute_spot_fleet_instance_request(
         self,
         provision_parameters: ProvisionParameters,
         instance_parameters: AWSInstanceParams,
         count: int,
         tags: List[TagsType]) -> List[Instance]:
     request_id = create_spot_fleet_instance_request(
         region_name=provision_parameters.region_name,
         count=count,
         price=provision_parameters.price,
         fleet_role=self._iam_fleet_role,
         instance_parameters=instance_parameters.dict(exclude_none=True, exclude_unset=True, exclude_defaults=True),
     )
     instance_ids = wait_for_provision_request_done(
         region_name=provision_parameters.region_name,
         request_ids=[request_id],
         is_fleet=True,
     )
     if not instance_ids:
         with contextlib.suppress(Exception):
             self._ec2_client(provision_parameters).cancel_spot_fleet_requests(
                 SpotFleetRequestIds=[request_id], TerminateInstances=True)
         return []
     LOGGER.info('Spot fleet instances: %s', instance_ids)
     for ind, instance_id in enumerate(instance_ids):
         instance_tags = tags.pop()
         set_tags_on_instances(
             region_name=provision_parameters.region_name,
             instance_ids=[instance_id],
             tags={'Name': 'spot_fleet_{}_{}'.format(instance_id, ind)} | instance_tags,
         )
     self._ec2_client(provision_parameters).cancel_spot_fleet_requests(
         SpotFleetRequestIds=[request_id], TerminateInstances=False)
     return [find_instance_by_id(
         region_name=provision_parameters.region_name, instance_id=instance_id) for instance_id in instance_ids]