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
|
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
__all__ = [
"Command",
]
import transaction
from getpass import getuser
from optparse import make_option
from subprocess import (
Popen,
PIPE,
)
from storm.zope.zstorm import global_zstorm as zstorm
from django.core.management.base import BaseCommand
from zope.component import provideUtility
from lpresults.database.django.schema import get_schema
from lpresults.database.store import (
get_store,
get_store_names,
)
DEFAULT_DBADMIN = "postgres"
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option(
"--dbadmin",
default=DEFAULT_DBADMIN,
dest="dbadmin",
help="Database administrator for communicating with psql.",
),
)
help = "Create the database tables for all apps."
args = "[store_name ...]"
def _communicate_with_psql(self, dbadmin, query):
"""
Open a connection as the postgres user and execute query,
returning the result for further processing.
"""
args = [
"/usr/bin/psql", "--tuples-only", "--no-align",
"--field-separator=','"]
if getuser() != dbadmin:
args[:0] = ["sudo", "-u", dbadmin, "-s"]
popen = Popen(args, stdin=PIPE, stdout=PIPE)
output, errors = popen.communicate(query)
return output.strip()
def create_users(self, dbadmin):
output = self._communicate_with_psql(dbadmin,
"SELECT usename FROM pg_user WHERE usename LIKE 'lpresults%'")
existing_users = set([username
for username in output.split("\n")
if username])
# XXX: The password should be randomly generated and stored
# in a secure location.
password = "lpresults"
required_users = set(["lpresults"])
for user in sorted(required_users.difference(existing_users)):
self._communicate_with_psql(dbadmin,
"""CREATE ROLE %s PASSWORD '%s'"""
""" NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN""" %
(user, password))
def handle(self, *store_names, **options):
dbadmin = options.get("dbadmin", DEFAULT_DBADMIN)
self.create_users(dbadmin)
provideUtility(zstorm)
if not store_names:
store_names = get_store_names()
for store_name in store_names:
schema_name = store_name.split("-")[0]
schema = get_schema(schema_name)
store = get_store(store_name)
print "Synchronizing %s store..." % store_name
schema.upgrade(store)
transaction.commit()
|