Skip to content
iidioteque edited this page Oct 12, 2012 · 1 revision
  1. Server installation
  2. Writing an app
  3. Deploying the app on the server
  4. Using the installed app

Ubuntu

1. Server Installation

1.1 Install the following packages:

$ sudo apt-get install \
cocaine-generic-slave=0.8.0-22.f13aecbc \
cocaine-server=0.8.0-22.f13aecbc \
cocaine-tools=0.8.0-22.f13aecbc \
libcocaine-common1=0.8.0-22.f13aecbc \
libcocaine-core1=0.8.0-22.f13aecbc \
libcocaine-plugin-dealer=0.8.0-4.a0dfec27 \
libcocaine-plugin-python=0.8.0-4.a0dfec27

1.2 Check that the installation was successful:

$ ps aux | grep -v grep | grep cocained

You should see the running daemon:

cocaine  32449  0.0  0.0 178156  5144 ?        Ssl  20:47   0:00 /usr/bin/cocained tcp://*:5000
--daemonize --pidfile /var/run/cocaine/cocained.pid --configuration /etc/cocaine/cocaine.conf

1.3 Check the list of the existing apps in Cocaine:

$ cocaine-info

If there was no apps deployed on the node earlier, then you will see something similar to the following output:

{
    u'jobs':
    {
        u'pending': 0,
        u'processed': 0
        },
        u'loggers': 5,
        u'route': u'my_host_name',
        u'sockets': 1,
        u'uptime': 9.6699349880218506
}

2. Writing an app

Let’s write an app on Python which contains one method that takes a string and adds to it the current date and returns the result.

from datetime import datetime

def add_time_func(io):
    # Read the request
    input_data = io.read()

    # Get the current time
    now = datetime.now()
    now_str = now.strftime("%Y-%m-%d %H:%M")

    # Concatenate the strings and return the response
    io.write(input_data + ': ' + now_str)

Save the code in a file named __init__.py

3. Deploying the app on the server

3.1 In order to install the app on the server, you’ll need to write a manifest in JSON format:

{
    "type" : "python",
    "engine" :
    {
        "heartbeat-timeout" : 60,
        "pool-limit" : 100,
        "queue-limit" : 1000,
        "grow-threshold" : 1
    },
    "drivers" :
    {
        "add_time_func" :
        {
            "emit" : "add_time_func",
            "endpoint" : "tcp://*:9903",
            "type" : "native-server",
            "backlog": 10000
        }
    }
}

Save the manifest in a file named manifest.json.

3.2 In order to delpoy the app, use the utility named cocaine-tool which accepts an app manifest and the app itself packed into an archive. You can pack the app with the following command:

$ tar czvf test_app.tar.gz __init__.py

Deploy it on the server:

$ sudo cocaine-tool upload -m manifest.json -p test_app.tar.gz -n cocaine_test

Please note that we are deploying this app under the name cocaine_test as follows from the above command. If all goes well we will see the following output in the console:

Detected app type: 'python', package compression: 'gzip'.
storage/core: creating the 'apps' namespace, path: '/var/lib/cocaine/apps'
The 'cocaine_test' app has been successfully deployed.

3.3 To complete the installation restart the server:

$ sudo /etc/init.d/cocaine-server restart

If all goes well cocaine-info will show the following information:

{
    u'apps': {
        u'cocaine_test': {
            u'drivers': {
                u'add_time_func': {
                    u'endpoint': u'tcp://my_host_name:9903',
                    u'route': u'my_host_name/cocaine_test/add_time_func',
                    u'type': u'native-server'
               }
            },
            u'queue-depth': 0,
            u'slaves': {
                u'busy': 0,
                u'total': 0
            },
            u'state': u'running'
        }
    },
    u'jobs': {
        u'pending': 0,
        u'processed': 0
    },
    u'loggers': 9,
    u'route': u'my_host_name',
    u'sockets': 3,
    u'uptime': 1.695981979370117
}

Done. You have successfully installed and deployed your first application cocaine_test.

4. Using the installed app

4.1 Install the client module (Cocaine Dealer) and Python bindings with the following command:

$ sudo apt-get install cocaine-framework-python=0.8.1-14.461ac70d eblob=0.16.7

4.2 Now you need to write the configuration files.

The first file should contain a list of nodes with our app (which can be IP-address or hostname). Let the name of the node on which the app was deployed will be my_host_name, write it to a new file named cloudmap.

The second file — a configuration file for Cocaine Dealer. It looks like this:

{
    "version" : 1,
    "services" : {
        "cocaine_test" : {
            "app" : "cocaine_test",
            "autodiscovery" : {
                "type" : "FILE",
                "source" : "/path/to/cloudmap"
            }
        }
    }
}

Save it to a file named dealer_config.json.

4.3 Write the following code on Python:

from cocaine.client import Client

c = Client('dealer_config.json')
print c.get('cocaine_test/add_time_func', 'cocaine server time', max_retries = -1)

In the end you’ll get the following output:

['cocaine server time: 2012-08-27 23:47']