Exemplo n.º 1
0
def update(data: dict, user: str, device: Device, file: File) -> dict:
    """
    Update the content of a file.

    :param data: The given data.
    :param user: The user uuid.
    :param device: The device of the file.
    :param file: The file.
    :return: The response
    """

    if file.is_directory:
        return directories_can_not_be_updated

    file.content = data["content"]
    wrapper.session.commit()

    m.contact_user(
        user,
        {
            "notify-id": "file-update",
            "origin": "update",
            "device_uuid": device.uuid,
            "data": {
                "created": [],
                "deleted": [],
                "changed": [file.uuid]
            },
        },
    )

    return file.serialize
Exemplo n.º 2
0
def set_wallet(data: dict, user: str, service: Service) -> dict:
    wallet_uuid: str = data["wallet_uuid"]

    if not exists_wallet(wallet_uuid):
        return wallet_not_found

    miner: Miner = wrapper.session.query(Miner).get(service.uuid)

    update_miner(miner)

    # notify old wallet owner
    wallet_owner: Optional[str] = get_wallet_owner(miner.wallet)
    if wallet_owner is not None:
        m.contact_user(
            wallet_owner, {
                "notify-id": "miner-disconnected",
                "origin": "miner/wallet",
                "wallet_uuid": miner.wallet
            })

    miner.wallet = wallet_uuid
    wrapper.session.commit()

    # notify new wallet owner
    wallet_owner: Optional[str] = get_wallet_owner(miner.wallet)
    if wallet_owner is not None:
        m.contact_user(
            wallet_owner, {
                "notify-id": "miner-connected",
                "origin": "miner/wallet",
                "wallet_uuid": miner.wallet
            })

    return miner.serialize
Exemplo n.º 3
0
def hardware_scale(data: dict, user: str) -> dict:
    ser: Service = wrapper.session.query(Service).get(data["service_uuid"])
    if ser is None:
        return service_not_found

    wl: Workload = wrapper.session.query(Workload).get(data["device_uuid"])
    other: List[Service] = wrapper.session.query(Service).filter_by(
        device_uuid=data["device_uuid"]).all()

    wl.service(turn(ser.export()))

    new: Tuple[float, float, float, float, float] = dict2tuple(data)

    scales: Tuple[float, float, float, float, float] = generate_scale(new, wl)

    wl.service(new)
    ser.overwrite(new)

    scale_resources(other, scales)

    return_value: dict = {
        "service_uuid": ser.service_uuid,
        "cpu": ser.allocated_cpu * scales[0],
        "ram": ser.allocated_ram * scales[1],
        "gpu": ser.allocated_gpu * scales[2],
        "disk": ser.allocated_disk * scales[3],
        "network": ser.allocated_network * scales[4],
    }

    m.contact_user(data["user"],
                   wl.workload_notification("device-hardware-scale"))

    return return_value
Exemplo n.º 4
0
def hardware_stop(data: dict, microservice: str) -> dict:
    ser: Service = wrapper.session.query(Service).get(data["service_uuid"])
    if ser is None:
        return service_not_running

    wl: Workload = wrapper.session.query(Workload).get(data["device_uuid"])
    if wl is None:
        return device_not_found

    attributes: Tuple[float, float, float, float, float] = turn(ser.export())

    wrapper.session.delete(ser)
    wrapper.session.commit()

    wl.service(attributes)
    new_scales: Tuple[float, float, float, float,
                      float] = generate_scale(attributes, wl)

    other: List[Service] = wrapper.session.query(Service).filter_by(
        device_uuid=data["device_uuid"]).all()
    scale_resources(other, new_scales)

    m.contact_user(data["user"],
                   wl.workload_notification("device-hardware-stop"))

    return success
Exemplo n.º 5
0
def move(data: dict, user: str, device: Device, file: File) -> dict:
    new_filename = data["new_filename"]
    new_parent_dir_uuid = data["new_parent_dir_uuid"]

    target_file: Optional[File] = (wrapper.session.query(File).filter_by(
        device=device.uuid,
        filename=new_filename,
        parent_dir_uuid=new_parent_dir_uuid).first())
    if target_file is not None:
        return file_already_exists

    target_dir: Optional[File] = (wrapper.session.query(File).filter_by(
        device=device.uuid, is_directory=True,
        uuid=new_parent_dir_uuid).first())
    if target_dir is None and new_parent_dir_uuid is not None:
        return parent_directory_not_found

    if file.is_directory:
        parent_to_check: Optional[File] = (
            wrapper.session.query(File).filter_by(
                device=device.uuid, uuid=new_parent_dir_uuid).first())
        if parent_to_check is not None:
            while parent_to_check is not None:
                if parent_to_check.uuid == file.uuid:
                    return can_not_move_dir_into_itself
                parent_to_check: Optional[File] = (
                    wrapper.session.query(File).filter_by(
                        device=device.uuid,
                        uuid=parent_to_check.parent_dir_uuid).first())

    file.filename = new_filename
    file.parent_dir_uuid = new_parent_dir_uuid
    wrapper.session.commit()

    m.contact_user(
        user,
        {
            "notify-id": "file-update",
            "origin": "update",
            "device_uuid": device.uuid,
            "data": {
                "created": [],
                "deleted": [],
                "changed": [file.uuid]
            },
        },
    )

    return file.serialize
