#!/usr/bin/env python # coding: utf-8 --- sidebar_label: "Prolog Script" sidebar_position: 4 --- # # Running a Prolog Script # # # [![stars - badge-generator](https://img.shields.io/github/stars/bacalhau-project/bacalhau?style=social)](https://github.com/bacalhau-project/bacalhau) # ## Introduction # 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. # Prolog is well-suited for specific tasks that benefit from rule-based logical queries such as searching databases, voice control systems, and filling templates. # # # ## TD;LR # A quick guide on how to run a hello world script on Bacalhau # # ## Prerequisites # # To get started, you need to install the Bacalhau client, see more information [here](https://docs.bacalhau.org/getting-started/installation) # # ## Running Locally​ # # To get started, install swipl # # In[ ]: get_ipython().run_cell_magic('bash', '', 'sudo add-apt-repository ppa:swi-prolog/stable\nsudo apt-get update\nsudo apt-get install swi-prolog\n') # Create a file called `helloworld.pl`. The following script prints ‘Hello World’ to the stdout # # In[ ]: get_ipython().run_cell_magic('writefile', 'helloworld.pl', "hello_world :- write('Hello World'), nl, \n halt.\n") # Running the script to print out the output # # In[ ]: get_ipython().run_cell_magic('bash', '', 'swipl -q -s helloworld.pl -g hello_world\n') # After the script has run successfully locally we can now run it on Bacalhau. # # Before running it on Bacalhau we need to upload it to IPFS. # # Using the `IPFS cli` # # In[ ]: get_ipython().system('wget https://dist.ipfs.io/go-ipfs/v0.4.2/go-ipfs_v0.4.2_linux-amd64.tar.gz') get_ipython().system('tar xvfz go-ipfs_v0.4.2_linux-amd64.tar.gz') get_ipython().system('mv go-ipfs/ipfs /usr/local/bin/ipfs') get_ipython().system('ipfs init') get_ipython().system('ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme') get_ipython().system('ipfs config Addresses.Gateway /ip4/127.0.0.1/tcp/8082') get_ipython().system('ipfs config Addresses.API /ip4/127.0.0.1/tcp/5002') get_ipython().system('nohup ipfs daemon > startup.log &') # Run the command below to check if our script has been uploaded. This commmand outputs the CID. # In[ ]: get_ipython().system('ipfs add helloworld.pl') # Copy the CID of the file which in this case is `QmYq9ipYf3vsj7iLv5C67BXZcpLHxZbvFAJbtj7aKN5qii` # 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/). # # # ## Running a Bacalhau Job # # # To submit a job, run the following Bacalhau command: # In[ ]: get_ipython().run_cell_magic('bash', '--out job_id', 'bacalhau docker run \\\n-i ipfs://QmYq9ipYf3vsj7iLv5C67BXZcpLHxZbvFAJbtj7aKN5qii:/helloworld.pl \\\n--wait \\\n--id-only \\\nswipl \\\n -- swipl -q -s helloworld.pl -g hello_world\n') # ### Structure of the Command # # # `-i: ipfs://< CID >:/< name-of-the-script >`: we will mount the script to the container using the -v flag # # `Swipl`: flag # # `-q`: running in quiet mode # # `-s`: load file as a script in this case we want to run the script helloworld.pl # # `-g`: is the name of the function you want to execute in this case its hello_world # # 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. # In[ ]: get_ipython().run_line_magic('env', 'JOB_ID={job_id}') # ## Checking the State of your Jobs # # - **Job status**: You can check the status of the job using `bacalhau list`. # In[ ]: get_ipython().run_cell_magic('bash', '', 'bacalhau list --id-filter ${JOB_ID} --wide\n') # When it says `Published` or `Completed`, that means the job is done, and we can get the results. # # - **Job information**: You can find out more information about your job by using `bacalhau describe`. # In[ ]: get_ipython().run_cell_magic('bash', '', 'bacalhau describe ${JOB_ID}\n') # - **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. # In[ ]: get_ipython().run_cell_magic('bash', '', 'rm -rf results && mkdir -p results\nbacalhau get $JOB_ID --output-dir results\n') # ## Viewing your Job Output # # To view the file, run the following command: # In[ ]: get_ipython().run_cell_magic('bash', '', 'cat results/stdout\n')