Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop' into feature/gis to aid development of the GIS…
Browse files Browse the repository at this point in the history
… features.

Conflicts:
	cookbooks/main/recipes/default.rb
	cookbooks/postgres/recipes/default.rb
	cookbooks/python/recipes/default.rb
  • Loading branch information
d0ugal committed Sep 14, 2011
2 parents 0789987 + 62a8453 commit 68ae0f7
Show file tree
Hide file tree
Showing 18 changed files with 551 additions and 181 deletions.
8 changes: 8 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The chef recipies where originally created by Dougal Matthews.

The following is a list of all the contributors.

Dougal Matthews <dougal85@gmail.com> https://github.com/d0ugal
Roger Boardman https://github.com/boardman
Vasil Vangelovski https://github.com/vvangelovski
Mathew Taylor https://github.com/Mathew
8 changes: 8 additions & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The chef recipies where originally created by Dougal Matthews.

The following is a list of all the contributors.

Dougal Matthews <dougal85@gmail.com> https://github.com/d0ugal
Roger Boardman https://github.com/boardman
Vasil Vangelovski https://github.com/vvangelovski
Mathew Taylor https://github.com/Mathew
27 changes: 27 additions & 0 deletions LICENCE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) Dougal Matthews and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of django-taggit nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 changes: 4 additions & 40 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
==========================================================
``Chef Recipies`` -- For working with Vagrant.
``Python Stack`` -- For local development and production
==========================================================

A set of Chef recipies that I am using to provision virtual machines with
Vagrant for **local development**. They cut corners, do bad things and for this
reason should **not** be used for production/deployment or any publicly
visible servers - however, they could be adapted fairly easily to work well
with both.

Please excuse my poor attempt at Ruby. This is my first attempt, suggestions
and patches are welcome.


Try It
==========================================================

gem install vagrant

cd to somewhere you want to mame a vagrant project

vagrant init

Replace the VagrantFile contents with vagrant_files/python_postgres in this repo

vagrant up

Hopefully that works. Otherwise, go to http://vagrantup.com/ for help.


What's on the box?
==========================================================

Currently this is tailored very much towards the tools that I liked to use.
However I'd really like to add more functionality and make it generic so pull
requests are very welcome.

A few system libraries that I frequently use; ack, vim, git, svn, libxml-dev

Some enforcement of my own personal development rules (pip wont work outside
of a virtualenv). However, you will have a virtualenv setup with the name that
you set the PROJECT_NAME to in the Vagrantfile.

A set of Chef recipies that I am using to provision virtual machines both for
development and production. This is a highly opinionated stack and very much
focused towards Python.
30 changes: 2 additions & 28 deletions cookbooks/main/files/default/bashrc_extra
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,16 @@ export GIT_EDITOR=nano
alias ack='ack-grep'
alias hack='history | ack -i '

# I always forget the v in vim.
alias vi='vim'

# Nice ls tweaks.
alias ls='ls -F'
alias ll='ls -lh'

# extract archives -- usage: extract </path/to/archive/>
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip "$1" ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

# Prevent pip from doing anything unless you're inside an activated virtualenv.
function pip {
PIP=`which pip`
if [ "x$VIRTUAL_ENV" = 'x' ]; then
echo "No virtualenv activated; bailing."
echo "No virtualenv activated. If you really want to make global changes, sudo it."
else
$PIP "$@"
fi
}

}
71 changes: 59 additions & 12 deletions cookbooks/main/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
=begin
This recipe is a utility recipe, installing a number of additions that are
useful and required 90% of the time.
=end

execute "update-apt" do
command "sudo apt-get update"
end

%w{ack-grep aptitude vim git-core subversion mercurial libxml2-dev}.each do |pkg|
# Install Ack, Git, Hg, SVN, xml libs and jpeg libs.
%w{psmisc ack-grep aptitude vim git-core subversion mercurial libxml2-dev
libjpeg62-dev zlib1g-dev}.each do |pkg|
package pkg do
action :install
end
Expand All @@ -16,7 +23,8 @@
end
end

# Allow the user to specify some other system packages to install
# On a project basis, allow a set of packages to be defined that will be
# installed at almost the start of the setup process.
if node.has_key?("system_packages")
node[:system_packages].each do |pkg|
package pkg do
Expand All @@ -25,15 +33,54 @@
end
end

cookbook_file "/home/vagrant/.bashrc_extra" do
source "bashrc_extra"
mode 0640
owner "vagrant"
group "vagrant"
action :create_if_missing
end
if node.has_key?("user")

user_info = node[:user]

user user_info[:username] do
shell "/bin/bash"
supports :manage_home => true
home "/home/#{user_info[:username]}"
end

directory "/home/#{user_info[:username]}/.ssh" do
owner user_info[:username]
group user_info[:username]
mode 0700
end

if user_info.has_key?("ssh_key")
file "/home/#{user_info[:username]}/.ssh/authorized_keys" do
owner user_info[:username]
group user_info[:group]
mode 0600
content user_info[:ssh_key]
end
end

group user_info[:group] do
members user_info[:username]
append true
end

directory "/home/#{user_info[:username]}" do
owner user_info[:username]
group user_info[:group]
mode 0775
end


cookbook_file "/home/#{user_info[:username]}/.bashrc_extra" do
source "bashrc_extra"
mode 0640
owner user_info[:username]
group user_info[:group]
action :create_if_missing
end

execute "source-bachrc-extra" do
command "echo \"source /home/#{node[:user_name]}/.bashrc_extra\" >> /home/#{node[:user_name]}/.bashrc"
not_if "grep bashrc_extra /home/#{node[:user_name]}/.bashrc"
end

