Create your first FeatureCloud app

Using FeatureCloud (FC) pip package, we can quickly develop federated apps that can be run on the FeatureCloud platform. This story will discuss how beginners can create apps using the FC pip package.

Perquisites:

  • Python
  • FeatureCloud library

To install featurecloud:

$ pip install featurecloud

For creating apps, one should use the FeatureCloud CLI :

$ featurecloud app new <APP_NAME> <template_name>

Where APP_NAME is the name to assign to the directory containing the app, and template-name is the URL of a specific sample app provided on FC GitHub repositories. Without any specific template in mind to try, one can go with the default template.

$ featurecloud app new fc_test

After creating the app following files will be available in fc_test directory:

fc_test
├── Dockerfile
├── LICENSE
├── main.py
├── README.md
├── requirements.txt
└── server_config
├── docker-entrypoint.sh
├── nginx
└── supervisord.conf

Generally FC uses dockerizatoin and ever app should include some useful files for that purpose:

  1. Dockerfile
  2. server-config(docker-entrypoint.sh, nginx, and supervisord.conf)

Besides Dockerization, FC apps should always include two essential files, primarily app-specific: main and requirements. The requirements file includes a list of python libraries that should be installed in the container.

main

Every app should include a main.py file containing or importing the states and running the application inside the container. It also always imports api_server and web_server from the FeatureCloud API package to bind with the bottle, regardless of functionality:

from bottle import Bottle
from api.http_ctrl import api_server
from api.http_web import web_server
from FeatureCloud.engine.app import app
server = Bottle()

Merely importing the states or defining them in the main.py file, register them into the app instance. Each app includes at least two states, where all states will be registered for the same App instance, the instance in the FeatureCloud engine package.
Apps’ operations should be implemented inside a state, so developers should extend the app_state abstract class and register it.

$ from FeatureCloud.app.engine.app import app, AppState, app_state

Every FC app includes at least two states, initial and terminal. Except for the terminal, all the states should be explicitly defined and registered to the app. FeatureCloud app class will be instantiated automatically, and states will be registered to the same instance by calling @app_state method and setting the state name. Here, we define the initial state:

@app_state(name='initial')
class InitialState(AppState):
def register(self):
self.register_transition('terminal')

def run(self):
self.log('Hello World!')
return 'terminal'

For every state, developers should overload two abstract methods: register and run. Register method takes the name of states that the current state is allowed to transition to. Meanwhile, the last declared state should always transition to the terminal state. Accordingly, because we defined only one state, we registered the transition to terminal state for it. run method is responsible for carrying on all the computations and communications. The dynamic transitions between states will be possible by returning the name of the following state in the run method.

Complete example

Merely defining the states in the main file will be enough because all states will be automatically registered to the app. If states are implemented in other files, they should be imported in the main file.

from bottle import Bottle
from FeatureCloud.app.api.http_ctrl import api_server
from FeatureCloud.app.api.http_web import web_server
from FeatureCloud.app.engine.app import app, AppState, app_state
server = Bottle()
@app_state(name='initial')
class InitialState(AppState):
def register(self):
self.register_transition('terminal')
def run(self):
self.log('Hello World!')
return 'terminal'
if __name__ == '__main__':
app.register()
server = Bottle()
server.mount('/api', api_server)
server.mount('/web', web_server)
server.run(host=host, port=port)

Building and running the app

To build your app, you can use app build command, which is equal to running docker build . --tag DOCKER_IMAGE_NAME :

$ featurecloud app build fc_test

Now, by checking available docker images, you can find fc_test . To execute the app, you should run the FeatureCloud controller first:

$ featurecloud controller start

and then execute the app in the FeatureCloud testbed

$ featurecloud test start --app-image fc_test

Finally, you can find the results in the FeatureCloud testbed.

--

--