def create(self, ninsts, avzone, user_data, spot_price=0): if self.exists(): raise Exception("%s exists" % (self.debug_str(),)) # create a placement group placement_group = Cluster.create_placement_group(self.gname) if not placement_group: self.log_error("%s - Error creating placement group", self.debug_str()) raise Exception("Error creating placement group") # create a launch configuration Cluster.create_launch_config(self.gname, UserCluster.IMAGE_ID, UserCluster.INSTANCE_TYPE, UserCluster.KEY_NAME, UserCluster.SEC_GRPS, user_data=user_data, spot_price=spot_price) self.log_info("Launch configuration %s", self.gname) # create an autoscale group agrp = Cluster.create_autoscale_group(self.gname, self.gname, self.gname, ninsts, [avzone]) if agrp is None: self.log_error("%s - Error creating autoscale group", self.debug_str()) raise Exception("Error creating placement group") self.log_info("Autoscaling group %r", agrp) self.exists()
def exists(self): self.placement_group = Cluster.get_placement_group(self.gname) self.autoscale_group = Cluster.get_autoscale_group(self.gname) self.launch_config = Cluster.get_launch_config(self.gname) self.refresh_instances() self.log_debug("%s - placement_group:%r, autoscale_group:%r, launch_config:%r, count:%d", self.debug_str(), (self.placement_group is not None), (self.launch_config is not None), (self.autoscale_group is not None), len(self.instances)) return ((self.placement_group is not None) or (self.launch_config is not None) or (self.autoscale_group is not None))
def list_all(): ascale_conn = Cluster._autoscale() pgrps = [x for x in Cluster.get_placement_groups() if x.name.startswith(UserCluster.NAME_PFX)] UserCluster.log_info("%d placement groups", len(pgrps)) for grp in pgrps: UserCluster.log_info("\t%s, %s, %s", grp.name, grp.strategy, repr(grp.state)) configs = [x for x in ascale_conn.get_all_launch_configurations() if x.name.startswith(UserCluster.NAME_PFX)] UserCluster.log_info("%d launch configurations", len(configs)) for config in configs: UserCluster.log_info("\t%s", config.name) agrps = [x for x in ascale_conn.get_all_groups() if x.name.startswith(UserCluster.NAME_PFX)] UserCluster.log_info("%d autoscale groups", len(agrps)) for grp in agrps: UserCluster.log_info("\t%s", grp.name) return pgrps, configs, agrps
def delete(self): if not self.exists(): return if len(self.instances) > 0: raise Exception("Can not delete %s with %d active instances" % (self.debug_str(), len(self.instances))) Cluster.delete_autoscale_group(self.gname) Cluster.delete_launch_config(self.gname) Cluster.delete_placement_group(self.gname)
def get(instance_type): nowtime = datetime.datetime.now(pytz.utc) cached = None if instance_type in SpotPriceCache.CACHE: cached = SpotPriceCache.CACHE[instance_type] if (cached['time'] - nowtime).total_seconds() < SpotPriceCache.SPOT_PRICE_REFETCH_SECS: cached = None if cached is None: newprices = Cluster.get_spot_price(instance_type) cached = { 'time': nowtime, 'prices': newprices } SpotPriceCache.CACHE[instance_type] = cached return cached['prices']
def set_capacity(self, ninst): conn = Cluster._autoscale() conn.set_desired_capacity(self.gname, ninst, False)
def refresh_instances(self): self.instances = Cluster.get_autoscaled_instances(self.gname) self.public_ips = Cluster.get_public_ips_by_placement_group(self.gname) self.public_hosts = Cluster.get_public_hostnames_by_placement_group(self.gname) self.private_ips = Cluster.get_private_ips_by_placement_group(self.gname) self.private_hosts = Cluster.get_private_hostnames_by_placement_group(self.gname)