-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup_postgis.py
106 lines (90 loc) · 3.39 KB
/
setup_postgis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
setup_postgis.py
A fabric file for setting up postgis 2.1 database on a freebsd server
with postgres 9.3
- assumes a freebsd system with:
- freebsd 9.2
- python 2.7 installed
- bash installed with symlink to /bin/bash pointing to
/usr/local/bin/bash
- configure the assorted DB_* variables as follows:
DB_ADMIN - your db administrator
DB_PASSWORD - the password for the db administrator
DB_DATABASE - name of the database to create
"""
import sys
import random
import string
from fabric.api import env, sudo, cd, task, settings, run
from ilogue.fexpect import expect, expecting
from ilogue.fexpect import sudo as expectsudo
DB_ADMIN="pgdba"
DB_PASSWORD="apassword"
DB_DATABASE="postgisdb"
def _a_bit_of_random():
charset = string.ascii_lowercase + string.digits
r = ''.join(random.sample(charset * 6, 6))
return r
# pkg is the path after /usr/ports/<X>
# - for instance postgis21 is databases/postgis21
def _install_package(pkg, args = None):
with cd("/usr/ports/%s" % pkg):
with settings(warn_only = True):
result = run("PORT=`cat Makefile | grep -m 1 PORTNAME | awk '{print $2}'`; pkg_version | grep $PORT")
if result.return_code != 0:
sudo("make %s BATCH=yes" % ("" if args == None else args))
sudo("make %s BATCH=yes install" % ("" if args == None else args))
else:
print >> sys.stderr, "Package already installed: %s" % pkg
return False
return True
# pkg is the path after /usr/ports/<X>
# - for instance postgis21 is databases/postgis21
def _clean_package(pkg):
with cd("/usr/ports/%s" % pkg):
sudo("make clean BATCH=yes")
def install_package(pkg, args = None):
r = _install_package(pkg, args)
if r:
_clean_package(pkg)
def _install_postgres():
install_package('databases/postgresql93-server')
install_package('databases/postgresql93-client')
def _install_postgis():
install_package('databases/postgis21', args='WITHOUT_X11=yes WITHOUT_TIFF=yes WITH_RASTER=on')
def _setup_postgres():
# start at boot
filename = "/tmp/foo_%s" % _a_bit_of_random()
sudo("cat /etc/rc.conf | grep -v postgresql > %s" % filename)
sudo("echo 'postgresql_enable=\"YES\"' >> %s" % filename)
sudo("mv %s /etc/rc.conf" % filename)
# init the db
sudo("/usr/local/etc/rc.d/postgresql initdb")
#start the db
sudo("/usr/local/etc/rc.d/postgresql start")
# create the user
prompts = []
prompts += expect("Enter password for new role:", DB_PASSWORD)
prompts += expect("Enter it again:", DB_PASSWORD)
with expecting(prompts):
expectsudo("createuser -d -S -R -P %s" % DB_ADMIN, user="pgsql")
@task
def create_database():
# create the db
with settings(warn_only = True):
# create the table
result = sudo("psql -l | grep %s" % DB_DATABASE, user="pgsql")
if result.return_code != 0:
sudo("createdb -O %s %s" % (DB_ADMIN, DB_DATABASE), user="pgsql")
# add the language
result = sudo("createlang -U %s -l %s | grep plpgsql" % (DB_ADMIN, DB_DATABASE), user="pgsql")
if result.return_code != 0:
sudo("createlang -U %s plpgsql %s" % (DB_ADMIN, DB_DATABASE), user="pgsql")
# enable postgis on db
sudo('psql -U pgsql -d %s -c "CREATE EXTENSION IF NOT EXISTS postgis;"' % DB_DATABASE, user="pgsql")
sudo('psql -U pgsql -d %s -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;"' % DB_DATABASE, user="pgsql")
@task
def install_postgres():
_install_postgres()
_install_postgis()
_setup_postgres()