Skip to content

Instantly share code, notes, and snippets.

@dansimau
Forked from urschrei/mkflask.sh
Created May 20, 2012 23:52
Show Gist options
  • Select an option

  • Save dansimau/2759962 to your computer and use it in GitHub Desktop.

Select an option

Save dansimau/2759962 to your computer and use it in GitHub Desktop.

Revisions

  1. dansimau revised this gist May 24, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions db/README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Run `fab db.build` to init alembic in this directory.
  2. dansimau revised this gist May 24, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion fabfile/db.py
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,11 @@
    from fabric.colors import cyan
    from utils import do

    config_file_path = 'db/alembic.ini'

    @task
    def build():
    """Initialise and migrate database to latest version."""
    print(cyan('\nUpdating database...'))
    with settings(hide('warnings'), warn_only=True):
    do('venv/bin/alembic init db/postgresql')
    do('venv/bin/alembic -c %s init db/postgresql' % config_file_path)
  3. dansimau revised this gist May 21, 2012. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,19 @@
    After running `fab virtualenv.build` to install packages from requirements.txt,
    you should freeze installed packages at their installed versions by running:
    ## Quick start

    venv/bin/pip freeze > requirements.txt
    After installing [Vagrant](http://vagrantup.com/), create and boot the VM:

    vagrant up

    SSH to the VM:

    vagrant ssh

    Run your app:

    fab app.run

    ## Notes

    After initial boot, you should freeze the newly-installed pip packages at their versions:

    pip freeze > requirements.txt
  4. dansimau created this gist May 20, 2012.
    4 changes: 4 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    *~
    *.pyc
    .vagrant
    venv
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    After running `fab virtualenv.build` to install packages from requirements.txt,
    you should freeze installed packages at their installed versions by running:

    venv/bin/pip freeze > requirements.txt
    55 changes: 55 additions & 0 deletions Vagrantfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    # -*- mode: ruby -*-
    # vi: set ft=ruby :

    Vagrant::Config.run do |config|
    # All Vagrant configuration is done here. The most common configuration
    # options are documented and commented below. For a complete reference,
    # please see the online documentation at vagrantup.com.

    # Every Vagrant virtual environment requires a box to build off of.
    config.vm.box = "precise64"

    # The url from where the 'config.vm.box' box will be fetched if it
    # doesn't already exist on the user's system.
    config.vm.box_url = "http://files.vagrantup.com/precise64.box"

    # Boot with a GUI so you can see the screen. (Default is headless)
    # config.vm.boot_mode = :gui

    # Assign this VM to a host-only network IP, allowing you to access it
    # via the IP. Host-only networks can talk to the host machine as well as
    # any other machines on the same network, but cannot be accessed (through this
    # network interface) by any external networks.
    config.vm.network :hostonly, "172.16.1.2"

    # Assign this VM to a bridged network, allowing you to connect directly to a
    # network using the host's network device. This makes the VM appear as another
    # physical device on your network.
    # config.vm.network :bridged

    # Forward a port from the guest to the host, which allows for outside
    # computers to access the VM, whereas host only networking does not.
    config.vm.forward_port 5000, 5000

    # Share an additional folder to the guest VM. The first argument is
    # an identifier, the second is the path on the guest to mount the
    # folder, and the third is the path on the host to the actual folder.
    config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true)

    # Update apt
    config.vm.provision :shell, :inline => "aptitude -q2 update"

    # Enable provisioning with Puppet stand alone. Puppet manifests
    # are contained in a directory path relative to this Vagrantfile.
    # You will need to create the manifests directory and a manifest in
    # the file base.pp in the manifests_path directory.
    config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "puppet/manifests"
    puppet.module_path = "puppet/modules"
    puppet.manifest_file = "standalone.pp"
    end

    # Application provision
    config.vm.provision :shell, :inline => "cd /vagrant && stdbuf -o0 fab build; exit 0"

    end
    5 changes: 5 additions & 0 deletions app/__init__.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    from flask import Flask

    app = Flask(__name__)
    app.config.from_pyfile('%s/config/default.cfg' % app.root_path)
    app.config.from_envvar('FLASK_CONFIG')
    1 change: 1 addition & 0 deletions app/config/default.cfg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    SECRET_KEY = ''
    1 change: 1 addition & 0 deletions app/config/dev.cfg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    DEBUG = True
    12 changes: 12 additions & 0 deletions fabfile/__init__.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    from fabric.decorators import task

    import app
    import db
    import puppet
    import virtualenv

    @task
    def build():
    """Execute build tasks for all components."""
    virtualenv.build()
    db.build()
    8 changes: 8 additions & 0 deletions fabfile/app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    from fabric.decorators import task
    from fabric.context_managers import settings
    from utils import do

    @task
    def run():
    """Start app in debug mode (for development)."""
    do('export FLASK_CONFIG=$PWD/app/config/dev.cfg && venv/bin/python ./run.py')
    13 changes: 13 additions & 0 deletions fabfile/config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    remote_path = ''

    branches = {
    'staging': {
    'hosts': ''
    },
    'uat': {
    'hosts': ''
    },
    'production': {
    'hosts': ''
    },
    }
    11 changes: 11 additions & 0 deletions fabfile/db.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    from fabric.decorators import task
    from fabric.context_managers import settings, hide
    from fabric.colors import cyan
    from utils import do

    @task
    def build():
    """Initialise and migrate database to latest version."""
    print(cyan('\nUpdating database...'))
    with settings(hide('warnings'), warn_only=True):
    do('venv/bin/alembic init db/postgresql')
    16 changes: 16 additions & 0 deletions fabfile/puppet.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    from fabric.api import env
    from fabric.decorators import task
    from fabric.colors import cyan
    from utils import do

    @task
    def check():
    """Syntax check on Puppet config."""
    print(cyan('\nChecking puppet syntax...'))
    do('find puppet -type f -name \'*.pp\' |xargs puppet parser validate')

    @task
    def apply():
    """Apply Puppet manifest."""
    print(cyan('\nApplying puppet manifest...'))
    do('sudo puppet apply --modulepath=puppet/modules/ puppet/manifests/standalone.pp' % env)
    35 changes: 35 additions & 0 deletions fabfile/utils.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    from fabric.api import require, env, local, run as fab_run
    from fabric.utils import abort
    import config

    def do(*args, **kwargs):
    """
    Runs command locally or remotely depending on whether a remote host has
    been specified.
    """
    if env.host_string:
    with settings(cd(config.remote_path)):
    return fab_run(*args, **kwargs)
    else:
    return local(*args, **kwargs)

    def require_host():
    """
    Forces a remote host to be set, automatically detecting it from the current
    git branch if possible.
    """
    if not env.host_string:

    # Detect current branch from git, based on config
    branch = local('git symbolic-ref -q HEAD', capture=True).split('/')[2]

    # Ensure the current branch matches up to a known branch
    if branch in config.branches:
    env.branch = branch

    require('branch')

    # Manually set host_string variable
    env.host_string = env.branches[branch]['hosts']

    require('host_string')
    19 changes: 19 additions & 0 deletions fabfile/virtualenv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    from fabric.decorators import task
    from fabric.context_managers import settings, hide
    from fabric.colors import cyan, red
    from fabric.utils import abort
    from utils import do

    @task
    def build():
    """Build or update the virtualenv."""
    with settings(hide('stdout')):
    print(cyan('\nUpdating venv, installing packages...'))
    do('[ -e venv ] || virtualenv venv --no-site-packages')
    # annoyingly, pip prints errors to stdout (instead of stderr), so we
    # have to check the return code and output only if there's an error.
    with settings(warn_only=True):
    pip = do('venv/bin/pip install -r requirements.txt', capture=True)
    if pip.failed:
    print(red(pip))
    abort("pip exited with return code %i" % pip.return_code)
    25 changes: 25 additions & 0 deletions puppet/manifests/lib/line.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    define line($file, $line, $ensure = 'present') {
    case $ensure {
    default : { err ( "unknown ensure value ${ensure}" ) }
    present: {
    exec { "/bin/echo '${line}' >> '${file}'":
    unless => "/bin/grep -qFx '${line}' '${file}'"
    }
    }
    absent: {
    exec { "/bin/grep -vFx '${line}' '${file}' | /usr/bin/tee '${file}' >/dev/null 2>&1":
    onlyif => "/bin/grep -qFx '${line}' '${file}'"
    }
    }
    uncomment: {
    exec { "/bin/sed -i -e'/${line}/s/#\\+//' '${file}'":
    onlyif => "/bin/grep '${line}' '${file}' | /bin/grep '^#' | /usr/bin/wc -l"
    }
    }
    comment: {
    exec { "/bin/sed -i -e'/${line}/s/\\(.\\+\\)$/#\\1/' '${file}'":
    onlyif => "/usr/bin/test `/bin/grep '${line}' '${file}' | /bin/grep -v '^#' | /usr/bin/wc -l` -ne 0"
    }
    }
    }
    }
    11 changes: 11 additions & 0 deletions puppet/manifests/standalone.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #
    # Standalone manifest - for dev Vagrant box.
    #

    import 'lib/*.pp'

    include fabric
    include git
    include postgresql
    include python
    include vagrant
    6 changes: 6 additions & 0 deletions puppet/modules/fabric/manifests/init.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class fabric {
    package { 'Fabric':
    provider => 'pip',
    ensure => 'present',
    }
    }
    5 changes: 5 additions & 0 deletions puppet/modules/git/manifests/init.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    class git {
    package { 'git':
    ensure => 'installed',
    }
    }
    10 changes: 10 additions & 0 deletions puppet/modules/postgresql/manifests/init.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class postgresql {
    # postgresql-dev required for Python's psycopg2
    package { [ 'postgresql', 'postgresql-server-dev-all' ]:
    ensure => 'installed',
    }
    service { 'postgresql':
    ensure => running,
    require => Package[postgresql],
    }
    }
    6 changes: 6 additions & 0 deletions puppet/modules/python/manifests/init.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class python {
    include python::modules
    package { 'python':
    ensure => installed,
    }
    }
    5 changes: 5 additions & 0 deletions puppet/modules/python/manifests/modules.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    class python::modules {
    package { [ 'python-virtualenv', 'python-dev', ]:
    ensure => 'installed',
    }
    }
    7 changes: 7 additions & 0 deletions puppet/modules/vagrant/manifests/init.pp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class vagrant {
    line { 'line-venv-activate':
    ensure => present,
    file => '/home/vagrant/.bashrc',
    line => 'cd /vagrant && . venv/bin/activate',
    }
    }
    5 changes: 5 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    alembic
    Flask
    Flask-SQLAlchemy
    Flask-Assets
    psycopg2
    3 changes: 3 additions & 0 deletions run.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #!/usr/bin/env python
    from app import app
    app.run(host='0.0.0.0')