def get_spot_prices(self, resource, tenant): """ Get the current spot price for each instance type. """ new_time = ProvisionerConfig().simulate_time now = new_time.strftime('%Y-%m-%d %H:%M:%S') start_time = (ProvisionerConfig().simulate_time - datetime.timedelta(seconds=60)) start_time_z = start_time.strftime('%Y-%m-%d %H:%M:%S') conn = boto.connect_ec2(tenant.access_key, tenant.secret_key) jobCost = 0 timeStr = str(now).replace(" ", "T") + "Z" startTimeStr = str(start_time_z).replace(" ", "T") + "Z" prices = conn.get_spot_price_history( instance_type=resource.type, product_description="Linux/UNIX (Amazon VPC)", end_time=timeStr, start_time=startTimeStr) lowest_price = 1000000 for price in prices: for key, val in tenant.subnets.iteritems(): if (price.availability_zone == key and val == resource.subnet and float(price.price) < lowest_price): lowest_price = float(price.price) return lowest_price
def get_spot_prices(instances, tenant): """ Get the current spot price for each instance type. """ new_time = ProvisionerConfig().simulate_time now = new_time.strftime('%Y-%m-%d %H:%M:%S') conn = boto.connect_ec2(tenant.access_key, tenant.secret_key) timeStr = str(now).replace(" ", "T") + "Z" for ins in instances: prices = conn.get_spot_price_history( instance_type=ins.type, product_description="Linux/UNIX (Amazon VPC)", end_time=timeStr, start_time=timeStr) for price in prices: for key, val in tenant.subnets.iteritems(): if price.availability_zone == key: ins.spot.update({price.availability_zone: price.price})
def load_drafts_data(self): """ To speed this up, load in all the drafts data once per provisioning cycle """ cur_time = datetime.datetime.utcnow() if ProvisionerConfig().simulate: cur_time = ProvisionerConfig().simulator.get_fake_time() minus_ten = cur_time - datetime.timedelta(seconds=600) query = ("select * from drafts_price where timestamp < " "'%s'::TIMESTAMP and timestamp > '%s'::TIMESTAMP") % ( cur_time.strftime("%Y-%m-%d %H:%M"), minus_ten.strftime("%Y-%m-%d %H:%M")) self.drafts_data = [] logger.debug('getting drafts data: ' + query) rows = ProvisionerConfig().dbconn.execute(query) for row in rows: data = {'time': row['time'], 'price': row['price'], 'zone': row['zone'], 'type': row['type']} self.drafts_data.append(data)