Merge lp:~jtv/maas/db-setup into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Merged
Merged at revision: 4
Proposed branch: lp:~jtv/maas/db-setup
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 182 lines (+141/-7)
3 files modified
Makefile (+1/-0)
bin/maasdb.sh (+133/-0)
src/maas/development.py (+7/-7)
To merge this branch: bzr merge lp:~jtv/maas/db-setup
Reviewer Review Type Date Requested Status
MAAS Development Pending
Review via email: mp+88703@code.launchpad.net

Commit message

Database control scripting.

Description of the change

Proposed initial scripting to initialize / start / query / stop / delete databases.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2012-01-16 08:33:18 +0000
3+++ Makefile 2012-01-16 15:57:24 +0000
4@@ -35,6 +35,7 @@
5 bin/django runserver 8000
6
7 harness:
8+ . bin/maasdb.sh ; maasdb_init_db db/development disposable
9 bin/django shell
10
11 syncdb:
12
13=== added directory 'bin'
14=== added file 'bin/maasdb.sh'
15--- bin/maasdb.sh 1970-01-01 00:00:00 +0000
16+++ bin/maasdb.sh 2012-01-16 15:57:24 +0000
17@@ -0,0 +1,133 @@
18+# MaaS database control functions
19+#
20+# The control functions take as their first argument a database cluster's data
21+# directory. This is where the database's socket, pidfile, log, and data will
22+# reside. The data directory must start with a slash.
23+#
24+# Some design choices for this module:
25+#
26+# * Everything is PostgreSQL on Ubuntu.
27+# * POSIX shell. Test your changes in dash, not just in bash.
28+# * Each branch gets its own cluster(s). Kill & delete when done.
29+# * One database per cluster. May change this later if it's a problem.
30+# * Databases run under the system user that creates them. No root required.
31+# * No global configuration apart from a basic PostgreSQL install.
32+# * Connections use Unix sockets. No TCP port hogging.
33+
34+POSTGRES_VERSION=9.1
35+PGCTL="/usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_ctl"
36+
37+
38+# Figure out the full data directory path for a given cluster, even if it's a
39+# relative path.
40+maasdb_locate() {
41+ DATADIR="$1"
42+ if test -z "$1"
43+ then
44+ echo "Specify a data directory for the MaaS database cluster." >&2
45+ return 1
46+ fi
47+ if ! echo "$DATADIR" | grep '^/'
48+ then
49+ echo "`pwd`/$DATADIR"
50+ fi
51+}
52+
53+
54+# Create a database cluster.
55+maasdb_create_cluster() {
56+ DATADIR="`maasdb_locate "$1"`"
57+ if ! test -d "$DATADIR/base"
58+ then
59+ mkdir -p -- "$DATADIR"
60+ $PGCTL init -D "$DATADIR" -o '-E utf8'
61+ fi
62+}
63+
64+
65+# Start a database cluster.
66+maasdb_start_cluster() {
67+ DATADIR="`maasdb_locate "$1"`"
68+ # Pass "disposable" as the second argument if the data in this database
69+ # is not important at all and you're willing to cut corners for speed.
70+ DISPOSABLE="$2"
71+
72+ if test "$DISPOSABLE" = "disposable"
73+ then
74+ # -F -- don't bother fsync'ing.
75+ EXTRA_POSTGRES_OPTS="-F"
76+ else
77+ EXTRA_POSTGRES_OPTS=""
78+ fi
79+
80+ maasdb_create_cluster "$DATADIR"
81+
82+ if ! test -f "$DATADIR/postmaster.pid"
83+ then
84+ # pg_ctl options:
85+ # -D <dir> -- data directory.
86+ # -l <file> -- log file.
87+ # -w -- wait until startup is complete.
88+ # postgres options:
89+ # -h <arg> -- host name; empty arg means Unix socket only.
90+ # -k -- socket directory.
91+ $PGCTL start \
92+ -D "$DATADIR" -l "$DATADIR/backend.log" -w \
93+ -o "-h '' -k '$DATADIR' $EXTRA_POSTGRES_OPTS"
94+ fi
95+}
96+
97+
98+# Stop a database cluster.
99+maasdb_stop_cluster() {
100+ DATADIR="`maasdb_locate "$1"`"
101+ if test -f "$DATADIR/postmaster.pid"
102+ then
103+ $PGCTL stop -D "$DATADIR"
104+ fi
105+}
106+
107+
108+# Initialize a MaaS database.
109+maasdb_init_db() {
110+ DATADIR="`maasdb_locate "$1"`"
111+ # Pass "disposable" as the second argument if the data in this database
112+ # is not important at all and you're willing to cut corners for speed.
113+ DISPOSABLE="$2"
114+ MARKER="$DATADIR/maas-created"
115+ maasdb_start_cluster "$DATADIR" "$DISPOSABLE"
116+ if ! test -f "$MARKER"
117+ then
118+ createdb -h "$DATADIR" maas && touch "$MARKER"
119+ fi
120+}
121+
122+
123+# Open a psql shell on a MaaS database.
124+maasdb_shell() {
125+ DATADIR="`maasdb_locate "$1"`"
126+ maasdb_init_db "$DATADIR"
127+ psql -h "$DATADIR" maas
128+}
129+
130+
131+# Execute a query on a MaaS database.
132+maasdb_query() {
133+ DATADIR="`maasdb_locate "$1"`"
134+ QUERY="$2"
135+ maasdb_init_db "$DATADIR"
136+ psql -h "$DATADIR" maas -c "$QUERY"
137+}
138+
139+
140+# Delete an entire MaaS database and cluster. Use only with extreme care!
141+maasdb_delete_cluster() {
142+ DATADIR="`maasdb_locate "$1"`"
143+ # Before deleting anything, does this at least look like a MaaS database
144+ # cluster?
145+ if test -d "$DATADIR/base"
146+ then
147+ maasdb_stop_cluster "$DATADIR"
148+ rm -rf -- "$DATADIR"
149+ fi
150+}
151
152=== modified file 'src/maas/development.py'
153--- src/maas/development.py 2012-01-16 10:59:22 +0000
154+++ src/maas/development.py 2012-01-16 15:57:24 +0000
155@@ -16,12 +16,12 @@
156
157 DATABASES = {
158 'default': {
159- 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
160- 'NAME': 'maas', # Or path to database file if using sqlite3.
161- 'USER': 'maas', # Not used with sqlite3.
162- 'PASSWORD': 'maas', # Not used with sqlite3.
163- 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
164- 'PORT': '', # Set to empty string for default. Not used with sqlite3.
165+ # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' etc.
166+ 'ENGINE': 'django.db.backends.postgresql_psycopg2',
167+ 'NAME': 'maas',
168+ # For PostgreSQL, a "hostname" starting with a slash indicates a
169+ # Unix socket directory.
170+ 'HOST': '%s/db/development' % os.getcwd(),
171 }
172 }
173
174@@ -32,7 +32,7 @@
175 # timezone as the operating system.
176 # If running in a Windows environment this must be set to the same as your
177 # system time zone.
178-TIME_ZONE = 'America/Chicago'
179+TIME_ZONE = None
180
181 # Language code for this installation. All choices can be found here:
182 # http://www.i18nguy.com/unicode/language-identifiers.html