{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "sidebar_label: \"Prolog Script\"\n", "sidebar_position: 4\n", "---" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Running a Prolog Script\n", "\n", "\n", "[![stars - badge-generator](https://img.shields.io/github/stars/bacalhau-project/bacalhau?style=social)](https://github.com/bacalhau-project/bacalhau)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "nj1-Kp2gw7kU" }, "source": [ "## Introduction\n", "Prolog is intended primarily as a declarative programming language: the program logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations.\n", "Prolog is well-suited for specific tasks that benefit from rule-based logical queries such as searching databases, voice control systems, and filling templates.\n", "\n", "\n", "## TD;LR\n", "A quick guide on how to run a hello world script on Bacalhau\n", "\n", "## Prerequisites\n", "\n", "To get started, you need to install the Bacalhau client, see more information [here](https://docs.bacalhau.org/getting-started/installation)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "DrdQTcaPxDll" }, "source": [ "## Running Locally​\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "IvMPtL4xxGFj" }, "source": [ "To get started, install swipl \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "exnVKu_pxH_r", "outputId": "1d536b36-3864-4efa-c27c-071b9b3ac1f6", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "sudo add-apt-repository ppa:swi-prolog/stable\n", "sudo apt-get update\n", "sudo apt-get install swi-prolog" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "DmWJsNYVxNzk" }, "source": [ "Create a file called `helloworld.pl`. The following script prints ‘Hello World’ to the stdout\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "f6mZ2BJBxK_A", "outputId": "8b5129d1-fb7e-4f48-a5f2-368aa3d2df62", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%writefile helloworld.pl\n", "hello_world :- write('Hello World'), nl, \n", " halt." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "ob7hpDK8xblS" }, "source": [ "Running the script to print out the output\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eGKyvFesxdkC", "outputId": "c261cf03-9a48-405f-bef0-38661b436e13", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "swipl -q -s helloworld.pl -g hello_world" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "S_0U_66dzGUB" }, "source": [ "After the script has run successfully locally we can now run it on Bacalhau. \n", "\n", "Before running it on Bacalhau we need to upload it to IPFS.\n", "\n", "Using the `IPFS cli`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aCfmI2YY4JFE", "outputId": "b106a21f-1513-4305-9203-151c369df2c5", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "!wget https://dist.ipfs.io/go-ipfs/v0.4.2/go-ipfs_v0.4.2_linux-amd64.tar.gz\n", "!tar xvfz go-ipfs_v0.4.2_linux-amd64.tar.gz\n", "!mv go-ipfs/ipfs /usr/local/bin/ipfs\n", "!ipfs init\n", "!ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme\n", "!ipfs config Addresses.Gateway /ip4/127.0.0.1/tcp/8082\n", "!ipfs config Addresses.API /ip4/127.0.0.1/tcp/5002\n", "!nohup ipfs daemon > startup.log &" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Run the command below to check if our script has been uploaded. This commmand outputs the CID." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fnDSoKm44NQj", "outputId": "cc31251c-c29d-48bf-b282-22105cfa68b3", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "!ipfs add helloworld.pl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy the CID of the file which in this case is `QmYq9ipYf3vsj7iLv5C67BXZcpLHxZbvFAJbtj7aKN5qii`" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "1EthK_pazMcY" }, "source": [ "Since the data uploaded To IPFS isn’t pinned, we will need to do that manually. Check this information on how to pin your [data](https://docs.bacalhau.org/data-ingestion/pin) We recommend using [NFT.Storage](https://nft.storage/).\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "XX_26vGczhUZ" }, "source": [ "## Running a Bacalhau Job \n", "\n", "\n", "To submit a job, run the following Bacalhau command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "0sToMfZa0kqq", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash --out job_id\n", "bacalhau docker run \\\n", "-i ipfs://QmYq9ipYf3vsj7iLv5C67BXZcpLHxZbvFAJbtj7aKN5qii:/helloworld.pl \\\n", "--wait \\\n", "--id-only \\\n", "swipl \\\n", " -- swipl -q -s helloworld.pl -g hello_world" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "6CbHWnN009Rw" }, "source": [ "### Structure of the Command\n", "\n", "\n", "`-i: ipfs://< CID >:/< name-of-the-script >`: we will mount the script to the container using the -v flag\n", "\n", "`Swipl`: flag\n", "\n", "`-q`: running in quiet mode\n", "\n", "`-s`: load file as a script in this case we want to run the script helloworld.pl\n", "\n", "`-g`: is the name of the function you want to execute in this case its hello_world\n", "\n", "When a job is submitted, Bacalhau prints out the related `job_id`. We store that in an environment variable so that we can reuse it later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Gg6riPesa5BN", "outputId": "7ad204f8-ba7d-47b3-b729-dd1ba5666d5d", "tags": [ "skip-execution", "remove_cell" ] }, "outputs": [], "source": [ "%env JOB_ID={job_id}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Checking the State of your Jobs\n", "\n", "- **Job status**: You can check the status of the job using `bacalhau list`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q0pc73fqa5BO", "outputId": "96c40b17-39b0-42db-8e8b-42e7a370311e", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "bacalhau list --id-filter ${JOB_ID} --wide" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "wElIl4UYa5BO" }, "source": [ "When it says `Published` or `Completed`, that means the job is done, and we can get the results.\n", "\n", "- **Job information**: You can find out more information about your job by using `bacalhau describe`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "gRgrNAm7a5BO", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "bacalhau describe ${JOB_ID}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "- **Job download**: You can download your job results directly by using `bacalhau get`. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "duvEDPr-a5BO", "outputId": "b90c0d5f-9cf9-49e0-e7ac-ef55fd262891", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "rm -rf results && mkdir -p results\n", "bacalhau get $JOB_ID --output-dir results" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "YZEeFzMEa5BO" }, "source": [ "## Viewing your Job Output\n", "\n", "To view the file, run the following command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zhk8g_LMa5BO", "outputId": "dc11348a-7dc4-451c-d5b3-5c218c7076a4", "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "cat results/stdout" ] } ], "metadata": { "colab": { "collapsed_sections": [], "provenance": [] }, "kernelspec": { "display_name": "Python 3.10.6 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" }, "vscode": { "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" } } }, "nbformat": 4, "nbformat_minor": 0 }