Tutorial — Step by step guide to execute a function with Planetr.io

planetr.io
Sep 5, 2018 · 5 min read

Greetings,

This blog is to explain all the steps needed to install Planetr.io decentralized master node, deploy code written in Node.js to find the weather in a city, and how to execute it by passing the city name.

Steps followed:

  1. Install and Planetr.io master node on your laptop or desktop (or even on a cloud server)

This tutorial will be a great starting point for developers who want to get started with Planetr. You can adapt the steps to do other computational tasks or use other language runtimes like Python or Go.

What is needed:

  1. A laptop or desktop running Mac, Linux or Windows and connected to internet. It must be 64bit.

Step 1 — Install and run Planetr.io master node

For this tutorial, a Mac laptop is assumed but it can be easily adapted to other operating systems.

Install the latest version of Docker on your laptop. Follow Docker Installation steps. Community edition is sufficient. See Docker Getting Started if needed.

Open a terminal from Launchpad.

Confirm that docker installation and permissions are working by running:

$ sudo docker info

enter the laptop password when prompted.

Pull Planetr master node image from docker hub:

$ sudo docker pull planetr/master

Check if the node is running by executing the following command

$ sudo docker run planetr/master -h

Create a folder mydata to persist the database and related files of master node:

$ mkdir ~/mydata


Step 2 — Create an EOS account and run the master node

Create a EOS account on Testnet and get free PLANET tokens.

$ sudo docker run planetr/master -c tom123

Note: replace tom123 with any name you like.

You should see a response like this:

*** PLANETR EOS TESTNET ***

Account Name: tom123
Private Key: 5K3SpdD*********************pKcW
Public Key: EOS6RuWuTMKLGYm6GWcSanCe4gPYcbq6aT1AZrQdJpuS9cBjWEyw4
Token Balance: 100.00000000 PLANET

Your EOS account tom123 is created and 100 PLANET tokens are added to it on the Testnet.

Now with that account let us start the master node and expose port 8000 and mount ~/mydata folder using tom123 EOS account.

$ sudo docker run -it -p 8000:8000 -v ~/mydata:/data planetr/master -a tom123

You will be prompted to enter the private key of the tom123 account

info: Planetr Master Node v0.7 beta
info: Network: TESTNET
Enter EOS Account Private Key to enable transactions (used only in this machine for this session):

Enter the private key (in this case 5K3SpdDHzfZ8XBYjG2pMszVT2nv7zj2EEhd3XJs7MLmDznwpKcW)

Press enter.

You should see that the node is running at port 8000

info: Validating EOS Account tom123
info: EOS Account, Balance: 100.00000000 PLANET
info: Planetr app running at: http://localhost:8000


Step 3 — Write weather fetching logic in Node.js and deploy on Testnet

Refer Node.js developer guide if needed.

Create a folder named weather and init a Node.js project in it and install weather-js module to find the weather .

$ mkdir ~/weather
$ cd ~/weather
$ npm init
$ npm install weather-js — save

Using your favorite file editor create a new file index.js in ~/weather folder, and paste the following code in it. Save the file.

const weather = require(‘weather-js’);

module.exports = function (params, callback) {

let city = params.city;
weather.find({search: city, degreeType: ‘F’}, function(err, result) {
if(err) return callback(err);

var res = ‘not found’
if(result && result.length>0){
res = result[0].current;
}

callback(null, res);
});
};

You can see that the code expects a parameter “city” and it fetches the weather data using weather-js module and return it as JSON.

Since the program uses an external module weather-js, zip the entire folder as weather.zip including the sub folder node_modules. This way node_modules dependency is also been packaged.

$ zip -r weather.zip *


Step 4 — Deploy the code to Testnet using Planetr console

In a browser access http://localhost:8000

Master Node Login Page

Click on “Create Account” found at the top right corner of the login page.

Enter your email and password. Signup and you be auto logged into the console.

Planetr Console

Click on Create and enter the following details, choose index.zip file as upload.

Name: weather-find
Runtime: Node v9.5
File: weather.zip

Click Save.

The status of the function created will be “PENDING”. Wait for sometime and refresh the screen. The function will be deployed to the Testnet network and status should be “DEPLOYED”.

Function Deployed

Step 5 — Execute and fetch the weather

In this case we can use the browser itself to access the API using GET HTTP protocol, but you may use cURL or Postman which support GET and POST.

Open a browser window and access the below URL which you got in the function view page above.

http://localhost:8000/exec/PLANETR:zdpuAngbAncr3xovkodtVXtG2ryqz6CT3LCLjzEvCfgiCkeH3?city=Texas

Note that we are passing a parameter city = Texas. You can pass any city name as needed.

You will be seeing the response JSON similar as below:

{“request”:”5b9008eb298240003281e26f”}

This is the ID of the asynchronous request we just executed on the Testnet. Depending on the network traffic and complexity of the code it may take a moment to complete the execution. You can view the results by accessing the following URL in a browser:

http://localhost:8000/result/5b9008eb298240003281e26f

Note: In your case you may have a different request ID.

If the execution is complete and is successful, you will see a similar response JSON.

{
“type”:”FUNCTION”,“usage”:2.7341,”attempted”:1,
“_id”:”5b9008eb298240003281e26f”,
“signature”:”PLANETR:zdpuAngbAncr3xovkodtVXtG2ryqz6CT3LCLjzEvCfgiCkeH3",
“status”:”COMPLETED”,
“req”:”{\”city\”:\”Texas\”}”,
“res”:”{\”status\”:\”SUCCESS\”,\”data\”:{\”temperature\”:\”81\”,\”observationpoint\”:\”Texas\”,\”humidity\”:\”78\”}}”,
“log”:””
}

Congratulations! You have successfully installed, deployed function code and executed it on Planetr decentralized compute network.

You may also notice that PLANET tokens are been transferred to from your EOS account tom123 to the compute node which executed your code.

Good luck!

Planetr.io Channel

Decentralized Serverless Computing - Planetary Scale

planetr.io

Written by

Planetary Scale Serverless Computing

Planetr.io Channel

Decentralized Serverless Computing - Planetary Scale