Hawkes Blog
Windows reboot | Linux be root
Hawkes Blog

Deploy Pelican with Fabric

Short How-To: deploy your pelican blog with fabric

as you might know, this blog is powered by pelican

I use my hp n54l at home to write articles, make changes on this blog and push these changes to my webserver for testing and release with fabric.


1. install and prepare

fabric

apt-get update; apt-get install python-pip
pip install fabric
I had created a dedicated user for my blog.
the homedir is set to /var/www
login via ssh is only allowed with keys


2. create a fabfile.py

switch to the base directory of your pelican blog (in my case ~/DEV/pelican-blog)
and create a file called fablife.py
my fabfile.py (should be self-explaining)
import fabric.api as fab

import fabric.contrib.project as project
import os
import shutil

# Remote server configuration
# user@host:port
host = [ "blog@the-hawkes.de:44443" ]

# Local path configuration (can be absolute or relative to fabfile)
#local build path
fab.env.deploy_path    = "blog-dev"
#my pelican theme static folder
fab.env.static_path    = "mybootstrap/static"
#source path of external sources + git path
fab.env.dev_path       = "/root/DEV"
fab.env.git_path       = "{dev_path}/git-source".format(**fab.env)
#beta deploy path on the webserver
fab.env.beta_dest_path = "/var/www/beta-the-hawkes/htdocs/"
#the live deploy path on the webserver
fab.env.live_dest_path = "/var/www/the-hawkes/htdocs/"


def clean():
    """
        do a local cleanup
    """
    if os.path.isdir(fab.env.deploy_path):
        fab.local("rm -rf {deploy_path}".format(**fab.env))
        fab.local("mkdir {deploy_path}".format(**fab.env))


def build():
    """
        build local only
    """
    clean()
    fab.local("pelican -s pelicanconf.py -o {deploy_path}".format(**fab.env))


def updatedep():
    """
        update all external sources from my github folder
    """
    if os.path.isdir(fab.env.static_path):
        shutil.rmtree(fab.env.static_path)
        fab.local("mkdir {static_path}".format(**fab.env))
    fab.local("cp -a {git_path}/bootstrap/dist {static_path}/bootstrap".format(**fab.env))
    fab.local("cp {git_path}/twitter-bootstrap-hover-dropdown/twitter-bootstrap-hover-dropdown.min.js {static_path}/bootstrap/js/".format(**fab.env))
    fab.local("mkdir {static_path}/font-awesome".format(**fab.env))
    fab.local("cp -a {git_path}/Font-Awesome/fonts {static_path}/font-awesome/".format(**fab.env))
    fab.local("cp -a {git_path}/Font-Awesome/css {static_path}/font-awesome/".format(**fab.env))
    fab.local("mkdir {static_path}/jquery".format(**fab.env))
    fab.local("wget --directory-prefix={static_path}/jquery http://code.jquery.com/jquery-latest.min.js".format(**fab.env))
    fab.local("cp -a {git_path}/swipebox/source {static_path}/swipebox".format(**fab.env))
    fab.local("cp -a {dev_path}/syntax {static_path}/".format(**fab.env))
    fab.local("cp {git_path}/custom.css {static_path}".format(**fab.env))


@fab.hosts(host)
def fixpermissions(path):
    """
        need to fix permissions after deploy because the blog source
        is located on a samba share
    """
    fab.run("find {0} -type d -exec chmod 755 {{}} \;".format(path))
    fab.run("find {0} -type f -exec chmod 644 {{}} \;".format(path))


@fab.hosts(host)
def beta():
    """
        build my beta blog
        http://beta.the-hawkes.de

        testing new posts and changes
    """
    build()
    project.rsync_project(
        remote_dir=fab.env.beta_dest_path,
        #excluded files
        exclude="",
        local_dir=fab.env.deploy_path.rstrip("/") + "/",
        delete=True
    )
    #always fix permissions after deploy
    fixpermissions(fab.env.beta_dest_path)


@fab.hosts(host)
def live():
    """
        build and deploy live blog
        http://www.the-hawkes.de
    """
    build()
    project.rsync_project(
        remote_dir=fab.env.live_dest_path,
        #excluded files
        exclude="",
        local_dir=fab.env.deploy_path.rstrip("/") + "/",
        delete=True
    )
    #always fix permissions after deploy
    fixpermissions(fab.env.live_dest_path)

Small Info:

  • fab.run -> remote execution
  • fab.local -> local execution
  • project.rsync_project -> use rsync to deploy

3. test and deploy

A typical deploy

cd ~/DEV/pelican-blog
fab clean updatedep beta
  • cleans the output folder
  • it updates all external dependences
  • build the blog
  • deploy it to my webserver as the new beta version

after I reviewed my changes

fab live
  • simply rebuild and deply to the the live folder

get a list of all available fab commands

fab -l
Available commands:

beta            build my beta blog
build           build local
clean           do a local cleanup
fixpermissions  need to fix permissions after deploy because the blog source
live            build and deploy live blog
updatedep       update all external sources from my github folder

my fabfile.py is still under development and I will always try to improve the way I deploy. Maybe I will add some new function later.

MfG Hawkes


comments powered by Disqus