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

Commit

Permalink
Reworked the GIS code so that its only run once to create the templat…
Browse files Browse the repository at this point in the history
…e if there is a database with gis enabled. It now works over multiple runs and doesn't attempt to create the gis template if it already exists.

I've also added a second config that is used for testing purposes and makes sure that we can have multiple gis enabled projects and databases.
  • Loading branch information
d0ugal committed Sep 15, 2011
1 parent ecfdae1 commit 0d68445
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 30 deletions.
70 changes: 40 additions & 30 deletions cookbooks/postgres/recipes/default.rb
Expand Up @@ -49,42 +49,52 @@

node.databases.each do |name, info|

# This isn't the best place to put it. Since we could try and install all
# the GIS stuff for each database... although given that the package
# management stuff is all good this shouldn't be a problem.
if info.has_key?("gis") and info[:gis]
%w{postgresql-server-dev-8.4 postgresql-8.4-postgis}.each do |pkg|
package pkg do
action :install
end
if not info.has_key?("gis") or not info[:gis]
next
end

execute "post-gis" do
command 'echo "
deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu lucid main
deb-src http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu lucid main" >> /etc/apt/sources.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 314DF160
sudo apt-get update'
not_if "sudo cat /etc/apt/sources.list | grep -i ubuntugis-unstable"
end

%w{python-psycopg2 postgresql binutils proj gdal-bin postgresql-8.4-postgis
postgresql-server-dev-8.4}.each do |pkg|
package pkg do
action :install
end
end

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 #{info[:username]}"
# It seems Chef's package installer doesn't support virtual packages.
execute "gdal-contrib" do
command "sudo apt-get install -y gdal-contrib"
end

script "postgres-postgis-template" do
interpreter "bash"
user "postgres"
cwd "/tmp"
code "
wget http://docs.djangoproject.com/en/1.3/_downloads/create_template_postgis-1.5.sh -O /tmp/create_template_postgis-1.5.sh
bash /tmp/create_template_postgis-1.5.sh
"
not_if "sudo -u postgres -- psql -c \"SELECT datname FROM pg_database where datistemplate=true;\" | grep -i template_postgis"
end

template = "template0"
# We only want to do this for the first database with gis enabled.
break

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

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 usename FROM pg_user;\" | grep -i #{info[:username]}"
end

if info.has_key?("gis") and info[:gis]
Expand All @@ -95,7 +105,7 @@

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}"
not_if "sudo -u postgres -- psql -c \"SELECT datname FROM pg_database;\" | grep -i #{name}"
end

end
Expand Down
48 changes: 48 additions & 0 deletions site_configs/prod_example2.json
@@ -0,0 +1,48 @@
{
"run_list": [ "main", "python", "postgres"],

"project_name": "test_project",

"user": {
"username": "my2site",
"password": "my2sitepassword",
"group": "my2siteusers"
},

"virtualenvs": {
"my2site" : {
"packages": ["ipython", "PIL"]
},
"my2site2" : {
},
"my2site3" : {
"packages": ["ipython", "PIL"]
},
"my2site4" : {
}
},

"postgres_password": "postgres",

"databases": {
"my2_database": {
"username": "my_user",
"password": "password"
},
"my2_second_database": {
"username": "my_user",
"password": "password",
"gis": true
},
"my2_database2": {
"username": "my_user",
"password": "password"
},
"my2_second_database2": {
"username": "my_user",
"password": "password",
"gis": true
}
}

}

0 comments on commit 0d68445

Please sign in to comment.