Enable vagrant env for development.

This commit is contained in:
Tom Price
2015-07-21 21:24:58 +01:00
parent 163c77880b
commit 9220363369
9 changed files with 413 additions and 1 deletions

11
config/vagrant.yml Normal file
View File

@@ -0,0 +1,11 @@
django:
settings_module: PyRIGS.settings
virtualenv:
envname: myvirtualenv
foreman:
procfile: Procfile
db:
name: postgres_db
user: postgres_db
password: postgres_db

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Via https://github.com/kiere/vagrant-heroku-cedar-14/blob/master/config/vagrant/build_dependency_setup.sh
echo "=== Begin Vagrant Provisioning using 'config/vagrant/build_dependency_setup.sh'"
# Install build dependencies for a sane build environment
apt-get -y update
apt-get -y install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
# Other things that we may need installed before anything else.
apt-get install -y libmemcached-dev
apt-get build-dep python-lxml
echo "=== End Vagrant Provisioning using 'config/vagrant/build_dependency_setup.sh'"

View File

@@ -0,0 +1,36 @@
#!/bin/bash
# Install and (if there's a Procfile) start foreman.
# Needs to come after the virtualenv has been set up.
# Expects three arguments:
VIRTUALENV_NAME=$1
DJANGO_SETTINGS_MODULE=$2
PROCFILE=$3
echo "=== Begin Vagrant Provisioning using 'config/vagrant/foreman_setup.sh'"
gem install foreman --no-ri --no-rdoc
if ! grep -Fq "DJANGO_SETTINGS_MODULE" /home/vagrant/.bashrc; then
echo "export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}" >> /home/vagrant/.bashrc
fi
if [[ -f /vagrant/$PROCFILE ]]; then
echo "Procfile found; starting foreman."
export DJANGO_SETTINGS_MODULE="$DJANGO_SETTINGS_MODULE"
# Ensure the virtualenv settings in .profile are loaded:
source /home/vagrant/.profile
# Run with & to release the terminal.
# Although that may also rely on the Procfile's processes having their
# output sent to a file, not stdout/stderr.
foreman start -f /vagrant/$PROCFILE &
else
echo "No Procfile found; not starting foreman."
fi
echo "=== End Vagrant Provisioning using 'config/vagrant/foreman_setup.sh'"

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Via https://github.com/kiere/vagrant-heroku-cedar-14/blob/master/config/vagrant/git_setup.sh
echo "=== Begin Vagrant Provisioning using 'config/vagrant/git_setup.sh'"
# Install Git if not available
if [ -z `which git` ]; then
echo "===== Installing Git"
apt-get -y update
apt-get -y install git-core
fi
echo "=== End Vagrant Provisioning using 'config/vagrant/git_setup.sh'"

View File

@@ -0,0 +1,112 @@
#!/bin/sh -e
# Expects three arguments:
# $1 - database name
# $2 - database user
# #3 - database user password
# Via https://github.com/kiere/vagrant-heroku-cedar-14/blob/master/config/vagrant/postgresql_setup.sh
echo "=== Begin Vagrant Provisioning using 'config/vagrant/postgresql_setup.sh'"
APP_DB_NAME=$1
APP_DB_USER=$2
APP_DB_PASS=$3
# Edit the following to change the version of PostgreSQL that is installed
PG_VERSION=9.4
###########################################################
# Changes below this line are probably not necessary
###########################################################
print_db_usage () {
echo "Your PostgreSQL database has been setup and can be accessed on your local machine on the forwarded port (default: 15432)"
echo " Host: localhost"
echo " Port: 15432"
echo " Database: $APP_DB_NAME"
echo " Username: $APP_DB_USER"
echo " Password: $APP_DB_PASS"
echo ""
echo "Admin access to postgres user via VM:"
echo " vagrant ssh"
echo " sudo su - postgres"
echo ""
echo "psql access to app database user via VM:"
echo " vagrant ssh"
echo " sudo su - postgres"
echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost $APP_DB_NAME"
echo ""
echo "Env variable for application development:"
echo " DATABASE_URL=postgresql://$APP_DB_USER:$APP_DB_PASS@localhost:15432/$APP_DB_NAME"
echo ""
echo "Local command to access the database via psql:"
echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost -p 15432 $APP_DB_NAME"
}
export DEBIAN_FRONTEND=noninteractive
PROVISIONED_ON=/etc/vm_provision_on_timestamp
if [ -f "$PROVISIONED_ON" ]
then
echo "VM was already provisioned at: $(cat $PROVISIONED_ON)"
echo "To run system updates manually login via 'vagrant ssh' and run 'apt-get update && apt-get upgrade'"
echo ""
print_db_usage
exit
fi
PG_REPO_APT_SOURCE=/etc/apt/sources.list.d/pgdg.list
if [ ! -f "$PG_REPO_APT_SOURCE" ]
then
# Add PG apt repo:
echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > "$PG_REPO_APT_SOURCE"
# Add PGDG repo key:
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add -
fi
# Update package list and upgrade all packages
apt-get update
apt-get -y upgrade
apt-get -y install "postgresql-$PG_VERSION" "postgresql-contrib-$PG_VERSION"
apt-get -y install libpq-dev # For building ruby 'pg' gem
PG_CONF="/etc/postgresql/$PG_VERSION/main/postgresql.conf"
PG_HBA="/etc/postgresql/$PG_VERSION/main/pg_hba.conf"
PG_DIR="/var/lib/postgresql/$PG_VERSION/main"
# Edit postgresql.conf to change listen address to '*':
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" "$PG_CONF"
# Append to pg_hba.conf to add password auth:
echo "host all all all md5" >> "$PG_HBA"
# Explicitly set default client_encoding
echo "client_encoding = utf8" >> "$PG_CONF"
# Restart so that all new config is loaded:
service postgresql restart
cat << EOF | su - postgres -c psql
-- Create the database user:
CREATE USER $APP_DB_USER PASSWORD '$APP_DB_PASS';
EOF
cat << EOF | su - postgres -c psql
-- Create the database:
CREATE DATABASE $APP_DB_NAME WITH OWNER=$APP_DB_USER
LC_COLLATE='en_US.utf8'
LC_CTYPE='en_US.utf8'
ENCODING='UTF8'
TEMPLATE=template0;
EOF
# Tag the provision time:
date > "$PROVISIONED_ON"
echo "Successfully created PostgreSQL dev virtual machine."
echo ""
print_db_usage
echo "=== End Vagrant Provisioning using 'config/vagrant/postgresql_setup.sh'"

