Friday, July 11, 2014

Continuous integration using Travis CI

It has been tough keeping the frameworks functionality working after the basic prototype. The prototype is great - it has most basic features. But adding new feature or fixing bugs was breaking functionality here and there. So, instead of coding new functionality I have added about 21 tests. Most of them are functional tests at this moment. And while writing them design and functionality bugs were revealed and fixed. I strongly feel we need more tests before adding any more functionality, but for now the tests are covering most of the code path and can fix remaining bugs and start writing unit tests.

I have added the project to Travis CI which is great free tool to build and integration test. The following image shows current status of build/ test run:

Build Status

It runs every time a new push is made to the Github repository. This image is actually dynamic and shows current status of Cosmos Framework. If you can not see it look here.

Setting up TravisCI is easy. First login to https://travis-ci.org/ using your Github account and Travis CI will show the list of public repositories. Now enable any repository Travis should track. Then we need to add a file named .travis.yml at the root of the repository. This file contains basic settings like language and script to run. For cosmos we need MongoDB which can also be added usnder required services. Here is the first version of the configuration file being currently used to run build and tests:


language: python
python:
  - "2.7"
# command to install dependencies
install:
  - pip install tornado
  - pip install motor
  - pip install mongolog
  - pip install mock
  - pip install requests

services:
  - mongodb

before_script:
#This mongodb command should not have a newline in real file
- mongo 127.0.0.1/cosmos --eval 'db.cosmos.users.insert(
     {
         "username":"admin",
         "roles": ["43425097-e630-41ea-88eb-17b339339706"],
         "email":"admin@cosmosframework.com",
         "password":"hmac:1ddb4e6b0b810f59018babbb5dc0eed4"
     });'

# command to run tests
script: python setup.py test

We are installing the required python packages and adding mongodb as required service. Before running script we want to add our admin user to mongodb which is done in before_script section.

When we add this file, commit and push the change to Github, Travis gets notification using webhook about the change and start deploying a system and run tests. It shows the status using an image (as shown above) that changes based on the build/test result.

This is a very nice free service which you may use for your opensource project and for each change it will run your tests and report if anything is broken.

Wednesday, July 2, 2014

How to create angular app and host on heroku using cosmos frameowk in 5 simple steps

Angular seed project is configured to use npm to do basic tasks like download required packages. You should have node installed on your machine. You won't need it after you download the required package. Cosmos will host the application using tornado in both development and production environment. In production use a reverse proxy like nginx to serve the static files. Now follow the steps to create and run the angular-seed application.

Create angular project using cosmosadmin

If you do not have mongodb or virtualenv installed install those first. For ubuntu its like:

sudo apt-get install mongodb
sudo apt-get install python-virtualenv

First create a virtualenv and activate it:

virtualenv --no-site-packages testenv
source testenv/bin/activate
Now install cosmos inside it:
pip install cosmos

Run new-project command with angular from console from any directory to create new project:

cosmosadmin new-project angular

This will create basic cosmos application and download angular-seed project from github. Here is sample output.

new-project
-----------Cloning angular seed project--------------

Cloning into 'angular-seed'...
remote: Counting objects: 2475, done.
remote: Compressing objects: 100% (1237/1237), done.
remote: Total 2475 (delta 1071), reused 2475 (delta 1071)
Receiving objects: 100% (2475/2475), 13.80 MiB | 2.19 MiB/s, done.
Resolving deltas: 100% (1071/1071), done.
Checking connectivity... done.
----------- You should run "npm install" from angular-seed directory now -------------

You should now open settings.py and local_settings.py files to adjust database and other settings.

Now change directory to angular-seed and run npm install:

cd angular-seed
npm install

This will download all required packages for angular-seed application.

Now go back to project directory and run:

cd ..
python cosmosmain.py

You may now browse to http://localhost:8080/ to view your newly created application.

Host the app on heroku

If you want to host your application on heroku you need Procfile and requirements.txt files. You may create these files using following command:

python cosmosmain.py add-herokusettings

Now run the application using foreman (you should have heroku toolbelt installed on the machine):

foreman start

You may now browse to http://localhost:5000/ to view your application. Now commit and push the application to heroku for deployment.

Tuesday, July 1, 2014

How to install cosmos framework

The framework is uploaded to python package index to make it simple to install as library. It is recommended that you use virtualenv, but it is not required.

First create a virtualenv and activate it:

virtualenv --no-site-packages testenv
source testenv/bin/activate
Now install cosmos inside it:
pip install cosmos
A console script named cosmosadmin has now been created for administration tasks. Run new-project command from console from any directory to create new project:
cosmosadmin new-project

The app folder is the root of the webserver in the create project by default. Index page is mapped to serve templates/index.html with current_user passed to it. The /service/* endpoint is mapped to the service handler. You may look into endpoints.py for other endpoints.

After creating the project change the values in the settings.py file (or create a new file named local_settings.py nad add it to your .gitignore file- so this settings does not end up in the source repository) for the database and create admin user:

python cosmosmain.py new-admin
Now start the project:
python cosmosmain.py start-service
And browse to http://localhost:8080/ to see the start page.