Example #1
0
def create_tensor_msg(
    msg: CreateTensorMessage,
    node: AbstractNode,
) -> CreateTensorResponse:
    try:
        payload = msg.content

        new_tensor = th.tensor(payload["tensor"])
        new_tensor.tag(*payload.get("tags", []))
        new_tensor.describe(payload.get("description", ""))

        id_at_location = UID()

        # Step 2: create message which contains object to send
        storable = StorableObject(
            id=id_at_location,
            data=new_tensor,
            tags=new_tensor.tags,
            description=new_tensor.description,
            search_permissions={VerifyAll(): None}
            if payload.get("searchable", False)
            else {},
        )

        obj_msg = SaveObjectAction(obj=storable, address=node.address)

        signed_message = obj_msg.sign(
            signing_key=SigningKey(
                payload["internal_key"].encode("utf-8"), encoder=HexEncoder
            )
        )

        node.recv_immediate_msg_without_reply(msg=signed_message)

        return CreateTensorResponse(
            address=msg.reply_to,
            status_code=200,
            content={
                "msg": "Tensor created succesfully!",
                "tensor_id": str(id_at_location.value),
            },
        )
    except Exception as e:
        return CreateTensorResponse(
            address=msg.reply_to,
            status_code=200,
            content={"error": str(e)},
        )
Example #2
0
def create_initial_setup(msg: CreateInitialSetUpMessage, node: AbstractNode,
                         verify_key: VerifyKey) -> CreateInitialSetUpResponse:
    _email = msg.content.get("email", None)
    _password = msg.content.get("password", None)

    # Get Payload Content
    configs = {
        "node_name": msg.content.get("node_name", ""),
        "private_key": msg.content.get("private_key", ""),
        "aws_credentials": msg.content.get("aws_credentials", ""),
        "gcp_credentials": msg.content.get("gcp_credentials", ""),
        "azure_credentials": msg.content.get("azure_credentials", ""),
        "cache_strategy": msg.content.get("cache_strategy", ""),
        "replicate_db": msg.content.get("replicate_db", False),
        "auto_scale": msg.content.get("auto_scale", ""),
        "tensor_expiration_policy": msg.content.get("tensor_expiration_policy",
                                                    0),
        "allow_user_signup": msg.content.get("allow_user_signup", False),
    }

    _current_user_id = msg.content.get("current_user", None)

    users = node.users

    if not _current_user_id:
        try:
            _current_user_id = users.first(verify_key=verify_key.encode(
                encoder=HexEncoder).decode("utf-8")).id
        except Exception:
            pass

    _admin_role = node.roles.first(name="Owner")

    _mandatory_request_fields = _email and _password and configs["node_name"]

    # Check if email/password/node_name fields are empty
    if not _mandatory_request_fields:
        raise MissingRequestKeyError(
            message=
            "Invalid request payload, empty fields (email/password/node_name)!"
        )

    config_obj = SetupConfig(**configs)

    # Change Node Name
    node.name = config_obj.node_name

    # Change Node Root Key (if requested)
    if config_obj.private_key != "":
        try:
            private_key = SigningKey(config_obj.encode("utf-8"),
                                     encoder=HexEncoder)
        except Exception:
            raise InvalidParameterValueError("Invalid Signing Key!")
        node.root_key = private_key
        node.verify_key = private_key.verify_key

    # Create Admin User
    _node_private_key = node.signing_key.encode(
        encoder=HexEncoder).decode("utf-8")
    _verify_key = node.signing_key.verify_key.encode(
        encoder=HexEncoder).decode("utf-8")
    _admin_role = node.roles.first(name="Owner")
    _user = users.signup(
        email=_email,
        password=_password,
        role=_admin_role.id,
        private_key=_node_private_key,
        verify_key=_verify_key,
    )

    # Final status / message
    final_msg = "Running initial setup!"
    node.setup.register(**configs)
    return CreateInitialSetUpResponse(
        address=msg.reply_to,
        status_code=200,
        content={"msg": final_msg},
    )