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 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
  - "2.7"
# command to install dependencies
  - pip install tornado
  - pip install motor
  - pip install mongolog
  - pip install mock
  - pip install requests

  - mongodb

#This mongodb command should not have a newline in real file
- mongo --eval 'db.cosmos.users.insert(
         "roles": ["43425097-e630-41ea-88eb-17b339339706"],

# command to run tests
script: python 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.

-----------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 and 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 ..

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 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 for other endpoints.

After creating the project change the values in the file (or create a new file named 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 new-admin
Now start the project:
python start-service
And browse to http://localhost:8080/ to see the start page.