Skip to content

Commit e675087

Browse files
committed
added a postinstall script to generate a "test" db.
1 parent db279d8 commit e675087

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

app/postgresql.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
import json
3+
import psycopg2
4+
from os import path
5+
from time import sleep
6+
from sys import stdout as out
7+
8+
if path.exists('/home/dotcloud/environment.json'):
9+
with open('/home/dotcloud/environment.json') as f:
10+
env = json.load(f)
11+
else:
12+
env = {
13+
'DOTCLOUD_DB_SQL_LOGIN': 'root',
14+
'DOTCLOUD_DB_SQL_PASSWORD': 'root',
15+
'DOTCLOUD_DB_SQL_HOST': 'localhost',
16+
'DOTCLOUD_DB_SQL_PORT': '5432',
17+
}
18+
19+
dbname = "test"
20+
21+
out.write("Creating the database...")
22+
out.flush()
23+
for i in xrange(1,30):
24+
try:
25+
conn = psycopg2.connect(
26+
user=env['DOTCLOUD_DB_SQL_LOGIN'],
27+
password=env['DOTCLOUD_DB_SQL_PASSWORD'],
28+
host=env['DOTCLOUD_DB_SQL_HOST'],
29+
port=env['DOTCLOUD_DB_SQL_PORT'],
30+
database='postgres')
31+
conn.set_isolation_level(0)
32+
cur = conn.cursor()
33+
cur.execute("CREATE DATABASE %s" % dbname)
34+
out.write("\n")
35+
break
36+
except Exception as e:
37+
raise e
38+
sleep(1)
39+
out.write(".")
40+
out.flush()
41+
42+
if __name__ == '__main__':
43+
import sys

dotcloud.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
www:
22
type: python
3+
postinstall: ./app/postgresql.py
34
db:
45
type: postgresql

0 commit comments

Comments
 (0)