Exemplo n.º 1
0
def get_zdb_farms_distribution(solution_uuid, farm_names, required_len):
    """
    get a farm distribution based on the total used storage of each farm
    """
    zdb_workloads = [
        workload for workload in solutions.get_workloads_by_uuid(solution_uuid)
        if workload.info.workload_type == WorkloadType.Zdb
    ]
    pool_farm_name = {}
    farm_used_storage = {farm_name: 1 for farm_name in farm_names}
    for workload in zdb_workloads:
        farm_name = pool_farm_name.get(workload.info.pool_id)
        if not farm_name:
            farm_id = deployer.get_pool_farm_id(workload.info.pool_id)
            farm_name = deployer._explorer.farms.get(farm_id).name
            pool_farm_name[workload.info.pool_id] = farm_name
        if farm_name in farm_names:
            farm_used_storage[farm_name] += workload.size
    """
    1- use total storage / farm_storage as weight for each farm
    2- do wieghted random choices
    """
    farm_weights = {}
    total_Storage = sum(farm_used_storage.values())
    for farm_name, used_storage in farm_used_storage.items():
        farm_weights[farm_name] = total_Storage / used_storage

    return random.choices(list(farm_weights.keys()),
                          list(farm_weights.values()),
                          k=required_len)
Exemplo n.º 2
0
    def list_pools(self, include_hidden) -> str:
        def pool_farm_from_cache(cache_dict, pool):
            for node_id in pool.node_ids:
                if node_id in cache_dict:
                    return cache_dict[node_id]

        def update_pool_farm_cache(cache_dict, pool, farm):
            for node_id in pool.node_ids:
                cache_dict[node_id] = farm

        res = []
        farm_names = {}
        node_to_farm = {}
        pool_factory = StoredFactory(PoolConfig)
        workloads_dict = {
            w.id: w
            for w in j.sals.zos.get().workloads.list(j.core.identity.me.tid,
                                                     NextAction.DEPLOY)
        }
        for pool in j.sals.zos.get().pools.list():
            if not pool.node_ids:
                continue
            hidden = False
            name = ""
            if f"pool_{pool.pool_id}" in pool_factory.list_all():
                local_config = pool_factory.get(f"pool_{pool.pool_id}")
                hidden = local_config.hidden
                name = local_config.name

            if not include_hidden and hidden:
                continue

            pool_dict = pool.to_dict()
            pool_dict["name"] = name
            pool_dict["hidden"] = hidden
            pool_dict["explorer_url"] = j.core.identity.me.explorer_url
            farm = pool_farm_from_cache(node_to_farm, pool)
            if not farm:
                farm_id = deployer.get_pool_farm_id(pool=pool)
                if farm_id >= 0:
                    farm = farm_names.get(farm_id)
                    if not farm:
                        farm = deployer._explorer.farms.get(farm_id)
                        farm_names[farm_id] = farm
                    update_pool_farm_cache(node_to_farm, pool, farm)
            if farm:
                pool_dict["farm"] = farm.name

            for i, wid in enumerate(pool_dict["active_workload_ids"]):
                if wid in workloads_dict:
                    pool_dict["active_workload_ids"][
                        i] = f"{workloads_dict[wid].info.workload_type.name} - {wid}"
                else:
                    # due to differnet next action. we'll just show the id
                    pool_dict["active_workload_ids"][i] = wid
            res.append(pool_dict)
        return j.data.serializers.json.dumps({"data": res})
Exemplo n.º 3
0
    def _preprare_extension_pool(self, farm_name, k8s_flavor, no_nodes, duration, public_ip=False, no_extend=False):
        """
        returns pool id after extension with enough cloud units
        duration in seconds
        """
        self.vdc_deployer.info(
            f"Preperaring pool for kubernetes cluster extension on farm {farm_name}, flavor: {k8s_flavor}, no_nodes: {no_nodes}, duration: {duration}, public_ip: {public_ip},vdc uuid: {self.vdc_uuid}"
        )
        k8s = K8s()
        k8s.size = k8s_flavor.value
        cloud_units = k8s.resource_units().cloud_units()
        cus = int(cloud_units.cu * duration * no_nodes)
        sus = int(cloud_units.su * duration * no_nodes)
        ipv4us = 0
        if public_ip:
            ipv4us = int(duration)

        farm = self.explorer.farms.get(farm_name=farm_name)
        pool_id = None
        self.vdc_deployer.info(f"kubernetes cluster extension: searching for existing pool on farm {farm_name}")
        for pool in self.zos.pools.list():
            farm_id = deployer.get_pool_farm_id(pool.pool_id, pool, self.identity.instance_name)
            if farm_id == farm.id:
                pool_id = pool.pool_id
                break

        if not pool_id:
            pool_info = self.vdc_deployer._retry_call(
                self.zos.pools.create,
                args=[math.ceil(cus), math.ceil(sus), ipv4us, farm_name, ["TFT"], j.core.identity.me],
            )
            pool_id = pool_info.reservation_id
            self.vdc_deployer.info(f"Kubernetes cluster extension: creating a new pool {pool_id}")
            self.vdc_deployer.info(f"New pool {pool_info.reservation_id} for kubernetes cluster extension.")
        elif no_extend:
            return pool_id
        else:
            self.vdc_deployer.info(f"Kubernetes cluster extension: found pool {pool_id}")
            node_ids = [node.node_id for node in self.zos.nodes_finder.nodes_search(farm_name=farm_name)]
            pool_info = self.vdc_deployer._retry_call(
                self.zos.pools.extend, args=[pool_id, cus, sus, ipv4us], kwargs={"node_ids": node_ids}
            )
            self.vdc_deployer.info(
                f"Using pool {pool_id} extension reservation: {pool_info.reservation_id} for kubernetes cluster extension."
            )

        self.vdc_deployer.pay(pool_info)

        success = self.vdc_deployer.wait_pool_payment(pool_info.reservation_id)
        if not success:
            raise j.exceptions.Runtime(f"Pool {pool_info.reservation_id} resource reservation timedout")

        return pool_id