execute "source-bachrc-extra" do
command "echo \"source /home/vagrant/.bashrc_extra\" >> /home/vagrant/.bashrc"
not_if "grep bashrc_extra /home/vagrant/.bashrc"
end
2 changes: 0 additions & 2 deletions cookbooks/postgres/files/default/pg_hba.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@

local all postgres ident
local all all password
host all all 0.0.0.0/0 md5

5 changes: 5 additions & 0 deletions cookbooks/postgres/files/default/pg_hba_dev.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#TYPE DATABASE USER CIDR-ADDRESS METHOD

local all postgres ident
local all all password
host all all 0.0.0.0/0 md5
107 changes: 63 additions & 44 deletions cookbooks/postgres/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,86 @@
end
end

cookbook_file "/etc/postgresql/8.4/main/pg_hba.conf" do
source "pg_hba.conf"
pg_hba_dev = node.has_key?("dev_env") and node.dev_env

if pg_hba_dev
pg_hba_conf = "/etc/postgresql/8.4/main/pg_hba_dev.conf"
pg_hba_conf_source = "pg_hba_dev.conf"
else
pg_hba_conf = "/etc/postgresql/8.4/main/pg_hba.conf"
pg_hba_conf_source = "pg_hba.conf"
end

cookbook_file pg_hba_conf do
source pg_hba_conf_source
mode 0600
owner "postgres"
group "postgres"
action :create
end

# Hackin' it up.

service "postgresql" do
service_name "postgresql-8.4"
supports :restart => true, :status => true, :reload => true
action :restart
end

execute "postgres-listen" do
command "echo \"listen_addresses = '*'\" >> /etc/postgresql/8.4/main/postgresql.conf"
notifies :restart, resources(:service => "postgresql")
if pg_hba_dev
execute "postgres-listen" do
command "echo \"listen_addresses = 'localhost'\" >> /etc/postgresql/8.4/main/postgresql.conf"
notifies :restart, resources(:service => "postgresql")
end
end

execute "postgres-change-password" do
command "sudo -u postgres psql -c \"ALTER ROLE postgres WITH PASSWORD 'postgres'\""
if node.has_key?("postgres_password")
execute "postgres-change-password" do
command "sudo -u postgres psql -c \"ALTER ROLE postgres WITH PASSWORD '#{node[:postgres_password]}'\""
end
end

# This is basically a big hack - I can't find a nice way to create a user and
# give them a password easily.
execute "postgres-createuser" do
command "sudo -u postgres psql -c \"CREATE ROLE #{node[:project_name]} NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN PASSWORD \'#{node[:project_name]}\';\""
not_if "sudo -u postgres psql -c \"SELECT * FROM pg_user;\" | grep #{node['project_name']}"
end

if node[:gis]
template_commands = [
"createdb -E UTF8 template_postgis -T template0 -l en_US.utf8",
"createlang -d template_postgis plpgsql",
"psql -d postgres -c \"UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';\"",
"psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis.sql",
"psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql",
"psql -d template_postgis -c \"GRANT ALL ON geometry_columns TO PUBLIC;\"",
"psql -d template_postgis -c \"GRANT ALL ON spatial_ref_sys TO PUBLIC;\""
]

template_commands.each_with_index do |cmd, i|
execute "postgis-template-create-step-#{i+1}" do
command cmd
user "postgres"
end
end
end
if node.has_key?("databases")

if node[:gis]
template = "template_postgis"
else
template = "template0"
end

node.databases.each do |name, info|

execute "postgres-createuser-#{info[:username]}" do
command "sudo -u postgres -- psql -c \"CREATE ROLE #{info[:username]} NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN PASSWORD \'#{info[:password]}\';\""
not_if "sudo -u postgres -- psql -c \"SELECT * FROM pg_user;\" | grep -i #{node['project_db_user']}"
end

template = "template0"

if info.has_key?("gis") and info[:gis]
template_commands = [
"createdb -E UTF8 template_postgis -T template0",
"createlang -d template_postgis plpgsql",
"psql -d postgres -c \"UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';\"",
"psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis.sql",
"psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql",
"psql -d template_postgis -c \"GRANT ALL ON geometry_columns TO PUBLIC;\"",
"psql -d template_postgis -c \"GRANT ALL ON spatial_ref_sys TO PUBLIC;\""
]

template_commands.each_with_index do |cmd, i|
execute "postgis-template-create-step-#{i+1}" do
command cmd
user "postgres"
end
end

end

if info.has_key?("gis") and info[:gis]
template = "template_postgis"
else
template = "template0"
end

execute "postgres-createdb-#{info[:name]}" do
command "sudo -u postgres -- createdb -T #{template} -E UTF8 -O #{info[:username]} #{name}"
not_if "sudo -u postgres -- psql -c \"SELECT * FROM pg_database;\" | grep -i #{name}"
end

end

# Create a database with the same name as the project and also with the owner
# set to the user we just created.
execute "postgres-createdb" do
command "sudo -u postgres createdb -T #{template} -E UTF8 -l en_US.utf8 -O #{node[:project_name]} #{node[:project_name]}"
not_if "sudo -u postgres psql -c \"SELECT * FROM pg_database;\" | grep #{node['project_name']}"
end
2 changes: 1 addition & 1 deletion cookbooks/python/files/default/pip.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
default-timeout = 60
respect-virtualenv = true
download-cache = ~/.pip/cache
log-file = /tmp/pip-log.txt
log-file = /var/log/pip-log.txt
4 changes: 1 addition & 3 deletions cookbooks/python/files/default/postactivate
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
alias dj='python manage.py'
alias djr='dj runserver 0.0.0.0:8000'
alias djs='dj shell'
alias djt='dj test'

cd /vagrant/
alias djt='dj test'
Loading

0 comments on commit 68ae0f7

Please sign in to comment.