Posts Tagged ‘deployment’

Heroku: development and production settings

November 2, 2013 Leave a comment

You have a project that you develop on your local machine and you deploy it on Heroku for instance. The two environments require different settings. For example, you test your app. with SQLite but in production you use PostgreSQL. How can the application configure itself to its environment?

I show you how to do it with Flask.

In your project folder:

$ heroku config:set HEROKU=1

It will create an environment variable at Heroku. These environment variables are persistent – they will remain in place across deploys and app restarts – so unless you need to change values, you only need to set them once.

Then create a file in your project folder:

import os

class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite://:memory:'

class ProductionConfig(Config):
    REDIS_URI = os.environ.get('REDISTOGO_URL')

class DevelopmentConfig(Config):
    DEBUG = True
    REDIS_URI = 'redis://localhost:6379'

class TestingConfig(Config):
    TESTING = True

Of course, you will have to customize it with your own settings.

Then, in your main file:

app = Flask(__name__)

if 'HEROKU' in os.environ:
    # production on Heroku
    # development on localhost

Now, if you want to access the configuration from different files of the project, use this:

from flask import current_app as app

Let’s see how to use Redis for instance. Apply the same idea with other databases too. Opening and closing can go in the before_request and teardown_request functions:

from flask import g
import redis

def before_request():
    g.redis = redis.from_url(app.config['REDIS_URI'])

def teardown_request(exception):
    pass    # g.redis doesn't need to be closed explicitly

If you need to access redis from other files, just import g and use g.redis .


deploy a git project to a remote host

October 23, 2013 Leave a comment

You have a project that you develop on your local machine. You want to deploy it (upload) to a production server.

For instance, you have a homepage and you want to upload it to your university server.

I assume your project on the local machine is stored in a dedicated folder. Turn this folder into a git repository. Example:

$ cd /home/jabba/projects/homepage
$ cat index.html
Awesome homepage v0
$ cat .gitignore
$ git init
$ git add .
$ git commit -m "initial commit"

We also need an uploader script called :

git ls-files -z | rsync --files-from - --copy-links -av0 . --rsh="ssh -p$TO_PORT" $TO_HOST:$TO_DIR

Just launch the script and it will copy all the checked in files to the server.

However! If you remove some files on your local machine, this script won’t remove them from the remote host. That is, you will have to delete files which got removed from the project.

This tip is from here.

Related work

  • Using Git to manage a web site (Shortly: you have a local git repo with your project. You create another git repo on the remote host and you push changes to this remote git repo. A customized post-receive hook will copy everything to the specified destination folder.)
Categories: bash Tags: , ,