View File

@@ -0,0 +1,23 @@
#!/bin/bash
# Install python and required python modules.
# pip and virtualenv are in virtualenv_setup.sh
# Initial part of this via
# https://github.com/torchbox/vagrant-django-base/blob/master/install.sh
echo "=== Begin Vagrant Provisioning using 'config/vagrant/python_setup.sh'"
apt-get update -y
# Python dev packages
apt-get install -y python python-dev python-setuptools python-pip
# Dependencies for image processing with Pillow (drop-in replacement for PIL)
# supporting: jpeg, tiff, png, freetype, littlecms
apt-get install -y libjpeg-dev libtiff-dev zlib1g-dev libfreetype6-dev liblcms2-dev
# lxml dependencies
apt-get install -y libxml2-dev libxslt1-dev python-dev
echo "=== End Vagrant Provisioning using 'config/vagrant/python_setup.sh'"

View File

@@ -0,0 +1,66 @@
#!/bin/bash
# This will:
# * Install pip, virtualenv and virtualenvwrapper.
# * Set up virtualenvwrapper's paths etc.
# * Create a new virtualenv.
# * Install any pip requirements from the requirements.txt file.
# Expects one argument, the name of the virtualenv.
# The name we'll use for the virtualenv in which we'll install requirements:
VENV_NAME=$1
echo "=== Begin Vagrant Provisioning using 'config/vagrant/virtualenv_setup.sh'"
# virtualenv global setup
if ! command -v pip; then
easy_install -U pip
fi
if [[ ! -f /usr/local/bin/virtualenv ]]; then
easy_install virtualenv virtualenvwrapper
fi
# If it doesn't look like .bashrc has the required virtualenvwrapper lines in,
# then add them.
if ! grep -Fq "WORKON_HOME" /home/vagrant/.profile; then
echo "Adding virtualenvwrapper locations to .profile"
if [[ -d /vagrant/config/virtualenvwrapper/vagrant ]]; then
echo "export VIRTUALENVWRAPPER_HOOK_DIR=/vagrant/config/virtualenvwrapper/vagrant" >> /home/vagrant/.profile
fi
echo "export WORKON_HOME=/home/vagrant/.virtualenvs" >> /home/vagrant/.profile
echo "export PROJECT_HOME=/home/vagrant/Devel" >> /home/vagrant/.profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/vagrant/.profile
fi
# Get .virtualenvwrapper env variables set up:
source /home/vagrant/.profile
if [[ -d /home/vagrant/.virtualenvs/$VENV_NAME ]]; then
echo "Activating virtualenv $VENV_NAME."
workon $VENV_NAME
else
echo "Making new virtualenv $VENV_NAME."
# Also switches to the virtualenv:
mkvirtualenv $VENV_NAME
# So that we can install things with pip while ssh'd in as vagrant user:
sudo chown -R vagrant:vagrant /home/vagrant/.virtualenvs/$VENV_NAME/
# Automatically switch to the virtual env on log in:
echo "workon $VENV_NAME" >> /home/vagrant/.profile
fi
# If we have a requirements.txt file in this project, then install
# everything in it with pip in a new virtualenv.
if [[ -f /vagrant/requirements.txt ]]; then
echo "Installing from ./requirements.txt with pip."
pip install -r /vagrant/requirements.txt
fi
echo "=== End Vagrant Provisioning using 'config/vagrant/virtualenv_setup.sh'"