Exemplo n.º 6
0
def delete_file(data: dict, user: str, device: Device, file: File) -> dict:
    """
    Delete a file.
    :param data: The given data.
    :param user: The user uuid.
    :param device: The device of the file.
    :param file: The file.
    :return: The response
    """

    if file.is_directory:
        stack_to_delete = []
        dirs = [file]
        while len(dirs) > 0:
            dir_to_check = dirs.pop()
            stack_to_delete.append(dir_to_check)
            files_in_dir: list = (wrapper.session.query(File).filter_by(
                device=device.uuid, parent_dir_uuid=dir_to_check.uuid).all())
            for child_file in files_in_dir:
                if child_file.is_directory:
                    dirs.append(child_file)
                else:
                    stack_to_delete.append(child_file)
    else:
        stack_to_delete = [file]

    deleted_files: List[str] = []
    while stack_to_delete:
        file_to_delete = stack_to_delete.pop()
        wrapper.session.delete(file_to_delete)
        deleted_files.append(file_to_delete.uuid)

    wrapper.session.commit()

    m.contact_user(
        user,
        {
            "notify-id": "file-update",
            "origin": "delete",
            "device_uuid": device.uuid,
            "data": {
                "created": [],
                "deleted": deleted_files,
                "changed": []
            },
        },
    )

    return success
Exemplo n.º 7
0
def create_file(data: dict, user: str, device: Device) -> dict:
    """
    Create a new file.
    :param data: The given data.
    :param user: The user uuid.
    :param device: The device of the file.
    :return: The response
    """

    filename: str = data["filename"]
    content: str = data["content"]
    is_directory: bool = data["is_directory"]
    parent_dir_uuid: str = data["parent_dir_uuid"]

    file_count: int = (wrapper.session.query(func.count(
        File.uuid)).filter_by(device=device.uuid,
                              filename=filename,
                              parent_dir_uuid=parent_dir_uuid).scalar())

    if file_count > 0:
        return file_already_exists

    parent_dir: File = (wrapper.session.query(File).filter_by(
        device=device.uuid, uuid=parent_dir_uuid, is_directory=True).first())
    if not parent_dir and parent_dir_uuid is not None:
        return parent_directory_not_found

    if is_directory and content != "":
        return directory_can_not_have_textcontent

    file: File = File.create(device.uuid, filename, content, parent_dir_uuid,
                             is_directory)

    m.contact_user(
        user,
        {
            "notify-id": "file-update",
            "origin": "create",
            "device_uuid": device.uuid,
            "data": {
                "created": [file.uuid],
                "deleted": [],
                "changed": []
            },
        },
    )

    return file.serialize
Exemplo n.º 8
0
def hardware_register(data: dict, microservice: str) -> dict:
    # cpu_requirements, ram_req, gpu_req, disk_req, network_req

    wl: Workload = wrapper.session.query(Workload).get(data["device_uuid"])

    if wl is None:
        return device_not_found

    ser: Service = wrapper.session.query(Service).get(data["service_uuid"])

    if ser is not None:
        return service_already_running

    other: List[Service] = wrapper.session.query(Service).filter_by(
        device_uuid=data["device_uuid"]).all()

    new: Tuple[float, float, float, float, float] = dict2tuple(data)

    scales: Tuple[float, float, float, float, float] = generate_scale(new, wl)

    scale_resources(other, scales)

    wl.service(new)

    ser: Service = Service.create(data["device_uuid"], data["service_uuid"],
                                  new)

    return_value: dict = {
        "service_uuid": ser.service_uuid,
        "cpu": ser.allocated_cpu * scales[0],
        "ram": ser.allocated_ram * scales[1],
        "gpu": ser.allocated_gpu * scales[2],
        "disk": ser.allocated_disk * scales[3],
        "network": ser.allocated_network * scales[4],
    }

    m.contact_user(data["user"],
                   wl.workload_notification("device-hardware-register"))

    return return_value
Exemplo n.º 9
0
def set_power(data: dict, user: str, service: Service) -> dict:
    miner: Miner = wrapper.session.query(Miner).get(service.uuid)

    if not exists_wallet(miner.wallet):
        return wallet_not_found

    power: int = data["power"]

    update_miner(miner)

    speed: float = change_miner_power(power, service.uuid, service.device,
                                      service.owner)
    if speed == -1:
        return could_not_start_service

    service.speed = speed
    service.running = power > 0
    miner.power = power
    if service.running:
        miner.started = int(time.time() * 1000)
    else:
        miner.started = None

    # notify wallet owner
    wallet_owner: Optional[str] = get_wallet_owner(miner.wallet)
    if wallet_owner is not None:
        m.contact_user(
            wallet_owner, {
                "notify-id": "miner-rate-changed",
                "origin": "miner/power",
                "wallet_uuid": miner.wallet
            })

    wrapper.session.commit()

    return miner.serialize