def _spot_low_price(self, region_id: int) -> float: from sdcm.utils.pricing import AWSPricing # pylint: disable=import-outside-toplevel aws_pricing = AWSPricing() on_demand_price = float( aws_pricing.get_on_demand_instance_price( region_name=self._region(region_id), instance_type=self._instance_type, )) return on_demand_price * self.params.get('spot_max_price')
def _get_spot_price(self, instance_type): """ Calculate spot price for bidding :return: spot bid price """ LOGGER.info('Calculating spot price based on OnDemand price') from sdcm.utils.pricing import AWSPricing # pylint: disable=import-outside-toplevel aws_pricing = AWSPricing() on_demand_price = float(aws_pricing.get_on_demand_instance_price(self.region_name, instance_type)) price = dict(max=on_demand_price, desired=on_demand_price * self.spot_max_price_percentage) LOGGER.info('Spot bid price: %s', price) return price
def _get_spot_price(self, instance_type): """ Calculate spot price for bidding :return: spot bid price """ LOGGER.info('Calculating spot price based on OnDemand price') aws_pricing = AWSPricing() on_demand_price = float( aws_pricing.get_on_demand_instance_price(self.region_name, instance_type)) price = dict(max=on_demand_price, desired=on_demand_price * self.spot_max_price_percentage) LOGGER.info('Spot bid price: %s', price) return price
class AWSInstance(CloudInstance): pricing = AWSPricing() def __init__(self, instance): self._instance = instance self._tags = aws_tags_to_dict(instance.get('Tags')) super().__init__( cloud="aws", name=self._tags.get("Name", NA), instance_id=instance['InstanceId'], region_az=instance["Placement"]["AvailabilityZone"], state=instance["State"]["Name"], lifecycle=InstanceLifecycle.SPOT if instance.get("SpotInstanceRequestId") else InstanceLifecycle.ON_DEMAND, instance_type=instance["InstanceType"], owner=self.get_owner(), create_time=instance['LaunchTime'], keep=self._tags.get("keep", ""), ) @property def region(self): return self.region_az[:-1] def get_owner_from_cloud_trail(self): try: client = boto3_client('cloudtrail', region_name=self._instance["Placement"] ["AvailabilityZone"][:-1]) result = client.lookup_events(LookupAttributes=[{ 'AttributeKey': 'ResourceName', 'AttributeValue': self._instance['InstanceId'] }]) for event in result["Events"]: if event['EventName'] == 'RunInstances': return event["Username"] except Exception as exc: # pylint: disable=broad-except LOGGER.warning( "Error occurred when trying to find an owner for '%s' in CloudTrail: %s", self._instance['InstanceId'], exc) return None def get_owner(self): # try to get the owner using tags if owner := self._tags.get("RunByUser", self._tags.get("Owner")): return owner # get the owner from the Cloud Trail if owner := self.get_owner_from_cloud_trail(): return owner
class AWSInstance(CloudInstance): # pylint: disable=too-few-public-methods, pricing = AWSPricing() def __init__(self, instance): tags = aws_tags_to_dict(instance.get('Tags')) super(AWSInstance, self).__init__( cloud="aws", name=tags.get("Name", NA), instance_id=instance['InstanceId'], region_az=instance["Placement"]["AvailabilityZone"], state=instance["State"]["Name"], lifecycle=InstanceLifecycle.SPOT if instance.get("SpotInstanceRequestId") else InstanceLifecycle.ON_DEMAND, instance_type=instance["InstanceType"], owner=tags.get("RunByUser", tags.get("Owner", NA)), create_time=instance['LaunchTime'], keep=tags.get("keep", ""), ) @property def region(self): return self.region_az[:-1]