Exemplo n.º 4
0
def get_farm_pool_id(farm_name):
    """
    returns a pool_id associated on the farm or create an empty pool and return its id
    """
    zos = get_zos()
    for pool in zos.pools.list():
        farm_id = deployer.get_pool_farm_id(pool.pool_id, pool)
        pool_farm_name = zos._explorer.farms.get(farm_id).name
        if farm_name == pool_farm_name:
            return pool.pool_id
    pool_info = zos.pools.create(0, 0, 0, farm_name)
    return pool_info.reservation_id
Exemplo n.º 5
0
 def __init__(self, farm_name=None, pool_id=None):
     self.zos = get_zos()
     self._pool_node_ids = None
     if not farm_name and not pool_id:
         raise j.exceptions.Validation("must pass farm_name or pool_id")
     if not farm_name and pool_id:
         pool = self.zos.pools.get(pool_id)
         self._pool_node_ids = pool.node_ids
         farm_id = deployer.get_pool_farm_id(pool_id, pool)
         farm_name = self.zos._explorer.farms.get(farm_id).name
     self.farm_name = farm_name
     self._nodes = []
     self._excluded_node_ids = set()
Exemplo n.º 6
0
    def _preprare_extension_pool(self, farm_name, vm_size, duration, public_ip=False):
        """
        returns pool id after extension with enough cloud units
        duration in seconds
        """
        self.vdc_deployer.info(
            f"Preperaring pool for virtual machine on farm {farm_name}, duration: {duration}, public_ip: {public_ip}"
        )
        vmachine = VirtualMachine()
        vmachine.size = vm_size
        cloud_units = vmachine.resource_units().cloud_units()
        cus = int(cloud_units.cu * duration)
        sus = int(cloud_units.su * duration)
        ipv4us = 0
        if public_ip:
            ipv4us = int(duration)

        farm = self.explorer.farms.get(farm_name=farm_name)
        pool_id = None
        self.vdc_deployer.info(f"Vmachine extension: searching for existing pool on farm {farm_name}")
        for pool in self.zos.pools.list():
            farm_id = deployer.get_pool_farm_id(pool.pool_id, pool, self.identity.instance_name)
            if farm_id == farm.id:
                pool_id = pool.pool_id
                break

        if not pool_id:
            pool_info = self.vdc_deployer._retry_call(
                self.zos.pools.create,
                args=[math.ceil(cus), math.ceil(sus), ipv4us, farm_name, ["TFT"], j.core.identity.me],
            )
            pool_id = pool_info.reservation_id
            self.vdc_deployer.info(f"Vmachine extension: creating a new pool {pool_id}")
            self.vdc_deployer.info(f"New pool {pool_info.reservation_id} for vmachine extension.")
        else:
            self.vdc_deployer.info(f"Vmachine extension: found pool {pool_id}")
            node_ids = [node.node_id for node in self.zos.nodes_finder.nodes_search(farm_name=farm_name)]
            pool_info = self.vdc_deployer._retry_call(
                self.zos.pools.extend, args=[pool_id, cus, sus, ipv4us], kwargs={"node_ids": node_ids}
            )
            self.vdc_deployer.info(
                f"Using pool {pool_id} extension reservation: {pool_info.reservation_id} for Vmachine extension."
            )

        self.vdc_deployer.pay(pool_info)

        success = self.vdc_deployer.wait_pool_payment(pool_info.reservation_id)
        if not success:
            raise j.exceptions.Runtime(f"Pool {pool_info.reservation_id} resource reservation timedout")

        return pool_id